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

Comparing libev/ev.c (file contents):
Revision 1.21 by root, Wed Oct 31 18:37:38 2007 UTC vs.
Revision 1.32 by root, Thu Nov 1 09:21:51 2007 UTC

24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 28 */
29#if EV_USE_CONFIG_H
30# include "config.h"
31#endif
29 32
30#include <math.h> 33#include <math.h>
31#include <stdlib.h> 34#include <stdlib.h>
32#include <unistd.h> 35#include <unistd.h>
33#include <fcntl.h> 36#include <fcntl.h>
36 39
37#include <stdio.h> 40#include <stdio.h>
38 41
39#include <assert.h> 42#include <assert.h>
40#include <errno.h> 43#include <errno.h>
44#include <sys/types.h>
45#include <sys/wait.h>
41#include <sys/time.h> 46#include <sys/time.h>
42#include <time.h> 47#include <time.h>
43 48
44#ifndef HAVE_MONOTONIC 49#ifndef EV_USE_MONOTONIC
45# ifdef CLOCK_MONOTONIC 50# ifdef CLOCK_MONOTONIC
46# define HAVE_MONOTONIC 1 51# define EV_USE_MONOTONIC 1
47# endif 52# endif
48#endif 53#endif
49 54
50#ifndef HAVE_SELECT 55#ifndef EV_USE_SELECT
51# define HAVE_SELECT 1 56# define EV_USE_SELECT 1
52#endif 57#endif
53 58
54#ifndef HAVE_EPOLL 59#ifndef EV_USE_EPOLL
55# define HAVE_EPOLL 0 60# define EV_USE_EPOLL 0
56#endif 61#endif
57 62
63#ifndef CLOCK_REALTIME
64# define EV_USE_REALTIME 0
65#endif
58#ifndef HAVE_REALTIME 66#ifndef EV_USE_REALTIME
59# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 67# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */
60#endif 68#endif
61 69
62#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 70#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
63#define MAX_BLOCKTIME 60. 71#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */
72#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
73#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
64 74
65#include "ev.h" 75#include "ev.h"
66 76
67typedef struct ev_watcher *W; 77typedef struct ev_watcher *W;
68typedef struct ev_watcher_list *WL; 78typedef struct ev_watcher_list *WL;
81/*****************************************************************************/ 91/*****************************************************************************/
82 92
83ev_tstamp 93ev_tstamp
84ev_time (void) 94ev_time (void)
85{ 95{
86#if HAVE_REALTIME 96#if EV_USE_REALTIME
87 struct timespec ts; 97 struct timespec ts;
88 clock_gettime (CLOCK_REALTIME, &ts); 98 clock_gettime (CLOCK_REALTIME, &ts);
89 return ts.tv_sec + ts.tv_nsec * 1e-9; 99 return ts.tv_sec + ts.tv_nsec * 1e-9;
90#else 100#else
91 struct timeval tv; 101 struct timeval tv;
95} 105}
96 106
97static ev_tstamp 107static ev_tstamp
98get_clock (void) 108get_clock (void)
99{ 109{
100#if HAVE_MONOTONIC 110#if EV_USE_MONOTONIC
101 if (have_monotonic) 111 if (have_monotonic)
102 { 112 {
103 struct timespec ts; 113 struct timespec ts;
104 clock_gettime (CLOCK_MONOTONIC, &ts); 114 clock_gettime (CLOCK_MONOTONIC, &ts);
105 return ts.tv_sec + ts.tv_nsec * 1e-9; 115 return ts.tv_sec + ts.tv_nsec * 1e-9;
107#endif 117#endif
108 118
109 return ev_time (); 119 return ev_time ();
110} 120}
111 121
122#define array_roundsize(base,n) ((n) | 4 & ~3)
123
112#define array_needsize(base,cur,cnt,init) \ 124#define array_needsize(base,cur,cnt,init) \
113 if ((cnt) > cur) \ 125 if ((cnt) > cur) \
114 { \ 126 { \
115 int newcnt = cur ? cur << 1 : 16; \ 127 int newcnt = cur; \
128 do \
129 { \
130 newcnt = array_roundsize (base, newcnt << 1); \
131 } \
132 while ((cnt) > newcnt); \
133 \
116 base = realloc (base, sizeof (*base) * (newcnt)); \ 134 base = realloc (base, sizeof (*base) * (newcnt)); \
117 init (base + cur, newcnt - cur); \ 135 init (base + cur, newcnt - cur); \
118 cur = newcnt; \ 136 cur = newcnt; \
119 } 137 }
120 138
121/*****************************************************************************/ 139/*****************************************************************************/
122 140
123typedef struct 141typedef struct
124{ 142{
125 struct ev_io *head; 143 struct ev_io *head;
126 unsigned char wev, rev; /* want, received event set */ 144 int events;
127} ANFD; 145} ANFD;
128 146
129static ANFD *anfds; 147static ANFD *anfds;
130static int anfdmax; 148static int anfdmax;
131 149
132static int *fdchanges;
133static int fdchangemax, fdchangecnt;
134
135static void 150static void
136anfds_init (ANFD *base, int count) 151anfds_init (ANFD *base, int count)
137{ 152{
138 while (count--) 153 while (count--)
139 { 154 {
140 base->head = 0; 155 base->head = 0;
141 base->wev = base->rev = EV_NONE; 156 base->events = EV_NONE;
142 ++base; 157 ++base;
143 } 158 }
144} 159}
145 160
146typedef struct 161typedef struct
153static int pendingmax, pendingcnt; 168static int pendingmax, pendingcnt;
154 169
155static void 170static void
156event (W w, int events) 171event (W w, int events)
157{ 172{
158 if (w->active) 173 if (w->pending)
174 {
175 pendings [w->pending - 1].events |= events;
176 return;
159 { 177 }
178
160 w->pending = ++pendingcnt; 179 w->pending = ++pendingcnt;
161 array_needsize (pendings, pendingmax, pendingcnt, ); 180 array_needsize (pendings, pendingmax, pendingcnt, );
162 pendings [pendingcnt - 1].w = w; 181 pendings [pendingcnt - 1].w = w;
163 pendings [pendingcnt - 1].events = events; 182 pendings [pendingcnt - 1].events = events;
164 } 183}
184
185static void
186queue_events (W *events, int eventcnt, int type)
187{
188 int i;
189
190 for (i = 0; i < eventcnt; ++i)
191 event (events [i], type);
165} 192}
166 193
167static void 194static void
168fd_event (int fd, int events) 195fd_event (int fd, int events)
169{ 196{
177 if (ev) 204 if (ev)
178 event ((W)w, ev); 205 event ((W)w, ev);
179 } 206 }
180} 207}
181 208
209/*****************************************************************************/
210
211static int *fdchanges;
212static int fdchangemax, fdchangecnt;
213
182static void 214static void
183queue_events (W *events, int eventcnt, int type) 215fd_reify (void)
184{ 216{
185 int i; 217 int i;
186 218
187 for (i = 0; i < eventcnt; ++i) 219 for (i = 0; i < fdchangecnt; ++i)
188 event (events [i], type); 220 {
221 int fd = fdchanges [i];
222 ANFD *anfd = anfds + fd;
223 struct ev_io *w;
224
225 int events = 0;
226
227 for (w = anfd->head; w; w = w->next)
228 events |= w->events;
229
230 anfd->events &= ~EV_REIFY;
231
232 if (anfd->events != events)
233 {
234 method_modify (fd, anfd->events, events);
235 anfd->events = events;
236 }
237 }
238
239 fdchangecnt = 0;
240}
241
242static void
243fd_change (int fd)
244{
245 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0)
246 return;
247
248 anfds [fd].events |= EV_REIFY;
249
250 ++fdchangecnt;
251 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
252 fdchanges [fdchangecnt - 1] = fd;
189} 253}
190 254
191/* called on EBADF to verify fds */ 255/* called on EBADF to verify fds */
192static void 256static void
193fd_recheck () 257fd_recheck (void)
194{ 258{
195 int fd; 259 int fd;
196 260
197 for (fd = 0; fd < anfdmax; ++fd) 261 for (fd = 0; fd < anfdmax; ++fd)
198 if (anfds [fd].wev) 262 if (anfds [fd].events)
199 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 263 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
200 while (anfds [fd].head) 264 while (anfds [fd].head)
265 {
201 evio_stop (anfds [fd].head); 266 ev_io_stop (anfds [fd].head);
267 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT);
268 }
202} 269}
203 270
204/*****************************************************************************/ 271/*****************************************************************************/
205 272
206static struct ev_timer **timers; 273static struct ev_timer **timers;
315 382
316 /* rather than sort out wether we really need nb, set it */ 383 /* rather than sort out wether we really need nb, set it */
317 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 384 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
318 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 385 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
319 386
320 evio_set (&sigev, sigpipe [0], EV_READ); 387 ev_io_set (&sigev, sigpipe [0], EV_READ);
321 evio_start (&sigev); 388 ev_io_start (&sigev);
322} 389}
323 390
324/*****************************************************************************/ 391/*****************************************************************************/
325 392
326static struct ev_idle **idles; 393static struct ev_idle **idles;
332static struct ev_check **checks; 399static struct ev_check **checks;
333static int checkmax, checkcnt; 400static int checkmax, checkcnt;
334 401
335/*****************************************************************************/ 402/*****************************************************************************/
336 403
404static struct ev_child *childs [PID_HASHSIZE];
405static struct ev_signal childev;
406
407#ifndef WCONTINUED
408# define WCONTINUED 0
409#endif
410
411static void
412childcb (struct ev_signal *sw, int revents)
413{
414 struct ev_child *w;
415 int pid, status;
416
417 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
418 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
419 if (w->pid == pid || w->pid == -1)
420 {
421 w->status = status;
422 event ((W)w, EV_CHILD);
423 }
424}
425
426/*****************************************************************************/
427
337#if HAVE_EPOLL 428#if EV_USE_EPOLL
338# include "ev_epoll.c" 429# include "ev_epoll.c"
339#endif 430#endif
340#if HAVE_SELECT 431#if EV_USE_SELECT
341# include "ev_select.c" 432# include "ev_select.c"
342#endif 433#endif
343 434
435int
436ev_version_major (void)
437{
438 return EV_VERSION_MAJOR;
439}
440
441int
442ev_version_minor (void)
443{
444 return EV_VERSION_MINOR;
445}
446
344int ev_init (int flags) 447int ev_init (int flags)
345{ 448{
346#if HAVE_MONOTONIC
347 {
348 struct timespec ts;
349 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
350 have_monotonic = 1;
351 }
352#endif
353
354 ev_now = ev_time ();
355 now = get_clock ();
356 diff = ev_now - now;
357
358 if (pipe (sigpipe))
359 return 0;
360
361 ev_method = EVMETHOD_NONE;
362#if HAVE_EPOLL
363 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
364#endif
365#if HAVE_SELECT
366 if (ev_method == EVMETHOD_NONE) select_init (flags);
367#endif
368
369 if (ev_method) 449 if (!ev_method)
450 {
451#if EV_USE_MONOTONIC
370 { 452 {
453 struct timespec ts;
454 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
455 have_monotonic = 1;
456 }
457#endif
458
459 ev_now = ev_time ();
460 now = get_clock ();
461 diff = ev_now - now;
462
463 if (pipe (sigpipe))
464 return 0;
465
466 ev_method = EVMETHOD_NONE;
467#if EV_USE_EPOLL
468 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
469#endif
470#if EV_USE_SELECT
471 if (ev_method == EVMETHOD_NONE) select_init (flags);
472#endif
473
474 if (ev_method)
475 {
371 evw_init (&sigev, sigcb); 476 ev_watcher_init (&sigev, sigcb);
372 siginit (); 477 siginit ();
478
479 ev_signal_init (&childev, childcb, SIGCHLD);
480 ev_signal_start (&childev);
481 }
373 } 482 }
374 483
375 return ev_method; 484 return ev_method;
376} 485}
377 486
378/*****************************************************************************/ 487/*****************************************************************************/
379 488
489void
380void ev_prefork (void) 490ev_prefork (void)
381{ 491{
382 /* nop */ 492 /* nop */
383} 493}
384 494
495void
385void ev_postfork_parent (void) 496ev_postfork_parent (void)
386{ 497{
387 /* nop */ 498 /* nop */
388} 499}
389 500
501void
390void ev_postfork_child (void) 502ev_postfork_child (void)
391{ 503{
392#if HAVE_EPOLL 504#if EV_USE_EPOLL
393 if (ev_method == EVMETHOD_EPOLL) 505 if (ev_method == EVMETHOD_EPOLL)
394 epoll_postfork_child (); 506 epoll_postfork_child ();
395#endif 507#endif
396 508
397 evio_stop (&sigev); 509 ev_io_stop (&sigev);
398 close (sigpipe [0]); 510 close (sigpipe [0]);
399 close (sigpipe [1]); 511 close (sigpipe [1]);
400 pipe (sigpipe); 512 pipe (sigpipe);
401 siginit (); 513 siginit ();
402} 514}
403 515
404/*****************************************************************************/ 516/*****************************************************************************/
405 517
406static void 518static 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 () 519call_pending (void)
434{ 520{
435 while (pendingcnt) 521 while (pendingcnt)
436 { 522 {
437 ANPENDING *p = pendings + --pendingcnt; 523 ANPENDING *p = pendings + --pendingcnt;
438 524
443 } 529 }
444 } 530 }
445} 531}
446 532
447static void 533static void
448timers_reify () 534timers_reify (void)
449{ 535{
450 while (timercnt && timers [0]->at <= now) 536 while (timercnt && timers [0]->at <= now)
451 { 537 {
452 struct ev_timer *w = timers [0]; 538 struct ev_timer *w = timers [0];
453
454 event ((W)w, EV_TIMEOUT);
455 539
456 /* first reschedule or stop timer */ 540 /* first reschedule or stop timer */
457 if (w->repeat) 541 if (w->repeat)
458 { 542 {
459 w->at = now + w->repeat; 543 w->at = now + w->repeat;
460 assert (("timer timeout in the past, negative repeat?", w->at > now)); 544 assert (("timer timeout in the past, negative repeat?", w->at > now));
461 downheap ((WT *)timers, timercnt, 0); 545 downheap ((WT *)timers, timercnt, 0);
462 } 546 }
463 else 547 else
464 evtimer_stop (w); /* nonrepeating: stop timer */ 548 ev_timer_stop (w); /* nonrepeating: stop timer */
465 }
466}
467 549
550 event ((W)w, EV_TIMEOUT);
551 }
552}
553
468static void 554static void
469periodics_reify () 555periodics_reify (void)
470{ 556{
471 while (periodiccnt && periodics [0]->at <= ev_now) 557 while (periodiccnt && periodics [0]->at <= ev_now)
472 { 558 {
473 struct ev_periodic *w = periodics [0]; 559 struct ev_periodic *w = periodics [0];
474 560
478 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 564 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
479 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 565 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
480 downheap ((WT *)periodics, periodiccnt, 0); 566 downheap ((WT *)periodics, periodiccnt, 0);
481 } 567 }
482 else 568 else
483 evperiodic_stop (w); /* nonrepeating: stop timer */ 569 ev_periodic_stop (w); /* nonrepeating: stop timer */
484 570
485 event ((W)w, EV_TIMEOUT); 571 event ((W)w, EV_TIMEOUT);
486 } 572 }
487} 573}
488 574
500 { 586 {
501 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; 587 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
502 588
503 if (fabs (diff) >= 1e-4) 589 if (fabs (diff) >= 1e-4)
504 { 590 {
505 evperiodic_stop (w); 591 ev_periodic_stop (w);
506 evperiodic_start (w); 592 ev_periodic_start (w);
507 593
508 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 594 i = 0; /* restart loop, inefficient, but time jumps should be rare */
509 } 595 }
510 } 596 }
511 } 597 }
512} 598}
513 599
514static void 600static void
515time_update () 601time_update (void)
516{ 602{
517 int i; 603 int i;
518 604
519 ev_now = ev_time (); 605 ev_now = ev_time ();
520 606
554int ev_loop_done; 640int ev_loop_done;
555 641
556void ev_loop (int flags) 642void ev_loop (int flags)
557{ 643{
558 double block; 644 double block;
559 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 645 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
560 646
561 do 647 do
562 { 648 {
563 /* queue check watchers (and execute them) */ 649 /* queue check watchers (and execute them) */
564 if (preparecnt) 650 if (preparecnt)
669} 755}
670 756
671/*****************************************************************************/ 757/*****************************************************************************/
672 758
673void 759void
674evio_start (struct ev_io *w) 760ev_io_start (struct ev_io *w)
675{ 761{
676 if (ev_is_active (w)) 762 if (ev_is_active (w))
677 return; 763 return;
678 764
679 int fd = w->fd; 765 int fd = w->fd;
680 766
681 ev_start ((W)w, 1); 767 ev_start ((W)w, 1);
682 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 768 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
683 wlist_add ((WL *)&anfds[fd].head, (WL)w); 769 wlist_add ((WL *)&anfds[fd].head, (WL)w);
684 770
685 ++fdchangecnt; 771 fd_change (fd);
686 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
687 fdchanges [fdchangecnt - 1] = fd;
688} 772}
689 773
690void 774void
691evio_stop (struct ev_io *w) 775ev_io_stop (struct ev_io *w)
692{ 776{
693 ev_clear ((W)w); 777 ev_clear ((W)w);
694 if (!ev_is_active (w)) 778 if (!ev_is_active (w))
695 return; 779 return;
696 780
697 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 781 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
698 ev_stop ((W)w); 782 ev_stop ((W)w);
699 783
700 ++fdchangecnt; 784 fd_change (w->fd);
701 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
702 fdchanges [fdchangecnt - 1] = w->fd;
703} 785}
704 786
705void 787void
706evtimer_start (struct ev_timer *w) 788ev_timer_start (struct ev_timer *w)
707{ 789{
708 if (ev_is_active (w)) 790 if (ev_is_active (w))
709 return; 791 return;
710 792
711 w->at += now; 793 w->at += now;
717 timers [timercnt - 1] = w; 799 timers [timercnt - 1] = w;
718 upheap ((WT *)timers, timercnt - 1); 800 upheap ((WT *)timers, timercnt - 1);
719} 801}
720 802
721void 803void
722evtimer_stop (struct ev_timer *w) 804ev_timer_stop (struct ev_timer *w)
723{ 805{
724 ev_clear ((W)w); 806 ev_clear ((W)w);
725 if (!ev_is_active (w)) 807 if (!ev_is_active (w))
726 return; 808 return;
727 809
735 817
736 ev_stop ((W)w); 818 ev_stop ((W)w);
737} 819}
738 820
739void 821void
740evtimer_again (struct ev_timer *w) 822ev_timer_again (struct ev_timer *w)
741{ 823{
742 if (ev_is_active (w)) 824 if (ev_is_active (w))
743 { 825 {
744 if (w->repeat) 826 if (w->repeat)
745 { 827 {
746 w->at = now + w->repeat; 828 w->at = now + w->repeat;
747 downheap ((WT *)timers, timercnt, w->active - 1); 829 downheap ((WT *)timers, timercnt, w->active - 1);
748 } 830 }
749 else 831 else
750 evtimer_stop (w); 832 ev_timer_stop (w);
751 } 833 }
752 else if (w->repeat) 834 else if (w->repeat)
753 evtimer_start (w); 835 ev_timer_start (w);
754} 836}
755 837
756void 838void
757evperiodic_start (struct ev_periodic *w) 839ev_periodic_start (struct ev_periodic *w)
758{ 840{
759 if (ev_is_active (w)) 841 if (ev_is_active (w))
760 return; 842 return;
761 843
762 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 844 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
770 periodics [periodiccnt - 1] = w; 852 periodics [periodiccnt - 1] = w;
771 upheap ((WT *)periodics, periodiccnt - 1); 853 upheap ((WT *)periodics, periodiccnt - 1);
772} 854}
773 855
774void 856void
775evperiodic_stop (struct ev_periodic *w) 857ev_periodic_stop (struct ev_periodic *w)
776{ 858{
777 ev_clear ((W)w); 859 ev_clear ((W)w);
778 if (!ev_is_active (w)) 860 if (!ev_is_active (w))
779 return; 861 return;
780 862
786 868
787 ev_stop ((W)w); 869 ev_stop ((W)w);
788} 870}
789 871
790void 872void
791evsignal_start (struct ev_signal *w) 873ev_signal_start (struct ev_signal *w)
792{ 874{
793 if (ev_is_active (w)) 875 if (ev_is_active (w))
794 return; 876 return;
795 877
796 ev_start ((W)w, 1); 878 ev_start ((W)w, 1);
806 sigaction (w->signum, &sa, 0); 888 sigaction (w->signum, &sa, 0);
807 } 889 }
808} 890}
809 891
810void 892void
811evsignal_stop (struct ev_signal *w) 893ev_signal_stop (struct ev_signal *w)
812{ 894{
813 ev_clear ((W)w); 895 ev_clear ((W)w);
814 if (!ev_is_active (w)) 896 if (!ev_is_active (w))
815 return; 897 return;
816 898
819 901
820 if (!signals [w->signum - 1].head) 902 if (!signals [w->signum - 1].head)
821 signal (w->signum, SIG_DFL); 903 signal (w->signum, SIG_DFL);
822} 904}
823 905
906void
824void evidle_start (struct ev_idle *w) 907ev_idle_start (struct ev_idle *w)
825{ 908{
826 if (ev_is_active (w)) 909 if (ev_is_active (w))
827 return; 910 return;
828 911
829 ev_start ((W)w, ++idlecnt); 912 ev_start ((W)w, ++idlecnt);
830 array_needsize (idles, idlemax, idlecnt, ); 913 array_needsize (idles, idlemax, idlecnt, );
831 idles [idlecnt - 1] = w; 914 idles [idlecnt - 1] = w;
832} 915}
833 916
917void
834void evidle_stop (struct ev_idle *w) 918ev_idle_stop (struct ev_idle *w)
835{ 919{
836 ev_clear ((W)w); 920 ev_clear ((W)w);
837 if (ev_is_active (w)) 921 if (ev_is_active (w))
838 return; 922 return;
839 923
840 idles [w->active - 1] = idles [--idlecnt]; 924 idles [w->active - 1] = idles [--idlecnt];
841 ev_stop ((W)w); 925 ev_stop ((W)w);
842} 926}
843 927
928void
844void evprepare_start (struct ev_prepare *w) 929ev_prepare_start (struct ev_prepare *w)
845{ 930{
846 if (ev_is_active (w)) 931 if (ev_is_active (w))
847 return; 932 return;
848 933
849 ev_start ((W)w, ++preparecnt); 934 ev_start ((W)w, ++preparecnt);
850 array_needsize (prepares, preparemax, preparecnt, ); 935 array_needsize (prepares, preparemax, preparecnt, );
851 prepares [preparecnt - 1] = w; 936 prepares [preparecnt - 1] = w;
852} 937}
853 938
939void
854void evprepare_stop (struct ev_prepare *w) 940ev_prepare_stop (struct ev_prepare *w)
855{ 941{
856 ev_clear ((W)w); 942 ev_clear ((W)w);
857 if (ev_is_active (w)) 943 if (ev_is_active (w))
858 return; 944 return;
859 945
860 prepares [w->active - 1] = prepares [--preparecnt]; 946 prepares [w->active - 1] = prepares [--preparecnt];
861 ev_stop ((W)w); 947 ev_stop ((W)w);
862} 948}
863 949
950void
864void evcheck_start (struct ev_check *w) 951ev_check_start (struct ev_check *w)
865{ 952{
866 if (ev_is_active (w)) 953 if (ev_is_active (w))
867 return; 954 return;
868 955
869 ev_start ((W)w, ++checkcnt); 956 ev_start ((W)w, ++checkcnt);
870 array_needsize (checks, checkmax, checkcnt, ); 957 array_needsize (checks, checkmax, checkcnt, );
871 checks [checkcnt - 1] = w; 958 checks [checkcnt - 1] = w;
872} 959}
873 960
961void
874void evcheck_stop (struct ev_check *w) 962ev_check_stop (struct ev_check *w)
875{ 963{
876 ev_clear ((W)w); 964 ev_clear ((W)w);
877 if (ev_is_active (w)) 965 if (ev_is_active (w))
878 return; 966 return;
879 967
880 checks [w->active - 1] = checks [--checkcnt]; 968 checks [w->active - 1] = checks [--checkcnt];
969 ev_stop ((W)w);
970}
971
972void
973ev_child_start (struct ev_child *w)
974{
975 if (ev_is_active (w))
976 return;
977
978 ev_start ((W)w, 1);
979 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
980}
981
982void
983ev_child_stop (struct ev_child *w)
984{
985 ev_clear ((W)w);
986 if (ev_is_active (w))
987 return;
988
989 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
881 ev_stop ((W)w); 990 ev_stop ((W)w);
882} 991}
883 992
884/*****************************************************************************/ 993/*****************************************************************************/
885 994
895once_cb (struct ev_once *once, int revents) 1004once_cb (struct ev_once *once, int revents)
896{ 1005{
897 void (*cb)(int revents, void *arg) = once->cb; 1006 void (*cb)(int revents, void *arg) = once->cb;
898 void *arg = once->arg; 1007 void *arg = once->arg;
899 1008
900 evio_stop (&once->io); 1009 ev_io_stop (&once->io);
901 evtimer_stop (&once->to); 1010 ev_timer_stop (&once->to);
902 free (once); 1011 free (once);
903 1012
904 cb (revents, arg); 1013 cb (revents, arg);
905} 1014}
906 1015
920ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1029ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
921{ 1030{
922 struct ev_once *once = malloc (sizeof (struct ev_once)); 1031 struct ev_once *once = malloc (sizeof (struct ev_once));
923 1032
924 if (!once) 1033 if (!once)
925 cb (EV_ERROR, arg); 1034 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
926 else 1035 else
927 { 1036 {
928 once->cb = cb; 1037 once->cb = cb;
929 once->arg = arg; 1038 once->arg = arg;
930 1039
931 evw_init (&once->io, once_cb_io); 1040 ev_watcher_init (&once->io, once_cb_io);
932
933 if (fd >= 0) 1041 if (fd >= 0)
934 { 1042 {
935 evio_set (&once->io, fd, events); 1043 ev_io_set (&once->io, fd, events);
936 evio_start (&once->io); 1044 ev_io_start (&once->io);
937 } 1045 }
938 1046
939 evw_init (&once->to, once_cb_to); 1047 ev_watcher_init (&once->to, once_cb_to);
940
941 if (timeout >= 0.) 1048 if (timeout >= 0.)
942 { 1049 {
943 evtimer_set (&once->to, timeout, 0.); 1050 ev_timer_set (&once->to, timeout, 0.);
944 evtimer_start (&once->to); 1051 ev_timer_start (&once->to);
945 } 1052 }
946 } 1053 }
947} 1054}
948 1055
949/*****************************************************************************/ 1056/*****************************************************************************/
960 1067
961static void 1068static void
962ocb (struct ev_timer *w, int revents) 1069ocb (struct ev_timer *w, int revents)
963{ 1070{
964 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 1071 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
965 evtimer_stop (w); 1072 ev_timer_stop (w);
966 evtimer_start (w); 1073 ev_timer_start (w);
967} 1074}
968 1075
969static void 1076static void
970scb (struct ev_signal *w, int revents) 1077scb (struct ev_signal *w, int revents)
971{ 1078{
972 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1079 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
973 evio_stop (&wio); 1080 ev_io_stop (&wio);
974 evio_start (&wio); 1081 ev_io_start (&wio);
975} 1082}
976 1083
977static void 1084static void
978gcb (struct ev_signal *w, int revents) 1085gcb (struct ev_signal *w, int revents)
979{ 1086{
983 1090
984int main (void) 1091int main (void)
985{ 1092{
986 ev_init (0); 1093 ev_init (0);
987 1094
988 evio_init (&wio, sin_cb, 0, EV_READ); 1095 ev_io_init (&wio, sin_cb, 0, EV_READ);
989 evio_start (&wio); 1096 ev_io_start (&wio);
990 1097
991 struct ev_timer t[10000]; 1098 struct ev_timer t[10000];
992 1099
993#if 0 1100#if 0
994 int i; 1101 int i;
995 for (i = 0; i < 10000; ++i) 1102 for (i = 0; i < 10000; ++i)
996 { 1103 {
997 struct ev_timer *w = t + i; 1104 struct ev_timer *w = t + i;
998 evw_init (w, ocb, i); 1105 ev_watcher_init (w, ocb, i);
999 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1106 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1000 evtimer_start (w); 1107 ev_timer_start (w);
1001 if (drand48 () < 0.5) 1108 if (drand48 () < 0.5)
1002 evtimer_stop (w); 1109 ev_timer_stop (w);
1003 } 1110 }
1004#endif 1111#endif
1005 1112
1006 struct ev_timer t1; 1113 struct ev_timer t1;
1007 evtimer_init (&t1, ocb, 5, 10); 1114 ev_timer_init (&t1, ocb, 5, 10);
1008 evtimer_start (&t1); 1115 ev_timer_start (&t1);
1009 1116
1010 struct ev_signal sig; 1117 struct ev_signal sig;
1011 evsignal_init (&sig, scb, SIGQUIT); 1118 ev_signal_init (&sig, scb, SIGQUIT);
1012 evsignal_start (&sig); 1119 ev_signal_start (&sig);
1013 1120
1014 struct ev_check cw; 1121 struct ev_check cw;
1015 evcheck_init (&cw, gcb); 1122 ev_check_init (&cw, gcb);
1016 evcheck_start (&cw); 1123 ev_check_start (&cw);
1017 1124
1018 struct ev_idle iw; 1125 struct ev_idle iw;
1019 evidle_init (&iw, gcb); 1126 ev_idle_init (&iw, gcb);
1020 evidle_start (&iw); 1127 ev_idle_start (&iw);
1021 1128
1022 ev_loop (0); 1129 ev_loop (0);
1023 1130
1024 return 0; 1131 return 0;
1025} 1132}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines