1 | #include <math.h> |
1 | #include <math.h> |
2 | #include <stdlib.h> |
2 | #include <stdlib.h> |
|
|
3 | #include <unistd.h> |
|
|
4 | #include <fcntl.h> |
|
|
5 | #include <signal.h> |
|
|
6 | #include <stddef.h> |
3 | |
7 | |
4 | #include <stdio.h> |
8 | #include <stdio.h> |
5 | |
9 | |
6 | #include <assert.h> |
10 | #include <assert.h> |
7 | #include <errno.h> |
11 | #include <errno.h> |
8 | #include <sys/time.h> |
12 | #include <sys/time.h> |
9 | #include <time.h> |
13 | #include <time.h> |
10 | |
14 | |
11 | #ifdef CLOCK_MONOTONIC |
|
|
12 | # define HAVE_MONOTONIC 1 |
|
|
13 | #endif |
|
|
14 | |
|
|
15 | #define HAVE_EPOLL 1 |
15 | #define HAVE_EPOLL 1 |
16 | #define HAVE_REALTIME 1 |
16 | |
|
|
17 | #ifndef HAVE_MONOTONIC |
|
|
18 | # ifdef CLOCK_MONOTONIC |
|
|
19 | # define HAVE_MONOTONIC 1 |
|
|
20 | # endif |
|
|
21 | #endif |
|
|
22 | |
|
|
23 | #ifndef HAVE_SELECT |
17 | #define HAVE_SELECT 1 |
24 | # define HAVE_SELECT 1 |
|
|
25 | #endif |
|
|
26 | |
|
|
27 | #ifndef HAVE_EPOLL |
|
|
28 | # define HAVE_EPOLL 0 |
|
|
29 | #endif |
|
|
30 | |
|
|
31 | #ifndef HAVE_REALTIME |
|
|
32 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
|
|
33 | #endif |
18 | |
34 | |
19 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
35 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
20 | #define MAX_BLOCKTIME 60. |
36 | #define MAX_BLOCKTIME 60. |
21 | |
37 | |
22 | #include "ev.h" |
38 | #include "ev.h" |
23 | |
39 | |
24 | struct ev_watcher { |
40 | typedef struct ev_watcher *W; |
25 | EV_WATCHER (ev_watcher); |
|
|
26 | }; |
|
|
27 | |
|
|
28 | struct ev_watcher_list { |
41 | typedef struct ev_watcher_list *WL; |
29 | EV_WATCHER_LIST (ev_watcher_list); |
42 | typedef struct ev_watcher_time *WT; |
30 | }; |
|
|
31 | |
43 | |
32 | static ev_tstamp now, diff; /* monotonic clock */ |
44 | static ev_tstamp now, diff; /* monotonic clock */ |
33 | ev_tstamp ev_now; |
45 | ev_tstamp ev_now; |
34 | int ev_method; |
46 | int ev_method; |
35 | |
47 | |
36 | static int have_monotonic; /* runtime */ |
48 | static int have_monotonic; /* runtime */ |
37 | |
49 | |
38 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
50 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
39 | static void (*method_modify)(int fd, int oev, int nev); |
51 | static void (*method_modify)(int fd, int oev, int nev); |
40 | static void (*method_poll)(ev_tstamp timeout); |
52 | static void (*method_poll)(ev_tstamp timeout); |
|
|
53 | |
|
|
54 | /*****************************************************************************/ |
41 | |
55 | |
42 | ev_tstamp |
56 | ev_tstamp |
43 | ev_time (void) |
57 | ev_time (void) |
44 | { |
58 | { |
45 | #if HAVE_REALTIME |
59 | #if HAVE_REALTIME |
… | |
… | |
70 | |
84 | |
71 | #define array_needsize(base,cur,cnt,init) \ |
85 | #define array_needsize(base,cur,cnt,init) \ |
72 | if ((cnt) > cur) \ |
86 | if ((cnt) > cur) \ |
73 | { \ |
87 | { \ |
74 | int newcnt = cur ? cur << 1 : 16; \ |
88 | int newcnt = cur ? cur << 1 : 16; \ |
75 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
|
|
76 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
89 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
77 | init (base + cur, newcnt - cur); \ |
90 | init (base + cur, newcnt - cur); \ |
78 | cur = newcnt; \ |
91 | cur = newcnt; \ |
79 | } |
92 | } |
80 | |
93 | |
|
|
94 | /*****************************************************************************/ |
|
|
95 | |
81 | typedef struct |
96 | typedef struct |
82 | { |
97 | { |
83 | struct ev_io *head; |
98 | struct ev_io *head; |
84 | unsigned char wev, rev; /* want, received event set */ |
99 | unsigned char wev, rev; /* want, received event set */ |
85 | } ANFD; |
100 | } ANFD; |
… | |
… | |
101 | } |
116 | } |
102 | } |
117 | } |
103 | |
118 | |
104 | typedef struct |
119 | typedef struct |
105 | { |
120 | { |
106 | struct ev_watcher *w; |
121 | W w; |
107 | int events; |
122 | int events; |
108 | } ANPENDING; |
123 | } ANPENDING; |
109 | |
124 | |
110 | static ANPENDING *pendings; |
125 | static ANPENDING *pendings; |
111 | static int pendingmax, pendingcnt; |
126 | static int pendingmax, pendingcnt; |
112 | |
127 | |
113 | static void |
128 | static void |
114 | event (struct ev_watcher *w, int events) |
129 | event (W w, int events) |
115 | { |
130 | { |
|
|
131 | if (w->active) |
|
|
132 | { |
116 | w->pending = ++pendingcnt; |
133 | w->pending = ++pendingcnt; |
117 | array_needsize (pendings, pendingmax, pendingcnt, ); |
134 | array_needsize (pendings, pendingmax, pendingcnt, ); |
118 | pendings [pendingcnt - 1].w = w; |
135 | pendings [pendingcnt - 1].w = w; |
119 | pendings [pendingcnt - 1].events = events; |
136 | pendings [pendingcnt - 1].events = events; |
|
|
137 | } |
120 | } |
138 | } |
121 | |
139 | |
122 | static void |
140 | static void |
123 | fd_event (int fd, int events) |
141 | fd_event (int fd, int events) |
124 | { |
142 | { |
… | |
… | |
128 | for (w = anfd->head; w; w = w->next) |
146 | for (w = anfd->head; w; w = w->next) |
129 | { |
147 | { |
130 | int ev = w->events & events; |
148 | int ev = w->events & events; |
131 | |
149 | |
132 | if (ev) |
150 | if (ev) |
133 | event ((struct ev_watcher *)w, ev); |
151 | event ((W)w, ev); |
134 | } |
152 | } |
135 | } |
153 | } |
136 | |
154 | |
|
|
155 | static void |
|
|
156 | queue_events (W *events, int eventcnt, int type) |
|
|
157 | { |
|
|
158 | int i; |
|
|
159 | |
|
|
160 | for (i = 0; i < eventcnt; ++i) |
|
|
161 | event (events [i], type); |
|
|
162 | } |
|
|
163 | |
|
|
164 | /*****************************************************************************/ |
|
|
165 | |
137 | static struct ev_timer **atimers; |
166 | static struct ev_timer **timers; |
138 | static int atimermax, atimercnt; |
167 | static int timermax, timercnt; |
139 | |
168 | |
140 | static struct ev_timer **rtimers; |
169 | static struct ev_periodic **periodics; |
141 | static int rtimermax, rtimercnt; |
170 | static int periodicmax, periodiccnt; |
142 | |
171 | |
143 | static void |
172 | static void |
144 | upheap (struct ev_timer **timers, int k) |
173 | upheap (WT *timers, int k) |
145 | { |
174 | { |
146 | struct ev_timer *w = timers [k]; |
175 | WT w = timers [k]; |
147 | |
176 | |
148 | while (k && timers [k >> 1]->at > w->at) |
177 | while (k && timers [k >> 1]->at > w->at) |
149 | { |
178 | { |
150 | timers [k] = timers [k >> 1]; |
179 | timers [k] = timers [k >> 1]; |
151 | timers [k]->active = k + 1; |
180 | timers [k]->active = k + 1; |
… | |
… | |
156 | timers [k]->active = k + 1; |
185 | timers [k]->active = k + 1; |
157 | |
186 | |
158 | } |
187 | } |
159 | |
188 | |
160 | static void |
189 | static void |
161 | downheap (struct ev_timer **timers, int N, int k) |
190 | downheap (WT *timers, int N, int k) |
162 | { |
191 | { |
163 | struct ev_timer *w = timers [k]; |
192 | WT w = timers [k]; |
164 | |
193 | |
165 | while (k < (N >> 1)) |
194 | while (k < (N >> 1)) |
166 | { |
195 | { |
167 | int j = k << 1; |
196 | int j = k << 1; |
168 | |
197 | |
… | |
… | |
179 | |
208 | |
180 | timers [k] = w; |
209 | timers [k] = w; |
181 | timers [k]->active = k + 1; |
210 | timers [k]->active = k + 1; |
182 | } |
211 | } |
183 | |
212 | |
184 | static struct ev_signal **signals; |
213 | /*****************************************************************************/ |
|
|
214 | |
|
|
215 | typedef struct |
|
|
216 | { |
|
|
217 | struct ev_signal *head; |
|
|
218 | sig_atomic_t gotsig; |
|
|
219 | } ANSIG; |
|
|
220 | |
|
|
221 | static ANSIG *signals; |
185 | static int signalmax; |
222 | static int signalmax; |
186 | |
223 | |
|
|
224 | static int sigpipe [2]; |
|
|
225 | static sig_atomic_t gotsig; |
|
|
226 | static struct ev_io sigev; |
|
|
227 | |
187 | static void |
228 | static void |
188 | signals_init (struct ev_signal **base, int count) |
229 | signals_init (ANSIG *base, int count) |
189 | { |
230 | { |
190 | while (count--) |
231 | while (count--) |
191 | *base++ = 0; |
232 | { |
|
|
233 | base->head = 0; |
|
|
234 | base->gotsig = 0; |
|
|
235 | ++base; |
|
|
236 | } |
192 | } |
237 | } |
|
|
238 | |
|
|
239 | static void |
|
|
240 | sighandler (int signum) |
|
|
241 | { |
|
|
242 | signals [signum - 1].gotsig = 1; |
|
|
243 | |
|
|
244 | if (!gotsig) |
|
|
245 | { |
|
|
246 | gotsig = 1; |
|
|
247 | write (sigpipe [1], &gotsig, 1); |
|
|
248 | } |
|
|
249 | } |
|
|
250 | |
|
|
251 | static void |
|
|
252 | sigcb (struct ev_io *iow, int revents) |
|
|
253 | { |
|
|
254 | struct ev_signal *w; |
|
|
255 | int sig; |
|
|
256 | |
|
|
257 | gotsig = 0; |
|
|
258 | read (sigpipe [0], &revents, 1); |
|
|
259 | |
|
|
260 | for (sig = signalmax; sig--; ) |
|
|
261 | if (signals [sig].gotsig) |
|
|
262 | { |
|
|
263 | signals [sig].gotsig = 0; |
|
|
264 | |
|
|
265 | for (w = signals [sig].head; w; w = w->next) |
|
|
266 | event ((W)w, EV_SIGNAL); |
|
|
267 | } |
|
|
268 | } |
|
|
269 | |
|
|
270 | static void |
|
|
271 | siginit (void) |
|
|
272 | { |
|
|
273 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
|
|
274 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
|
|
275 | |
|
|
276 | /* rather than sort out wether we really need nb, set it */ |
|
|
277 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
|
|
278 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
279 | |
|
|
280 | evio_set (&sigev, sigpipe [0], EV_READ); |
|
|
281 | evio_start (&sigev); |
|
|
282 | } |
|
|
283 | |
|
|
284 | /*****************************************************************************/ |
|
|
285 | |
|
|
286 | static struct ev_idle **idles; |
|
|
287 | static int idlemax, idlecnt; |
|
|
288 | |
|
|
289 | static struct ev_check **checks; |
|
|
290 | static int checkmax, checkcnt; |
|
|
291 | |
|
|
292 | /*****************************************************************************/ |
193 | |
293 | |
194 | #if HAVE_EPOLL |
294 | #if HAVE_EPOLL |
195 | # include "ev_epoll.c" |
295 | # include "ev_epoll.c" |
196 | #endif |
296 | #endif |
197 | #if HAVE_SELECT |
297 | #if HAVE_SELECT |
… | |
… | |
210 | |
310 | |
211 | ev_now = ev_time (); |
311 | ev_now = ev_time (); |
212 | now = get_clock (); |
312 | now = get_clock (); |
213 | diff = ev_now - now; |
313 | diff = ev_now - now; |
214 | |
314 | |
|
|
315 | if (pipe (sigpipe)) |
|
|
316 | return 0; |
|
|
317 | |
|
|
318 | ev_method = EVMETHOD_NONE; |
215 | #if HAVE_EPOLL |
319 | #if HAVE_EPOLL |
216 | if (epoll_init (flags)) |
320 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
217 | return ev_method; |
|
|
218 | #endif |
321 | #endif |
219 | #if HAVE_SELECT |
322 | #if HAVE_SELECT |
220 | if (select_init (flags)) |
323 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
221 | return ev_method; |
|
|
222 | #endif |
324 | #endif |
223 | |
325 | |
224 | ev_method = EVMETHOD_NONE; |
326 | if (ev_method) |
|
|
327 | { |
|
|
328 | evw_init (&sigev, sigcb); |
|
|
329 | siginit (); |
|
|
330 | } |
|
|
331 | |
225 | return ev_method; |
332 | return ev_method; |
226 | } |
333 | } |
227 | |
334 | |
|
|
335 | /*****************************************************************************/ |
|
|
336 | |
228 | void ev_prefork (void) |
337 | void ev_prefork (void) |
229 | { |
338 | { |
|
|
339 | /* nop */ |
230 | } |
340 | } |
231 | |
341 | |
232 | void ev_postfork_parent (void) |
342 | void ev_postfork_parent (void) |
233 | { |
343 | { |
|
|
344 | /* nop */ |
234 | } |
345 | } |
235 | |
346 | |
236 | void ev_postfork_child (void) |
347 | void ev_postfork_child (void) |
237 | { |
348 | { |
238 | #if HAVE_EPOLL |
349 | #if HAVE_EPOLL |
239 | if (ev_method == EVMETHOD_EPOLL) |
350 | if (ev_method == EVMETHOD_EPOLL) |
240 | epoll_postfork_child (); |
351 | epoll_postfork_child (); |
241 | #endif |
352 | #endif |
|
|
353 | |
|
|
354 | evio_stop (&sigev); |
|
|
355 | close (sigpipe [0]); |
|
|
356 | close (sigpipe [1]); |
|
|
357 | pipe (sigpipe); |
|
|
358 | siginit (); |
242 | } |
359 | } |
|
|
360 | |
|
|
361 | /*****************************************************************************/ |
243 | |
362 | |
244 | static void |
363 | static void |
245 | fd_reify (void) |
364 | fd_reify (void) |
246 | { |
365 | { |
247 | int i; |
366 | int i; |
… | |
… | |
285 | |
404 | |
286 | pendingcnt = 0; |
405 | pendingcnt = 0; |
287 | } |
406 | } |
288 | |
407 | |
289 | static void |
408 | static void |
290 | timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) |
409 | timers_reify () |
291 | { |
410 | { |
292 | while (timercnt && timers [0]->at <= now) |
411 | while (timercnt && timers [0]->at <= now) |
293 | { |
412 | { |
294 | struct ev_timer *w = timers [0]; |
413 | struct ev_timer *w = timers [0]; |
|
|
414 | |
|
|
415 | event ((W)w, EV_TIMEOUT); |
295 | |
416 | |
296 | /* first reschedule or stop timer */ |
417 | /* first reschedule or stop timer */ |
297 | if (w->repeat) |
418 | if (w->repeat) |
298 | { |
419 | { |
299 | if (w->is_abs) |
|
|
300 | w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
301 | else |
|
|
302 | w->at = now + w->repeat; |
420 | w->at = now + w->repeat; |
303 | |
421 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
304 | assert (w->at > now); |
|
|
305 | |
|
|
306 | downheap (timers, timercnt, 0); |
422 | downheap ((WT *)timers, timercnt, 0); |
307 | } |
423 | } |
308 | else |
424 | else |
309 | { |
|
|
310 | evtimer_stop (w); /* nonrepeating: stop timer */ |
425 | evtimer_stop (w); /* nonrepeating: stop timer */ |
311 | --timercnt; /* maybe pass by reference instead? */ |
426 | } |
|
|
427 | } |
|
|
428 | |
|
|
429 | static void |
|
|
430 | periodics_reify () |
|
|
431 | { |
|
|
432 | while (periodiccnt && periodics [0]->at <= ev_now) |
|
|
433 | { |
|
|
434 | struct ev_periodic *w = periodics [0]; |
|
|
435 | |
|
|
436 | /* first reschedule or stop timer */ |
|
|
437 | if (w->interval) |
312 | } |
438 | { |
|
|
439 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
|
|
440 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
|
|
441 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
442 | } |
|
|
443 | else |
|
|
444 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
313 | |
445 | |
314 | event ((struct ev_watcher *)w, EV_TIMEOUT); |
446 | event ((W)w, EV_TIMEOUT); |
|
|
447 | } |
|
|
448 | } |
|
|
449 | |
|
|
450 | static void |
|
|
451 | periodics_reschedule (ev_tstamp diff) |
|
|
452 | { |
|
|
453 | int i; |
|
|
454 | |
|
|
455 | /* adjust periodics after time jump */ |
|
|
456 | for (i = 0; i < periodiccnt; ++i) |
|
|
457 | { |
|
|
458 | struct ev_periodic *w = periodics [i]; |
|
|
459 | |
|
|
460 | if (w->interval) |
|
|
461 | { |
|
|
462 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
463 | |
|
|
464 | if (fabs (diff) >= 1e-4) |
|
|
465 | { |
|
|
466 | evperiodic_stop (w); |
|
|
467 | evperiodic_start (w); |
|
|
468 | |
|
|
469 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
470 | } |
|
|
471 | } |
315 | } |
472 | } |
316 | } |
473 | } |
317 | |
474 | |
318 | static void |
475 | static void |
319 | time_update () |
476 | time_update () |
320 | { |
477 | { |
321 | int i; |
478 | int i; |
|
|
479 | |
322 | ev_now = ev_time (); |
480 | ev_now = ev_time (); |
323 | |
481 | |
324 | if (have_monotonic) |
482 | if (have_monotonic) |
325 | { |
483 | { |
326 | ev_tstamp odiff = diff; |
484 | ev_tstamp odiff = diff; |
327 | |
485 | |
328 | /* detecting time jumps is much more difficult */ |
|
|
329 | for (i = 2; --i; ) /* loop a few times, before making important decisions */ |
486 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
330 | { |
487 | { |
331 | now = get_clock (); |
488 | now = get_clock (); |
332 | diff = ev_now - now; |
489 | diff = ev_now - now; |
333 | |
490 | |
334 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
491 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
335 | return; /* all is well */ |
492 | return; /* all is well */ |
336 | |
493 | |
337 | ev_now = ev_time (); |
494 | ev_now = ev_time (); |
338 | } |
495 | } |
339 | |
496 | |
340 | /* time jump detected, reschedule atimers */ |
497 | periodics_reschedule (diff - odiff); |
341 | for (i = 0; i < atimercnt; ++i) |
498 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
342 | { |
|
|
343 | struct ev_timer *w = atimers [i]; |
|
|
344 | w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
345 | } |
|
|
346 | } |
499 | } |
347 | else |
500 | else |
348 | { |
501 | { |
349 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
502 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
350 | /* time jump detected, adjust rtimers */ |
503 | { |
|
|
504 | periodics_reschedule (ev_now - now); |
|
|
505 | |
|
|
506 | /* adjust timers. this is easy, as the offset is the same for all */ |
351 | for (i = 0; i < rtimercnt; ++i) |
507 | for (i = 0; i < timercnt; ++i) |
352 | rtimers [i]->at += ev_now - now; |
508 | timers [i]->at += diff; |
|
|
509 | } |
353 | |
510 | |
354 | now = ev_now; |
511 | now = ev_now; |
355 | } |
512 | } |
356 | } |
513 | } |
357 | |
514 | |
358 | int ev_loop_done; |
515 | int ev_loop_done; |
359 | |
516 | |
360 | void ev_loop (int flags) |
517 | void ev_loop (int flags) |
361 | { |
518 | { |
362 | double block; |
519 | double block; |
363 | ev_loop_done = flags & EVLOOP_ONESHOT; |
520 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
|
|
521 | |
|
|
522 | if (checkcnt) |
|
|
523 | { |
|
|
524 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
525 | call_pending (); |
|
|
526 | } |
364 | |
527 | |
365 | do |
528 | do |
366 | { |
529 | { |
367 | /* update fd-related kernel structures */ |
530 | /* update fd-related kernel structures */ |
368 | fd_reify (); |
531 | fd_reify (); |
369 | |
532 | |
370 | /* calculate blocking time */ |
533 | /* calculate blocking time */ |
|
|
534 | |
|
|
535 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
|
|
536 | ev_now = ev_time (); |
|
|
537 | |
371 | if (flags & EVLOOP_NONBLOCK) |
538 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
372 | block = 0.; |
539 | block = 0.; |
373 | else |
540 | else |
374 | { |
541 | { |
375 | block = MAX_BLOCKTIME; |
542 | block = MAX_BLOCKTIME; |
376 | |
543 | |
377 | if (rtimercnt) |
544 | if (timercnt) |
378 | { |
545 | { |
379 | ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; |
546 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
380 | if (block > to) block = to; |
547 | if (block > to) block = to; |
381 | } |
548 | } |
382 | |
549 | |
383 | if (atimercnt) |
550 | if (periodiccnt) |
384 | { |
551 | { |
385 | ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; |
552 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
386 | if (block > to) block = to; |
553 | if (block > to) block = to; |
387 | } |
554 | } |
388 | |
555 | |
389 | if (block < 0.) block = 0.; |
556 | if (block < 0.) block = 0.; |
390 | } |
557 | } |
… | |
… | |
392 | method_poll (block); |
559 | method_poll (block); |
393 | |
560 | |
394 | /* update ev_now, do magic */ |
561 | /* update ev_now, do magic */ |
395 | time_update (); |
562 | time_update (); |
396 | |
563 | |
397 | /* put pending timers into pendign queue and reschedule them */ |
564 | /* queue pending timers and reschedule them */ |
398 | /* absolute timers first */ |
565 | periodics_reify (); /* absolute timers first */ |
399 | timers_reify (atimers, atimercnt, ev_now); |
|
|
400 | /* relative timers second */ |
566 | timers_reify (); /* relative timers second */ |
401 | timers_reify (rtimers, rtimercnt, now); |
567 | |
|
|
568 | /* queue idle watchers unless io or timers are pending */ |
|
|
569 | if (!pendingcnt) |
|
|
570 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
|
|
571 | |
|
|
572 | /* queue check and possibly idle watchers */ |
|
|
573 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
402 | |
574 | |
403 | call_pending (); |
575 | call_pending (); |
404 | } |
576 | } |
405 | while (!ev_loop_done); |
577 | while (!ev_loop_done); |
406 | } |
|
|
407 | |
578 | |
|
|
579 | if (ev_loop_done != 2) |
|
|
580 | ev_loop_done = 0; |
|
|
581 | } |
|
|
582 | |
|
|
583 | /*****************************************************************************/ |
|
|
584 | |
408 | static void |
585 | static void |
409 | wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
586 | wlist_add (WL *head, WL elem) |
410 | { |
587 | { |
411 | elem->next = *head; |
588 | elem->next = *head; |
412 | *head = elem; |
589 | *head = elem; |
413 | } |
590 | } |
414 | |
591 | |
415 | static void |
592 | static void |
416 | wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
593 | wlist_del (WL *head, WL elem) |
417 | { |
594 | { |
418 | while (*head) |
595 | while (*head) |
419 | { |
596 | { |
420 | if (*head == elem) |
597 | if (*head == elem) |
421 | { |
598 | { |
… | |
… | |
426 | head = &(*head)->next; |
603 | head = &(*head)->next; |
427 | } |
604 | } |
428 | } |
605 | } |
429 | |
606 | |
430 | static void |
607 | static void |
431 | ev_start (struct ev_watcher *w, int active) |
608 | ev_clear (W w) |
432 | { |
609 | { |
|
|
610 | if (w->pending) |
|
|
611 | { |
|
|
612 | pendings [w->pending - 1].w = 0; |
433 | w->pending = 0; |
613 | w->pending = 0; |
|
|
614 | } |
|
|
615 | } |
|
|
616 | |
|
|
617 | static void |
|
|
618 | ev_start (W w, int active) |
|
|
619 | { |
434 | w->active = active; |
620 | w->active = active; |
435 | } |
621 | } |
436 | |
622 | |
437 | static void |
623 | static void |
438 | ev_stop (struct ev_watcher *w) |
624 | ev_stop (W w) |
439 | { |
625 | { |
440 | if (w->pending) |
|
|
441 | pendings [w->pending - 1].w = 0; |
|
|
442 | |
|
|
443 | w->active = 0; |
626 | w->active = 0; |
444 | /* nop */ |
|
|
445 | } |
627 | } |
|
|
628 | |
|
|
629 | /*****************************************************************************/ |
446 | |
630 | |
447 | void |
631 | void |
448 | evio_start (struct ev_io *w) |
632 | evio_start (struct ev_io *w) |
449 | { |
633 | { |
450 | if (ev_is_active (w)) |
634 | if (ev_is_active (w)) |
451 | return; |
635 | return; |
452 | |
636 | |
453 | int fd = w->fd; |
637 | int fd = w->fd; |
454 | |
638 | |
455 | ev_start ((struct ev_watcher *)w, 1); |
639 | ev_start ((W)w, 1); |
456 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
640 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
457 | wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); |
641 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
458 | |
642 | |
459 | ++fdchangecnt; |
643 | ++fdchangecnt; |
460 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
644 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
461 | fdchanges [fdchangecnt - 1] = fd; |
645 | fdchanges [fdchangecnt - 1] = fd; |
462 | } |
646 | } |
463 | |
647 | |
464 | void |
648 | void |
465 | evio_stop (struct ev_io *w) |
649 | evio_stop (struct ev_io *w) |
466 | { |
650 | { |
|
|
651 | ev_clear ((W)w); |
467 | if (!ev_is_active (w)) |
652 | if (!ev_is_active (w)) |
468 | return; |
653 | return; |
469 | |
654 | |
470 | wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); |
655 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
471 | ev_stop ((struct ev_watcher *)w); |
656 | ev_stop ((W)w); |
472 | |
657 | |
473 | ++fdchangecnt; |
658 | ++fdchangecnt; |
474 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
659 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
475 | fdchanges [fdchangecnt - 1] = w->fd; |
660 | fdchanges [fdchangecnt - 1] = w->fd; |
476 | } |
661 | } |
… | |
… | |
479 | evtimer_start (struct ev_timer *w) |
664 | evtimer_start (struct ev_timer *w) |
480 | { |
665 | { |
481 | if (ev_is_active (w)) |
666 | if (ev_is_active (w)) |
482 | return; |
667 | return; |
483 | |
668 | |
484 | if (w->is_abs) |
669 | w->at += now; |
485 | { |
|
|
486 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
|
|
487 | if (w->repeat) |
|
|
488 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
|
|
489 | |
670 | |
490 | ev_start ((struct ev_watcher *)w, ++atimercnt); |
671 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
|
|
672 | |
|
|
673 | ev_start ((W)w, ++timercnt); |
491 | array_needsize (atimers, atimermax, atimercnt, ); |
674 | array_needsize (timers, timermax, timercnt, ); |
492 | atimers [atimercnt - 1] = w; |
675 | timers [timercnt - 1] = w; |
493 | upheap (atimers, atimercnt - 1); |
676 | upheap ((WT *)timers, timercnt - 1); |
494 | } |
|
|
495 | else |
|
|
496 | { |
|
|
497 | w->at += now; |
|
|
498 | |
|
|
499 | ev_start ((struct ev_watcher *)w, ++rtimercnt); |
|
|
500 | array_needsize (rtimers, rtimermax, rtimercnt, ); |
|
|
501 | rtimers [rtimercnt - 1] = w; |
|
|
502 | upheap (rtimers, rtimercnt - 1); |
|
|
503 | } |
|
|
504 | |
|
|
505 | } |
677 | } |
506 | |
678 | |
507 | void |
679 | void |
508 | evtimer_stop (struct ev_timer *w) |
680 | evtimer_stop (struct ev_timer *w) |
509 | { |
681 | { |
|
|
682 | ev_clear ((W)w); |
510 | if (!ev_is_active (w)) |
683 | if (!ev_is_active (w)) |
511 | return; |
684 | return; |
512 | |
685 | |
513 | if (w->is_abs) |
|
|
514 | { |
|
|
515 | if (w->active < atimercnt--) |
686 | if (w->active < timercnt--) |
516 | { |
687 | { |
517 | atimers [w->active - 1] = atimers [atimercnt]; |
688 | timers [w->active - 1] = timers [timercnt]; |
|
|
689 | downheap ((WT *)timers, timercnt, w->active - 1); |
|
|
690 | } |
|
|
691 | |
|
|
692 | w->at = w->repeat; |
|
|
693 | |
|
|
694 | ev_stop ((W)w); |
|
|
695 | } |
|
|
696 | |
|
|
697 | void |
|
|
698 | evtimer_again (struct ev_timer *w) |
|
|
699 | { |
|
|
700 | if (ev_is_active (w)) |
|
|
701 | { |
|
|
702 | if (w->repeat) |
|
|
703 | { |
|
|
704 | w->at = now + w->repeat; |
518 | downheap (atimers, atimercnt, w->active - 1); |
705 | downheap ((WT *)timers, timercnt, w->active - 1); |
519 | } |
|
|
520 | } |
|
|
521 | else |
|
|
522 | { |
|
|
523 | if (w->active < rtimercnt--) |
|
|
524 | { |
706 | } |
525 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
707 | else |
526 | downheap (rtimers, rtimercnt, w->active - 1); |
708 | evtimer_stop (w); |
527 | } |
709 | } |
|
|
710 | else if (w->repeat) |
|
|
711 | evtimer_start (w); |
|
|
712 | } |
|
|
713 | |
|
|
714 | void |
|
|
715 | evperiodic_start (struct ev_periodic *w) |
|
|
716 | { |
|
|
717 | if (ev_is_active (w)) |
|
|
718 | return; |
|
|
719 | |
|
|
720 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
|
|
721 | |
|
|
722 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
|
|
723 | if (w->interval) |
|
|
724 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
725 | |
|
|
726 | ev_start ((W)w, ++periodiccnt); |
|
|
727 | array_needsize (periodics, periodicmax, periodiccnt, ); |
|
|
728 | periodics [periodiccnt - 1] = w; |
|
|
729 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
730 | } |
|
|
731 | |
|
|
732 | void |
|
|
733 | evperiodic_stop (struct ev_periodic *w) |
|
|
734 | { |
|
|
735 | ev_clear ((W)w); |
|
|
736 | if (!ev_is_active (w)) |
|
|
737 | return; |
|
|
738 | |
|
|
739 | if (w->active < periodiccnt--) |
528 | } |
740 | { |
|
|
741 | periodics [w->active - 1] = periodics [periodiccnt]; |
|
|
742 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
|
|
743 | } |
529 | |
744 | |
530 | ev_stop ((struct ev_watcher *)w); |
745 | ev_stop ((W)w); |
531 | } |
746 | } |
532 | |
747 | |
533 | void |
748 | void |
534 | evsignal_start (struct ev_signal *w) |
749 | evsignal_start (struct ev_signal *w) |
535 | { |
750 | { |
536 | if (ev_is_active (w)) |
751 | if (ev_is_active (w)) |
537 | return; |
752 | return; |
538 | |
753 | |
539 | ev_start ((struct ev_watcher *)w, 1); |
754 | ev_start ((W)w, 1); |
540 | array_needsize (signals, signalmax, w->signum, signals_init); |
755 | array_needsize (signals, signalmax, w->signum, signals_init); |
541 | wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); |
756 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
|
|
757 | |
|
|
758 | if (!w->next) |
|
|
759 | { |
|
|
760 | struct sigaction sa; |
|
|
761 | sa.sa_handler = sighandler; |
|
|
762 | sigfillset (&sa.sa_mask); |
|
|
763 | sa.sa_flags = 0; |
|
|
764 | sigaction (w->signum, &sa, 0); |
|
|
765 | } |
542 | } |
766 | } |
543 | |
767 | |
544 | void |
768 | void |
545 | evsignal_stop (struct ev_signal *w) |
769 | evsignal_stop (struct ev_signal *w) |
546 | { |
770 | { |
|
|
771 | ev_clear ((W)w); |
547 | if (!ev_is_active (w)) |
772 | if (!ev_is_active (w)) |
548 | return; |
773 | return; |
549 | |
774 | |
550 | wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); |
775 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
551 | ev_stop ((struct ev_watcher *)w); |
776 | ev_stop ((W)w); |
552 | } |
|
|
553 | |
777 | |
|
|
778 | if (!signals [w->signum - 1].head) |
|
|
779 | signal (w->signum, SIG_DFL); |
|
|
780 | } |
|
|
781 | |
|
|
782 | void evidle_start (struct ev_idle *w) |
|
|
783 | { |
|
|
784 | if (ev_is_active (w)) |
|
|
785 | return; |
|
|
786 | |
|
|
787 | ev_start ((W)w, ++idlecnt); |
|
|
788 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
789 | idles [idlecnt - 1] = w; |
|
|
790 | } |
|
|
791 | |
|
|
792 | void evidle_stop (struct ev_idle *w) |
|
|
793 | { |
|
|
794 | ev_clear ((W)w); |
|
|
795 | if (ev_is_active (w)) |
|
|
796 | return; |
|
|
797 | |
|
|
798 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
799 | ev_stop ((W)w); |
|
|
800 | } |
|
|
801 | |
|
|
802 | void evcheck_start (struct ev_check *w) |
|
|
803 | { |
|
|
804 | if (ev_is_active (w)) |
|
|
805 | return; |
|
|
806 | |
|
|
807 | ev_start ((W)w, ++checkcnt); |
|
|
808 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
809 | checks [checkcnt - 1] = w; |
|
|
810 | } |
|
|
811 | |
|
|
812 | void evcheck_stop (struct ev_check *w) |
|
|
813 | { |
|
|
814 | ev_clear ((W)w); |
|
|
815 | if (ev_is_active (w)) |
|
|
816 | return; |
|
|
817 | |
|
|
818 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
819 | ev_stop ((W)w); |
|
|
820 | } |
|
|
821 | |
554 | /*****************************************************************************/ |
822 | /*****************************************************************************/ |
|
|
823 | |
|
|
824 | struct ev_once |
|
|
825 | { |
|
|
826 | struct ev_io io; |
|
|
827 | struct ev_timer to; |
|
|
828 | void (*cb)(int revents, void *arg); |
|
|
829 | void *arg; |
|
|
830 | }; |
|
|
831 | |
|
|
832 | static void |
|
|
833 | once_cb (struct ev_once *once, int revents) |
|
|
834 | { |
|
|
835 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
836 | void *arg = once->arg; |
|
|
837 | |
|
|
838 | evio_stop (&once->io); |
|
|
839 | evtimer_stop (&once->to); |
|
|
840 | free (once); |
|
|
841 | |
|
|
842 | cb (revents, arg); |
|
|
843 | } |
|
|
844 | |
|
|
845 | static void |
|
|
846 | once_cb_io (struct ev_io *w, int revents) |
|
|
847 | { |
|
|
848 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
849 | } |
|
|
850 | |
|
|
851 | static void |
|
|
852 | once_cb_to (struct ev_timer *w, int revents) |
|
|
853 | { |
|
|
854 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
855 | } |
|
|
856 | |
|
|
857 | void |
|
|
858 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
859 | { |
|
|
860 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
861 | |
|
|
862 | if (!once) |
|
|
863 | cb (EV_ERROR, arg); |
|
|
864 | else |
|
|
865 | { |
|
|
866 | once->cb = cb; |
|
|
867 | once->arg = arg; |
|
|
868 | |
|
|
869 | evw_init (&once->io, once_cb_io); |
|
|
870 | |
|
|
871 | if (fd >= 0) |
|
|
872 | { |
|
|
873 | evio_set (&once->io, fd, events); |
|
|
874 | evio_start (&once->io); |
|
|
875 | } |
|
|
876 | |
|
|
877 | evw_init (&once->to, once_cb_to); |
|
|
878 | |
|
|
879 | if (timeout >= 0.) |
|
|
880 | { |
|
|
881 | evtimer_set (&once->to, timeout, 0.); |
|
|
882 | evtimer_start (&once->to); |
|
|
883 | } |
|
|
884 | } |
|
|
885 | } |
|
|
886 | |
|
|
887 | /*****************************************************************************/ |
|
|
888 | |
555 | #if 1 |
889 | #if 0 |
|
|
890 | |
|
|
891 | struct ev_io wio; |
556 | |
892 | |
557 | static void |
893 | static void |
558 | sin_cb (struct ev_io *w, int revents) |
894 | sin_cb (struct ev_io *w, int revents) |
559 | { |
895 | { |
560 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
896 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
… | |
… | |
566 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
902 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
567 | evtimer_stop (w); |
903 | evtimer_stop (w); |
568 | evtimer_start (w); |
904 | evtimer_start (w); |
569 | } |
905 | } |
570 | |
906 | |
|
|
907 | static void |
|
|
908 | scb (struct ev_signal *w, int revents) |
|
|
909 | { |
|
|
910 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
911 | evio_stop (&wio); |
|
|
912 | evio_start (&wio); |
|
|
913 | } |
|
|
914 | |
|
|
915 | static void |
|
|
916 | gcb (struct ev_signal *w, int revents) |
|
|
917 | { |
|
|
918 | fprintf (stderr, "generic %x\n", revents); |
|
|
919 | |
|
|
920 | } |
|
|
921 | |
571 | int main (void) |
922 | int main (void) |
572 | { |
923 | { |
573 | struct ev_io sin; |
|
|
574 | |
|
|
575 | ev_init (0); |
924 | ev_init (0); |
576 | |
925 | |
577 | evw_init (&sin, sin_cb, 55); |
|
|
578 | evio_set (&sin, 0, EV_READ); |
926 | evio_init (&wio, sin_cb, 0, EV_READ); |
579 | evio_start (&sin); |
927 | evio_start (&wio); |
580 | |
928 | |
581 | struct ev_timer t[10000]; |
929 | struct ev_timer t[10000]; |
582 | |
930 | |
583 | #if 1 |
931 | #if 0 |
584 | int i; |
932 | int i; |
585 | for (i = 0; i < 10000; ++i) |
933 | for (i = 0; i < 10000; ++i) |
586 | { |
934 | { |
587 | struct ev_timer *w = t + i; |
935 | struct ev_timer *w = t + i; |
588 | evw_init (w, ocb, i); |
936 | evw_init (w, ocb, i); |
589 | evtimer_set_abs (w, drand48 (), 0.99775533); |
937 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
590 | evtimer_start (w); |
938 | evtimer_start (w); |
591 | if (drand48 () < 0.5) |
939 | if (drand48 () < 0.5) |
592 | evtimer_stop (w); |
940 | evtimer_stop (w); |
593 | } |
941 | } |
594 | #endif |
942 | #endif |
595 | |
943 | |
596 | struct ev_timer t1; |
944 | struct ev_timer t1; |
597 | evw_init (&t1, ocb, 0); |
945 | evtimer_init (&t1, ocb, 5, 10); |
598 | evtimer_set_abs (&t1, 5, 10); |
|
|
599 | evtimer_start (&t1); |
946 | evtimer_start (&t1); |
600 | |
947 | |
|
|
948 | struct ev_signal sig; |
|
|
949 | evsignal_init (&sig, scb, SIGQUIT); |
|
|
950 | evsignal_start (&sig); |
|
|
951 | |
|
|
952 | struct ev_check cw; |
|
|
953 | evcheck_init (&cw, gcb); |
|
|
954 | evcheck_start (&cw); |
|
|
955 | |
|
|
956 | struct ev_idle iw; |
|
|
957 | evidle_init (&iw, gcb); |
|
|
958 | evidle_start (&iw); |
|
|
959 | |
601 | ev_loop (0); |
960 | ev_loop (0); |
602 | |
961 | |
603 | return 0; |
962 | return 0; |
604 | } |
963 | } |
605 | |
964 | |