|
|
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> |
|
|
41 | #include <sys/types.h> |
|
|
42 | #include <sys/wait.h> |
11 | #include <sys/time.h> |
43 | #include <sys/time.h> |
12 | #include <time.h> |
44 | #include <time.h> |
13 | |
45 | |
14 | #ifndef HAVE_MONOTONIC |
46 | #ifndef HAVE_MONOTONIC |
15 | # ifdef CLOCK_MONOTONIC |
47 | # ifdef CLOCK_MONOTONIC |
… | |
… | |
29 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
30 | #endif |
62 | #endif |
31 | |
63 | |
32 | #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) */ |
33 | #define MAX_BLOCKTIME 60. |
65 | #define MAX_BLOCKTIME 60. |
|
|
66 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
34 | |
67 | |
35 | #include "ev.h" |
68 | #include "ev.h" |
36 | |
|
|
37 | struct ev_watcher { |
|
|
38 | EV_WATCHER (ev_watcher); |
|
|
39 | }; |
|
|
40 | |
|
|
41 | struct ev_watcher_list { |
|
|
42 | EV_WATCHER_LIST (ev_watcher_list); |
|
|
43 | }; |
|
|
44 | |
69 | |
45 | typedef struct ev_watcher *W; |
70 | typedef struct ev_watcher *W; |
46 | typedef struct ev_watcher_list *WL; |
71 | typedef struct ev_watcher_list *WL; |
|
|
72 | typedef struct ev_watcher_time *WT; |
47 | |
73 | |
48 | static ev_tstamp now, diff; /* monotonic clock */ |
74 | static ev_tstamp now, diff; /* monotonic clock */ |
49 | ev_tstamp ev_now; |
75 | ev_tstamp ev_now; |
50 | int ev_method; |
76 | int ev_method; |
51 | |
77 | |
… | |
… | |
87 | } |
113 | } |
88 | |
114 | |
89 | #define array_needsize(base,cur,cnt,init) \ |
115 | #define array_needsize(base,cur,cnt,init) \ |
90 | if ((cnt) > cur) \ |
116 | if ((cnt) > cur) \ |
91 | { \ |
117 | { \ |
92 | int newcnt = cur ? cur << 1 : 16; \ |
118 | int newcnt = cur; \ |
93 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
119 | do \ |
|
|
120 | { \ |
|
|
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
|
|
122 | } \ |
|
|
123 | while ((cnt) > newcnt); \ |
|
|
124 | \ |
94 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
95 | init (base + cur, newcnt - cur); \ |
126 | init (base + cur, newcnt - cur); \ |
96 | cur = newcnt; \ |
127 | cur = newcnt; \ |
97 | } |
128 | } |
98 | |
129 | |
… | |
… | |
131 | static int pendingmax, pendingcnt; |
162 | static int pendingmax, pendingcnt; |
132 | |
163 | |
133 | static void |
164 | static void |
134 | event (W w, int events) |
165 | event (W w, int events) |
135 | { |
166 | { |
|
|
167 | if (w->active) |
|
|
168 | { |
136 | w->pending = ++pendingcnt; |
169 | w->pending = ++pendingcnt; |
137 | array_needsize (pendings, pendingmax, pendingcnt, ); |
170 | array_needsize (pendings, pendingmax, pendingcnt, ); |
138 | pendings [pendingcnt - 1].w = w; |
171 | pendings [pendingcnt - 1].w = w; |
139 | pendings [pendingcnt - 1].events = events; |
172 | pendings [pendingcnt - 1].events = events; |
|
|
173 | } |
140 | } |
174 | } |
141 | |
175 | |
142 | static void |
176 | static void |
143 | fd_event (int fd, int events) |
177 | fd_event (int fd, int events) |
144 | { |
178 | { |
… | |
… | |
161 | |
195 | |
162 | for (i = 0; i < eventcnt; ++i) |
196 | for (i = 0; i < eventcnt; ++i) |
163 | event (events [i], type); |
197 | event (events [i], type); |
164 | } |
198 | } |
165 | |
199 | |
166 | /*****************************************************************************/ |
200 | /* called on EBADF to verify fds */ |
|
|
201 | static void |
|
|
202 | fd_recheck () |
|
|
203 | { |
|
|
204 | int fd; |
167 | |
205 | |
|
|
206 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
207 | if (anfds [fd].wev) |
|
|
208 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
|
|
209 | while (anfds [fd].head) |
|
|
210 | evio_stop (anfds [fd].head); |
|
|
211 | } |
|
|
212 | |
|
|
213 | /*****************************************************************************/ |
|
|
214 | |
168 | static struct ev_timer **atimers; |
215 | static struct ev_timer **timers; |
169 | static int atimermax, atimercnt; |
216 | static int timermax, timercnt; |
170 | |
217 | |
171 | static struct ev_timer **rtimers; |
218 | static struct ev_periodic **periodics; |
172 | static int rtimermax, rtimercnt; |
219 | static int periodicmax, periodiccnt; |
173 | |
220 | |
174 | static void |
221 | static void |
175 | upheap (struct ev_timer **timers, int k) |
222 | upheap (WT *timers, int k) |
176 | { |
223 | { |
177 | struct ev_timer *w = timers [k]; |
224 | WT w = timers [k]; |
178 | |
225 | |
179 | while (k && timers [k >> 1]->at > w->at) |
226 | while (k && timers [k >> 1]->at > w->at) |
180 | { |
227 | { |
181 | timers [k] = timers [k >> 1]; |
228 | timers [k] = timers [k >> 1]; |
182 | timers [k]->active = k + 1; |
229 | timers [k]->active = k + 1; |
… | |
… | |
187 | timers [k]->active = k + 1; |
234 | timers [k]->active = k + 1; |
188 | |
235 | |
189 | } |
236 | } |
190 | |
237 | |
191 | static void |
238 | static void |
192 | downheap (struct ev_timer **timers, int N, int k) |
239 | downheap (WT *timers, int N, int k) |
193 | { |
240 | { |
194 | struct ev_timer *w = timers [k]; |
241 | WT w = timers [k]; |
195 | |
242 | |
196 | while (k < (N >> 1)) |
243 | while (k < (N >> 1)) |
197 | { |
244 | { |
198 | int j = k << 1; |
245 | int j = k << 1; |
199 | |
246 | |
… | |
… | |
286 | /*****************************************************************************/ |
333 | /*****************************************************************************/ |
287 | |
334 | |
288 | static struct ev_idle **idles; |
335 | static struct ev_idle **idles; |
289 | static int idlemax, idlecnt; |
336 | static int idlemax, idlecnt; |
290 | |
337 | |
|
|
338 | static struct ev_prepare **prepares; |
|
|
339 | static int preparemax, preparecnt; |
|
|
340 | |
291 | static struct ev_check **checks; |
341 | static struct ev_check **checks; |
292 | static int checkmax, checkcnt; |
342 | static int checkmax, checkcnt; |
293 | |
343 | |
294 | /*****************************************************************************/ |
344 | /*****************************************************************************/ |
295 | |
345 | |
|
|
346 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
347 | static struct ev_signal childev; |
|
|
348 | |
|
|
349 | #ifndef WCONTINUED |
|
|
350 | # define WCONTINUED 0 |
|
|
351 | #endif |
|
|
352 | |
|
|
353 | static void |
|
|
354 | childcb (struct ev_signal *sw, int revents) |
|
|
355 | { |
|
|
356 | struct ev_child *w; |
|
|
357 | int pid, status; |
|
|
358 | |
|
|
359 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
|
|
360 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
361 | if (w->pid == pid || w->pid == -1) |
|
|
362 | { |
|
|
363 | w->status = status; |
|
|
364 | event ((W)w, EV_CHILD); |
|
|
365 | } |
|
|
366 | } |
|
|
367 | |
|
|
368 | /*****************************************************************************/ |
|
|
369 | |
296 | #if HAVE_EPOLL |
370 | #if HAVE_EPOLL |
297 | # include "ev_epoll.c" |
371 | # include "ev_epoll.c" |
298 | #endif |
372 | #endif |
299 | #if HAVE_SELECT |
373 | #if HAVE_SELECT |
300 | # include "ev_select.c" |
374 | # include "ev_select.c" |
301 | #endif |
375 | #endif |
302 | |
376 | |
303 | int ev_init (int flags) |
377 | int ev_init (int flags) |
304 | { |
378 | { |
|
|
379 | if (!ev_method) |
|
|
380 | { |
305 | #if HAVE_MONOTONIC |
381 | #if HAVE_MONOTONIC |
306 | { |
382 | { |
307 | struct timespec ts; |
383 | struct timespec ts; |
308 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
384 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
309 | have_monotonic = 1; |
385 | have_monotonic = 1; |
310 | } |
386 | } |
311 | #endif |
387 | #endif |
312 | |
388 | |
313 | ev_now = ev_time (); |
389 | ev_now = ev_time (); |
314 | now = get_clock (); |
390 | now = get_clock (); |
315 | diff = ev_now - now; |
391 | diff = ev_now - now; |
316 | |
392 | |
317 | if (pipe (sigpipe)) |
393 | if (pipe (sigpipe)) |
318 | return 0; |
394 | return 0; |
319 | |
395 | |
320 | ev_method = EVMETHOD_NONE; |
396 | ev_method = EVMETHOD_NONE; |
321 | #if HAVE_EPOLL |
397 | #if HAVE_EPOLL |
322 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
398 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
323 | #endif |
399 | #endif |
324 | #if HAVE_SELECT |
400 | #if HAVE_SELECT |
325 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
401 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
326 | #endif |
402 | #endif |
327 | |
403 | |
328 | if (ev_method) |
404 | if (ev_method) |
329 | { |
405 | { |
330 | evw_init (&sigev, sigcb, 0); |
406 | evw_init (&sigev, sigcb); |
331 | siginit (); |
407 | siginit (); |
|
|
408 | |
|
|
409 | evsignal_init (&childev, childcb, SIGCHLD); |
|
|
410 | evsignal_start (&childev); |
|
|
411 | } |
332 | } |
412 | } |
333 | |
413 | |
334 | return ev_method; |
414 | return ev_method; |
335 | } |
415 | } |
336 | |
416 | |
… | |
… | |
389 | } |
469 | } |
390 | |
470 | |
391 | static void |
471 | static void |
392 | call_pending () |
472 | call_pending () |
393 | { |
473 | { |
394 | int i; |
474 | while (pendingcnt) |
395 | |
|
|
396 | for (i = 0; i < pendingcnt; ++i) |
|
|
397 | { |
475 | { |
398 | ANPENDING *p = pendings + i; |
476 | ANPENDING *p = pendings + --pendingcnt; |
399 | |
477 | |
400 | if (p->w) |
478 | if (p->w) |
401 | { |
479 | { |
402 | p->w->pending = 0; |
480 | p->w->pending = 0; |
403 | p->w->cb (p->w, p->events); |
481 | p->w->cb (p->w, p->events); |
404 | } |
482 | } |
405 | } |
483 | } |
406 | |
|
|
407 | pendingcnt = 0; |
|
|
408 | } |
484 | } |
409 | |
485 | |
410 | static void |
486 | static void |
411 | timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) |
487 | timers_reify () |
412 | { |
488 | { |
413 | while (timercnt && timers [0]->at <= now) |
489 | while (timercnt && timers [0]->at <= now) |
414 | { |
490 | { |
415 | struct ev_timer *w = timers [0]; |
491 | struct ev_timer *w = timers [0]; |
|
|
492 | |
|
|
493 | event ((W)w, EV_TIMEOUT); |
416 | |
494 | |
417 | /* first reschedule or stop timer */ |
495 | /* first reschedule or stop timer */ |
418 | if (w->repeat) |
496 | if (w->repeat) |
419 | { |
497 | { |
420 | if (w->is_abs) |
|
|
421 | w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
422 | else |
|
|
423 | w->at = now + w->repeat; |
498 | w->at = now + w->repeat; |
424 | |
499 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
425 | assert (w->at > now); |
|
|
426 | |
|
|
427 | downheap (timers, timercnt, 0); |
500 | downheap ((WT *)timers, timercnt, 0); |
428 | } |
501 | } |
429 | else |
502 | else |
430 | { |
|
|
431 | evtimer_stop (w); /* nonrepeating: stop timer */ |
503 | evtimer_stop (w); /* nonrepeating: stop timer */ |
432 | --timercnt; /* maybe pass by reference instead? */ |
504 | } |
|
|
505 | } |
|
|
506 | |
|
|
507 | static void |
|
|
508 | periodics_reify () |
|
|
509 | { |
|
|
510 | while (periodiccnt && periodics [0]->at <= ev_now) |
|
|
511 | { |
|
|
512 | struct ev_periodic *w = periodics [0]; |
|
|
513 | |
|
|
514 | /* first reschedule or stop timer */ |
|
|
515 | if (w->interval) |
433 | } |
516 | { |
|
|
517 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
|
|
518 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
|
|
519 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
520 | } |
|
|
521 | else |
|
|
522 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
434 | |
523 | |
435 | event ((W)w, EV_TIMEOUT); |
524 | event ((W)w, EV_TIMEOUT); |
436 | } |
525 | } |
437 | } |
526 | } |
438 | |
527 | |
439 | static void |
528 | static void |
|
|
529 | periodics_reschedule (ev_tstamp diff) |
|
|
530 | { |
|
|
531 | int i; |
|
|
532 | |
|
|
533 | /* adjust periodics after time jump */ |
|
|
534 | for (i = 0; i < periodiccnt; ++i) |
|
|
535 | { |
|
|
536 | struct ev_periodic *w = periodics [i]; |
|
|
537 | |
|
|
538 | if (w->interval) |
|
|
539 | { |
|
|
540 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
541 | |
|
|
542 | if (fabs (diff) >= 1e-4) |
|
|
543 | { |
|
|
544 | evperiodic_stop (w); |
|
|
545 | evperiodic_start (w); |
|
|
546 | |
|
|
547 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
548 | } |
|
|
549 | } |
|
|
550 | } |
|
|
551 | } |
|
|
552 | |
|
|
553 | static void |
440 | time_update () |
554 | time_update () |
441 | { |
555 | { |
442 | int i; |
556 | int i; |
|
|
557 | |
443 | ev_now = ev_time (); |
558 | ev_now = ev_time (); |
444 | |
559 | |
445 | if (have_monotonic) |
560 | if (have_monotonic) |
446 | { |
561 | { |
447 | ev_tstamp odiff = diff; |
562 | ev_tstamp odiff = diff; |
448 | |
563 | |
449 | /* detecting time jumps is much more difficult */ |
|
|
450 | for (i = 2; --i; ) /* loop a few times, before making important decisions */ |
564 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
451 | { |
565 | { |
452 | now = get_clock (); |
566 | now = get_clock (); |
453 | diff = ev_now - now; |
567 | diff = ev_now - now; |
454 | |
568 | |
455 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
569 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
456 | return; /* all is well */ |
570 | return; /* all is well */ |
457 | |
571 | |
458 | ev_now = ev_time (); |
572 | ev_now = ev_time (); |
459 | } |
573 | } |
460 | |
574 | |
461 | /* time jump detected, reschedule atimers */ |
575 | periodics_reschedule (diff - odiff); |
462 | for (i = 0; i < atimercnt; ++i) |
576 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
463 | { |
|
|
464 | struct ev_timer *w = atimers [i]; |
|
|
465 | w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
466 | } |
|
|
467 | } |
577 | } |
468 | else |
578 | else |
469 | { |
579 | { |
470 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
580 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
471 | /* time jump detected, adjust rtimers */ |
581 | { |
|
|
582 | periodics_reschedule (ev_now - now); |
|
|
583 | |
|
|
584 | /* adjust timers. this is easy, as the offset is the same for all */ |
472 | for (i = 0; i < rtimercnt; ++i) |
585 | for (i = 0; i < timercnt; ++i) |
473 | rtimers [i]->at += ev_now - now; |
586 | timers [i]->at += diff; |
|
|
587 | } |
474 | |
588 | |
475 | now = ev_now; |
589 | now = ev_now; |
476 | } |
590 | } |
477 | } |
591 | } |
478 | |
592 | |
479 | int ev_loop_done; |
593 | int ev_loop_done; |
480 | |
594 | |
481 | void ev_loop (int flags) |
595 | void ev_loop (int flags) |
482 | { |
596 | { |
483 | double block; |
597 | double block; |
484 | ev_loop_done = flags & EVLOOP_ONESHOT; |
598 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
485 | |
|
|
486 | if (checkcnt) |
|
|
487 | { |
|
|
488 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
489 | call_pending (); |
|
|
490 | } |
|
|
491 | |
599 | |
492 | do |
600 | do |
493 | { |
601 | { |
|
|
602 | /* queue check watchers (and execute them) */ |
|
|
603 | if (preparecnt) |
|
|
604 | { |
|
|
605 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
|
|
606 | call_pending (); |
|
|
607 | } |
|
|
608 | |
494 | /* update fd-related kernel structures */ |
609 | /* update fd-related kernel structures */ |
495 | fd_reify (); |
610 | fd_reify (); |
496 | |
611 | |
497 | /* calculate blocking time */ |
612 | /* calculate blocking time */ |
|
|
613 | |
|
|
614 | /* we only need this for !monotonic clockor timers, but as we basically |
|
|
615 | always have timers, we just calculate it always */ |
|
|
616 | ev_now = ev_time (); |
|
|
617 | |
498 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
618 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
499 | block = 0.; |
619 | block = 0.; |
500 | else |
620 | else |
501 | { |
621 | { |
502 | block = MAX_BLOCKTIME; |
622 | block = MAX_BLOCKTIME; |
503 | |
623 | |
504 | if (rtimercnt) |
624 | if (timercnt) |
505 | { |
625 | { |
506 | ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; |
626 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
507 | if (block > to) block = to; |
627 | if (block > to) block = to; |
508 | } |
628 | } |
509 | |
629 | |
510 | if (atimercnt) |
630 | if (periodiccnt) |
511 | { |
631 | { |
512 | ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; |
632 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
513 | if (block > to) block = to; |
633 | if (block > to) block = to; |
514 | } |
634 | } |
515 | |
635 | |
516 | if (block < 0.) block = 0.; |
636 | if (block < 0.) block = 0.; |
517 | } |
637 | } |
… | |
… | |
520 | |
640 | |
521 | /* update ev_now, do magic */ |
641 | /* update ev_now, do magic */ |
522 | time_update (); |
642 | time_update (); |
523 | |
643 | |
524 | /* queue pending timers and reschedule them */ |
644 | /* queue pending timers and reschedule them */ |
525 | /* absolute timers first */ |
645 | timers_reify (); /* relative timers called last */ |
526 | timers_reify (atimers, atimercnt, ev_now); |
646 | periodics_reify (); /* absolute timers called first */ |
527 | /* relative timers second */ |
|
|
528 | timers_reify (rtimers, rtimercnt, now); |
|
|
529 | |
647 | |
530 | /* queue idle watchers unless io or timers are pending */ |
648 | /* queue idle watchers unless io or timers are pending */ |
531 | if (!pendingcnt) |
649 | if (!pendingcnt) |
532 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
650 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
533 | |
651 | |
534 | /* queue check and possibly idle watchers */ |
652 | /* queue check watchers, to be executed first */ |
|
|
653 | if (checkcnt) |
535 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
654 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
536 | |
655 | |
537 | call_pending (); |
656 | call_pending (); |
538 | } |
657 | } |
539 | while (!ev_loop_done); |
658 | while (!ev_loop_done); |
|
|
659 | |
|
|
660 | if (ev_loop_done != 2) |
|
|
661 | ev_loop_done = 0; |
540 | } |
662 | } |
541 | |
663 | |
542 | /*****************************************************************************/ |
664 | /*****************************************************************************/ |
543 | |
665 | |
544 | static void |
666 | static void |
… | |
… | |
562 | head = &(*head)->next; |
684 | head = &(*head)->next; |
563 | } |
685 | } |
564 | } |
686 | } |
565 | |
687 | |
566 | static void |
688 | static void |
|
|
689 | ev_clear (W w) |
|
|
690 | { |
|
|
691 | if (w->pending) |
|
|
692 | { |
|
|
693 | pendings [w->pending - 1].w = 0; |
|
|
694 | w->pending = 0; |
|
|
695 | } |
|
|
696 | } |
|
|
697 | |
|
|
698 | static void |
567 | ev_start (W w, int active) |
699 | ev_start (W w, int active) |
568 | { |
700 | { |
569 | w->pending = 0; |
|
|
570 | w->active = active; |
701 | w->active = active; |
571 | } |
702 | } |
572 | |
703 | |
573 | static void |
704 | static void |
574 | ev_stop (W w) |
705 | ev_stop (W w) |
575 | { |
706 | { |
576 | if (w->pending) |
|
|
577 | pendings [w->pending - 1].w = 0; |
|
|
578 | |
|
|
579 | w->active = 0; |
707 | w->active = 0; |
580 | } |
708 | } |
581 | |
709 | |
582 | /*****************************************************************************/ |
710 | /*****************************************************************************/ |
583 | |
711 | |
… | |
… | |
599 | } |
727 | } |
600 | |
728 | |
601 | void |
729 | void |
602 | evio_stop (struct ev_io *w) |
730 | evio_stop (struct ev_io *w) |
603 | { |
731 | { |
|
|
732 | ev_clear ((W)w); |
604 | if (!ev_is_active (w)) |
733 | if (!ev_is_active (w)) |
605 | return; |
734 | return; |
606 | |
735 | |
607 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
736 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
608 | ev_stop ((W)w); |
737 | ev_stop ((W)w); |
… | |
… | |
616 | evtimer_start (struct ev_timer *w) |
745 | evtimer_start (struct ev_timer *w) |
617 | { |
746 | { |
618 | if (ev_is_active (w)) |
747 | if (ev_is_active (w)) |
619 | return; |
748 | return; |
620 | |
749 | |
621 | if (w->is_abs) |
750 | w->at += now; |
622 | { |
|
|
623 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
|
|
624 | if (w->repeat) |
|
|
625 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
|
|
626 | |
751 | |
|
|
752 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
|
|
753 | |
627 | ev_start ((W)w, ++atimercnt); |
754 | ev_start ((W)w, ++timercnt); |
628 | array_needsize (atimers, atimermax, atimercnt, ); |
755 | array_needsize (timers, timermax, timercnt, ); |
629 | atimers [atimercnt - 1] = w; |
756 | timers [timercnt - 1] = w; |
630 | upheap (atimers, atimercnt - 1); |
757 | upheap ((WT *)timers, timercnt - 1); |
631 | } |
|
|
632 | else |
|
|
633 | { |
|
|
634 | w->at += now; |
|
|
635 | |
|
|
636 | ev_start ((W)w, ++rtimercnt); |
|
|
637 | array_needsize (rtimers, rtimermax, rtimercnt, ); |
|
|
638 | rtimers [rtimercnt - 1] = w; |
|
|
639 | upheap (rtimers, rtimercnt - 1); |
|
|
640 | } |
|
|
641 | |
|
|
642 | } |
758 | } |
643 | |
759 | |
644 | void |
760 | void |
645 | evtimer_stop (struct ev_timer *w) |
761 | evtimer_stop (struct ev_timer *w) |
646 | { |
762 | { |
|
|
763 | ev_clear ((W)w); |
647 | if (!ev_is_active (w)) |
764 | if (!ev_is_active (w)) |
648 | return; |
765 | return; |
649 | |
766 | |
650 | if (w->is_abs) |
|
|
651 | { |
|
|
652 | if (w->active < atimercnt--) |
767 | if (w->active < timercnt--) |
653 | { |
768 | { |
654 | atimers [w->active - 1] = atimers [atimercnt]; |
769 | timers [w->active - 1] = timers [timercnt]; |
|
|
770 | downheap ((WT *)timers, timercnt, w->active - 1); |
|
|
771 | } |
|
|
772 | |
|
|
773 | w->at = w->repeat; |
|
|
774 | |
|
|
775 | ev_stop ((W)w); |
|
|
776 | } |
|
|
777 | |
|
|
778 | void |
|
|
779 | evtimer_again (struct ev_timer *w) |
|
|
780 | { |
|
|
781 | if (ev_is_active (w)) |
|
|
782 | { |
|
|
783 | if (w->repeat) |
|
|
784 | { |
|
|
785 | w->at = now + w->repeat; |
655 | downheap (atimers, atimercnt, w->active - 1); |
786 | downheap ((WT *)timers, timercnt, w->active - 1); |
656 | } |
|
|
657 | } |
|
|
658 | else |
|
|
659 | { |
|
|
660 | if (w->active < rtimercnt--) |
|
|
661 | { |
787 | } |
662 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
788 | else |
663 | downheap (rtimers, rtimercnt, w->active - 1); |
789 | evtimer_stop (w); |
664 | } |
790 | } |
|
|
791 | else if (w->repeat) |
|
|
792 | evtimer_start (w); |
|
|
793 | } |
|
|
794 | |
|
|
795 | void |
|
|
796 | evperiodic_start (struct ev_periodic *w) |
|
|
797 | { |
|
|
798 | if (ev_is_active (w)) |
|
|
799 | return; |
|
|
800 | |
|
|
801 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
|
|
802 | |
|
|
803 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
|
|
804 | if (w->interval) |
|
|
805 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
806 | |
|
|
807 | ev_start ((W)w, ++periodiccnt); |
|
|
808 | array_needsize (periodics, periodicmax, periodiccnt, ); |
|
|
809 | periodics [periodiccnt - 1] = w; |
|
|
810 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
811 | } |
|
|
812 | |
|
|
813 | void |
|
|
814 | evperiodic_stop (struct ev_periodic *w) |
|
|
815 | { |
|
|
816 | ev_clear ((W)w); |
|
|
817 | if (!ev_is_active (w)) |
|
|
818 | return; |
|
|
819 | |
|
|
820 | if (w->active < periodiccnt--) |
|
|
821 | { |
|
|
822 | periodics [w->active - 1] = periodics [periodiccnt]; |
|
|
823 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
665 | } |
824 | } |
666 | |
825 | |
667 | ev_stop ((W)w); |
826 | ev_stop ((W)w); |
668 | } |
827 | } |
669 | |
828 | |
… | |
… | |
688 | } |
847 | } |
689 | |
848 | |
690 | void |
849 | void |
691 | evsignal_stop (struct ev_signal *w) |
850 | evsignal_stop (struct ev_signal *w) |
692 | { |
851 | { |
|
|
852 | ev_clear ((W)w); |
693 | if (!ev_is_active (w)) |
853 | if (!ev_is_active (w)) |
694 | return; |
854 | return; |
695 | |
855 | |
696 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
856 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
697 | ev_stop ((W)w); |
857 | ev_stop ((W)w); |
… | |
… | |
710 | idles [idlecnt - 1] = w; |
870 | idles [idlecnt - 1] = w; |
711 | } |
871 | } |
712 | |
872 | |
713 | void evidle_stop (struct ev_idle *w) |
873 | void evidle_stop (struct ev_idle *w) |
714 | { |
874 | { |
|
|
875 | ev_clear ((W)w); |
|
|
876 | if (ev_is_active (w)) |
|
|
877 | return; |
|
|
878 | |
715 | idles [w->active - 1] = idles [--idlecnt]; |
879 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
880 | ev_stop ((W)w); |
|
|
881 | } |
|
|
882 | |
|
|
883 | void evprepare_start (struct ev_prepare *w) |
|
|
884 | { |
|
|
885 | if (ev_is_active (w)) |
|
|
886 | return; |
|
|
887 | |
|
|
888 | ev_start ((W)w, ++preparecnt); |
|
|
889 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
890 | prepares [preparecnt - 1] = w; |
|
|
891 | } |
|
|
892 | |
|
|
893 | void evprepare_stop (struct ev_prepare *w) |
|
|
894 | { |
|
|
895 | ev_clear ((W)w); |
|
|
896 | if (ev_is_active (w)) |
|
|
897 | return; |
|
|
898 | |
|
|
899 | prepares [w->active - 1] = prepares [--preparecnt]; |
716 | ev_stop ((W)w); |
900 | ev_stop ((W)w); |
717 | } |
901 | } |
718 | |
902 | |
719 | void evcheck_start (struct ev_check *w) |
903 | void evcheck_start (struct ev_check *w) |
720 | { |
904 | { |
… | |
… | |
726 | checks [checkcnt - 1] = w; |
910 | checks [checkcnt - 1] = w; |
727 | } |
911 | } |
728 | |
912 | |
729 | void evcheck_stop (struct ev_check *w) |
913 | void evcheck_stop (struct ev_check *w) |
730 | { |
914 | { |
|
|
915 | ev_clear ((W)w); |
|
|
916 | if (ev_is_active (w)) |
|
|
917 | return; |
|
|
918 | |
731 | checks [w->active - 1] = checks [--checkcnt]; |
919 | checks [w->active - 1] = checks [--checkcnt]; |
732 | ev_stop ((W)w); |
920 | ev_stop ((W)w); |
733 | } |
921 | } |
734 | |
922 | |
|
|
923 | void evchild_start (struct ev_child *w) |
|
|
924 | { |
|
|
925 | if (ev_is_active (w)) |
|
|
926 | return; |
|
|
927 | |
|
|
928 | ev_start ((W)w, 1); |
|
|
929 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
930 | } |
|
|
931 | |
|
|
932 | void evchild_stop (struct ev_child *w) |
|
|
933 | { |
|
|
934 | ev_clear ((W)w); |
|
|
935 | if (ev_is_active (w)) |
|
|
936 | return; |
|
|
937 | |
|
|
938 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
939 | ev_stop ((W)w); |
|
|
940 | } |
|
|
941 | |
|
|
942 | /*****************************************************************************/ |
|
|
943 | |
|
|
944 | struct ev_once |
|
|
945 | { |
|
|
946 | struct ev_io io; |
|
|
947 | struct ev_timer to; |
|
|
948 | void (*cb)(int revents, void *arg); |
|
|
949 | void *arg; |
|
|
950 | }; |
|
|
951 | |
|
|
952 | static void |
|
|
953 | once_cb (struct ev_once *once, int revents) |
|
|
954 | { |
|
|
955 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
956 | void *arg = once->arg; |
|
|
957 | |
|
|
958 | evio_stop (&once->io); |
|
|
959 | evtimer_stop (&once->to); |
|
|
960 | free (once); |
|
|
961 | |
|
|
962 | cb (revents, arg); |
|
|
963 | } |
|
|
964 | |
|
|
965 | static void |
|
|
966 | once_cb_io (struct ev_io *w, int revents) |
|
|
967 | { |
|
|
968 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
969 | } |
|
|
970 | |
|
|
971 | static void |
|
|
972 | once_cb_to (struct ev_timer *w, int revents) |
|
|
973 | { |
|
|
974 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
975 | } |
|
|
976 | |
|
|
977 | void |
|
|
978 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
979 | { |
|
|
980 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
981 | |
|
|
982 | if (!once) |
|
|
983 | cb (EV_ERROR, arg); |
|
|
984 | else |
|
|
985 | { |
|
|
986 | once->cb = cb; |
|
|
987 | once->arg = arg; |
|
|
988 | |
|
|
989 | evw_init (&once->io, once_cb_io); |
|
|
990 | |
|
|
991 | if (fd >= 0) |
|
|
992 | { |
|
|
993 | evio_set (&once->io, fd, events); |
|
|
994 | evio_start (&once->io); |
|
|
995 | } |
|
|
996 | |
|
|
997 | evw_init (&once->to, once_cb_to); |
|
|
998 | |
|
|
999 | if (timeout >= 0.) |
|
|
1000 | { |
|
|
1001 | evtimer_set (&once->to, timeout, 0.); |
|
|
1002 | evtimer_start (&once->to); |
|
|
1003 | } |
|
|
1004 | } |
|
|
1005 | } |
|
|
1006 | |
735 | /*****************************************************************************/ |
1007 | /*****************************************************************************/ |
736 | |
1008 | |
737 | #if 0 |
1009 | #if 0 |
|
|
1010 | |
|
|
1011 | struct ev_io wio; |
738 | |
1012 | |
739 | static void |
1013 | static void |
740 | sin_cb (struct ev_io *w, int revents) |
1014 | sin_cb (struct ev_io *w, int revents) |
741 | { |
1015 | { |
742 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
1016 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
… | |
… | |
752 | |
1026 | |
753 | static void |
1027 | static void |
754 | scb (struct ev_signal *w, int revents) |
1028 | scb (struct ev_signal *w, int revents) |
755 | { |
1029 | { |
756 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1030 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
1031 | evio_stop (&wio); |
|
|
1032 | evio_start (&wio); |
757 | } |
1033 | } |
758 | |
1034 | |
759 | static void |
1035 | static void |
760 | gcb (struct ev_signal *w, int revents) |
1036 | gcb (struct ev_signal *w, int revents) |
761 | { |
1037 | { |
762 | fprintf (stderr, "generic %x\n", revents); |
1038 | fprintf (stderr, "generic %x\n", revents); |
|
|
1039 | |
763 | } |
1040 | } |
764 | |
1041 | |
765 | int main (void) |
1042 | int main (void) |
766 | { |
1043 | { |
767 | struct ev_io sin; |
|
|
768 | |
|
|
769 | ev_init (0); |
1044 | ev_init (0); |
770 | |
1045 | |
771 | evw_init (&sin, sin_cb, 55); |
|
|
772 | evio_set (&sin, 0, EV_READ); |
1046 | evio_init (&wio, sin_cb, 0, EV_READ); |
773 | evio_start (&sin); |
1047 | evio_start (&wio); |
774 | |
1048 | |
775 | struct ev_timer t[10000]; |
1049 | struct ev_timer t[10000]; |
776 | |
1050 | |
777 | #if 0 |
1051 | #if 0 |
778 | int i; |
1052 | int i; |
779 | for (i = 0; i < 10000; ++i) |
1053 | for (i = 0; i < 10000; ++i) |
780 | { |
1054 | { |
781 | struct ev_timer *w = t + i; |
1055 | struct ev_timer *w = t + i; |
782 | evw_init (w, ocb, i); |
1056 | evw_init (w, ocb, i); |
783 | evtimer_set_abs (w, drand48 (), 0.99775533); |
1057 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
784 | evtimer_start (w); |
1058 | evtimer_start (w); |
785 | if (drand48 () < 0.5) |
1059 | if (drand48 () < 0.5) |
786 | evtimer_stop (w); |
1060 | evtimer_stop (w); |
787 | } |
1061 | } |
788 | #endif |
1062 | #endif |
789 | |
1063 | |
790 | struct ev_timer t1; |
1064 | struct ev_timer t1; |
791 | evw_init (&t1, ocb, 0); |
1065 | evtimer_init (&t1, ocb, 5, 10); |
792 | evtimer_set_abs (&t1, 5, 10); |
|
|
793 | evtimer_start (&t1); |
1066 | evtimer_start (&t1); |
794 | |
1067 | |
795 | struct ev_signal sig; |
1068 | struct ev_signal sig; |
796 | evw_init (&sig, scb, 65535); |
|
|
797 | evsignal_set (&sig, SIGQUIT); |
1069 | evsignal_init (&sig, scb, SIGQUIT); |
798 | evsignal_start (&sig); |
1070 | evsignal_start (&sig); |
799 | |
1071 | |
800 | struct ev_check cw; |
1072 | struct ev_check cw; |
801 | evw_init (&cw, gcb, 0); |
1073 | evcheck_init (&cw, gcb); |
802 | evcheck_start (&cw); |
1074 | evcheck_start (&cw); |
803 | |
1075 | |
804 | struct ev_idle iw; |
1076 | struct ev_idle iw; |
805 | evw_init (&iw, gcb, 0); |
1077 | evidle_init (&iw, gcb); |
806 | evidle_start (&iw); |
1078 | evidle_start (&iw); |
807 | |
1079 | |
808 | ev_loop (0); |
1080 | ev_loop (0); |
809 | |
1081 | |
810 | return 0; |
1082 | return 0; |