-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.c
More file actions
executable file
·304 lines (257 loc) · 7.42 KB
/
Copy pathtimer.c
File metadata and controls
executable file
·304 lines (257 loc) · 7.42 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "time_util.h"
#include "llist.h"
#include "timer.h"
#include "hash.h"
#include "stdbool.h"
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
struct timer_queue {
struct list_head all_timers;
struct timer_list *active_node;
pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_cond_t waiter_cond;
pthread_t timer_thread;
};
#define HASH_BITS 2
#define HASH_SIZE 4
static pthread_once_t time_base_once = PTHREAD_ONCE_INIT;
static struct timer_queue queues[HASH_SIZE];
static void* timer_entry(void *arg);
static int
init_timer_queue(struct timer_queue *q)
{
int ret;
INIT_LIST_HEAD(&q->all_timers);
q->active_node = NULL;
pthread_mutex_init(&q->mutex, NULL);
pthread_cond_init(&q->cond, NULL);
pthread_cond_init(&q->waiter_cond, NULL);
if ((ret = pthread_create(&q->timer_thread, NULL, timer_entry, q))) {
fprintf(stderr, "%s %s: can not create timer thread, %s\n",
__FILE__, __func__, strerror(ret));
return -ret;
}
return 0;
}
static unsigned
timer_hash_idx(struct timer_list *timer)
{
return hash_32((uintptr_t)timer, HASH_BITS);
}
static void
init_timer_base(void)
{
int i;
for (i = 0; i < HASH_SIZE; ++i) {
init_timer_queue(&queues[i]);
}
usleep(1);
}
void
__util_init_timer(struct timer_list *timer, timer_func_t func, unsigned long flags)
{
timer->entry.prev = 0;
timer->entry.next = 0;
timer->function = func;
timer->flags = flags;
}
static int
detach_if_pending(struct timer_list *timer)
{
if (!timer_pending(timer))
return 0;
list_del(&timer->entry);
timer->entry.prev = 0;
timer->entry.next = 0;
return 1;
}
static struct timer_queue *
get_timer_queue(struct timer_list *timer)
{
int idx;
idx = timer_hash_idx(timer);
return &queues[idx];
}
static int
__add_timer(struct timer_list *timer)
{
struct timer_queue *q = get_timer_queue(timer);
struct timer_list *c, *prev;
INIT_LIST_HEAD(&timer->entry);
if (list_empty(&q->all_timers)) {
list_add(&timer->entry, &q->all_timers);
return 1;
}
prev = NULL;
list_for_each_entry(c, &q->all_timers, entry) {
if (timer->expires < c->expires)
break;
prev = c;
}
list_add_tail(&timer->entry, &c->entry);
return (prev == NULL);
}
/* mod_timer of an inactive timer returns 0, mod_timer of an active timer returns 1. */
static int
mod_timer(struct timer_list *timer, unsigned long expires, bool pending_only)
{
pthread_once(&time_base_once, init_timer_base);
struct timer_queue *q = get_timer_queue(timer);
int ret;
int at_head;
pthread_mutex_lock(&q->mutex);
ret = detach_if_pending(timer);
if (!ret && pending_only)
goto out_unlock;
timer->expires = expires;
at_head = __add_timer(timer);
if (at_head) {
pthread_cond_signal(&q->cond);
}
out_unlock:
pthread_mutex_unlock(&q->mutex);
return (ret);
}
/* mod_timer of an inactive timer returns 0, mod_timer of an active timer returns 1. */
int
util_mod_timer(struct timer_list *timer, unsigned long expires)
{
return mod_timer(timer, expires, false);
}
int
util_mod_timer_pending(struct timer_list *timer, unsigned long expires)
{
return mod_timer(timer, expires, true);
}
void
util_add_timer(struct timer_list *timer)
{
pthread_once(&time_base_once, init_timer_base);
if (timer_pending(timer)) {
fprintf(stderr, "%s %s: fault, timer pending\n", __FILE__, __func__);
abort();
}
util_mod_timer(timer, timer->expires);
}
/**
* del_timer - deactivate a timer.
* @timer: the timer to be deactivated
*
* del_timer() deactivates a timer - this works on both active and inactive
* timers.
*
* The function returns whether it has deactivated a pending timer or not.
* (ie. del_timer() of an inactive timer returns 0, del_timer() of an
* active timer returns 1.)
**/
int
util_del_timer(struct timer_list *timer)
{
pthread_once(&time_base_once, init_timer_base);
struct timer_queue *q = get_timer_queue(timer);
int ret = 0;
if (timer_pending(timer)) {
pthread_mutex_lock(&q->mutex);
ret = detach_if_pending(timer);
pthread_mutex_unlock(&q->mutex);
}
return ret;
}
/**
* util_try_to_del_timer_sync - Try to deactivate a timer
* @timer: timer to delete
*
* This function tries to deactivate a timer. Upon successful (ret >= 0)
* exit the timer is not queued and the handler is not running on any CPU.
**/
int
util_try_to_del_timer_sync(struct timer_list *timer)
{
pthread_once(&time_base_once, init_timer_base);
struct timer_queue *q = get_timer_queue(timer);
int ret = -1;
pthread_mutex_lock(&q->mutex);
if (q->active_node != timer) {
ret = detach_if_pending(timer);
}
pthread_mutex_unlock(&q->mutex);
return ret;
}
static void
del_timer_wait_running(struct timer_list *timer)
{
struct timer_queue *q = get_timer_queue(timer);
pthread_mutex_lock(&q->mutex);
while (q->active_node == timer) {
pthread_cond_wait(&q->waiter_cond, &q->mutex);
}
pthread_mutex_unlock(&q->mutex);
}
/**
* util_del_timer_sync - deactivate a timer and wait for the handler to finish.
* @timer: the timer to be deactivated
*
* This function only differs from del_timer() : besides deactivating
* the timer it also makes sure the handler has finished executing.
*
* Synchronization rules: Callers must prevent restarting of the timer,
* otherwise this function is meaningless.
* The caller must not hold locks which would prevent completion of the timer's
* handler. The timer's handler must not call add_timer(). Upon exit the
* timer is not queued and the handler is not running on any CPU.
**/
int
util_del_timer_sync(struct timer_list *timer)
{
int ret;
pthread_once(&time_base_once, init_timer_base);
do {
ret = util_try_to_del_timer_sync(timer);
if (ret < 0) {
del_timer_wait_running(timer);
}
} while (ret < 0);
return ret;
}
static void *
timer_entry(void *arg)
{
struct timer_queue *q = (struct timer_queue *)arg;
struct timer_list *timer;
struct timespec ts, ts2;
pthread_setname_np(pthread_self(), "timer_list");
pthread_mutex_lock(&q->mutex);
for (;;) {
long now = util_get_jiffies();
while (!list_empty(&q->all_timers)) {
timer = list_first_entry(&q->all_timers, struct timer_list, entry);
if (timer->expires > now)
break;
list_del(&timer->entry);
timer->entry.next = NULL;
q->active_node = timer;
pthread_mutex_unlock(&q->mutex);
(*(timer->function))(timer);
pthread_mutex_lock(&q->mutex);
q->active_node = NULL;
pthread_cond_broadcast(&q->waiter_cond);
}
if (list_empty(&q->all_timers)) {
pthread_cond_wait(&q->cond, &q->mutex);
} else {
timer = list_first_entry(&q->all_timers, struct timer_list, entry);
long diff = timer->expires - now;
clock_gettime(CLOCK_REALTIME, &ts);
ts2.tv_sec = diff / PFS_HZ;
ts2.tv_nsec = ((diff % PFS_HZ) * 1000UL * 1000UL * 1000UL)/PFS_HZ;
timespecadd(&ts, &ts2, &ts);
pthread_cond_timedwait(&q->cond, &q->mutex, &ts);
}
}
pthread_mutex_unlock(&q->mutex);
return 0;
}