This file is indexed.

/usr/include/ucommon/timers.h is in libucommon-dev 7.0.0-9.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
// Copyright (C) 2015 Cherokees of Idaho.
//
// This file is part of GNU uCommon C++.
//
// GNU uCommon C++ is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU uCommon C++ is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Realtime timers and timer queues.
 * This offers ucommon support for realtime high-resolution threadsafe
 * timers and timer queues.  Threads may be scheduled by timers and timer
 * queues may be used to inject timer events into callback objects or through
 * virtuals.
 * @file ucommon/timers.h
 */

#ifndef _UCOMMON_TIMERS_H_
#define _UCOMMON_TIMERS_H_

#ifndef _UCOMMON_LINKED_H_
#include <ucommon/linked.h>
#endif

#ifndef _MSWINDOWS_
#include <unistd.h>
#include <sys/time.h>
#endif

#include <time.h>

namespace ucommon {

/**
 * Timer class to use when scheduling realtime events.  The timer generally
 * uses millisecond values but has a microsecond accuracy.  On platforms that
 * support it, the timer uses posix realtime monotonic clock extensions,
 * otherwise lower accuracy timer systems might be used.
 */
class __EXPORT Timer
{
private:
    friend class Conditional;
    friend class Semaphore;
    friend class Event;

#if _POSIX_TIMERS > 0 && defined(POSIX_TIMERS)
    timespec timer;
#else
#undef  POSIX_TIMERS    // make sure not used if no support
    timeval timer;
#endif
    bool updated;

protected:
    /**
     * Check if timer has been updated since last check.  This also sets
     * updated false.
     * @return true if was updated.
     */
    bool update(void);

    /**
     * Check if timer active.
     * @return true if active.
     */
    bool is_active(void) const;

public:
    static const timeout_t inf = ((timeout_t)(-1));
    static const time_t reset = ((time_t)(0));

#ifdef  _MSWINDOWS_
    typedef unsigned __int64 tick_t;
#else
    typedef uint64_t tick_t;
#endif

    /**
     * Construct an untriggered timer set to the time of creation.
     */
    Timer();

    /**
     * Construct a triggered timer that expires at specified offset.
     * @param offset to expire in milliseconds.
     */
    Timer(timeout_t offset);

    /**
     * Construct a triggered timer that expires at specified offset.
     * @param offset to expire in seconds.
     */
    Timer(time_t offset);

    /**
     * Construct a timer from a copy of another timer.
     * @param copy of timer to construct from.
     */
    Timer(const Timer& copy);

    /**
     * Set the timer to expire.
     * @param expire time in milliseconds.
     */
    void set(timeout_t expire);

    /**
     * Set the timer to expire.
     * @param expire time in seconds.
     */
    void set(time_t expire);

    /**
     * Set (update) the timer with current time.
     */
    void set(void);

    /**
     * Clear pending timer, has no value.
     */
    void clear(void);

    /**
     * Get remaining time until the timer expires.
     * @return 0 if expired or milliseconds still waiting.
     */
    timeout_t get(void) const;

    /**
     * Get remaining time until timer expires by reference.
     * @return 0 if expired or milliseconds still waiting.
     */
    inline timeout_t operator*() const {
        return get();
    }

    /**
     * Check if timer has expired.
     * @return true if timer still pending.
     */
    bool operator!() const;

    /**
     * Check if timer expired for is() expression.
     * @return true if timer expired.
     */
    operator bool() const;

    /**
     * Set timer expiration.
     * @param expire timer in specified seconds.
     */
    Timer& operator=(time_t expire);

    /**
     * Set timer expiration.
     * @param expire timer in milliseconds.
     */
    Timer& operator=(timeout_t expire);

    /**
     * Adjust timer expiration.
     * @param expire time to add in seconds.
     */
    Timer& operator+=(time_t expire);

    /**
     * Adjust timer expiration.
     * @param expire time to add in milliseconds.
     */
    Timer& operator+=(timeout_t expire);

    /**
     * Adjust timer expiration.
     * @param expire time to subtract in seconds.
     */
    Timer& operator-=(time_t expire);

    /**
     * Adjust timer expiration.
     * @param expire time to subtract in milliseconds.
     */
    Timer& operator-=(timeout_t expire);

    /**
     * Compute difference between two timers.
     * @param timer to use for difference.
     * @return difference in milliseconds.
     */
    timeout_t operator-(const Timer& timer);

    /**
     * Compare timers if same timeout.
     * @param timer to compare with.
     * @return true if same.
     */
    bool operator==(const Timer& timer) const;

    /**
     * Compare timers if not same timeout.
     * @param timer to compare with.
     * @return true if not same.
     */
    bool operator!=(const Timer& timer) const;

    /**
     * Compare timers if earlier timeout than another timer.
     * @param timer to compare with.
     * @return true if earlier.
     */
    bool operator<(const Timer& timer) const;

