LCOV - code coverage report
Current view: top level - matcher - matchtimeout.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core fcfb185a9dd5 Lines: 22 23 95.7 %
Date: 2019-04-18 16:33:14 Functions: 4 4 100.0 %
Branches: 6 8 75.0 %

           Branch data     Line data    Source code
       1                 :            : /** @file matchtimeout.h
       2                 :            :  * @brief Time limits for the matcher
       3                 :            :  */
       4                 :            : /* Copyright (C) 2013,2014,2015,2016,2017 Olly Betts
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU General Public License as
       8                 :            :  * published by the Free Software Foundation; either version 2 of the
       9                 :            :  * License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This program is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :            :  * GNU General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU General Public License
      17                 :            :  * along with this program; if not, write to the Free Software
      18                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
      19                 :            :  * USA
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef XAPIAN_INCLUDED_MATCHTIMEOUT_H
      23                 :            : #define XAPIAN_INCLUDED_MATCHTIMEOUT_H
      24                 :            : 
      25                 :            : #ifndef PACKAGE
      26                 :            : # error config.h must be included first in each C++ source file
      27                 :            : #endif
      28                 :            : 
      29                 :            : #ifdef HAVE_TIMER_CREATE
      30                 :            : #include "realtime.h"
      31                 :            : 
      32                 :            : #include <signal.h>
      33                 :            : #include <time.h>
      34                 :            : #include "safeunistd.h" // For _POSIX_* feature test macros.
      35                 :            : 
      36                 :            : extern "C" {
      37                 :            : 
      38                 :            : static void
      39                 :          3 : set_timeout_flag(union sigval sv)
      40                 :            : {
      41                 :          3 :     *(reinterpret_cast<volatile bool*>(sv.sival_ptr)) = true;
      42                 :          3 : }
      43                 :            : 
      44                 :            : }
      45                 :            : 
      46                 :            : // The monotonic clock is the better basis for timeouts, but not always
      47                 :            : // available.
      48                 :            : 
      49                 :            : #ifndef _POSIX_MONOTONIC_CLOCK
      50                 :            : const clockid_t TIMEOUT_CLOCK = CLOCK_REALTIME;
      51                 :            : #elif defined __sun
      52                 :            : // Solaris defines CLOCK_MONOTONIC, but "man timer_create" doesn't mention it
      53                 :            : // and when using it timer_create() fails with "EPERM" (perhaps you need to be
      54                 :            : // root to use it?  I can't test that).
      55                 :            : //
      56                 :            : // Solaris defines _POSIX_MONOTONIC_CLOCK so we need to special case.
      57                 :            : const clockid_t TIMEOUT_CLOCK = CLOCK_REALTIME;
      58                 :            : #elif defined __CYGWIN__
      59                 :            : // https://cygwin.com/cygwin-api/std-notes.html currently (2016-05-13) says:
      60                 :            : //
      61                 :            : //     clock_nanosleep currently supports only CLOCK_REALTIME and
      62                 :            : //     CLOCK_MONOTONIC.  clock_setres, clock_settime, and timer_create
      63                 :            : //     currently support only CLOCK_REALTIME.
      64                 :            : //
      65                 :            : // So CLOCK_MONOTONIC is defined, but not supported by timer_create().
      66                 :            : const clockid_t TIMEOUT_CLOCK = CLOCK_REALTIME;
      67                 :            : #else
      68                 :            : const clockid_t TIMEOUT_CLOCK = CLOCK_MONOTONIC;
      69                 :            : #endif
      70                 :            : 
      71                 :            : class TimeOut {
      72                 :            :     struct sigevent sev;
      73                 :            :     timer_t timerid;
      74                 :            :     volatile bool expired;
      75                 :            : 
      76                 :            :     TimeOut(const TimeOut&) = delete;
      77                 :            : 
      78                 :            :     TimeOut& operator=(const TimeOut&) = delete;
      79                 :            : 
      80                 :            :   public:
      81                 :     149253 :     explicit TimeOut(double limit) : expired(false) {
      82         [ +  + ]:     149253 :         if (limit > 0) {
      83                 :          3 :             sev.sigev_notify = SIGEV_THREAD;
      84                 :          3 :             sev.sigev_notify_function = set_timeout_flag;
      85                 :          3 :             sev.sigev_notify_attributes = NULL;
      86                 :            :             sev.sigev_value.sival_ptr =
      87                 :          3 :                 static_cast<void*>(const_cast<bool*>(&expired));
      88         [ +  - ]:          3 :             if (usual(timer_create(TIMEOUT_CLOCK, &sev, &timerid) == 0)) {
      89                 :            :                 struct itimerspec interval;
      90                 :          3 :                 interval.it_interval.tv_sec = 0;
      91                 :          3 :                 interval.it_interval.tv_nsec = 0;
      92                 :          3 :                 RealTime::to_timespec(limit, &interval.it_value);
      93         [ +  - ]:          3 :                 if (usual(timer_settime(timerid, 0, &interval, NULL) == 0)) {
      94                 :            :                     // Timeout successfully set.
      95                 :          3 :                     return;
      96                 :            :                 }
      97                 :          0 :                 timer_delete(timerid);
      98                 :            :             }
      99                 :            :         }
     100                 :     149250 :         sev.sigev_notify = SIGEV_NONE;
     101                 :            :     }
     102                 :            : 
     103                 :     149253 :     ~TimeOut() {
     104         [ +  + ]:     149253 :         if (sev.sigev_notify != SIGEV_NONE) {
     105                 :          3 :             timer_delete(timerid);
     106                 :          3 :             sev.sigev_notify = SIGEV_NONE;
     107                 :            :         }
     108                 :     149253 :     }
     109                 :            : 
     110                 :       5096 :     bool timed_out() const { return expired; }
     111                 :            : };
     112                 :            : #else
     113                 :            : class TimeOut {
     114                 :            :   public:
     115                 :            :     explicit TimeOut(double) { }
     116                 :            :     bool timed_out() const { return false; }
     117                 :            : };
     118                 :            : #endif
     119                 :            : 
     120                 :            : #endif // XAPIAN_INCLUDED_MATCHTIMEOUT_H

Generated by: LCOV version 1.11