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

Comparing libev/ev.c (file contents):
Revision 1.20 by root, Wed Oct 31 18:28:00 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;
332static struct ev_check **checks; 344static struct ev_check **checks;
333static int checkmax, checkcnt; 345static int checkmax, checkcnt;
334 346
335/*****************************************************************************/ 347/*****************************************************************************/
336 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
337#if HAVE_EPOLL 373#if HAVE_EPOLL
338# include "ev_epoll.c" 374# include "ev_epoll.c"
339#endif 375#endif
340#if HAVE_SELECT 376#if HAVE_SELECT
341# include "ev_select.c" 377# include "ev_select.c"
342#endif 378#endif
343 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
344int ev_init (int flags) 392int ev_init (int flags)
345{ 393{
394 if (!ev_method)
395 {
346#if HAVE_MONOTONIC 396#if HAVE_MONOTONIC
347 { 397 {
348 struct timespec ts; 398 struct timespec ts;
349 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 399 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
350 have_monotonic = 1; 400 have_monotonic = 1;
351 } 401 }
352#endif 402#endif
353 403
354 ev_now = ev_time (); 404 ev_now = ev_time ();
355 now = get_clock (); 405 now = get_clock ();
356 diff = ev_now - now; 406 diff = ev_now - now;
357 407
358 if (pipe (sigpipe)) 408 if (pipe (sigpipe))
359 return 0; 409 return 0;
360 410
361 ev_method = EVMETHOD_NONE; 411 ev_method = EVMETHOD_NONE;
362#if HAVE_EPOLL 412#if HAVE_EPOLL
363 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 413 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
364#endif 414#endif
365#if HAVE_SELECT 415#if HAVE_SELECT
366 if (ev_method == EVMETHOD_NONE) select_init (flags); 416 if (ev_method == EVMETHOD_NONE) select_init (flags);
367#endif 417#endif
368 418
369 if (ev_method) 419 if (ev_method)
370 { 420 {
371 evw_init (&sigev, sigcb); 421 evw_init (&sigev, sigcb);
372 siginit (); 422 siginit ();
423
424 evsignal_init (&childev, childcb, SIGCHLD);
425 evsignal_start (&childev);
426 }
373 } 427 }
374 428
375 return ev_method; 429 return ev_method;
376} 430}
377 431
378/*****************************************************************************/ 432/*****************************************************************************/
379 433
434void
380void ev_prefork (void) 435ev_prefork (void)
381{ 436{
382 /* nop */ 437 /* nop */
383} 438}
384 439
440void
385void ev_postfork_parent (void) 441ev_postfork_parent (void)
386{ 442{
387 /* nop */ 443 /* nop */
388} 444}
389 445
446void
390void ev_postfork_child (void) 447ev_postfork_child (void)
391{ 448{
392#if HAVE_EPOLL 449#if HAVE_EPOLL
393 if (ev_method == EVMETHOD_EPOLL) 450 if (ev_method == EVMETHOD_EPOLL)
394 epoll_postfork_child (); 451 epoll_postfork_child ();
395#endif 452#endif
428 485
429 fdchangecnt = 0; 486 fdchangecnt = 0;
430} 487}
431 488
432static void 489static void
433call_pending () 490call_pending (void)
434{ 491{
435 while (pendingcnt) 492 while (pendingcnt)
436 { 493 {
437 ANPENDING *p = pendings + --pendingcnt; 494 ANPENDING *p = pendings + --pendingcnt;
438 495
443 } 500 }
444 } 501 }
445} 502}
446 503
447static void 504static void
448timers_reify () 505timers_reify (void)
449{ 506{
450 while (timercnt && timers [0]->at <= now) 507 while (timercnt && timers [0]->at <= now)
451 { 508 {
452 struct ev_timer *w = timers [0]; 509 struct ev_timer *w = timers [0];
453 510
464 evtimer_stop (w); /* nonrepeating: stop timer */ 521 evtimer_stop (w); /* nonrepeating: stop timer */
465 } 522 }
466} 523}
467 524
468static void 525static void
469periodics_reify () 526periodics_reify (void)
470{ 527{
471 while (periodiccnt && periodics [0]->at <= ev_now) 528 while (periodiccnt && periodics [0]->at <= ev_now)
472 { 529 {
473 struct ev_periodic *w = periodics [0]; 530 struct ev_periodic *w = periodics [0];
474 531
510 } 567 }
511 } 568 }
512} 569}
513 570
514static void 571static void
515time_update () 572time_update (void)
516{ 573{
517 int i; 574 int i;
518 575
519 ev_now = ev_time (); 576 ev_now = ev_time ();
520 577
559 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 616 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
560 617
561 do 618 do
562 { 619 {
563 /* queue check watchers (and execute them) */ 620 /* queue check watchers (and execute them) */
564 if (checkcnt) 621 if (preparecnt)
565 { 622 {
566 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 623 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
567 call_pending (); 624 call_pending ();
568 } 625 }
569 626
570 /* update fd-related kernel structures */ 627 /* update fd-related kernel structures */
571 fd_reify (); 628 fd_reify ();
572 629
573 /* calculate blocking time */ 630 /* calculate blocking time */
574 631
575 /* 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 */
576 ev_now = ev_time (); 634 ev_now = ev_time ();
577 635
578 if (flags & EVLOOP_NONBLOCK || idlecnt) 636 if (flags & EVLOOP_NONBLOCK || idlecnt)
579 block = 0.; 637 block = 0.;
580 else 638 else
682 wlist_add ((WL *)&anfds[fd].head, (WL)w); 740 wlist_add ((WL *)&anfds[fd].head, (WL)w);
683 741
684 ++fdchangecnt; 742 ++fdchangecnt;
685 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 743 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
686 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
687} 748}
688 749
689void 750void
690evio_stop (struct ev_io *w) 751evio_stop (struct ev_io *w)
691{ 752{
753 if (w->fd == 9)
754 printf ("stop %p:%x\n", w, w->events);//D
692 ev_clear ((W)w); 755 ev_clear ((W)w);
693 if (!ev_is_active (w)) 756 if (!ev_is_active (w))
694 return; 757 return;
695 758
696 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 759 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
875 ev_clear ((W)w); 938 ev_clear ((W)w);
876 if (ev_is_active (w)) 939 if (ev_is_active (w))
877 return; 940 return;
878 941
879 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);
880 ev_stop ((W)w); 962 ev_stop ((W)w);
881} 963}
882 964
883/*****************************************************************************/ 965/*****************************************************************************/
884 966

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines