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