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