… | |
… | |
9 | #include <assert.h> |
9 | #include <assert.h> |
10 | #include <errno.h> |
10 | #include <errno.h> |
11 | #include <sys/time.h> |
11 | #include <sys/time.h> |
12 | #include <time.h> |
12 | #include <time.h> |
13 | |
13 | |
14 | #ifdef CLOCK_MONOTONIC |
|
|
15 | # define HAVE_MONOTONIC 1 |
|
|
16 | #endif |
|
|
17 | |
|
|
18 | #define HAVE_REALTIME 1 |
|
|
19 | #define HAVE_EPOLL 1 |
14 | #define HAVE_EPOLL 1 |
|
|
15 | |
|
|
16 | #ifndef HAVE_MONOTONIC |
|
|
17 | # ifdef CLOCK_MONOTONIC |
|
|
18 | # define HAVE_MONOTONIC 1 |
|
|
19 | # endif |
|
|
20 | #endif |
|
|
21 | |
|
|
22 | #ifndef HAVE_SELECT |
20 | #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 |
21 | |
33 | |
22 | #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) */ |
23 | #define MAX_BLOCKTIME 60. |
35 | #define MAX_BLOCKTIME 60. |
24 | |
36 | |
25 | #include "ev.h" |
37 | #include "ev.h" |
26 | |
38 | |
27 | struct ev_watcher { |
39 | typedef struct ev_watcher *W; |
28 | EV_WATCHER (ev_watcher); |
|
|
29 | }; |
|
|
30 | |
|
|
31 | struct ev_watcher_list { |
40 | typedef struct ev_watcher_list *WL; |
32 | EV_WATCHER_LIST (ev_watcher_list); |
41 | typedef struct ev_watcher_time *WT; |
33 | }; |
|
|
34 | |
42 | |
35 | static ev_tstamp now, diff; /* monotonic clock */ |
43 | static ev_tstamp now, diff; /* monotonic clock */ |
36 | ev_tstamp ev_now; |
44 | ev_tstamp ev_now; |
37 | int ev_method; |
45 | int ev_method; |
38 | |
46 | |
… | |
… | |
75 | |
83 | |
76 | #define array_needsize(base,cur,cnt,init) \ |
84 | #define array_needsize(base,cur,cnt,init) \ |
77 | if ((cnt) > cur) \ |
85 | if ((cnt) > cur) \ |
78 | { \ |
86 | { \ |
79 | int newcnt = cur ? cur << 1 : 16; \ |
87 | int newcnt = cur ? cur << 1 : 16; \ |
80 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
|
|
81 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
88 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
82 | init (base + cur, newcnt - cur); \ |
89 | init (base + cur, newcnt - cur); \ |
83 | cur = newcnt; \ |
90 | cur = newcnt; \ |
84 | } |
91 | } |
85 | |
92 | |
… | |
… | |
108 | } |
115 | } |
109 | } |
116 | } |
110 | |
117 | |
111 | typedef struct |
118 | typedef struct |
112 | { |
119 | { |
113 | struct ev_watcher *w; |
120 | W w; |
114 | int events; |
121 | int events; |
115 | } ANPENDING; |
122 | } ANPENDING; |
116 | |
123 | |
117 | static ANPENDING *pendings; |
124 | static ANPENDING *pendings; |
118 | static int pendingmax, pendingcnt; |
125 | static int pendingmax, pendingcnt; |
119 | |
126 | |
120 | static void |
127 | static void |
121 | event (struct ev_watcher *w, int events) |
128 | event (W w, int events) |
122 | { |
129 | { |
123 | w->pending = ++pendingcnt; |
130 | w->pending = ++pendingcnt; |
124 | array_needsize (pendings, pendingmax, pendingcnt, ); |
131 | array_needsize (pendings, pendingmax, pendingcnt, ); |
125 | pendings [pendingcnt - 1].w = w; |
132 | pendings [pendingcnt - 1].w = w; |
126 | pendings [pendingcnt - 1].events = events; |
133 | pendings [pendingcnt - 1].events = events; |
… | |
… | |
135 | for (w = anfd->head; w; w = w->next) |
142 | for (w = anfd->head; w; w = w->next) |
136 | { |
143 | { |
137 | int ev = w->events & events; |
144 | int ev = w->events & events; |
138 | |
145 | |
139 | if (ev) |
146 | if (ev) |
140 | event ((struct ev_watcher *)w, ev); |
147 | event ((W)w, ev); |
141 | } |
148 | } |
142 | } |
149 | } |
143 | |
150 | |
144 | /*****************************************************************************/ |
151 | static void |
|
|
152 | queue_events (W *events, int eventcnt, int type) |
|
|
153 | { |
|
|
154 | int i; |
145 | |
155 | |
|
|
156 | for (i = 0; i < eventcnt; ++i) |
|
|
157 | event (events [i], type); |
|
|
158 | } |
|
|
159 | |
|
|
160 | /*****************************************************************************/ |
|
|
161 | |
146 | static struct ev_timer **atimers; |
162 | static struct ev_timer **timers; |
147 | static int atimermax, atimercnt; |
163 | static int timermax, timercnt; |
148 | |
164 | |
149 | static struct ev_timer **rtimers; |
165 | static struct ev_periodic **periodics; |
150 | static int rtimermax, rtimercnt; |
166 | static int periodicmax, periodiccnt; |
151 | |
167 | |
152 | static void |
168 | static void |
153 | upheap (struct ev_timer **timers, int k) |
169 | upheap (WT *timers, int k) |
154 | { |
170 | { |
155 | struct ev_timer *w = timers [k]; |
171 | WT w = timers [k]; |
156 | |
172 | |
157 | while (k && timers [k >> 1]->at > w->at) |
173 | while (k && timers [k >> 1]->at > w->at) |
158 | { |
174 | { |
159 | timers [k] = timers [k >> 1]; |
175 | timers [k] = timers [k >> 1]; |
160 | timers [k]->active = k + 1; |
176 | timers [k]->active = k + 1; |
… | |
… | |
165 | timers [k]->active = k + 1; |
181 | timers [k]->active = k + 1; |
166 | |
182 | |
167 | } |
183 | } |
168 | |
184 | |
169 | static void |
185 | static void |
170 | downheap (struct ev_timer **timers, int N, int k) |
186 | downheap (WT *timers, int N, int k) |
171 | { |
187 | { |
172 | struct ev_timer *w = timers [k]; |
188 | WT w = timers [k]; |
173 | |
189 | |
174 | while (k < (N >> 1)) |
190 | while (k < (N >> 1)) |
175 | { |
191 | { |
176 | int j = k << 1; |
192 | int j = k << 1; |
177 | |
193 | |
… | |
… | |
241 | if (signals [sig].gotsig) |
257 | if (signals [sig].gotsig) |
242 | { |
258 | { |
243 | signals [sig].gotsig = 0; |
259 | signals [sig].gotsig = 0; |
244 | |
260 | |
245 | for (w = signals [sig].head; w; w = w->next) |
261 | for (w = signals [sig].head; w; w = w->next) |
246 | event ((struct ev_watcher *)w, EV_SIGNAL); |
262 | event ((W)w, EV_SIGNAL); |
247 | } |
263 | } |
248 | } |
264 | } |
249 | |
265 | |
250 | static void |
266 | static void |
251 | siginit (void) |
267 | siginit (void) |
… | |
… | |
258 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
274 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
259 | |
275 | |
260 | evio_set (&sigev, sigpipe [0], EV_READ); |
276 | evio_set (&sigev, sigpipe [0], EV_READ); |
261 | evio_start (&sigev); |
277 | evio_start (&sigev); |
262 | } |
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; |
263 | |
287 | |
264 | /*****************************************************************************/ |
288 | /*****************************************************************************/ |
265 | |
289 | |
266 | #if HAVE_EPOLL |
290 | #if HAVE_EPOLL |
267 | # include "ev_epoll.c" |
291 | # include "ev_epoll.c" |
… | |
… | |
295 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
319 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
296 | #endif |
320 | #endif |
297 | |
321 | |
298 | if (ev_method) |
322 | if (ev_method) |
299 | { |
323 | { |
300 | evw_init (&sigev, sigcb, 0); |
324 | evw_init (&sigev, sigcb); |
301 | siginit (); |
325 | siginit (); |
302 | } |
326 | } |
303 | |
327 | |
304 | return ev_method; |
328 | return ev_method; |
305 | } |
329 | } |
306 | |
330 | |
307 | /*****************************************************************************/ |
331 | /*****************************************************************************/ |
308 | |
332 | |
309 | void ev_prefork (void) |
333 | void ev_prefork (void) |
310 | { |
334 | { |
|
|
335 | /* nop */ |
311 | } |
336 | } |
312 | |
337 | |
313 | void ev_postfork_parent (void) |
338 | void ev_postfork_parent (void) |
314 | { |
339 | { |
|
|
340 | /* nop */ |
315 | } |
341 | } |
316 | |
342 | |
317 | void ev_postfork_child (void) |
343 | void ev_postfork_child (void) |
318 | { |
344 | { |
319 | #if HAVE_EPOLL |
345 | #if HAVE_EPOLL |
… | |
… | |
328 | siginit (); |
354 | siginit (); |
329 | } |
355 | } |
330 | |
356 | |
331 | /*****************************************************************************/ |
357 | /*****************************************************************************/ |
332 | |
358 | |
333 | static ev_hook hooks [EVHOOK_NUM]; |
|
|
334 | |
|
|
335 | void |
|
|
336 | ev_hook_register (int type, ev_hook hook) |
|
|
337 | { |
|
|
338 | hooks [type] = hook; |
|
|
339 | } |
|
|
340 | |
|
|
341 | void |
|
|
342 | ev_hook_unregister (int type, ev_hook hook) |
|
|
343 | { |
|
|
344 | hooks [type] = 0; |
|
|
345 | } |
|
|
346 | |
|
|
347 | static void |
|
|
348 | hook_call (int type) |
|
|
349 | { |
|
|
350 | if (hooks [type]) |
|
|
351 | hooks [type] (); |
|
|
352 | } |
|
|
353 | |
|
|
354 | static void |
359 | static void |
355 | fd_reify (void) |
360 | fd_reify (void) |
356 | { |
361 | { |
357 | int i; |
362 | int i; |
358 | |
363 | |
… | |
… | |
395 | |
400 | |
396 | pendingcnt = 0; |
401 | pendingcnt = 0; |
397 | } |
402 | } |
398 | |
403 | |
399 | static void |
404 | static void |
400 | timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) |
405 | timers_reify () |
401 | { |
406 | { |
402 | while (timercnt && timers [0]->at <= now) |
407 | while (timercnt && timers [0]->at <= now) |
403 | { |
408 | { |
404 | struct ev_timer *w = timers [0]; |
409 | struct ev_timer *w = timers [0]; |
405 | |
410 | |
406 | /* first reschedule or stop timer */ |
411 | /* first reschedule or stop timer */ |
407 | if (w->repeat) |
412 | if (w->repeat) |
408 | { |
413 | { |
409 | if (w->is_abs) |
|
|
410 | w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
411 | else |
|
|
412 | w->at = now + w->repeat; |
414 | w->at = now + w->repeat; |
413 | |
415 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
414 | assert (w->at > now); |
|
|
415 | |
|
|
416 | downheap (timers, timercnt, 0); |
416 | downheap ((WT *)timers, timercnt, 0); |
417 | } |
417 | } |
418 | else |
418 | else |
419 | { |
|
|
420 | evtimer_stop (w); /* nonrepeating: stop timer */ |
419 | evtimer_stop (w); /* nonrepeating: stop timer */ |
421 | --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) |
422 | } |
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 */ |
423 | |
441 | |
424 | 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 | } |
425 | } |
468 | } |
426 | } |
469 | } |
427 | |
470 | |
428 | static void |
471 | static void |
429 | time_update () |
472 | time_update () |
430 | { |
473 | { |
431 | int i; |
474 | int i; |
|
|
475 | |
432 | ev_now = ev_time (); |
476 | ev_now = ev_time (); |
433 | |
477 | |
434 | if (have_monotonic) |
478 | if (have_monotonic) |
435 | { |
479 | { |
436 | ev_tstamp odiff = diff; |
480 | ev_tstamp odiff = diff; |
437 | |
481 | |
438 | /* detecting time jumps is much more difficult */ |
|
|
439 | 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 */ |
440 | { |
483 | { |
441 | now = get_clock (); |
484 | now = get_clock (); |
442 | diff = ev_now - now; |
485 | diff = ev_now - now; |
443 | |
486 | |
444 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
487 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
445 | return; /* all is well */ |
488 | return; /* all is well */ |
446 | |
489 | |
447 | ev_now = ev_time (); |
490 | ev_now = ev_time (); |
448 | } |
491 | } |
449 | |
492 | |
450 | /* time jump detected, reschedule atimers */ |
493 | periodics_reschedule (diff - odiff); |
451 | for (i = 0; i < atimercnt; ++i) |
494 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
452 | { |
|
|
453 | struct ev_timer *w = atimers [i]; |
|
|
454 | w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
455 | } |
|
|
456 | } |
495 | } |
457 | else |
496 | else |
458 | { |
497 | { |
459 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
498 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
460 | /* 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 */ |
461 | for (i = 0; i < rtimercnt; ++i) |
503 | for (i = 0; i < timercnt; ++i) |
462 | rtimers [i]->at += ev_now - now; |
504 | timers [i]->at += diff; |
|
|
505 | } |
463 | |
506 | |
464 | now = ev_now; |
507 | now = ev_now; |
465 | } |
508 | } |
466 | } |
509 | } |
467 | |
510 | |
468 | int ev_loop_done; |
511 | int ev_loop_done; |
469 | |
512 | |
470 | void ev_loop (int flags) |
513 | void ev_loop (int flags) |
471 | { |
514 | { |
472 | double block; |
515 | double block; |
473 | 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 | } |
474 | |
523 | |
475 | do |
524 | do |
476 | { |
525 | { |
477 | hook_call (EVHOOK_PREPOLL); |
|
|
478 | |
|
|
479 | /* update fd-related kernel structures */ |
526 | /* update fd-related kernel structures */ |
480 | fd_reify (); |
527 | fd_reify (); |
481 | |
528 | |
482 | /* 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 | |
483 | if (flags & EVLOOP_NONBLOCK) |
534 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
484 | block = 0.; |
535 | block = 0.; |
485 | else |
536 | else |
486 | { |
537 | { |
487 | block = MAX_BLOCKTIME; |
538 | block = MAX_BLOCKTIME; |
488 | |
539 | |
489 | if (rtimercnt) |
540 | if (timercnt) |
490 | { |
541 | { |
491 | 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; |
492 | if (block > to) block = to; |
543 | if (block > to) block = to; |
493 | } |
544 | } |
494 | |
545 | |
495 | if (atimercnt) |
546 | if (periodiccnt) |
496 | { |
547 | { |
497 | ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; |
548 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
498 | if (block > to) block = to; |
549 | if (block > to) block = to; |
499 | } |
550 | } |
500 | |
551 | |
501 | if (block < 0.) block = 0.; |
552 | if (block < 0.) block = 0.; |
502 | } |
553 | } |
… | |
… | |
504 | method_poll (block); |
555 | method_poll (block); |
505 | |
556 | |
506 | /* update ev_now, do magic */ |
557 | /* update ev_now, do magic */ |
507 | time_update (); |
558 | time_update (); |
508 | |
559 | |
509 | hook_call (EVHOOK_POSTPOLL); |
|
|
510 | |
|
|
511 | /* put pending timers into pendign queue and reschedule them */ |
560 | /* queue pending timers and reschedule them */ |
512 | /* absolute timers first */ |
561 | periodics_reify (); /* absolute timers first */ |
513 | timers_reify (atimers, atimercnt, ev_now); |
|
|
514 | /* relative timers second */ |
562 | timers_reify (); /* relative timers second */ |
515 | 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); |
516 | |
570 | |
517 | call_pending (); |
571 | call_pending (); |
518 | } |
572 | } |
519 | while (!ev_loop_done); |
573 | while (!ev_loop_done); |
520 | } |
|
|
521 | |
574 | |
522 | /*****************************************************************************/ |
575 | if (ev_loop_done != 2) |
|
|
576 | ev_loop_done = 0; |
|
|
577 | } |
523 | |
578 | |
|
|
579 | /*****************************************************************************/ |
|
|
580 | |
524 | static void |
581 | static void |
525 | wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
582 | wlist_add (WL *head, WL elem) |
526 | { |
583 | { |
527 | elem->next = *head; |
584 | elem->next = *head; |
528 | *head = elem; |
585 | *head = elem; |
529 | } |
586 | } |
530 | |
587 | |
531 | static void |
588 | static void |
532 | wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
589 | wlist_del (WL *head, WL elem) |
533 | { |
590 | { |
534 | while (*head) |
591 | while (*head) |
535 | { |
592 | { |
536 | if (*head == elem) |
593 | if (*head == elem) |
537 | { |
594 | { |
… | |
… | |
542 | head = &(*head)->next; |
599 | head = &(*head)->next; |
543 | } |
600 | } |
544 | } |
601 | } |
545 | |
602 | |
546 | static void |
603 | static void |
547 | ev_start (struct ev_watcher *w, int active) |
604 | ev_start (W w, int active) |
548 | { |
605 | { |
549 | w->pending = 0; |
606 | w->pending = 0; |
550 | w->active = active; |
607 | w->active = active; |
551 | } |
608 | } |
552 | |
609 | |
553 | static void |
610 | static void |
554 | ev_stop (struct ev_watcher *w) |
611 | ev_stop (W w) |
555 | { |
612 | { |
556 | if (w->pending) |
613 | if (w->pending) |
557 | pendings [w->pending - 1].w = 0; |
614 | pendings [w->pending - 1].w = 0; |
558 | |
615 | |
559 | w->active = 0; |
616 | w->active = 0; |
560 | /* nop */ |
|
|
561 | } |
617 | } |
562 | |
618 | |
563 | /*****************************************************************************/ |
619 | /*****************************************************************************/ |
564 | |
620 | |
565 | void |
621 | void |
… | |
… | |
568 | if (ev_is_active (w)) |
624 | if (ev_is_active (w)) |
569 | return; |
625 | return; |
570 | |
626 | |
571 | int fd = w->fd; |
627 | int fd = w->fd; |
572 | |
628 | |
573 | ev_start ((struct ev_watcher *)w, 1); |
629 | ev_start ((W)w, 1); |
574 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
630 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
575 | wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); |
631 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
576 | |
632 | |
577 | ++fdchangecnt; |
633 | ++fdchangecnt; |
578 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
634 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
579 | fdchanges [fdchangecnt - 1] = fd; |
635 | fdchanges [fdchangecnt - 1] = fd; |
580 | } |
636 | } |
… | |
… | |
583 | evio_stop (struct ev_io *w) |
639 | evio_stop (struct ev_io *w) |
584 | { |
640 | { |
585 | if (!ev_is_active (w)) |
641 | if (!ev_is_active (w)) |
586 | return; |
642 | return; |
587 | |
643 | |
588 | 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); |
589 | ev_stop ((struct ev_watcher *)w); |
645 | ev_stop ((W)w); |
590 | |
646 | |
591 | ++fdchangecnt; |
647 | ++fdchangecnt; |
592 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
648 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
593 | fdchanges [fdchangecnt - 1] = w->fd; |
649 | fdchanges [fdchangecnt - 1] = w->fd; |
594 | } |
650 | } |
595 | |
651 | |
|
|
652 | |
596 | void |
653 | void |
597 | evtimer_start (struct ev_timer *w) |
654 | evtimer_start (struct ev_timer *w) |
598 | { |
655 | { |
599 | if (ev_is_active (w)) |
656 | if (ev_is_active (w)) |
600 | return; |
657 | return; |
601 | |
658 | |
602 | if (w->is_abs) |
659 | w->at += now; |
603 | { |
|
|
604 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
|
|
605 | if (w->repeat) |
|
|
606 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
|
|
607 | |
660 | |
608 | 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); |
609 | array_needsize (atimers, atimermax, atimercnt, ); |
664 | array_needsize (timers, timermax, timercnt, ); |
610 | atimers [atimercnt - 1] = w; |
665 | timers [timercnt - 1] = w; |
611 | upheap (atimers, atimercnt - 1); |
666 | upheap ((WT *)timers, timercnt - 1); |
612 | } |
|
|
613 | else |
|
|
614 | { |
|
|
615 | w->at += now; |
|
|
616 | |
|
|
617 | ev_start ((struct ev_watcher *)w, ++rtimercnt); |
|
|
618 | array_needsize (rtimers, rtimermax, rtimercnt, ); |
|
|
619 | rtimers [rtimercnt - 1] = w; |
|
|
620 | upheap (rtimers, rtimercnt - 1); |
|
|
621 | } |
|
|
622 | |
|
|
623 | } |
667 | } |
624 | |
668 | |
625 | void |
669 | void |
626 | evtimer_stop (struct ev_timer *w) |
670 | evtimer_stop (struct ev_timer *w) |
627 | { |
671 | { |
628 | if (!ev_is_active (w)) |
672 | if (!ev_is_active (w)) |
629 | return; |
673 | return; |
630 | |
674 | |
631 | if (w->is_abs) |
|
|
632 | { |
|
|
633 | if (w->active < atimercnt--) |
675 | if (w->active < timercnt--) |
634 | { |
676 | { |
635 | 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; |
636 | downheap (atimers, atimercnt, w->active - 1); |
694 | downheap ((WT *)timers, timercnt, w->active - 1); |
637 | } |
|
|
638 | } |
|
|
639 | else |
|
|
640 | { |
|
|
641 | if (w->active < rtimercnt--) |
|
|
642 | { |
695 | } |
643 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
696 | else |
644 | downheap (rtimers, rtimercnt, w->active - 1); |
697 | evtimer_stop (w); |
645 | } |
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--) |
646 | } |
728 | { |
|
|
729 | periodics [w->active - 1] = periodics [periodiccnt]; |
|
|
730 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
|
|
731 | } |
647 | |
732 | |
648 | ev_stop ((struct ev_watcher *)w); |
733 | ev_stop ((W)w); |
649 | } |
734 | } |
650 | |
735 | |
651 | void |
736 | void |
652 | evsignal_start (struct ev_signal *w) |
737 | evsignal_start (struct ev_signal *w) |
653 | { |
738 | { |
654 | if (ev_is_active (w)) |
739 | if (ev_is_active (w)) |
655 | return; |
740 | return; |
656 | |
741 | |
657 | ev_start ((struct ev_watcher *)w, 1); |
742 | ev_start ((W)w, 1); |
658 | array_needsize (signals, signalmax, w->signum, signals_init); |
743 | array_needsize (signals, signalmax, w->signum, signals_init); |
659 | wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); |
744 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
660 | |
745 | |
661 | if (!w->next) |
746 | if (!w->next) |
662 | { |
747 | { |
663 | struct sigaction sa; |
748 | struct sigaction sa; |
664 | sa.sa_handler = sighandler; |
749 | sa.sa_handler = sighandler; |
… | |
… | |
672 | evsignal_stop (struct ev_signal *w) |
757 | evsignal_stop (struct ev_signal *w) |
673 | { |
758 | { |
674 | if (!ev_is_active (w)) |
759 | if (!ev_is_active (w)) |
675 | return; |
760 | return; |
676 | |
761 | |
677 | wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); |
762 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
678 | ev_stop ((struct ev_watcher *)w); |
763 | ev_stop ((W)w); |
679 | |
764 | |
680 | if (!signals [w->signum - 1].head) |
765 | if (!signals [w->signum - 1].head) |
681 | signal (w->signum, SIG_DFL); |
766 | signal (w->signum, SIG_DFL); |
682 | } |
767 | } |
683 | |
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 | |
684 | /*****************************************************************************/ |
801 | /*****************************************************************************/ |
|
|
802 | |
685 | #if 1 |
803 | #if 0 |
|
|
804 | |
|
|
805 | struct ev_io wio; |
686 | |
806 | |
687 | static void |
807 | static void |
688 | sin_cb (struct ev_io *w, int revents) |
808 | sin_cb (struct ev_io *w, int revents) |
689 | { |
809 | { |
690 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
810 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
… | |
… | |
700 | |
820 | |
701 | static void |
821 | static void |
702 | scb (struct ev_signal *w, int revents) |
822 | scb (struct ev_signal *w, int revents) |
703 | { |
823 | { |
704 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
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 | |
705 | } |
834 | } |
706 | |
835 | |
707 | int main (void) |
836 | int main (void) |
708 | { |
837 | { |
709 | struct ev_io sin; |
|
|
710 | |
|
|
711 | ev_init (0); |
838 | ev_init (0); |
712 | |
839 | |
713 | evw_init (&sin, sin_cb, 55); |
|
|
714 | evio_set (&sin, 0, EV_READ); |
840 | evio_init (&wio, sin_cb, 0, EV_READ); |
715 | evio_start (&sin); |
841 | evio_start (&wio); |
716 | |
842 | |
717 | struct ev_timer t[10000]; |
843 | struct ev_timer t[10000]; |
718 | |
844 | |
719 | #if 1 |
845 | #if 0 |
720 | int i; |
846 | int i; |
721 | for (i = 0; i < 10000; ++i) |
847 | for (i = 0; i < 10000; ++i) |
722 | { |
848 | { |
723 | struct ev_timer *w = t + i; |
849 | struct ev_timer *w = t + i; |
724 | evw_init (w, ocb, i); |
850 | evw_init (w, ocb, i); |
725 | evtimer_set_abs (w, drand48 (), 0.99775533); |
851 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
726 | evtimer_start (w); |
852 | evtimer_start (w); |
727 | if (drand48 () < 0.5) |
853 | if (drand48 () < 0.5) |
728 | evtimer_stop (w); |
854 | evtimer_stop (w); |
729 | } |
855 | } |
730 | #endif |
856 | #endif |
731 | |
857 | |
732 | struct ev_timer t1; |
858 | struct ev_timer t1; |
733 | evw_init (&t1, ocb, 0); |
859 | evtimer_init (&t1, ocb, 5, 10); |
734 | evtimer_set_abs (&t1, 5, 10); |
|
|
735 | evtimer_start (&t1); |
860 | evtimer_start (&t1); |
736 | |
861 | |
737 | struct ev_signal sig; |
862 | struct ev_signal sig; |
738 | evw_init (&sig, scb, 65535); |
|
|
739 | evsignal_set (&sig, SIGQUIT); |
863 | evsignal_init (&sig, scb, SIGQUIT); |
740 | evsignal_start (&sig); |
864 | evsignal_start (&sig); |
741 | |
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 | |
742 | ev_loop (0); |
874 | ev_loop (0); |
743 | |
875 | |
744 | return 0; |
876 | return 0; |
745 | } |
877 | } |
746 | |
878 | |