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.27 by root, Wed Oct 31 22:16:36 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
121/*****************************************************************************/ 130/*****************************************************************************/
122 131
123typedef struct 132typedef struct
124{ 133{
125 struct ev_io *head; 134 struct ev_io *head;
126 unsigned char wev, rev; /* want, received event set */ 135 int events;
127} ANFD; 136} ANFD;
128 137
129static ANFD *anfds; 138static ANFD *anfds;
130static int anfdmax; 139static int anfdmax;
131 140
132static int *fdchanges;
133static int fdchangemax, fdchangecnt;
134
135static void 141static void
136anfds_init (ANFD *base, int count) 142anfds_init (ANFD *base, int count)
137{ 143{
138 while (count--) 144 while (count--)
139 { 145 {
140 base->head = 0; 146 base->head = 0;
141 base->wev = base->rev = EV_NONE; 147 base->events = EV_NONE;
142 ++base; 148 ++base;
143 } 149 }
144} 150}
145 151
146typedef struct 152typedef struct
163 pendings [pendingcnt - 1].events = events; 169 pendings [pendingcnt - 1].events = events;
164 } 170 }
165} 171}
166 172
167static void 173static void
174queue_events (W *events, int eventcnt, int type)
175{
176 int i;
177
178 for (i = 0; i < eventcnt; ++i)
179 event (events [i], type);
180}
181
182static void
168fd_event (int fd, int events) 183fd_event (int fd, int events)
169{ 184{
170 ANFD *anfd = anfds + fd; 185 ANFD *anfd = anfds + fd;
171 struct ev_io *w; 186 struct ev_io *w;
172 187
177 if (ev) 192 if (ev)
178 event ((W)w, ev); 193 event ((W)w, ev);
179 } 194 }
180} 195}
181 196
197/*****************************************************************************/
198
199static int *fdchanges;
200static int fdchangemax, fdchangecnt;
201
182static void 202static void
183queue_events (W *events, int eventcnt, int type) 203fd_reify (void)
184{ 204{
185 int i; 205 int i;
186 206
187 for (i = 0; i < eventcnt; ++i) 207 for (i = 0; i < fdchangecnt; ++i)
188 event (events [i], type); 208 {
209 int fd = fdchanges [i];
210 ANFD *anfd = anfds + fd;
211 struct ev_io *w;
212
213 int events = 0;
214
215 for (w = anfd->head; w; w = w->next)
216 events |= w->events;
217
218 anfd->events &= ~EV_REIFY;
219
220 if (anfd->events != events)
221 {
222 method_modify (fd, anfd->events, events);
223 anfd->events = events;
224 }
225 }
226
227 fdchangecnt = 0;
228}
229
230static void
231fd_change (int fd)
232{
233 if (anfds [fd].events & EV_REIFY)
234 return;
235
236 anfds [fd].events |= EV_REIFY;
237
238 ++fdchangecnt;
239 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
240 fdchanges [fdchangecnt - 1] = fd;
189} 241}
190 242
191/* called on EBADF to verify fds */ 243/* called on EBADF to verify fds */
192static void 244static void
193fd_recheck () 245fd_recheck (void)
194{ 246{
195 int fd; 247 int fd;
196 248
197 for (fd = 0; fd < anfdmax; ++fd) 249 for (fd = 0; fd < anfdmax; ++fd)
198 if (anfds [fd].wev) 250 if (anfds [fd].events)
199 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 251 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
200 while (anfds [fd].head) 252 while (anfds [fd].head)
253 {
254 event ((W)anfds [fd].head, EV_ERROR);
201 evio_stop (anfds [fd].head); 255 evio_stop (anfds [fd].head);
256 }
202} 257}
203 258
204/*****************************************************************************/ 259/*****************************************************************************/
205 260
206static struct ev_timer **timers; 261static struct ev_timer **timers;
332static struct ev_check **checks; 387static struct ev_check **checks;
333static int checkmax, checkcnt; 388static int checkmax, checkcnt;
334 389
335/*****************************************************************************/ 390/*****************************************************************************/
336 391
392static struct ev_child *childs [PID_HASHSIZE];
393static struct ev_signal childev;
394
395#ifndef WCONTINUED
396# define WCONTINUED 0
397#endif
398
399static void
400childcb (struct ev_signal *sw, int revents)
401{
402 struct ev_child *w;
403 int pid, status;
404
405 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
406 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
407 if (w->pid == pid || w->pid == -1)
408 {
409 w->status = status;
410 event ((W)w, EV_CHILD);
411 }
412}
413
414/*****************************************************************************/
415
337#if HAVE_EPOLL 416#if HAVE_EPOLL
338# include "ev_epoll.c" 417# include "ev_epoll.c"
339#endif 418#endif
340#if HAVE_SELECT 419#if HAVE_SELECT
341# include "ev_select.c" 420# include "ev_select.c"
342#endif 421#endif
343 422
423int
424ev_version_major (void)
425{
426 return EV_VERSION_MAJOR;
427}
428
429int
430ev_version_minor (void)
431{
432 return EV_VERSION_MINOR;
433}
434
344int ev_init (int flags) 435int ev_init (int flags)
345{ 436{
437 if (!ev_method)
438 {
346#if HAVE_MONOTONIC 439#if HAVE_MONOTONIC
347 { 440 {
348 struct timespec ts; 441 struct timespec ts;
349 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 442 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
350 have_monotonic = 1; 443 have_monotonic = 1;
351 } 444 }
352#endif 445#endif
353 446
354 ev_now = ev_time (); 447 ev_now = ev_time ();
355 now = get_clock (); 448 now = get_clock ();
356 diff = ev_now - now; 449 diff = ev_now - now;
357 450
358 if (pipe (sigpipe)) 451 if (pipe (sigpipe))
359 return 0; 452 return 0;
360 453
361 ev_method = EVMETHOD_NONE; 454 ev_method = EVMETHOD_NONE;
362#if HAVE_EPOLL 455#if HAVE_EPOLL
363 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 456 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
364#endif 457#endif
365#if HAVE_SELECT 458#if HAVE_SELECT
366 if (ev_method == EVMETHOD_NONE) select_init (flags); 459 if (ev_method == EVMETHOD_NONE) select_init (flags);
367#endif 460#endif
368 461
369 if (ev_method) 462 if (ev_method)
370 { 463 {
371 evw_init (&sigev, sigcb); 464 evw_init (&sigev, sigcb);
372 siginit (); 465 siginit ();
466
467 evsignal_init (&childev, childcb, SIGCHLD);
468 evsignal_start (&childev);
469 }
373 } 470 }
374 471
375 return ev_method; 472 return ev_method;
376} 473}
377 474
378/*****************************************************************************/ 475/*****************************************************************************/
379 476
477void
380void ev_prefork (void) 478ev_prefork (void)
381{ 479{
382 /* nop */ 480 /* nop */
383} 481}
384 482
483void
385void ev_postfork_parent (void) 484ev_postfork_parent (void)
386{ 485{
387 /* nop */ 486 /* nop */
388} 487}
389 488
489void
390void ev_postfork_child (void) 490ev_postfork_child (void)
391{ 491{
392#if HAVE_EPOLL 492#if HAVE_EPOLL
393 if (ev_method == EVMETHOD_EPOLL) 493 if (ev_method == EVMETHOD_EPOLL)
394 epoll_postfork_child (); 494 epoll_postfork_child ();
395#endif 495#endif
402} 502}
403 503
404/*****************************************************************************/ 504/*****************************************************************************/
405 505
406static void 506static void
407fd_reify (void)
408{
409 int i;
410
411 for (i = 0; i < fdchangecnt; ++i)
412 {
413 int fd = fdchanges [i];
414 ANFD *anfd = anfds + fd;
415 struct ev_io *w;
416
417 int wev = 0;
418
419 for (w = anfd->head; w; w = w->next)
420 wev |= w->events;
421
422 if (anfd->wev != wev)
423 {
424 method_modify (fd, anfd->wev, wev);
425 anfd->wev = wev;
426 }
427 }
428
429 fdchangecnt = 0;
430}
431
432static void
433call_pending () 507call_pending (void)
434{ 508{
435 while (pendingcnt) 509 while (pendingcnt)
436 { 510 {
437 ANPENDING *p = pendings + --pendingcnt; 511 ANPENDING *p = pendings + --pendingcnt;
438 512
443 } 517 }
444 } 518 }
445} 519}
446 520
447static void 521static void
448timers_reify () 522timers_reify (void)
449{ 523{
450 while (timercnt && timers [0]->at <= now) 524 while (timercnt && timers [0]->at <= now)
451 { 525 {
452 struct ev_timer *w = timers [0]; 526 struct ev_timer *w = timers [0];
453 527
464 evtimer_stop (w); /* nonrepeating: stop timer */ 538 evtimer_stop (w); /* nonrepeating: stop timer */
465 } 539 }
466} 540}
467 541
468static void 542static void
469periodics_reify () 543periodics_reify (void)
470{ 544{
471 while (periodiccnt && periodics [0]->at <= ev_now) 545 while (periodiccnt && periodics [0]->at <= ev_now)
472 { 546 {
473 struct ev_periodic *w = periodics [0]; 547 struct ev_periodic *w = periodics [0];
474 548
510 } 584 }
511 } 585 }
512} 586}
513 587
514static void 588static void
515time_update () 589time_update (void)
516{ 590{
517 int i; 591 int i;
518 592
519 ev_now = ev_time (); 593 ev_now = ev_time ();
520 594
554int ev_loop_done; 628int ev_loop_done;
555 629
556void ev_loop (int flags) 630void ev_loop (int flags)
557{ 631{
558 double block; 632 double block;
559 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 633 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
560 634
561 do 635 do
562 { 636 {
563 /* queue check watchers (and execute them) */ 637 /* queue check watchers (and execute them) */
564 if (checkcnt) 638 if (preparecnt)
565 { 639 {
566 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 640 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
567 call_pending (); 641 call_pending ();
568 } 642 }
569 643
570 /* update fd-related kernel structures */ 644 /* update fd-related kernel structures */
571 fd_reify (); 645 fd_reify ();
572 646
573 /* calculate blocking time */ 647 /* calculate blocking time */
574 648
575 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ 649 /* we only need this for !monotonic clockor timers, but as we basically
650 always have timers, we just calculate it always */
576 ev_now = ev_time (); 651 ev_now = ev_time ();
577 652
578 if (flags & EVLOOP_NONBLOCK || idlecnt) 653 if (flags & EVLOOP_NONBLOCK || idlecnt)
579 block = 0.; 654 block = 0.;
580 else 655 else
679 754
680 ev_start ((W)w, 1); 755 ev_start ((W)w, 1);
681 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 756 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
682 wlist_add ((WL *)&anfds[fd].head, (WL)w); 757 wlist_add ((WL *)&anfds[fd].head, (WL)w);
683 758
684 ++fdchangecnt; 759 fd_change (fd);
685 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
686 fdchanges [fdchangecnt - 1] = fd;
687} 760}
688 761
689void 762void
690evio_stop (struct ev_io *w) 763evio_stop (struct ev_io *w)
691{ 764{
694 return; 767 return;
695 768
696 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 769 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
697 ev_stop ((W)w); 770 ev_stop ((W)w);
698 771
699 ++fdchangecnt; 772 fd_change (w->fd);
700 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
701 fdchanges [fdchangecnt - 1] = w->fd;
702} 773}
703 774
704void 775void
705evtimer_start (struct ev_timer *w) 776evtimer_start (struct ev_timer *w)
706{ 777{
875 ev_clear ((W)w); 946 ev_clear ((W)w);
876 if (ev_is_active (w)) 947 if (ev_is_active (w))
877 return; 948 return;
878 949
879 checks [w->active - 1] = checks [--checkcnt]; 950 checks [w->active - 1] = checks [--checkcnt];
951 ev_stop ((W)w);
952}
953
954void evchild_start (struct ev_child *w)
955{
956 if (ev_is_active (w))
957 return;
958
959 ev_start ((W)w, 1);
960 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
961}
962
963void evchild_stop (struct ev_child *w)
964{
965 ev_clear ((W)w);
966 if (ev_is_active (w))
967 return;
968
969 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
880 ev_stop ((W)w); 970 ev_stop ((W)w);
881} 971}
882 972
883/*****************************************************************************/ 973/*****************************************************************************/
884 974

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines