|
|
1 | /* |
|
|
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
|
|
3 | * All rights reserved. |
|
|
4 | * |
|
|
5 | * Redistribution and use in source and binary forms, with or without |
|
|
6 | * modification, are permitted provided that the following conditions are |
|
|
7 | * met: |
|
|
8 | * |
|
|
9 | * * Redistributions of source code must retain the above copyright |
|
|
10 | * notice, this list of conditions and the following disclaimer. |
|
|
11 | * |
|
|
12 | * * Redistributions in binary form must reproduce the above |
|
|
13 | * copyright notice, this list of conditions and the following |
|
|
14 | * disclaimer in the documentation and/or other materials provided |
|
|
15 | * with the distribution. |
|
|
16 | * |
|
|
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
|
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
|
19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
|
20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
|
21 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
|
22 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
|
23 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
|
27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
28 | */ |
|
|
29 | |
1 | #include <math.h> |
30 | #include <math.h> |
2 | #include <stdlib.h> |
31 | #include <stdlib.h> |
3 | #include <unistd.h> |
32 | #include <unistd.h> |
4 | #include <fcntl.h> |
33 | #include <fcntl.h> |
5 | #include <signal.h> |
34 | #include <signal.h> |
|
|
35 | #include <stddef.h> |
6 | |
36 | |
7 | #include <stdio.h> |
37 | #include <stdio.h> |
8 | |
38 | |
9 | #include <assert.h> |
39 | #include <assert.h> |
10 | #include <errno.h> |
40 | #include <errno.h> |
11 | #include <sys/time.h> |
41 | #include <sys/time.h> |
12 | #include <time.h> |
42 | #include <time.h> |
13 | |
43 | |
14 | #ifdef CLOCK_MONOTONIC |
|
|
15 | # define HAVE_MONOTONIC 1 |
|
|
16 | #endif |
|
|
17 | |
|
|
18 | #define HAVE_REALTIME 1 |
|
|
19 | #define HAVE_EPOLL 1 |
44 | #define HAVE_EPOLL 1 |
|
|
45 | |
|
|
46 | #ifndef HAVE_MONOTONIC |
|
|
47 | # ifdef CLOCK_MONOTONIC |
|
|
48 | # define HAVE_MONOTONIC 1 |
|
|
49 | # endif |
|
|
50 | #endif |
|
|
51 | |
|
|
52 | #ifndef HAVE_SELECT |
20 | #define HAVE_SELECT 1 |
53 | # define HAVE_SELECT 1 |
|
|
54 | #endif |
|
|
55 | |
|
|
56 | #ifndef HAVE_EPOLL |
|
|
57 | # define HAVE_EPOLL 0 |
|
|
58 | #endif |
|
|
59 | |
|
|
60 | #ifndef HAVE_REALTIME |
|
|
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
|
|
62 | #endif |
21 | |
63 | |
22 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
64 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
23 | #define MAX_BLOCKTIME 60. |
65 | #define MAX_BLOCKTIME 60. |
24 | |
66 | |
25 | #include "ev.h" |
67 | #include "ev.h" |
26 | |
68 | |
27 | struct ev_watcher { |
69 | typedef struct ev_watcher *W; |
28 | EV_WATCHER (ev_watcher); |
|
|
29 | }; |
|
|
30 | |
|
|
31 | struct ev_watcher_list { |
70 | typedef struct ev_watcher_list *WL; |
32 | EV_WATCHER_LIST (ev_watcher_list); |
71 | typedef struct ev_watcher_time *WT; |
33 | }; |
|
|
34 | |
72 | |
35 | static ev_tstamp now, diff; /* monotonic clock */ |
73 | static ev_tstamp now, diff; /* monotonic clock */ |
36 | ev_tstamp ev_now; |
74 | ev_tstamp ev_now; |
37 | int ev_method; |
75 | int ev_method; |
38 | |
76 | |
… | |
… | |
75 | |
113 | |
76 | #define array_needsize(base,cur,cnt,init) \ |
114 | #define array_needsize(base,cur,cnt,init) \ |
77 | if ((cnt) > cur) \ |
115 | if ((cnt) > cur) \ |
78 | { \ |
116 | { \ |
79 | int newcnt = cur ? cur << 1 : 16; \ |
117 | 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)); \ |
118 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
82 | init (base + cur, newcnt - cur); \ |
119 | init (base + cur, newcnt - cur); \ |
83 | cur = newcnt; \ |
120 | cur = newcnt; \ |
84 | } |
121 | } |
85 | |
122 | |
… | |
… | |
108 | } |
145 | } |
109 | } |
146 | } |
110 | |
147 | |
111 | typedef struct |
148 | typedef struct |
112 | { |
149 | { |
113 | struct ev_watcher *w; |
150 | W w; |
114 | int events; |
151 | int events; |
115 | } ANPENDING; |
152 | } ANPENDING; |
116 | |
153 | |
117 | static ANPENDING *pendings; |
154 | static ANPENDING *pendings; |
118 | static int pendingmax, pendingcnt; |
155 | static int pendingmax, pendingcnt; |
119 | |
156 | |
120 | static void |
157 | static void |
121 | event (struct ev_watcher *w, int events) |
158 | event (W w, int events) |
122 | { |
159 | { |
|
|
160 | if (w->active) |
|
|
161 | { |
123 | w->pending = ++pendingcnt; |
162 | w->pending = ++pendingcnt; |
124 | array_needsize (pendings, pendingmax, pendingcnt, ); |
163 | array_needsize (pendings, pendingmax, pendingcnt, ); |
125 | pendings [pendingcnt - 1].w = w; |
164 | pendings [pendingcnt - 1].w = w; |
126 | pendings [pendingcnt - 1].events = events; |
165 | pendings [pendingcnt - 1].events = events; |
|
|
166 | } |
127 | } |
167 | } |
128 | |
168 | |
129 | static void |
169 | static void |
130 | fd_event (int fd, int events) |
170 | fd_event (int fd, int events) |
131 | { |
171 | { |
… | |
… | |
135 | for (w = anfd->head; w; w = w->next) |
175 | for (w = anfd->head; w; w = w->next) |
136 | { |
176 | { |
137 | int ev = w->events & events; |
177 | int ev = w->events & events; |
138 | |
178 | |
139 | if (ev) |
179 | if (ev) |
140 | event ((struct ev_watcher *)w, ev); |
180 | event ((W)w, ev); |
141 | } |
181 | } |
142 | } |
182 | } |
143 | |
183 | |
144 | static void |
184 | static void |
145 | queue_events (struct ev_watcher **events, int eventcnt, int type) |
185 | queue_events (W *events, int eventcnt, int type) |
146 | { |
186 | { |
147 | int i; |
187 | int i; |
148 | |
188 | |
149 | for (i = 0; i < eventcnt; ++i) |
189 | for (i = 0; i < eventcnt; ++i) |
150 | event (events [i], type); |
190 | event (events [i], type); |
151 | } |
191 | } |
152 | |
192 | |
153 | /*****************************************************************************/ |
193 | /*****************************************************************************/ |
154 | |
194 | |
155 | static struct ev_timer **atimers; |
195 | static struct ev_timer **timers; |
156 | static int atimermax, atimercnt; |
196 | static int timermax, timercnt; |
157 | |
197 | |
158 | static struct ev_timer **rtimers; |
198 | static struct ev_periodic **periodics; |
159 | static int rtimermax, rtimercnt; |
199 | static int periodicmax, periodiccnt; |
160 | |
200 | |
161 | static void |
201 | static void |
162 | upheap (struct ev_timer **timers, int k) |
202 | upheap (WT *timers, int k) |
163 | { |
203 | { |
164 | struct ev_timer *w = timers [k]; |
204 | WT w = timers [k]; |
165 | |
205 | |
166 | while (k && timers [k >> 1]->at > w->at) |
206 | while (k && timers [k >> 1]->at > w->at) |
167 | { |
207 | { |
168 | timers [k] = timers [k >> 1]; |
208 | timers [k] = timers [k >> 1]; |
169 | timers [k]->active = k + 1; |
209 | timers [k]->active = k + 1; |
… | |
… | |
174 | timers [k]->active = k + 1; |
214 | timers [k]->active = k + 1; |
175 | |
215 | |
176 | } |
216 | } |
177 | |
217 | |
178 | static void |
218 | static void |
179 | downheap (struct ev_timer **timers, int N, int k) |
219 | downheap (WT *timers, int N, int k) |
180 | { |
220 | { |
181 | struct ev_timer *w = timers [k]; |
221 | WT w = timers [k]; |
182 | |
222 | |
183 | while (k < (N >> 1)) |
223 | while (k < (N >> 1)) |
184 | { |
224 | { |
185 | int j = k << 1; |
225 | int j = k << 1; |
186 | |
226 | |
… | |
… | |
250 | if (signals [sig].gotsig) |
290 | if (signals [sig].gotsig) |
251 | { |
291 | { |
252 | signals [sig].gotsig = 0; |
292 | signals [sig].gotsig = 0; |
253 | |
293 | |
254 | for (w = signals [sig].head; w; w = w->next) |
294 | for (w = signals [sig].head; w; w = w->next) |
255 | event ((struct ev_watcher *)w, EV_SIGNAL); |
295 | event ((W)w, EV_SIGNAL); |
256 | } |
296 | } |
257 | } |
297 | } |
258 | |
298 | |
259 | static void |
299 | static void |
260 | siginit (void) |
300 | siginit (void) |
… | |
… | |
312 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
352 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
313 | #endif |
353 | #endif |
314 | |
354 | |
315 | if (ev_method) |
355 | if (ev_method) |
316 | { |
356 | { |
317 | evw_init (&sigev, sigcb, 0); |
357 | evw_init (&sigev, sigcb); |
318 | siginit (); |
358 | siginit (); |
319 | } |
359 | } |
320 | |
360 | |
321 | return ev_method; |
361 | return ev_method; |
322 | } |
362 | } |
323 | |
363 | |
324 | /*****************************************************************************/ |
364 | /*****************************************************************************/ |
325 | |
365 | |
326 | void ev_prefork (void) |
366 | void ev_prefork (void) |
327 | { |
367 | { |
|
|
368 | /* nop */ |
328 | } |
369 | } |
329 | |
370 | |
330 | void ev_postfork_parent (void) |
371 | void ev_postfork_parent (void) |
331 | { |
372 | { |
|
|
373 | /* nop */ |
332 | } |
374 | } |
333 | |
375 | |
334 | void ev_postfork_child (void) |
376 | void ev_postfork_child (void) |
335 | { |
377 | { |
336 | #if HAVE_EPOLL |
378 | #if HAVE_EPOLL |
… | |
… | |
374 | } |
416 | } |
375 | |
417 | |
376 | static void |
418 | static void |
377 | call_pending () |
419 | call_pending () |
378 | { |
420 | { |
379 | int i; |
421 | while (pendingcnt) |
380 | |
|
|
381 | for (i = 0; i < pendingcnt; ++i) |
|
|
382 | { |
422 | { |
383 | ANPENDING *p = pendings + i; |
423 | ANPENDING *p = pendings + --pendingcnt; |
384 | |
424 | |
385 | if (p->w) |
425 | if (p->w) |
386 | { |
426 | { |
387 | p->w->pending = 0; |
427 | p->w->pending = 0; |
388 | p->w->cb (p->w, p->events); |
428 | p->w->cb (p->w, p->events); |
389 | } |
429 | } |
390 | } |
430 | } |
391 | |
|
|
392 | pendingcnt = 0; |
|
|
393 | } |
431 | } |
394 | |
432 | |
395 | static void |
433 | static void |
396 | timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) |
434 | timers_reify () |
397 | { |
435 | { |
398 | while (timercnt && timers [0]->at <= now) |
436 | while (timercnt && timers [0]->at <= now) |
399 | { |
437 | { |
400 | struct ev_timer *w = timers [0]; |
438 | struct ev_timer *w = timers [0]; |
|
|
439 | |
|
|
440 | event ((W)w, EV_TIMEOUT); |
401 | |
441 | |
402 | /* first reschedule or stop timer */ |
442 | /* first reschedule or stop timer */ |
403 | if (w->repeat) |
443 | if (w->repeat) |
404 | { |
444 | { |
405 | if (w->is_abs) |
|
|
406 | w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
407 | else |
|
|
408 | w->at = now + w->repeat; |
445 | w->at = now + w->repeat; |
409 | |
446 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
410 | assert (w->at > now); |
|
|
411 | |
|
|
412 | downheap (timers, timercnt, 0); |
447 | downheap ((WT *)timers, timercnt, 0); |
413 | } |
448 | } |
414 | else |
449 | else |
415 | { |
|
|
416 | evtimer_stop (w); /* nonrepeating: stop timer */ |
450 | evtimer_stop (w); /* nonrepeating: stop timer */ |
417 | --timercnt; /* maybe pass by reference instead? */ |
451 | } |
|
|
452 | } |
|
|
453 | |
|
|
454 | static void |
|
|
455 | periodics_reify () |
|
|
456 | { |
|
|
457 | while (periodiccnt && periodics [0]->at <= ev_now) |
|
|
458 | { |
|
|
459 | struct ev_periodic *w = periodics [0]; |
|
|
460 | |
|
|
461 | /* first reschedule or stop timer */ |
|
|
462 | if (w->interval) |
418 | } |
463 | { |
|
|
464 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
|
|
465 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
|
|
466 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
467 | } |
|
|
468 | else |
|
|
469 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
419 | |
470 | |
420 | event ((struct ev_watcher *)w, EV_TIMEOUT); |
471 | event ((W)w, EV_TIMEOUT); |
|
|
472 | } |
|
|
473 | } |
|
|
474 | |
|
|
475 | static void |
|
|
476 | periodics_reschedule (ev_tstamp diff) |
|
|
477 | { |
|
|
478 | int i; |
|
|
479 | |
|
|
480 | /* adjust periodics after time jump */ |
|
|
481 | for (i = 0; i < periodiccnt; ++i) |
|
|
482 | { |
|
|
483 | struct ev_periodic *w = periodics [i]; |
|
|
484 | |
|
|
485 | if (w->interval) |
|
|
486 | { |
|
|
487 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
488 | |
|
|
489 | if (fabs (diff) >= 1e-4) |
|
|
490 | { |
|
|
491 | evperiodic_stop (w); |
|
|
492 | evperiodic_start (w); |
|
|
493 | |
|
|
494 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
495 | } |
|
|
496 | } |
421 | } |
497 | } |
422 | } |
498 | } |
423 | |
499 | |
424 | static void |
500 | static void |
425 | time_update () |
501 | time_update () |
426 | { |
502 | { |
427 | int i; |
503 | int i; |
|
|
504 | |
428 | ev_now = ev_time (); |
505 | ev_now = ev_time (); |
429 | |
506 | |
430 | if (have_monotonic) |
507 | if (have_monotonic) |
431 | { |
508 | { |
432 | ev_tstamp odiff = diff; |
509 | ev_tstamp odiff = diff; |
433 | |
510 | |
434 | /* detecting time jumps is much more difficult */ |
|
|
435 | for (i = 2; --i; ) /* loop a few times, before making important decisions */ |
511 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
436 | { |
512 | { |
437 | now = get_clock (); |
513 | now = get_clock (); |
438 | diff = ev_now - now; |
514 | diff = ev_now - now; |
439 | |
515 | |
440 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
516 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
441 | return; /* all is well */ |
517 | return; /* all is well */ |
442 | |
518 | |
443 | ev_now = ev_time (); |
519 | ev_now = ev_time (); |
444 | } |
520 | } |
445 | |
521 | |
446 | /* time jump detected, reschedule atimers */ |
522 | periodics_reschedule (diff - odiff); |
447 | for (i = 0; i < atimercnt; ++i) |
523 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
448 | { |
|
|
449 | struct ev_timer *w = atimers [i]; |
|
|
450 | w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
451 | } |
|
|
452 | } |
524 | } |
453 | else |
525 | else |
454 | { |
526 | { |
455 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
527 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
456 | /* time jump detected, adjust rtimers */ |
528 | { |
|
|
529 | periodics_reschedule (ev_now - now); |
|
|
530 | |
|
|
531 | /* adjust timers. this is easy, as the offset is the same for all */ |
457 | for (i = 0; i < rtimercnt; ++i) |
532 | for (i = 0; i < timercnt; ++i) |
458 | rtimers [i]->at += ev_now - now; |
533 | timers [i]->at += diff; |
|
|
534 | } |
459 | |
535 | |
460 | now = ev_now; |
536 | now = ev_now; |
461 | } |
537 | } |
462 | } |
538 | } |
463 | |
539 | |
464 | int ev_loop_done; |
540 | int ev_loop_done; |
465 | |
541 | |
466 | void ev_loop (int flags) |
542 | void ev_loop (int flags) |
467 | { |
543 | { |
468 | double block; |
544 | double block; |
469 | ev_loop_done = flags & EVLOOP_ONESHOT; |
545 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
470 | |
546 | |
471 | if (checkcnt) |
547 | if (checkcnt) |
472 | { |
548 | { |
473 | queue_events (checks, checkcnt, EV_CHECK); |
549 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
474 | call_pending (); |
550 | call_pending (); |
475 | } |
551 | } |
476 | |
552 | |
477 | do |
553 | do |
478 | { |
554 | { |
479 | /* update fd-related kernel structures */ |
555 | /* update fd-related kernel structures */ |
480 | fd_reify (); |
556 | fd_reify (); |
481 | |
557 | |
482 | /* calculate blocking time */ |
558 | /* calculate blocking time */ |
|
|
559 | |
|
|
560 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
|
|
561 | ev_now = ev_time (); |
|
|
562 | |
483 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
563 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
484 | block = 0.; |
564 | block = 0.; |
485 | else |
565 | else |
486 | { |
566 | { |
487 | block = MAX_BLOCKTIME; |
567 | block = MAX_BLOCKTIME; |
488 | |
568 | |
489 | if (rtimercnt) |
569 | if (timercnt) |
490 | { |
570 | { |
491 | ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; |
571 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
492 | if (block > to) block = to; |
572 | if (block > to) block = to; |
493 | } |
573 | } |
494 | |
574 | |
495 | if (atimercnt) |
575 | if (periodiccnt) |
496 | { |
576 | { |
497 | ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; |
577 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
498 | if (block > to) block = to; |
578 | if (block > to) block = to; |
499 | } |
579 | } |
500 | |
580 | |
501 | if (block < 0.) block = 0.; |
581 | if (block < 0.) block = 0.; |
502 | } |
582 | } |
… | |
… | |
505 | |
585 | |
506 | /* update ev_now, do magic */ |
586 | /* update ev_now, do magic */ |
507 | time_update (); |
587 | time_update (); |
508 | |
588 | |
509 | /* queue pending timers and reschedule them */ |
589 | /* queue pending timers and reschedule them */ |
510 | /* absolute timers first */ |
590 | periodics_reify (); /* absolute timers first */ |
511 | timers_reify (atimers, atimercnt, ev_now); |
|
|
512 | /* relative timers second */ |
591 | timers_reify (); /* relative timers second */ |
513 | timers_reify (rtimers, rtimercnt, now); |
|
|
514 | |
592 | |
515 | /* queue idle watchers unless io or timers are pending */ |
593 | /* queue idle watchers unless io or timers are pending */ |
516 | if (!pendingcnt) |
594 | if (!pendingcnt) |
517 | queue_events (idles, idlecnt, EV_IDLE); |
595 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
518 | |
596 | |
519 | /* queue check and possibly idle watchers */ |
597 | /* queue check and possibly idle watchers */ |
520 | queue_events (checks, checkcnt, EV_CHECK); |
598 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
521 | |
599 | |
522 | call_pending (); |
600 | call_pending (); |
523 | } |
601 | } |
524 | while (!ev_loop_done); |
602 | while (!ev_loop_done); |
525 | } |
|
|
526 | |
603 | |
527 | /*****************************************************************************/ |
604 | if (ev_loop_done != 2) |
|
|
605 | ev_loop_done = 0; |
|
|
606 | } |
528 | |
607 | |
|
|
608 | /*****************************************************************************/ |
|
|
609 | |
529 | static void |
610 | static void |
530 | wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
611 | wlist_add (WL *head, WL elem) |
531 | { |
612 | { |
532 | elem->next = *head; |
613 | elem->next = *head; |
533 | *head = elem; |
614 | *head = elem; |
534 | } |
615 | } |
535 | |
616 | |
536 | static void |
617 | static void |
537 | wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
618 | wlist_del (WL *head, WL elem) |
538 | { |
619 | { |
539 | while (*head) |
620 | while (*head) |
540 | { |
621 | { |
541 | if (*head == elem) |
622 | if (*head == elem) |
542 | { |
623 | { |
… | |
… | |
547 | head = &(*head)->next; |
628 | head = &(*head)->next; |
548 | } |
629 | } |
549 | } |
630 | } |
550 | |
631 | |
551 | static void |
632 | static void |
552 | ev_start (struct ev_watcher *w, int active) |
633 | ev_clear (W w) |
553 | { |
634 | { |
|
|
635 | if (w->pending) |
|
|
636 | { |
|
|
637 | pendings [w->pending - 1].w = 0; |
554 | w->pending = 0; |
638 | w->pending = 0; |
|
|
639 | } |
|
|
640 | } |
|
|
641 | |
|
|
642 | static void |
|
|
643 | ev_start (W w, int active) |
|
|
644 | { |
555 | w->active = active; |
645 | w->active = active; |
556 | } |
646 | } |
557 | |
647 | |
558 | static void |
648 | static void |
559 | ev_stop (struct ev_watcher *w) |
649 | ev_stop (W w) |
560 | { |
650 | { |
561 | if (w->pending) |
|
|
562 | pendings [w->pending - 1].w = 0; |
|
|
563 | |
|
|
564 | w->active = 0; |
651 | w->active = 0; |
565 | /* nop */ |
|
|
566 | } |
652 | } |
567 | |
653 | |
568 | /*****************************************************************************/ |
654 | /*****************************************************************************/ |
569 | |
655 | |
570 | void |
656 | void |
… | |
… | |
573 | if (ev_is_active (w)) |
659 | if (ev_is_active (w)) |
574 | return; |
660 | return; |
575 | |
661 | |
576 | int fd = w->fd; |
662 | int fd = w->fd; |
577 | |
663 | |
578 | ev_start ((struct ev_watcher *)w, 1); |
664 | ev_start ((W)w, 1); |
579 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
665 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
580 | wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); |
666 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
581 | |
667 | |
582 | ++fdchangecnt; |
668 | ++fdchangecnt; |
583 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
669 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
584 | fdchanges [fdchangecnt - 1] = fd; |
670 | fdchanges [fdchangecnt - 1] = fd; |
585 | } |
671 | } |
586 | |
672 | |
587 | void |
673 | void |
588 | evio_stop (struct ev_io *w) |
674 | evio_stop (struct ev_io *w) |
589 | { |
675 | { |
|
|
676 | ev_clear ((W)w); |
590 | if (!ev_is_active (w)) |
677 | if (!ev_is_active (w)) |
591 | return; |
678 | return; |
592 | |
679 | |
593 | wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); |
680 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
594 | ev_stop ((struct ev_watcher *)w); |
681 | ev_stop ((W)w); |
595 | |
682 | |
596 | ++fdchangecnt; |
683 | ++fdchangecnt; |
597 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
684 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
598 | fdchanges [fdchangecnt - 1] = w->fd; |
685 | fdchanges [fdchangecnt - 1] = w->fd; |
599 | } |
686 | } |
… | |
… | |
602 | evtimer_start (struct ev_timer *w) |
689 | evtimer_start (struct ev_timer *w) |
603 | { |
690 | { |
604 | if (ev_is_active (w)) |
691 | if (ev_is_active (w)) |
605 | return; |
692 | return; |
606 | |
693 | |
607 | if (w->is_abs) |
694 | w->at += now; |
608 | { |
|
|
609 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
|
|
610 | if (w->repeat) |
|
|
611 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
|
|
612 | |
695 | |
613 | ev_start ((struct ev_watcher *)w, ++atimercnt); |
696 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
|
|
697 | |
|
|
698 | ev_start ((W)w, ++timercnt); |
614 | array_needsize (atimers, atimermax, atimercnt, ); |
699 | array_needsize (timers, timermax, timercnt, ); |
615 | atimers [atimercnt - 1] = w; |
700 | timers [timercnt - 1] = w; |
616 | upheap (atimers, atimercnt - 1); |
701 | upheap ((WT *)timers, timercnt - 1); |
617 | } |
|
|
618 | else |
|
|
619 | { |
|
|
620 | w->at += now; |
|
|
621 | |
|
|
622 | ev_start ((struct ev_watcher *)w, ++rtimercnt); |
|
|
623 | array_needsize (rtimers, rtimermax, rtimercnt, ); |
|
|
624 | rtimers [rtimercnt - 1] = w; |
|
|
625 | upheap (rtimers, rtimercnt - 1); |
|
|
626 | } |
|
|
627 | |
|
|
628 | } |
702 | } |
629 | |
703 | |
630 | void |
704 | void |
631 | evtimer_stop (struct ev_timer *w) |
705 | evtimer_stop (struct ev_timer *w) |
632 | { |
706 | { |
|
|
707 | ev_clear ((W)w); |
633 | if (!ev_is_active (w)) |
708 | if (!ev_is_active (w)) |
634 | return; |
709 | return; |
635 | |
710 | |
636 | if (w->is_abs) |
|
|
637 | { |
|
|
638 | if (w->active < atimercnt--) |
711 | if (w->active < timercnt--) |
639 | { |
712 | { |
640 | atimers [w->active - 1] = atimers [atimercnt]; |
713 | timers [w->active - 1] = timers [timercnt]; |
|
|
714 | downheap ((WT *)timers, timercnt, w->active - 1); |
|
|
715 | } |
|
|
716 | |
|
|
717 | w->at = w->repeat; |
|
|
718 | |
|
|
719 | ev_stop ((W)w); |
|
|
720 | } |
|
|
721 | |
|
|
722 | void |
|
|
723 | evtimer_again (struct ev_timer *w) |
|
|
724 | { |
|
|
725 | if (ev_is_active (w)) |
|
|
726 | { |
|
|
727 | if (w->repeat) |
|
|
728 | { |
|
|
729 | w->at = now + w->repeat; |
641 | downheap (atimers, atimercnt, w->active - 1); |
730 | downheap ((WT *)timers, timercnt, w->active - 1); |
642 | } |
|
|
643 | } |
|
|
644 | else |
|
|
645 | { |
|
|
646 | if (w->active < rtimercnt--) |
|
|
647 | { |
731 | } |
648 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
732 | else |
649 | downheap (rtimers, rtimercnt, w->active - 1); |
733 | evtimer_stop (w); |
650 | } |
734 | } |
|
|
735 | else if (w->repeat) |
|
|
736 | evtimer_start (w); |
|
|
737 | } |
|
|
738 | |
|
|
739 | void |
|
|
740 | evperiodic_start (struct ev_periodic *w) |
|
|
741 | { |
|
|
742 | if (ev_is_active (w)) |
|
|
743 | return; |
|
|
744 | |
|
|
745 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
|
|
746 | |
|
|
747 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
|
|
748 | if (w->interval) |
|
|
749 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
750 | |
|
|
751 | ev_start ((W)w, ++periodiccnt); |
|
|
752 | array_needsize (periodics, periodicmax, periodiccnt, ); |
|
|
753 | periodics [periodiccnt - 1] = w; |
|
|
754 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
755 | } |
|
|
756 | |
|
|
757 | void |
|
|
758 | evperiodic_stop (struct ev_periodic *w) |
|
|
759 | { |
|
|
760 | ev_clear ((W)w); |
|
|
761 | if (!ev_is_active (w)) |
|
|
762 | return; |
|
|
763 | |
|
|
764 | if (w->active < periodiccnt--) |
651 | } |
765 | { |
|
|
766 | periodics [w->active - 1] = periodics [periodiccnt]; |
|
|
767 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
|
|
768 | } |
652 | |
769 | |
653 | ev_stop ((struct ev_watcher *)w); |
770 | ev_stop ((W)w); |
654 | } |
771 | } |
655 | |
772 | |
656 | void |
773 | void |
657 | evsignal_start (struct ev_signal *w) |
774 | evsignal_start (struct ev_signal *w) |
658 | { |
775 | { |
659 | if (ev_is_active (w)) |
776 | if (ev_is_active (w)) |
660 | return; |
777 | return; |
661 | |
778 | |
662 | ev_start ((struct ev_watcher *)w, 1); |
779 | ev_start ((W)w, 1); |
663 | array_needsize (signals, signalmax, w->signum, signals_init); |
780 | array_needsize (signals, signalmax, w->signum, signals_init); |
664 | wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); |
781 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
665 | |
782 | |
666 | if (!w->next) |
783 | if (!w->next) |
667 | { |
784 | { |
668 | struct sigaction sa; |
785 | struct sigaction sa; |
669 | sa.sa_handler = sighandler; |
786 | sa.sa_handler = sighandler; |
… | |
… | |
674 | } |
791 | } |
675 | |
792 | |
676 | void |
793 | void |
677 | evsignal_stop (struct ev_signal *w) |
794 | evsignal_stop (struct ev_signal *w) |
678 | { |
795 | { |
|
|
796 | ev_clear ((W)w); |
679 | if (!ev_is_active (w)) |
797 | if (!ev_is_active (w)) |
680 | return; |
798 | return; |
681 | |
799 | |
682 | wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); |
800 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
683 | ev_stop ((struct ev_watcher *)w); |
801 | ev_stop ((W)w); |
684 | |
802 | |
685 | if (!signals [w->signum - 1].head) |
803 | if (!signals [w->signum - 1].head) |
686 | signal (w->signum, SIG_DFL); |
804 | signal (w->signum, SIG_DFL); |
687 | } |
805 | } |
688 | |
806 | |
689 | void evidle_start (struct ev_idle *w) |
807 | void evidle_start (struct ev_idle *w) |
690 | { |
808 | { |
691 | if (ev_is_active (w)) |
809 | if (ev_is_active (w)) |
692 | return; |
810 | return; |
693 | |
811 | |
694 | ev_start ((struct ev_watcher *)w, ++idlecnt); |
812 | ev_start ((W)w, ++idlecnt); |
695 | array_needsize (idles, idlemax, idlecnt, ); |
813 | array_needsize (idles, idlemax, idlecnt, ); |
696 | idles [idlecnt - 1] = w; |
814 | idles [idlecnt - 1] = w; |
697 | } |
815 | } |
698 | |
816 | |
699 | void evidle_stop (struct ev_idle *w) |
817 | void evidle_stop (struct ev_idle *w) |
700 | { |
818 | { |
|
|
819 | ev_clear ((W)w); |
|
|
820 | if (ev_is_active (w)) |
|
|
821 | return; |
|
|
822 | |
701 | idles [w->active - 1] = idles [--idlecnt]; |
823 | idles [w->active - 1] = idles [--idlecnt]; |
702 | ev_stop ((struct ev_watcher *)w); |
824 | ev_stop ((W)w); |
703 | } |
825 | } |
704 | |
826 | |
705 | void evcheck_start (struct ev_check *w) |
827 | void evcheck_start (struct ev_check *w) |
706 | { |
828 | { |
707 | if (ev_is_active (w)) |
829 | if (ev_is_active (w)) |
708 | return; |
830 | return; |
709 | |
831 | |
710 | ev_start ((struct ev_watcher *)w, ++checkcnt); |
832 | ev_start ((W)w, ++checkcnt); |
711 | array_needsize (checks, checkmax, checkcnt, ); |
833 | array_needsize (checks, checkmax, checkcnt, ); |
712 | checks [checkcnt - 1] = w; |
834 | checks [checkcnt - 1] = w; |
713 | } |
835 | } |
714 | |
836 | |
715 | void evcheck_stop (struct ev_check *w) |
837 | void evcheck_stop (struct ev_check *w) |
716 | { |
838 | { |
|
|
839 | ev_clear ((W)w); |
|
|
840 | if (ev_is_active (w)) |
|
|
841 | return; |
|
|
842 | |
717 | checks [w->active - 1] = checks [--checkcnt]; |
843 | checks [w->active - 1] = checks [--checkcnt]; |
718 | ev_stop ((struct ev_watcher *)w); |
844 | ev_stop ((W)w); |
719 | } |
845 | } |
720 | |
846 | |
721 | /*****************************************************************************/ |
847 | /*****************************************************************************/ |
|
|
848 | |
|
|
849 | struct ev_once |
|
|
850 | { |
|
|
851 | struct ev_io io; |
|
|
852 | struct ev_timer to; |
|
|
853 | void (*cb)(int revents, void *arg); |
|
|
854 | void *arg; |
|
|
855 | }; |
|
|
856 | |
|
|
857 | static void |
|
|
858 | once_cb (struct ev_once *once, int revents) |
|
|
859 | { |
|
|
860 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
861 | void *arg = once->arg; |
|
|
862 | |
|
|
863 | evio_stop (&once->io); |
|
|
864 | evtimer_stop (&once->to); |
|
|
865 | free (once); |
|
|
866 | |
|
|
867 | cb (revents, arg); |
|
|
868 | } |
|
|
869 | |
|
|
870 | static void |
|
|
871 | once_cb_io (struct ev_io *w, int revents) |
|
|
872 | { |
|
|
873 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
874 | } |
|
|
875 | |
|
|
876 | static void |
|
|
877 | once_cb_to (struct ev_timer *w, int revents) |
|
|
878 | { |
|
|
879 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
880 | } |
|
|
881 | |
|
|
882 | void |
|
|
883 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
884 | { |
|
|
885 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
886 | |
|
|
887 | if (!once) |
|
|
888 | cb (EV_ERROR, arg); |
|
|
889 | else |
|
|
890 | { |
|
|
891 | once->cb = cb; |
|
|
892 | once->arg = arg; |
|
|
893 | |
|
|
894 | evw_init (&once->io, once_cb_io); |
|
|
895 | |
|
|
896 | if (fd >= 0) |
|
|
897 | { |
|
|
898 | evio_set (&once->io, fd, events); |
|
|
899 | evio_start (&once->io); |
|
|
900 | } |
|
|
901 | |
|
|
902 | evw_init (&once->to, once_cb_to); |
|
|
903 | |
|
|
904 | if (timeout >= 0.) |
|
|
905 | { |
|
|
906 | evtimer_set (&once->to, timeout, 0.); |
|
|
907 | evtimer_start (&once->to); |
|
|
908 | } |
|
|
909 | } |
|
|
910 | } |
|
|
911 | |
|
|
912 | /*****************************************************************************/ |
|
|
913 | |
722 | #if 1 |
914 | #if 0 |
|
|
915 | |
|
|
916 | struct ev_io wio; |
723 | |
917 | |
724 | static void |
918 | static void |
725 | sin_cb (struct ev_io *w, int revents) |
919 | sin_cb (struct ev_io *w, int revents) |
726 | { |
920 | { |
727 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
921 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
… | |
… | |
737 | |
931 | |
738 | static void |
932 | static void |
739 | scb (struct ev_signal *w, int revents) |
933 | scb (struct ev_signal *w, int revents) |
740 | { |
934 | { |
741 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
935 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
936 | evio_stop (&wio); |
|
|
937 | evio_start (&wio); |
742 | } |
938 | } |
743 | |
939 | |
744 | static void |
940 | static void |
745 | gcb (struct ev_signal *w, int revents) |
941 | gcb (struct ev_signal *w, int revents) |
746 | { |
942 | { |
747 | fprintf (stderr, "generic %x\n", revents); |
943 | fprintf (stderr, "generic %x\n", revents); |
|
|
944 | |
748 | } |
945 | } |
749 | |
946 | |
750 | int main (void) |
947 | int main (void) |
751 | { |
948 | { |
752 | struct ev_io sin; |
|
|
753 | |
|
|
754 | ev_init (0); |
949 | ev_init (0); |
755 | |
950 | |
756 | evw_init (&sin, sin_cb, 55); |
|
|
757 | evio_set (&sin, 0, EV_READ); |
951 | evio_init (&wio, sin_cb, 0, EV_READ); |
758 | evio_start (&sin); |
952 | evio_start (&wio); |
759 | |
953 | |
760 | struct ev_timer t[10000]; |
954 | struct ev_timer t[10000]; |
761 | |
955 | |
762 | #if 0 |
956 | #if 0 |
763 | int i; |
957 | int i; |
764 | for (i = 0; i < 10000; ++i) |
958 | for (i = 0; i < 10000; ++i) |
765 | { |
959 | { |
766 | struct ev_timer *w = t + i; |
960 | struct ev_timer *w = t + i; |
767 | evw_init (w, ocb, i); |
961 | evw_init (w, ocb, i); |
768 | evtimer_set_abs (w, drand48 (), 0.99775533); |
962 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
769 | evtimer_start (w); |
963 | evtimer_start (w); |
770 | if (drand48 () < 0.5) |
964 | if (drand48 () < 0.5) |
771 | evtimer_stop (w); |
965 | evtimer_stop (w); |
772 | } |
966 | } |
773 | #endif |
967 | #endif |
774 | |
968 | |
775 | struct ev_timer t1; |
969 | struct ev_timer t1; |
776 | evw_init (&t1, ocb, 0); |
970 | evtimer_init (&t1, ocb, 5, 10); |
777 | evtimer_set_abs (&t1, 5, 10); |
|
|
778 | evtimer_start (&t1); |
971 | evtimer_start (&t1); |
779 | |
972 | |
780 | struct ev_signal sig; |
973 | struct ev_signal sig; |
781 | evw_init (&sig, scb, 65535); |
|
|
782 | evsignal_set (&sig, SIGQUIT); |
974 | evsignal_init (&sig, scb, SIGQUIT); |
783 | evsignal_start (&sig); |
975 | evsignal_start (&sig); |
784 | |
976 | |
785 | struct ev_check cw; |
977 | struct ev_check cw; |
786 | evw_init (&cw, gcb, 0); |
978 | evcheck_init (&cw, gcb); |
787 | evcheck_start (&cw); |
979 | evcheck_start (&cw); |
788 | |
980 | |
789 | struct ev_idle iw; |
981 | struct ev_idle iw; |
790 | evw_init (&iw, gcb, 0); |
982 | evidle_init (&iw, gcb); |
791 | evidle_start (&iw); |
983 | evidle_start (&iw); |
792 | |
984 | |
793 | ev_loop (0); |
985 | ev_loop (0); |
794 | |
986 | |
795 | return 0; |
987 | return 0; |