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

Comparing libev/ev.c (file contents):
Revision 1.19 by root, Wed Oct 31 17:55:55 2007 UTC vs.
Revision 1.25 by root, Wed Oct 31 21:34:45 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 45
44#ifndef HAVE_MONOTONIC 46#ifndef HAVE_MONOTONIC
45# ifdef CLOCK_MONOTONIC 47# ifdef CLOCK_MONOTONIC
59# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */
60#endif 62#endif
61 63
62#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) */
63#define MAX_BLOCKTIME 60. 65#define MAX_BLOCKTIME 60.
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
64 67
65#include "ev.h" 68#include "ev.h"
66 69
67typedef struct ev_watcher *W; 70typedef struct ev_watcher *W;
68typedef struct ev_watcher_list *WL; 71typedef struct ev_watcher_list *WL;
110} 113}
111 114
112#define array_needsize(base,cur,cnt,init) \ 115#define array_needsize(base,cur,cnt,init) \
113 if ((cnt) > cur) \ 116 if ((cnt) > cur) \
114 { \ 117 { \
115 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 \
116 base = realloc (base, sizeof (*base) * (newcnt)); \ 125 base = realloc (base, sizeof (*base) * (newcnt)); \
117 init (base + cur, newcnt - cur); \ 126 init (base + cur, newcnt - cur); \
118 cur = newcnt; \ 127 cur = newcnt; \
119 } 128 }
120 129
188 event (events [i], type); 197 event (events [i], type);
189} 198}
190 199
191/* called on EBADF to verify fds */ 200/* called on EBADF to verify fds */
192static void 201static void
193fd_recheck () 202fd_recheck (void)
194{ 203{
195 int fd; 204 int fd;
196 205
197 for (fd = 0; fd < anfdmax; ++fd) 206 for (fd = 0; fd < anfdmax; ++fd)
198 if (anfds [fd].wev) 207 if (anfds [fd].wev)
199 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 208 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
200 while (anfds [fd].head) 209 while (anfds [fd].head)
210 {
211 event ((W)anfds [fd].head, EV_ERROR);
201 evio_stop (anfds [fd].head); 212 evio_stop (anfds [fd].head);
213 }
202} 214}
203 215
204/*****************************************************************************/ 216/*****************************************************************************/
205 217
206static struct ev_timer **timers; 218static struct ev_timer **timers;
324/*****************************************************************************/ 336/*****************************************************************************/
325 337
326static struct ev_idle **idles; 338static struct ev_idle **idles;
327static int idlemax, idlecnt; 339static int idlemax, idlecnt;
328 340
341static struct ev_prepare **prepares;
342static int preparemax, preparecnt;
343
329static struct ev_check **checks; 344static struct ev_check **checks;
330static int checkmax, checkcnt; 345static int checkmax, checkcnt;
331 346
332/*****************************************************************************/ 347/*****************************************************************************/
333 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
334#if HAVE_EPOLL 373#if HAVE_EPOLL
335# include "ev_epoll.c" 374# include "ev_epoll.c"
336#endif 375#endif
337#if HAVE_SELECT 376#if HAVE_SELECT
338# include "ev_select.c" 377# include "ev_select.c"
339#endif 378#endif
340 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
341int ev_init (int flags) 392int ev_init (int flags)
342{ 393{
394 if (!ev_method)
395 {
343#if HAVE_MONOTONIC 396#if HAVE_MONOTONIC
344 { 397 {
345 struct timespec ts; 398 struct timespec ts;
346 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 399 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
347 have_monotonic = 1; 400 have_monotonic = 1;
348 } 401 }
349#endif 402#endif
350 403
351 ev_now = ev_time (); 404 ev_now = ev_time ();
352 now = get_clock (); 405 now = get_clock ();
353 diff = ev_now - now; 406 diff = ev_now - now;
354 407
355 if (pipe (sigpipe)) 408 if (pipe (sigpipe))
356 return 0; 409 return 0;
357 410
358 ev_method = EVMETHOD_NONE; 411 ev_method = EVMETHOD_NONE;
359#if HAVE_EPOLL 412#if HAVE_EPOLL
360 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 413 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
361#endif 414#endif
362#if HAVE_SELECT 415#if HAVE_SELECT
363 if (ev_method == EVMETHOD_NONE) select_init (flags); 416 if (ev_method == EVMETHOD_NONE) select_init (flags);
364#endif 417#endif
365 418
366 if (ev_method) 419 if (ev_method)
367 { 420 {
368 evw_init (&sigev, sigcb); 421 evw_init (&sigev, sigcb);
369 siginit (); 422 siginit ();
423
424 evsignal_init (&childev, childcb, SIGCHLD);
425 evsignal_start (&childev);
426 }
370 } 427 }
371 428
372 return ev_method; 429 return ev_method;
373} 430}
374 431
375/*****************************************************************************/ 432/*****************************************************************************/
376 433
434void
377void ev_prefork (void) 435ev_prefork (void)
378{ 436{
379 /* nop */ 437 /* nop */
380} 438}
381 439
440void
382void ev_postfork_parent (void) 441ev_postfork_parent (void)
383{ 442{
384 /* nop */ 443 /* nop */
385} 444}
386 445
446void
387void ev_postfork_child (void) 447ev_postfork_child (void)
388{ 448{
389#if HAVE_EPOLL 449#if HAVE_EPOLL
390 if (ev_method == EVMETHOD_EPOLL) 450 if (ev_method == EVMETHOD_EPOLL)
391 epoll_postfork_child (); 451 epoll_postfork_child ();
392#endif 452#endif
425 485
426 fdchangecnt = 0; 486 fdchangecnt = 0;
427} 487}
428 488
429static void 489static void
430call_pending () 490call_pending (void)
431{ 491{
432 while (pendingcnt) 492 while (pendingcnt)
433 { 493 {
434 ANPENDING *p = pendings + --pendingcnt; 494 ANPENDING *p = pendings + --pendingcnt;
435 495
440 } 500 }
441 } 501 }
442} 502}
443 503
444static void 504static void
445timers_reify () 505timers_reify (void)
446{ 506{
447 while (timercnt && timers [0]->at <= now) 507 while (timercnt && timers [0]->at <= now)
448 { 508 {
449 struct ev_timer *w = timers [0]; 509 struct ev_timer *w = timers [0];
450 510
461 evtimer_stop (w); /* nonrepeating: stop timer */ 521 evtimer_stop (w); /* nonrepeating: stop timer */
462 } 522 }
463} 523}
464 524
465static void 525static void
466periodics_reify () 526periodics_reify (void)
467{ 527{
468 while (periodiccnt && periodics [0]->at <= ev_now) 528 while (periodiccnt && periodics [0]->at <= ev_now)
469 { 529 {
470 struct ev_periodic *w = periodics [0]; 530 struct ev_periodic *w = periodics [0];
471 531
507 } 567 }
508 } 568 }
509} 569}
510 570
511static void 571static void
512time_update () 572time_update (void)
513{ 573{
514 int i; 574 int i;
515 575
516 ev_now = ev_time (); 576 ev_now = ev_time ();
517 577
553void ev_loop (int flags) 613void ev_loop (int flags)
554{ 614{
555 double block; 615 double block;
556 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 616 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
557 617
558 if (checkcnt)
559 {
560 queue_events ((W *)checks, checkcnt, EV_CHECK);
561 call_pending ();
562 }
563
564 do 618 do
565 { 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
566 /* update fd-related kernel structures */ 627 /* update fd-related kernel structures */
567 fd_reify (); 628 fd_reify ();
568 629
569 /* calculate blocking time */ 630 /* calculate blocking time */
570 631
571 /* 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 */
572 ev_now = ev_time (); 634 ev_now = ev_time ();
573 635
574 if (flags & EVLOOP_NONBLOCK || idlecnt) 636 if (flags & EVLOOP_NONBLOCK || idlecnt)
575 block = 0.; 637 block = 0.;
576 else 638 else
596 658
597 /* update ev_now, do magic */ 659 /* update ev_now, do magic */
598 time_update (); 660 time_update ();
599 661
600 /* queue pending timers and reschedule them */ 662 /* queue pending timers and reschedule them */
663 timers_reify (); /* relative timers called last */
601 periodics_reify (); /* absolute timers first */ 664 periodics_reify (); /* absolute timers called first */
602 timers_reify (); /* relative timers second */
603 665
604 /* queue idle watchers unless io or timers are pending */ 666 /* queue idle watchers unless io or timers are pending */
605 if (!pendingcnt) 667 if (!pendingcnt)
606 queue_events ((W *)idles, idlecnt, EV_IDLE); 668 queue_events ((W *)idles, idlecnt, EV_IDLE);
607 669
608 /* queue check and possibly idle watchers */ 670 /* queue check watchers, to be executed first */
671 if (checkcnt)
609 queue_events ((W *)checks, checkcnt, EV_CHECK); 672 queue_events ((W *)checks, checkcnt, EV_CHECK);
610 673
611 call_pending (); 674 call_pending ();
612 } 675 }
613 while (!ev_loop_done); 676 while (!ev_loop_done);
614 677
677 wlist_add ((WL *)&anfds[fd].head, (WL)w); 740 wlist_add ((WL *)&anfds[fd].head, (WL)w);
678 741
679 ++fdchangecnt; 742 ++fdchangecnt;
680 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 743 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
681 fdchanges [fdchangecnt - 1] = fd; 744 fdchanges [fdchangecnt - 1] = fd;
745
746 if (w->fd == 9)
747 printf ("start %p:%x\n", w, w->events);//D
682} 748}
683 749
684void 750void
685evio_stop (struct ev_io *w) 751evio_stop (struct ev_io *w)
686{ 752{
753 if (w->fd == 9)
754 printf ("stop %p:%x\n", w, w->events);//D
687 ev_clear ((W)w); 755 ev_clear ((W)w);
688 if (!ev_is_active (w)) 756 if (!ev_is_active (w))
689 return; 757 return;
690 758
691 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 759 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
833 901
834 idles [w->active - 1] = idles [--idlecnt]; 902 idles [w->active - 1] = idles [--idlecnt];
835 ev_stop ((W)w); 903 ev_stop ((W)w);
836} 904}
837 905
906void evprepare_start (struct ev_prepare *w)
907{
908 if (ev_is_active (w))
909 return;
910
911 ev_start ((W)w, ++preparecnt);
912 array_needsize (prepares, preparemax, preparecnt, );
913 prepares [preparecnt - 1] = w;
914}
915
916void evprepare_stop (struct ev_prepare *w)
917{
918 ev_clear ((W)w);
919 if (ev_is_active (w))
920 return;
921
922 prepares [w->active - 1] = prepares [--preparecnt];
923 ev_stop ((W)w);
924}
925
838void evcheck_start (struct ev_check *w) 926void evcheck_start (struct ev_check *w)
839{ 927{
840 if (ev_is_active (w)) 928 if (ev_is_active (w))
841 return; 929 return;
842 930
850 ev_clear ((W)w); 938 ev_clear ((W)w);
851 if (ev_is_active (w)) 939 if (ev_is_active (w))
852 return; 940 return;
853 941
854 checks [w->active - 1] = checks [--checkcnt]; 942 checks [w->active - 1] = checks [--checkcnt];
943 ev_stop ((W)w);
944}
945
946void evchild_start (struct ev_child *w)
947{
948 if (ev_is_active (w))
949 return;
950
951 ev_start ((W)w, 1);
952 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
953}
954
955void evchild_stop (struct ev_child *w)
956{
957 ev_clear ((W)w);
958 if (ev_is_active (w))
959 return;
960
961 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
855 ev_stop ((W)w); 962 ev_stop ((W)w);
856} 963}
857 964
858/*****************************************************************************/ 965/*****************************************************************************/
859 966

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines