debian: Add graphviz dependency
[gfxprim/pasky.git] / include / input / GP_Timer.h
blob9d6c8edb5862e4eeef59abb118842d312863b2cc
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
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. *
8 * *
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. *
13 * *
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 *
18 * *
19 * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
25 Timers and priority queue implementation.
29 #ifndef INPUT_GP_TIMER_H
30 #define INPUT_GP_TIMER_H
32 #include <stdint.h>
34 typedef struct GP_Timer {
35 /* Heap pointers and number of sons */
36 struct GP_Timer *left;
37 struct GP_Timer *right;
38 unsigned int sons;
40 /* Expiration time */
41 uint64_t expires;
43 * If not zero return value from Callback is ignored and
44 * timer is rescheduled each time it expires.
46 uint32_t period;
48 /* Timer id, showed in debug messages */
49 char id[10];
52 * Timer Callback
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);
58 void *priv;
59 } GP_Timer;
61 #define GP_TIMER_DECLARE(name, texpires, tperiod, tid, tCallback, tpriv) \
62 GP_Timer name = { \
63 .expires = texpires, \
64 .period = tperiod, \
65 .id = tid, \
66 .Callback = tCallback, \
67 .priv = tpriv \
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 */