1 /*****************************************************************************
2 * This file is part of gfxprim library. *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
19 * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz> *
21 *****************************************************************************/
25 Timers and priority queue implementation.
29 #ifndef INPUT_GP_TIMER_H
30 #define INPUT_GP_TIMER_H
34 typedef struct GP_Timer
{
35 /* Heap pointers and number of sons */
36 struct GP_Timer
*left
;
37 struct GP_Timer
*right
;
43 * If not zero return value from Callback is ignored and
44 * timer is rescheduled each time it expires.
48 /* Timer id, showed in debug messages */
54 * If non-zero is returned, the timer is rescheduled to expire
55 * return value from now.
57 uint32_t (*Callback
)(struct GP_Timer
*self
);
61 #define GP_TIMER_DECLARE(name, texpires, tperiod, tid, tCallback, tpriv) \
63 .expires = texpires, \
66 .Callback = tCallback, \
71 * Prints the structrue of binary heap into stdout, only for debugging.
73 void GP_TimerQueueDump(GP_Timer
*queue
);
76 * Inserts timer into the timer priority queue.
78 void GP_TimerQueueInsert(GP_Timer
**queue
, uint64_t now
, GP_Timer
*timer
);
81 * Removes timer from timer queue.
83 * This operation (in contrast with insert and process) runs in O(n) time.
85 void GP_TimerQueueRemove(GP_Timer
**queue
, GP_Timer
*timer
);
88 * Processes queue, all timers with expires <= now are processed.
90 * Returns number of timers processed.
92 int GP_TimerQueueProcess(GP_Timer
**queue
, uint64_t now
);
95 * Returns size of the queue, i.e. number of timers.
97 static inline unsigned int GP_TimerQueueSize(GP_Timer
*queue
)
99 return queue
? queue
->sons
+ 1 : 0;
102 #endif /* INPUT_GP_TIMER_H */