diff options
author | slvr <slvr@unsafeio.com> | 2013-08-12 13:08:03 +0100 |
---|---|---|
committer | slvr <slvr@unsafeio.com> | 2013-08-12 13:08:03 +0100 |
commit | 1a39c397c57cfc2008d432cc38458a45b961aff0 (patch) | |
tree | c6351c0b684d6f330f7b20db5fdcd15eedec3571 /core/timer.h | |
parent | 139d915482c82f2a4aa87b444008afffef728561 (diff) |
Implemented Deadline Timer and timer_test
Diffstat (limited to 'core/timer.h')
-rw-r--r-- | core/timer.h | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/core/timer.h b/core/timer.h new file mode 100644 index 00000000..fee66f29 --- /dev/null +++ b/core/timer.h | |||
@@ -0,0 +1,104 @@ | |||
1 | /* timer.h | ||
2 | * | ||
3 | * Timing subsystem. Provides deadline timers. | ||
4 | * All times are aliased to a second for efficiency. | ||
5 | * | ||
6 | * Timer Guarantees: | ||
7 | * - The callback will not be called before the timer expires. | ||
8 | * - The callback will be called sometime after the timer expires, | ||
9 | * on a best effort basis. | ||
10 | * - If timer_poll is called at least once a second, the callback | ||
11 | * will be called at most one second after it expires. | ||
12 | * | ||
13 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
14 | * | ||
15 | * This file is part of Tox. | ||
16 | * | ||
17 | * Tox is free software: you can redistribute it and/or modify | ||
18 | * it under the terms of the GNU General Public License as published by | ||
19 | * the Free Software Foundation, either version 3 of the License, or | ||
20 | * (at your option) any later version. | ||
21 | * | ||
22 | * Tox is distributed in the hope that it will be useful, | ||
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
25 | * GNU General Public License for more details. | ||
26 | * | ||
27 | * You should have received a copy of the GNU General Public License | ||
28 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
29 | * | ||
30 | */ | ||
31 | |||
32 | #ifndef TIMER_H | ||
33 | #define TIMER_H | ||
34 | |||
35 | #include <stdint.h> | ||
36 | #include <stdbool.h> | ||
37 | |||
38 | #define US_PER_SECOND 1000000 /* 1 s = 10^6 us */ | ||
39 | |||
40 | struct timer; | ||
41 | typedef struct timer timer; | ||
42 | |||
43 | /* If time_callback returns a non-zero value, timer t is deleted. | ||
44 | * You may call any of the timer functions within the callback: | ||
45 | * For example, you may call timer_start to restart the timer from | ||
46 | * within a callback. */ | ||
47 | typedef int (*timer_callback)(timer* t, void* userarg); | ||
48 | |||
49 | /* Initisalise timer subsystem */ | ||
50 | void timer_init(void); | ||
51 | |||
52 | /* Poll. (I will eventually replace all polling in Tox with an async system.) */ | ||
53 | void timer_poll(void); | ||
54 | |||
55 | /* Creates a new timer. Does not enqueue/start it. */ | ||
56 | timer* timer_new(void); | ||
57 | |||
58 | /* Destroys a timer instance. */ | ||
59 | void timer_delete(timer* t); | ||
60 | |||
61 | /* Sets up the timer callback. */ | ||
62 | void timer_setup(timer* t, timer_callback cb, void* userarg); | ||
63 | |||
64 | /* Accessor Function. */ | ||
65 | void* timer_get_userdata(timer* t); | ||
66 | |||
67 | /* Starts the timer so that it's called in sec seconds in the future from now. | ||
68 | * A non-positive value of sec results in the callback being called immediately. | ||
69 | * This function may be called again after a timer has been started to adjust | ||
70 | * the expiry time. */ | ||
71 | void timer_start(timer* t, int sec); | ||
72 | |||
73 | /* Stops the timer. Returns -1 if the timer was not active. */ | ||
74 | int timer_stop(timer* t); | ||
75 | |||
76 | /* Adds additionalsec seconds to the timer. | ||
77 | * Returns -1 and does nothing if the timer was not active. */ | ||
78 | int timer_delay(timer* t, int additonalsec); | ||
79 | |||
80 | /* Returns the time remaining on a timer in seconds. | ||
81 | * Returns -1 if the timer is not active. | ||
82 | * Returns 0 if the timer has expired and the callback hasn't been called yet. */ | ||
83 | int timer_time_remaining(timer* t); | ||
84 | |||
85 | /* Determines if timer is active. Returns TRUE if it is active */ | ||
86 | bool timer_is_active(timer* t); | ||
87 | |||
88 | /* Single-use timer. | ||
89 | * Creates a new timer, preforms setup and starts it. | ||
90 | * Callback must return a non-zero value to prevent memory leak. */ | ||
91 | void timer_single(timer_callback cb, void* userarg, int sec); | ||
92 | |||
93 | /* Single-use microsecond timer. | ||
94 | * Creates a new timer, preforms setup and starts it. | ||
95 | * Please do not use this when accuracy is not absolutely required. | ||
96 | * Use when one needs to time a period < 1 s. | ||
97 | * Use the more coarse timers above for periods > 5 s. | ||
98 | * WARNING: the callback will be called with NULL as the first argument */ | ||
99 | void timer_us(timer_callback cb, void* userarg, int us); | ||
100 | |||
101 | /* Internal Testing */ | ||
102 | void timer_internal_tests(bool(*)(bool, char*)); | ||
103 | |||
104 | #endif | ||