|
|
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> |
… | |
… | |
7 | |
36 | |
8 | #include <stdio.h> |
37 | #include <stdio.h> |
9 | |
38 | |
10 | #include <assert.h> |
39 | #include <assert.h> |
11 | #include <errno.h> |
40 | #include <errno.h> |
|
|
41 | #include <sys/types.h> |
|
|
42 | #include <sys/wait.h> |
12 | #include <sys/time.h> |
43 | #include <sys/time.h> |
13 | #include <time.h> |
44 | #include <time.h> |
14 | |
|
|
15 | #define HAVE_EPOLL 1 |
|
|
16 | |
45 | |
17 | #ifndef HAVE_MONOTONIC |
46 | #ifndef HAVE_MONOTONIC |
18 | # ifdef CLOCK_MONOTONIC |
47 | # ifdef CLOCK_MONOTONIC |
19 | # define HAVE_MONOTONIC 1 |
48 | # define HAVE_MONOTONIC 1 |
20 | # endif |
49 | # endif |
… | |
… | |
32 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
33 | #endif |
62 | #endif |
34 | |
63 | |
35 | #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) */ |
36 | #define MAX_BLOCKTIME 60. |
65 | #define MAX_BLOCKTIME 60. |
|
|
66 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
37 | |
67 | |
38 | #include "ev.h" |
68 | #include "ev.h" |
39 | |
69 | |
40 | typedef struct ev_watcher *W; |
70 | typedef struct ev_watcher *W; |
41 | typedef struct ev_watcher_list *WL; |
71 | typedef struct ev_watcher_list *WL; |
… | |
… | |
83 | } |
113 | } |
84 | |
114 | |
85 | #define array_needsize(base,cur,cnt,init) \ |
115 | #define array_needsize(base,cur,cnt,init) \ |
86 | if ((cnt) > cur) \ |
116 | if ((cnt) > cur) \ |
87 | { \ |
117 | { \ |
88 | int newcnt = cur ? cur << 1 : 16; \ |
118 | int newcnt = cur; \ |
|
|
119 | do \ |
|
|
120 | { \ |
|
|
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
|
|
122 | } \ |
|
|
123 | while ((cnt) > newcnt); \ |
|
|
124 | \ |
89 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
90 | init (base + cur, newcnt - cur); \ |
126 | init (base + cur, newcnt - cur); \ |
91 | cur = newcnt; \ |
127 | cur = newcnt; \ |
92 | } |
128 | } |
93 | |
129 | |
… | |
… | |
159 | |
195 | |
160 | for (i = 0; i < eventcnt; ++i) |
196 | for (i = 0; i < eventcnt; ++i) |
161 | event (events [i], type); |
197 | event (events [i], type); |
162 | } |
198 | } |
163 | |
199 | |
|
|
200 | /* called on EBADF to verify fds */ |
|
|
201 | static void |
|
|
202 | fd_recheck (void) |
|
|
203 | { |
|
|
204 | int fd; |
|
|
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 | { |
|
|
211 | event ((W)anfds [fd].head, EV_ERROR); |
|
|
212 | evio_stop (anfds [fd].head); |
|
|
213 | } |
|
|
214 | } |
|
|
215 | |
164 | /*****************************************************************************/ |
216 | /*****************************************************************************/ |
165 | |
217 | |
166 | static struct ev_timer **timers; |
218 | static struct ev_timer **timers; |
167 | static int timermax, timercnt; |
219 | static int timermax, timercnt; |
168 | |
220 | |
… | |
… | |
284 | /*****************************************************************************/ |
336 | /*****************************************************************************/ |
285 | |
337 | |
286 | static struct ev_idle **idles; |
338 | static struct ev_idle **idles; |
287 | static int idlemax, idlecnt; |
339 | static int idlemax, idlecnt; |
288 | |
340 | |
|
|
341 | static struct ev_prepare **prepares; |
|
|
342 | static int preparemax, preparecnt; |
|
|
343 | |
289 | static struct ev_check **checks; |
344 | static struct ev_check **checks; |
290 | static int checkmax, checkcnt; |
345 | static int checkmax, checkcnt; |
291 | |
346 | |
292 | /*****************************************************************************/ |
347 | /*****************************************************************************/ |
293 | |
348 | |
|
|
349 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
350 | static struct ev_signal childev; |
|
|
351 | |
|
|
352 | #ifndef WCONTINUED |
|
|
353 | # define WCONTINUED 0 |
|
|
354 | #endif |
|
|
355 | |
|
|
356 | static void |
|
|
357 | childcb (struct ev_signal *sw, int revents) |
|
|
358 | { |
|
|
359 | struct ev_child *w; |
|
|
360 | int pid, status; |
|
|
361 | |
|
|
362 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
|
|
363 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
364 | if (w->pid == pid || w->pid == -1) |
|
|
365 | { |
|
|
366 | w->status = status; |
|
|
367 | event ((W)w, EV_CHILD); |
|
|
368 | } |
|
|
369 | } |
|
|
370 | |
|
|
371 | /*****************************************************************************/ |
|
|
372 | |
294 | #if HAVE_EPOLL |
373 | #if HAVE_EPOLL |
295 | # include "ev_epoll.c" |
374 | # include "ev_epoll.c" |
296 | #endif |
375 | #endif |
297 | #if HAVE_SELECT |
376 | #if HAVE_SELECT |
298 | # include "ev_select.c" |
377 | # include "ev_select.c" |
299 | #endif |
378 | #endif |
300 | |
379 | |
|
|
380 | int |
|
|
381 | ev_version_major (void) |
|
|
382 | { |
|
|
383 | return EV_VERSION_MAJOR; |
|
|
384 | } |
|
|
385 | |
|
|
386 | int |
|
|
387 | ev_version_minor (void) |
|
|
388 | { |
|
|
389 | return EV_VERSION_MINOR; |
|
|
390 | } |
|
|
391 | |
301 | int ev_init (int flags) |
392 | int ev_init (int flags) |
302 | { |
393 | { |
|
|
394 | if (!ev_method) |
|
|
395 | { |
303 | #if HAVE_MONOTONIC |
396 | #if HAVE_MONOTONIC |
304 | { |
397 | { |
305 | struct timespec ts; |
398 | struct timespec ts; |
306 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
399 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
307 | have_monotonic = 1; |
400 | have_monotonic = 1; |
308 | } |
401 | } |
309 | #endif |
402 | #endif |
310 | |
403 | |
311 | ev_now = ev_time (); |
404 | ev_now = ev_time (); |
312 | now = get_clock (); |
405 | now = get_clock (); |
313 | diff = ev_now - now; |
406 | diff = ev_now - now; |
314 | |
407 | |
315 | if (pipe (sigpipe)) |
408 | if (pipe (sigpipe)) |
316 | return 0; |
409 | return 0; |
317 | |
410 | |
318 | ev_method = EVMETHOD_NONE; |
411 | ev_method = EVMETHOD_NONE; |
319 | #if HAVE_EPOLL |
412 | #if HAVE_EPOLL |
320 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
413 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
321 | #endif |
414 | #endif |
322 | #if HAVE_SELECT |
415 | #if HAVE_SELECT |
323 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
416 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
324 | #endif |
417 | #endif |
325 | |
418 | |
326 | if (ev_method) |
419 | if (ev_method) |
327 | { |
420 | { |
328 | evw_init (&sigev, sigcb); |
421 | evw_init (&sigev, sigcb); |
329 | siginit (); |
422 | siginit (); |
|
|
423 | |
|
|
424 | evsignal_init (&childev, childcb, SIGCHLD); |
|
|
425 | evsignal_start (&childev); |
|
|
426 | } |
330 | } |
427 | } |
331 | |
428 | |
332 | return ev_method; |
429 | return ev_method; |
333 | } |
430 | } |
334 | |
431 | |
335 | /*****************************************************************************/ |
432 | /*****************************************************************************/ |
336 | |
433 | |
|
|
434 | void |
337 | void ev_prefork (void) |
435 | ev_prefork (void) |
338 | { |
436 | { |
339 | /* nop */ |
437 | /* nop */ |
340 | } |
438 | } |
341 | |
439 | |
|
|
440 | void |
342 | void ev_postfork_parent (void) |
441 | ev_postfork_parent (void) |
343 | { |
442 | { |
344 | /* nop */ |
443 | /* nop */ |
345 | } |
444 | } |
346 | |
445 | |
|
|
446 | void |
347 | void ev_postfork_child (void) |
447 | ev_postfork_child (void) |
348 | { |
448 | { |
349 | #if HAVE_EPOLL |
449 | #if HAVE_EPOLL |
350 | if (ev_method == EVMETHOD_EPOLL) |
450 | if (ev_method == EVMETHOD_EPOLL) |
351 | epoll_postfork_child (); |
451 | epoll_postfork_child (); |
352 | #endif |
452 | #endif |
… | |
… | |
385 | |
485 | |
386 | fdchangecnt = 0; |
486 | fdchangecnt = 0; |
387 | } |
487 | } |
388 | |
488 | |
389 | static void |
489 | static void |
390 | call_pending () |
490 | call_pending (void) |
391 | { |
491 | { |
392 | int i; |
492 | while (pendingcnt) |
393 | |
|
|
394 | for (i = 0; i < pendingcnt; ++i) |
|
|
395 | { |
493 | { |
396 | ANPENDING *p = pendings + i; |
494 | ANPENDING *p = pendings + --pendingcnt; |
397 | |
495 | |
398 | if (p->w) |
496 | if (p->w) |
399 | { |
497 | { |
400 | p->w->pending = 0; |
498 | p->w->pending = 0; |
401 | p->w->cb (p->w, p->events); |
499 | p->w->cb (p->w, p->events); |
402 | } |
500 | } |
403 | } |
501 | } |
404 | |
|
|
405 | pendingcnt = 0; |
|
|
406 | } |
502 | } |
407 | |
503 | |
408 | static void |
504 | static void |
409 | timers_reify () |
505 | timers_reify (void) |
410 | { |
506 | { |
411 | while (timercnt && timers [0]->at <= now) |
507 | while (timercnt && timers [0]->at <= now) |
412 | { |
508 | { |
413 | struct ev_timer *w = timers [0]; |
509 | struct ev_timer *w = timers [0]; |
414 | |
510 | |
… | |
… | |
425 | evtimer_stop (w); /* nonrepeating: stop timer */ |
521 | evtimer_stop (w); /* nonrepeating: stop timer */ |
426 | } |
522 | } |
427 | } |
523 | } |
428 | |
524 | |
429 | static void |
525 | static void |
430 | periodics_reify () |
526 | periodics_reify (void) |
431 | { |
527 | { |
432 | while (periodiccnt && periodics [0]->at <= ev_now) |
528 | while (periodiccnt && periodics [0]->at <= ev_now) |
433 | { |
529 | { |
434 | struct ev_periodic *w = periodics [0]; |
530 | struct ev_periodic *w = periodics [0]; |
435 | |
531 | |
… | |
… | |
471 | } |
567 | } |
472 | } |
568 | } |
473 | } |
569 | } |
474 | |
570 | |
475 | static void |
571 | static void |
476 | time_update () |
572 | time_update (void) |
477 | { |
573 | { |
478 | int i; |
574 | int i; |
479 | |
575 | |
480 | ev_now = ev_time (); |
576 | ev_now = ev_time (); |
481 | |
577 | |
… | |
… | |
517 | void ev_loop (int flags) |
613 | void ev_loop (int flags) |
518 | { |
614 | { |
519 | double block; |
615 | double block; |
520 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
616 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
521 | |
617 | |
522 | if (checkcnt) |
|
|
523 | { |
|
|
524 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
525 | call_pending (); |
|
|
526 | } |
|
|
527 | |
|
|
528 | do |
618 | do |
529 | { |
619 | { |
|
|
620 | /* queue check watchers (and execute them) */ |
|
|
621 | if (preparecnt) |
|
|
622 | { |
|
|
623 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
|
|
624 | call_pending (); |
|
|
625 | } |
|
|
626 | |
530 | /* update fd-related kernel structures */ |
627 | /* update fd-related kernel structures */ |
531 | fd_reify (); |
628 | fd_reify (); |
532 | |
629 | |
533 | /* calculate blocking time */ |
630 | /* calculate blocking time */ |
534 | |
631 | |
535 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
632 | /* we only need this for !monotonic clockor timers, but as we basically |
|
|
633 | always have timers, we just calculate it always */ |
536 | ev_now = ev_time (); |
634 | ev_now = ev_time (); |
537 | |
635 | |
538 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
636 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
539 | block = 0.; |
637 | block = 0.; |
540 | else |
638 | else |
… | |
… | |
560 | |
658 | |
561 | /* update ev_now, do magic */ |
659 | /* update ev_now, do magic */ |
562 | time_update (); |
660 | time_update (); |
563 | |
661 | |
564 | /* queue pending timers and reschedule them */ |
662 | /* queue pending timers and reschedule them */ |
|
|
663 | timers_reify (); /* relative timers called last */ |
565 | periodics_reify (); /* absolute timers first */ |
664 | periodics_reify (); /* absolute timers called first */ |
566 | timers_reify (); /* relative timers second */ |
|
|
567 | |
665 | |
568 | /* queue idle watchers unless io or timers are pending */ |
666 | /* queue idle watchers unless io or timers are pending */ |
569 | if (!pendingcnt) |
667 | if (!pendingcnt) |
570 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
668 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
571 | |
669 | |
572 | /* queue check and possibly idle watchers */ |
670 | /* queue check watchers, to be executed first */ |
|
|
671 | if (checkcnt) |
573 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
672 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
574 | |
673 | |
575 | call_pending (); |
674 | call_pending (); |
576 | } |
675 | } |
577 | while (!ev_loop_done); |
676 | while (!ev_loop_done); |
578 | |
677 | |
… | |
… | |
797 | |
896 | |
798 | idles [w->active - 1] = idles [--idlecnt]; |
897 | idles [w->active - 1] = idles [--idlecnt]; |
799 | ev_stop ((W)w); |
898 | ev_stop ((W)w); |
800 | } |
899 | } |
801 | |
900 | |
|
|
901 | void evprepare_start (struct ev_prepare *w) |
|
|
902 | { |
|
|
903 | if (ev_is_active (w)) |
|
|
904 | return; |
|
|
905 | |
|
|
906 | ev_start ((W)w, ++preparecnt); |
|
|
907 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
908 | prepares [preparecnt - 1] = w; |
|
|
909 | } |
|
|
910 | |
|
|
911 | void evprepare_stop (struct ev_prepare *w) |
|
|
912 | { |
|
|
913 | ev_clear ((W)w); |
|
|
914 | if (ev_is_active (w)) |
|
|
915 | return; |
|
|
916 | |
|
|
917 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
918 | ev_stop ((W)w); |
|
|
919 | } |
|
|
920 | |
802 | void evcheck_start (struct ev_check *w) |
921 | void evcheck_start (struct ev_check *w) |
803 | { |
922 | { |
804 | if (ev_is_active (w)) |
923 | if (ev_is_active (w)) |
805 | return; |
924 | return; |
806 | |
925 | |
… | |
… | |
814 | ev_clear ((W)w); |
933 | ev_clear ((W)w); |
815 | if (ev_is_active (w)) |
934 | if (ev_is_active (w)) |
816 | return; |
935 | return; |
817 | |
936 | |
818 | checks [w->active - 1] = checks [--checkcnt]; |
937 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
938 | ev_stop ((W)w); |
|
|
939 | } |
|
|
940 | |
|
|
941 | void evchild_start (struct ev_child *w) |
|
|
942 | { |
|
|
943 | if (ev_is_active (w)) |
|
|
944 | return; |
|
|
945 | |
|
|
946 | ev_start ((W)w, 1); |
|
|
947 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
948 | } |
|
|
949 | |
|
|
950 | void evchild_stop (struct ev_child *w) |
|
|
951 | { |
|
|
952 | ev_clear ((W)w); |
|
|
953 | if (ev_is_active (w)) |
|
|
954 | return; |
|
|
955 | |
|
|
956 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
819 | ev_stop ((W)w); |
957 | ev_stop ((W)w); |
820 | } |
958 | } |
821 | |
959 | |
822 | /*****************************************************************************/ |
960 | /*****************************************************************************/ |
823 | |
961 | |