    /**
     * Compare timers if earlier than or equal to another timer.
     * @param timer to compare with.
     * @return true if earlier or same.
     */
    bool operator<=(const Timer& timer) const;

    /**
     * Compare timers if later timeout than another timer.
     * @param timer to compare with.
     * @return true if later.
     */
    bool operator>(const Timer& timer) const;

    /**
     * Compare timers if later than or equal to another timer.
     * @param timer to compare with.
     * @return true if later or same.
     */
    bool operator>=(const Timer& timer) const;

    /**
     * Sleep current thread until the specified timer expires.
     * @param timer to reference for sleep.
     */
    static void sync(Timer &timer);

    /**
     * Get timer ticks since uuid epoch.
     * @return timer ticks in 100ns resolution.
     */
    static tick_t ticks(void);
};

/**
 * A timer queue for timer events.  The timer queue is used to hold a
 * linked list of timers that must be processed together.  The timer
 * queue processes the timer event list and calls an expired function
 * on events that have expired.  The timer queue also determines the
 * wait time until the next timer will expire.  When timer events are
 * modified, they can retrigger the queue to re-examine the list to
 * find when the next timer will now expire.
 * @author David Sugar <dyfet@gnutelephony.org>
 */
class __EXPORT TimerQueue : public OrderedIndex
{
private:
    __DELETE_COPY(TimerQueue);

public:
    /**
     * A timer event object that lives on a timer queue.  Timer events are
     * triggered through the timer queue's expire method.  Timer events
     * also modify the queue when they are changed, particularly to force
     * re-evaluation of the expiration period.  This class is not used by
     * itself but rather as a base class for a timer event object.
     * @author David Sugar <dyfet@gnutelephony.org>
     */
    class __EXPORT event : protected Timer, public DLinkedObject
    {
    private:
        __DELETE_DEFAULTS(event);

    protected:
        friend class TimerQueue;

        /**
         * Construct a timer event object and initially arm.
         * @param expire timer in specified milliseconds.
         */
        event(timeout_t expire);

        /**
         * Construct an armed timer event object and attach to queue.
         * @param queue to add event to.
         * @param expire timer in specified milliseconds.
         */
        event(TimerQueue *queue, timeout_t expire);

        /**
         * Event method to call in derived class when timer expires.
         */
        virtual void expired(void) = 0;

        /**
         * Expected next timeout for the timer.  This may be overriden
         * for strategy purposes when evaluted by timer queue's expire.
         * @return milliseconds until timer next triggers.
         */
        virtual timeout_t timeout(void);

    public:
        /**
         * Detaches from queue when destroyed.
         */
        virtual ~event();

        /**
         * Attach event to a timer queue.  Detaches from previous list if
         * already attached elsewhere.
         * @param queue to attach to.
         */
        void attach(TimerQueue *queue);

        /**
         * Detach event from a timer queue.
         */
        void detach(void);

        /**
         * Arm event to trigger at specified timeout.
         * @param timeout to expire and trigger.
         */
        void arm(timeout_t timeout);

        /**
         * Disarm event.
         */
        void disarm(void);

        /**
         * Time remaining until expired.
         * @return milliseconds until timer expires.
         */
        inline timeout_t get(void) const {
            return Timer::get();
        }

        inline timeout_t operator*() const {
            return Timer::get();
        }

        /**
         * Notify timer queue that the timer has been updated.
         */
        void update(void);

        /**
         * Get the timer queue we are attached to.
         * @return timer queue or NULL if not attached.
         */
        inline TimerQueue *list(void) const {
            return static_cast<TimerQueue*>(Root);
        }
    };

protected:
    friend class event;

    /**
     * Called in derived class when the queue is being modified.
     * This is often used to lock the list.
     */
    virtual void modify(void) = 0;

    /**
     * Called in derived class after the queue has been modified.  This often
     * releases a lock that modify set and to wakeup a timer thread to
     * evaluate when the next timer will now expire.
     */
    virtual void update(void) = 0;

public:
    /**
     * Create an empty timer queue.
     */
    TimerQueue();

    /**
     * Destroy queue, does not remove event objects.
     */
    virtual ~TimerQueue();

    /**
     * Add a timer event to the timer queue.
     * @param timer event to add.
     */
    void operator+=(event &timer);

    /**
     * Remove a timer event from the timer queue.
     * @param timer event to remove.
     */
    void operator-=(event &timer);

    /**
     * Process timer queue and find when next event triggers.  This function
     * will call the expired methods on expired timers.  Normally this function
     * will be called in the context of a timer thread which sleeps for the
     * timeout returned unless it is awoken on an update event.
     * @return timeout until next timer expires in milliseconds.
     */
    timeout_t expire();
};

/**
 * A convenience type for timer queue timer events.
 */
typedef TimerQueue::event TQEvent;

/**
 * A convenience type for timers.
 */
typedef Timer timer_t;

} // namespace ucommon

#endif