summaryrefslogtreecommitdiff
path: root/core/timer.h
blob: 154913262f5dd76e6192ff89d8af392d06ab318c (plain)
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
/*  timer.h
 *
 *  Timing subsystem. Provides deadline timers.
 *  All times are aliased to a second for efficiency.
 *
 *  Timer Guarantees:
 *  - The callback will not be called before the timer expires.
 *  - The callback will be called sometime after the timer expires,
 *    on a best effort basis.
 *  - If timer_poll is called at least once a second, the callback
 *    will be called at most one second after it expires.
 *
 *  Copyright (C) 2013 Tox project All Rights Reserved.
 *
 *  This file is part of Tox.
 *
 *  Tox is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Tox 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Tox.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef TIMER_H
#define TIMER_H

#include <stdint.h>
#include <stdbool.h>

#define US_PER_SECOND 1000000 /* 1 s = 10^6 us */

struct timer;
typedef struct timer timer;

/* If time_callback returns a non-zero value, timer t is deleted.
 * You may call any of the timer functions within the callback:
 * For example, you may call timer_start to restart the timer from
 * within a callback. */
typedef int (*timer_callback)(timer *t, void *userarg);

/* Initisalise timer subsystem */
void timer_init(void);

/* Poll. (I will eventually replace all polling in Tox with an async system.) */
void timer_poll(void);

/* Creates a new timer. Does not enqueue/start it. */
timer *new_timer(void);

/* Destroys a timer instance. */
void delete_timer(timer *t);

/* Sets up the timer callback. */
void timer_setup(timer *t, timer_callback cb, void *userarg);

/* Accessor Function. */
void *timer_get_userdata(timer *t);

/* Starts the timer so that it's called in sec seconds in the future from now.
 * A non-positive value of sec results in the callback being called immediately.
 * This function may be called again after a timer has been started to adjust
 * the expiry time. */
void timer_start(timer *t, int sec);

/* Stops the timer. Returns -1 if the timer was not active. */
int timer_stop(timer *t);

/* Adds additionalsec seconds to the timer.
 * Returns -1 and does nothing if the timer was not active. */
int timer_delay(timer *t, int additonalsec);

/* Returns the time remaining on a timer in seconds.
 * Returns -1 if the timer is not active.
 * Returns 0 if the timer has expired and the callback hasn't been called yet. */
int timer_time_remaining(timer *t);

/* Determines if timer is active. Returns TRUE if it is active */
bool timer_is_active(timer *t);

/* Single-use timer.
 * Creates a new timer, preforms setup and starts it.
 * Callback must return a non-zero value to prevent memory leak. */
void timer_single(timer_callback cb, void *userarg, int sec);

/* Single-use microsecond timer.
 * Creates a new timer, preforms setup and starts it.
 * Please do not use this when accuracy is not absolutely required.
 * Use when one needs to time a period < 1 s.
 * Use the more coarse timers above for periods > 5 s.
 * WARNING: the callback will be called with NULL as the first argument */
void timer_us(timer_callback cb, void *userarg, int us);

/* Internal Testing */
void timer_internal_tests(bool( *)(bool, char *));

#endif