-
Notifications
You must be signed in to change notification settings - Fork 278
/
Copy pathsc_timer.h
145 lines (132 loc) · 4.49 KB
/
sc_timer.h
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
/*
* BSD-3-Clause
*
* Copyright 2021 Ozan Tezcan
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SC_TIMER_H
#define SC_TIMER_H
#define SC_TIMER_VERSION "2.0.0"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef SC_HAVE_CONFIG_H
#include "config.h"
#else
#define sc_timer_malloc malloc
#define sc_timer_free free
#endif
#define SC_TIMER_INVALID UINT64_MAX
struct sc_timer_data {
uint64_t timeout;
uint64_t type;
void *data;
};
struct sc_timer {
uint64_t timestamp;
uint32_t head;
uint32_t wheel;
struct sc_timer_data *list;
};
/**
* Init timer
*
* @param t timer
* @param timestamp current timestamp. Use monotonic timer source.
*/
void sc_timer_init(struct sc_timer *t, uint64_t timestamp);
/**
* Destroy timer.
* @param t timer
*/
void sc_timer_term(struct sc_timer *t);
/**
* Remove all timers without deallocating underlying memory.
* @param t timer
*/
void sc_timer_clear(struct sc_timer *t);
/**
* Add timer
* 'timeout' is relative to latest 'timestamp' value given to the 'timer'.
*
* e.g.,
* sc_timer_init(&timer, 1000); // Current timestamp is 1000.
* sc_timer_add(&timer, arg, 10); // Timeout will be at 1010.
* sc_timer_timeout(&timer, 2000, arg, callback); // Timestamp is now 2000.
* sc_timer_add(&timer, arg, 10); // Timeout will be at 2010.
*
*
* @param t timer
* @param timeout timeout value, this is relative to 'sc_timer_init's timer.
* e.g., sc_timer_init(&timer, 10); // say, start time is 10
* milliseconds
* @param data user data to pass into callback on 'sc_timer_timeout' call.
* @param type user data to pass into callback on 'sc_timer_timeout' call.
* @return SC_TIMER_INVALID on out of memory. Otherwise, timer id. You
* can cancel this timer via this id later.
*/
uint64_t sc_timer_add(struct sc_timer *t, uint64_t timeout, uint64_t type,
void *data);
/**
* uint64_t id = sc_timer_add(&timer, arg, 10);
* sc_timer_cancel(&timer, &id);
*
* @param t timer
* @param id timer id
*/
void sc_timer_cancel(struct sc_timer *t, uint64_t *id);
/**
* Checks timeouts and calls 'callback' function for each timeout.
*
* Logical pattern is :
*
* e.g.,
*
* struct sc_timer timer;
* sc_timer_init(&timer, time_ms());
* sc_timer_add(&timer, data, 100);
*
* while (true) {
* uint64_t timeout = sc_timer_timeout(&timer, time_ms(), arg, callback);
* sleep(timeout); // or select(timeout), epoll_wait(timeout) etc..
* }
*
*
* @param t timer
* @param timestamp current timestamp
* @param arg user data to user callback
* @param callback 'arg' is user data.
* 'timeout' is scheduled timeout for that timer.
* 'type' is what user passed on 'sc_timer_add'.
* 'data' is what user passed on 'sc_timer_add'.
* @return next timeout.
*/
uint64_t sc_timer_timeout(struct sc_timer *t, uint64_t timestamp, void *arg,
void (*callback)(void *arg, uint64_t timeout,
uint64_t type, void *data));
#endif