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