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