ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.18 by root, Wed Oct 31 16:29:52 2007 UTC vs.
Revision 1.24 by root, Wed Oct 31 20:46:44 2007 UTC

36 36
37#include <stdio.h> 37#include <stdio.h>
38 38
39#include <assert.h> 39#include <assert.h>
40#include <errno.h> 40#include <errno.h>
41#include <sys/types.h>
42#include <sys/wait.h>
41#include <sys/time.h> 43#include <sys/time.h>
42#include <time.h> 44#include <time.h>
43
44#define HAVE_EPOLL 1
45 45
46#ifndef HAVE_MONOTONIC 46#ifndef HAVE_MONOTONIC
47# ifdef CLOCK_MONOTONIC 47# ifdef CLOCK_MONOTONIC
48# define HAVE_MONOTONIC 1 48# define HAVE_MONOTONIC 1
49# endif 49# endif
61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */
62#endif 62#endif
63 63
64#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) */
65#define MAX_BLOCKTIME 60. 65#define MAX_BLOCKTIME 60.
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
66 67
67#include "ev.h" 68#include "ev.h"
68 69
69typedef struct ev_watcher *W; 70typedef struct ev_watcher *W;
70typedef struct ev_watcher_list *WL; 71typedef struct ev_watcher_list *WL;
112} 113}
113 114
114#define array_needsize(base,cur,cnt,init) \ 115#define array_needsize(base,cur,cnt,init) \
115 if ((cnt) > cur) \ 116 if ((cnt) > cur) \
116 { \ 117 { \
117 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 \
118 base = realloc (base, sizeof (*base) * (newcnt)); \ 125 base = realloc (base, sizeof (*base) * (newcnt)); \
119 init (base + cur, newcnt - cur); \ 126 init (base + cur, newcnt - cur); \
120 cur = newcnt; \ 127 cur = newcnt; \
121 } 128 }
122 129
188 195
189 for (i = 0; i < eventcnt; ++i) 196 for (i = 0; i < eventcnt; ++i)
190 event (events [i], type); 197 event (events [i], type);
191} 198}
192 199
200/* called on EBADF to verify fds */
201static void
202fd_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
193/*****************************************************************************/ 216/*****************************************************************************/
194 217
195static struct ev_timer **timers; 218static struct ev_timer **timers;
196static int timermax, timercnt; 219static int timermax, timercnt;
197 220
313/*****************************************************************************/ 336/*****************************************************************************/
314 337
315static struct ev_idle **idles; 338static struct ev_idle **idles;
316static int idlemax, idlecnt; 339static int idlemax, idlecnt;
317 340
341static struct ev_prepare **prepares;
342static int preparemax, preparecnt;
343
318static struct ev_check **checks; 344static struct ev_check **checks;
319static int checkmax, checkcnt; 345static int checkmax, checkcnt;
320 346
321/*****************************************************************************/ 347/*****************************************************************************/
322 348
349static struct ev_child *childs [PID_HASHSIZE];
350static struct ev_signal childev;
351
352#ifndef WCONTINUED
353# define WCONTINUED 0
354#endif
355
356static void
357childcb (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
323#if HAVE_EPOLL 373#if HAVE_EPOLL
324# include "ev_epoll.c" 374# include "ev_epoll.c"
325#endif 375#endif
326#if HAVE_SELECT 376#if HAVE_SELECT
327# include "ev_select.c" 377# include "ev_select.c"
328#endif 378#endif
329 379
380int
381ev_version_major (void)
382{
383 return EV_VERSION_MAJOR;
384}
385
386int
387ev_version_minor (void)
388{
389 return EV_VERSION_MINOR;
390}
391
330int ev_init (int flags) 392int ev_init (int flags)
331{ 393{
394 if (!ev_method)
395 {
332#if HAVE_MONOTONIC 396#if HAVE_MONOTONIC
333 { 397 {
334 struct timespec ts; 398 struct timespec ts;
335 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 399 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
336 have_monotonic = 1; 400 have_monotonic = 1;
337 } 401 }
338#endif 402#endif
339 403
340 ev_now = ev_time (); 404 ev_now = ev_time ();
341 now = get_clock (); 405 now = get_clock ();
342 diff = ev_now - now; 406 diff = ev_now - now;
343 407
344 if (pipe (sigpipe)) 408 if (pipe (sigpipe))
345 return 0; 409 return 0;
346 410
347 ev_method = EVMETHOD_NONE; 411 ev_method = EVMETHOD_NONE;
348#if HAVE_EPOLL 412#if HAVE_EPOLL
349 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 413 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
350#endif 414#endif
351#if HAVE_SELECT 415#if HAVE_SELECT
352 if (ev_method == EVMETHOD_NONE) select_init (flags); 416 if (ev_method == EVMETHOD_NONE) select_init (flags);
353#endif 417#endif
354 418
355 if (ev_method) 419 if (ev_method)
356 { 420 {
357 evw_init (&sigev, sigcb); 421 evw_init (&sigev, sigcb);
358 siginit (); 422 siginit ();
423
424 evsignal_init (&childev, childcb, SIGCHLD);
425 evsignal_start (&childev);
426 }
359 } 427 }
360 428
361 return ev_method; 429 return ev_method;
362} 430}
363 431
364/*****************************************************************************/ 432/*****************************************************************************/
365 433
434void
366void ev_prefork (void) 435ev_prefork (void)
367{ 436{
368 /* nop */ 437 /* nop */
369} 438}
370 439
440void
371void ev_postfork_parent (void) 441ev_postfork_parent (void)
372{ 442{
373 /* nop */ 443 /* nop */
374} 444}
375 445
446void
376void ev_postfork_child (void) 447ev_postfork_child (void)
377{ 448{
378#if HAVE_EPOLL 449#if HAVE_EPOLL
379 if (ev_method == EVMETHOD_EPOLL) 450 if (ev_method == EVMETHOD_EPOLL)
380 epoll_postfork_child (); 451 epoll_postfork_child ();
381#endif 452#endif
414 485
415 fdchangecnt = 0; 486 fdchangecnt = 0;
416} 487}
417 488
418static void 489static void
419call_pending () 490call_pending (void)
420{ 491{
421 while (pendingcnt) 492 while (pendingcnt)
422 { 493 {
423 ANPENDING *p = pendings + --pendingcnt; 494 ANPENDING *p = pendings + --pendingcnt;
424 495
429 } 500 }
430 } 501 }
431} 502}
432 503
433static void 504static void
434timers_reify () 505timers_reify (void)
435{ 506{
436 while (timercnt && timers [0]->at <= now) 507 while (timercnt && timers [0]->at <= now)
437 { 508 {
438 struct ev_timer *w = timers [0]; 509 struct ev_timer *w = timers [0];
439 510
450 evtimer_stop (w); /* nonrepeating: stop timer */ 521 evtimer_stop (w); /* nonrepeating: stop timer */
451 } 522 }
452} 523}
453 524
454static void 525static void
455periodics_reify () 526periodics_reify (void)
456{ 527{
457 while (periodiccnt && periodics [0]->at <= ev_now) 528 while (periodiccnt && periodics [0]->at <= ev_now)
458 { 529 {
459 struct ev_periodic *w = periodics [0]; 530 struct ev_periodic *w = periodics [0];
460 531
496 } 567 }
497 } 568 }
498} 569}
499 570
500static void 571static void
501time_update () 572time_update (void)
502{ 573{
503 int i; 574 int i;
504 575
505 ev_now = ev_time (); 576 ev_now = ev_time ();
506 577
542void ev_loop (int flags) 613void ev_loop (int flags)
543{ 614{
544 double block; 615 double block;
545 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 616 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
546 617
547 if (checkcnt)
548 {
549 queue_events ((W *)checks, checkcnt, EV_CHECK);
550 call_pending ();
551 }
552
553 do 618 do
554 { 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
555 /* update fd-related kernel structures */ 627 /* update fd-related kernel structures */
556 fd_reify (); 628 fd_reify ();
557 629
558 /* calculate blocking time */ 630 /* calculate blocking time */
559 631
560 /* 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 */
561 ev_now = ev_time (); 634 ev_now = ev_time ();
562 635
563 if (flags & EVLOOP_NONBLOCK || idlecnt) 636 if (flags & EVLOOP_NONBLOCK || idlecnt)
564 block = 0.; 637 block = 0.;
565 else 638 else
585 658
586 /* update ev_now, do magic */ 659 /* update ev_now, do magic */
587 time_update (); 660 time_update ();
588 661
589 /* queue pending timers and reschedule them */ 662 /* queue pending timers and reschedule them */
663 timers_reify (); /* relative timers called last */
590 periodics_reify (); /* absolute timers first */ 664 periodics_reify (); /* absolute timers called first */
591 timers_reify (); /* relative timers second */
592 665
593 /* queue idle watchers unless io or timers are pending */ 666 /* queue idle watchers unless io or timers are pending */
594 if (!pendingcnt) 667 if (!pendingcnt)
595 queue_events ((W *)idles, idlecnt, EV_IDLE); 668 queue_events ((W *)idles, idlecnt, EV_IDLE);
596 669
597 /* queue check and possibly idle watchers */ 670 /* queue check watchers, to be executed first */
671 if (checkcnt)
598 queue_events ((W *)checks, checkcnt, EV_CHECK); 672 queue_events ((W *)checks, checkcnt, EV_CHECK);
599 673
600 call_pending (); 674 call_pending ();
601 } 675 }
602 while (!ev_loop_done); 676 while (!ev_loop_done);
603 677
822 896
823 idles [w->active - 1] = idles [--idlecnt]; 897 idles [w->active - 1] = idles [--idlecnt];
824 ev_stop ((W)w); 898 ev_stop ((W)w);
825} 899}
826 900
901void 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
911void 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
827void evcheck_start (struct ev_check *w) 921void evcheck_start (struct ev_check *w)
828{ 922{
829 if (ev_is_active (w)) 923 if (ev_is_active (w))
830 return; 924 return;
831 925
839 ev_clear ((W)w); 933 ev_clear ((W)w);
840 if (ev_is_active (w)) 934 if (ev_is_active (w))
841 return; 935 return;
842 936
843 checks [w->active - 1] = checks [--checkcnt]; 937 checks [w->active - 1] = checks [--checkcnt];
938 ev_stop ((W)w);
939}
940
941void 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
950void 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);
844 ev_stop ((W)w); 957 ev_stop ((W)w);
845} 958}
846 959
847/*****************************************************************************/ 960/*****************************************************************************/
848 961

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines