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.31 by root, Thu Nov 1 09:05:33 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)
159 {
160 w->pending = ++pendingcnt; 173 w->pending = ++pendingcnt;
161 array_needsize (pendings, pendingmax, pendingcnt, ); 174 array_needsize (pendings, pendingmax, pendingcnt, );
162 pendings [pendingcnt - 1].w = w; 175 pendings [pendingcnt - 1].w = w;
163 pendings [pendingcnt - 1].events = events; 176 pendings [pendingcnt - 1].events = events;
164 } 177}
178
179static void
180queue_events (W *events, int eventcnt, int type)
181{
182 int i;
183
184 for (i = 0; i < eventcnt; ++i)
185 event (events [i], type);
165} 186}
166 187
167static void 188static void
168fd_event (int fd, int events) 189fd_event (int fd, int events)
169{ 190{
177 if (ev) 198 if (ev)
178 event ((W)w, ev); 199 event ((W)w, ev);
179 } 200 }
180} 201}
181 202
203/*****************************************************************************/
204
205static int *fdchanges;
206static int fdchangemax, fdchangecnt;
207
182static void 208static void
183queue_events (W *events, int eventcnt, int type) 209fd_reify (void)
184{ 210{
185 int i; 211 int i;
186 212
187 for (i = 0; i < eventcnt; ++i) 213 for (i = 0; i < fdchangecnt; ++i)
188 event (events [i], type); 214 {
215 int fd = fdchanges [i];
216 ANFD *anfd = anfds + fd;
217 struct ev_io *w;
218
219 int events = 0;
220
221 for (w = anfd->head; w; w = w->next)
222 events |= w->events;
223
224 anfd->events &= ~EV_REIFY;
225
226 if (anfd->events != events)
227 {
228 method_modify (fd, anfd->events, events);
229 anfd->events = events;
230 }
231 }
232
233 fdchangecnt = 0;
234}
235
236static void
237fd_change (int fd)
238{
239 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0)
240 return;
241
242 anfds [fd].events |= EV_REIFY;
243
244 ++fdchangecnt;
245 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
246 fdchanges [fdchangecnt - 1] = fd;
189} 247}
190 248
191/* called on EBADF to verify fds */ 249/* called on EBADF to verify fds */
192static void 250static void
193fd_recheck () 251fd_recheck (void)
194{ 252{
195 int fd; 253 int fd;
196 254
197 for (fd = 0; fd < anfdmax; ++fd) 255 for (fd = 0; fd < anfdmax; ++fd)
198 if (anfds [fd].wev) 256 if (anfds [fd].events)
199 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 257 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
200 while (anfds [fd].head) 258 while (anfds [fd].head)
259 {
260 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT);
201 evio_stop (anfds [fd].head); 261 ev_io_stop (anfds [fd].head);
262 }
202} 263}
203 264
204/*****************************************************************************/ 265/*****************************************************************************/
205 266
206static struct ev_timer **timers; 267static struct ev_timer **timers;
315 376
316 /* rather than sort out wether we really need nb, set it */ 377 /* rather than sort out wether we really need nb, set it */
317 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 378 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
318 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 379 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
319 380
320 evio_set (&sigev, sigpipe [0], EV_READ); 381 ev_io_set (&sigev, sigpipe [0], EV_READ);
321 evio_start (&sigev); 382 ev_io_start (&sigev);
322} 383}
323 384
324/*****************************************************************************/ 385/*****************************************************************************/
325 386
326static struct ev_idle **idles; 387static struct ev_idle **idles;
332static struct ev_check **checks; 393static struct ev_check **checks;
333static int checkmax, checkcnt; 394static int checkmax, checkcnt;
334 395
335/*****************************************************************************/ 396/*****************************************************************************/
336 397
398static struct ev_child *childs [PID_HASHSIZE];
399static struct ev_signal childev;
400
401#ifndef WCONTINUED
402# define WCONTINUED 0
403#endif
404
405static void
406childcb (struct ev_signal *sw, int revents)
407{
408 struct ev_child *w;
409 int pid, status;
410
411 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
412 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
413 if (w->pid == pid || w->pid == -1)
414 {
415 w->status = status;
416 event ((W)w, EV_CHILD);
417 }
418}
419
420/*****************************************************************************/
421
337#if HAVE_EPOLL 422#if EV_USE_EPOLL
338# include "ev_epoll.c" 423# include "ev_epoll.c"
339#endif 424#endif
340#if HAVE_SELECT 425#if EV_USE_SELECT
341# include "ev_select.c" 426# include "ev_select.c"
342#endif 427#endif
343 428
429int
430ev_version_major (void)
431{
432 return EV_VERSION_MAJOR;
433}
434
435int
436ev_version_minor (void)
437{
438 return EV_VERSION_MINOR;
439}
440
344int ev_init (int flags) 441int ev_init (int flags)
345{ 442{
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) 443 if (!ev_method)
444 {
445#if EV_USE_MONOTONIC
370 { 446 {
447 struct timespec ts;
448 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
449 have_monotonic = 1;
450 }
451#endif
452
453 ev_now = ev_time ();
454 now = get_clock ();
455 diff = ev_now - now;
456
457 if (pipe (sigpipe))
458 return 0;
459
460 ev_method = EVMETHOD_NONE;
461#if EV_USE_EPOLL
462 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
463#endif
464#if EV_USE_SELECT
465 if (ev_method == EVMETHOD_NONE) select_init (flags);
466#endif
467
468 if (ev_method)
469 {
371 evw_init (&sigev, sigcb); 470 ev_watcher_init (&sigev, sigcb);
372 siginit (); 471 siginit ();
472
473 ev_signal_init (&childev, childcb, SIGCHLD);
474 ev_signal_start (&childev);
475 }
373 } 476 }
374 477
375 return ev_method; 478 return ev_method;
376} 479}
377 480
378/*****************************************************************************/ 481/*****************************************************************************/
379 482
483void
380void ev_prefork (void) 484ev_prefork (void)
381{ 485{
382 /* nop */ 486 /* nop */
383} 487}
384 488
489void
385void ev_postfork_parent (void) 490ev_postfork_parent (void)
386{ 491{
387 /* nop */ 492 /* nop */
388} 493}
389 494
495void
390void ev_postfork_child (void) 496ev_postfork_child (void)
391{ 497{
392#if HAVE_EPOLL 498#if EV_USE_EPOLL
393 if (ev_method == EVMETHOD_EPOLL) 499 if (ev_method == EVMETHOD_EPOLL)
394 epoll_postfork_child (); 500 epoll_postfork_child ();
395#endif 501#endif
396 502
397 evio_stop (&sigev); 503 ev_io_stop (&sigev);
398 close (sigpipe [0]); 504 close (sigpipe [0]);
399 close (sigpipe [1]); 505 close (sigpipe [1]);
400 pipe (sigpipe); 506 pipe (sigpipe);
401 siginit (); 507 siginit ();
402} 508}
403 509
404/*****************************************************************************/ 510/*****************************************************************************/
405 511
406static void 512static 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 () 513call_pending (void)
434{ 514{
435 while (pendingcnt) 515 while (pendingcnt)
436 { 516 {
437 ANPENDING *p = pendings + --pendingcnt; 517 ANPENDING *p = pendings + --pendingcnt;
438 518
443 } 523 }
444 } 524 }
445} 525}
446 526
447static void 527static void
448timers_reify () 528timers_reify (void)
449{ 529{
450 while (timercnt && timers [0]->at <= now) 530 while (timercnt && timers [0]->at <= now)
451 { 531 {
452 struct ev_timer *w = timers [0]; 532 struct ev_timer *w = timers [0];
453
454 event ((W)w, EV_TIMEOUT);
455 533
456 /* first reschedule or stop timer */ 534 /* first reschedule or stop timer */
457 if (w->repeat) 535 if (w->repeat)
458 { 536 {
459 w->at = now + w->repeat; 537 w->at = now + w->repeat;
460 assert (("timer timeout in the past, negative repeat?", w->at > now)); 538 assert (("timer timeout in the past, negative repeat?", w->at > now));
461 downheap ((WT *)timers, timercnt, 0); 539 downheap ((WT *)timers, timercnt, 0);
462 } 540 }
463 else 541 else
464 evtimer_stop (w); /* nonrepeating: stop timer */ 542 ev_timer_stop (w); /* nonrepeating: stop timer */
465 }
466}
467 543
544 event ((W)w, EV_TIMEOUT);
545 }
546}
547
468static void 548static void
469periodics_reify () 549periodics_reify (void)
470{ 550{
471 while (periodiccnt && periodics [0]->at <= ev_now) 551 while (periodiccnt && periodics [0]->at <= ev_now)
472 { 552 {
473 struct ev_periodic *w = periodics [0]; 553 struct ev_periodic *w = periodics [0];
474 554
478 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 558 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)); 559 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
480 downheap ((WT *)periodics, periodiccnt, 0); 560 downheap ((WT *)periodics, periodiccnt, 0);
481 } 561 }
482 else 562 else
483 evperiodic_stop (w); /* nonrepeating: stop timer */ 563 ev_periodic_stop (w); /* nonrepeating: stop timer */
484 564
485 event ((W)w, EV_TIMEOUT); 565 event ((W)w, EV_TIMEOUT);
486 } 566 }
487} 567}
488 568
500 { 580 {
501 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; 581 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
502 582
503 if (fabs (diff) >= 1e-4) 583 if (fabs (diff) >= 1e-4)
504 { 584 {
505 evperiodic_stop (w); 585 ev_periodic_stop (w);
506 evperiodic_start (w); 586 ev_periodic_start (w);
507 587
508 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 588 i = 0; /* restart loop, inefficient, but time jumps should be rare */
509 } 589 }
510 } 590 }
511 } 591 }
512} 592}
513 593
514static void 594static void
515time_update () 595time_update (void)
516{ 596{
517 int i; 597 int i;
518 598
519 ev_now = ev_time (); 599 ev_now = ev_time ();
520 600
554int ev_loop_done; 634int ev_loop_done;
555 635
556void ev_loop (int flags) 636void ev_loop (int flags)
557{ 637{
558 double block; 638 double block;
559 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 639 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
560 640
561 do 641 do
562 { 642 {
563 /* queue check watchers (and execute them) */ 643 /* queue check watchers (and execute them) */
564 if (checkcnt) 644 if (preparecnt)
565 { 645 {
566 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 646 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
567 call_pending (); 647 call_pending ();
568 } 648 }
569 649
570 /* update fd-related kernel structures */ 650 /* update fd-related kernel structures */
571 fd_reify (); 651 fd_reify ();
572 652
573 /* calculate blocking time */ 653 /* calculate blocking time */
574 654
575 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ 655 /* we only need this for !monotonic clockor timers, but as we basically
656 always have timers, we just calculate it always */
576 ev_now = ev_time (); 657 ev_now = ev_time ();
577 658
578 if (flags & EVLOOP_NONBLOCK || idlecnt) 659 if (flags & EVLOOP_NONBLOCK || idlecnt)
579 block = 0.; 660 block = 0.;
580 else 661 else
668} 749}
669 750
670/*****************************************************************************/ 751/*****************************************************************************/
671 752
672void 753void
673evio_start (struct ev_io *w) 754ev_io_start (struct ev_io *w)
674{ 755{
675 if (ev_is_active (w)) 756 if (ev_is_active (w))
676 return; 757 return;
677 758
678 int fd = w->fd; 759 int fd = w->fd;
679 760
680 ev_start ((W)w, 1); 761 ev_start ((W)w, 1);
681 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 762 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
682 wlist_add ((WL *)&anfds[fd].head, (WL)w); 763 wlist_add ((WL *)&anfds[fd].head, (WL)w);
683 764
684 ++fdchangecnt; 765 fd_change (fd);
685 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
686 fdchanges [fdchangecnt - 1] = fd;
687} 766}
688 767
689void 768void
690evio_stop (struct ev_io *w) 769ev_io_stop (struct ev_io *w)
691{ 770{
692 ev_clear ((W)w); 771 ev_clear ((W)w);
693 if (!ev_is_active (w)) 772 if (!ev_is_active (w))
694 return; 773 return;
695 774
696 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 775 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
697 ev_stop ((W)w); 776 ev_stop ((W)w);
698 777
699 ++fdchangecnt; 778 fd_change (w->fd);
700 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
701 fdchanges [fdchangecnt - 1] = w->fd;
702} 779}
703 780
704void 781void
705evtimer_start (struct ev_timer *w) 782ev_timer_start (struct ev_timer *w)
706{ 783{
707 if (ev_is_active (w)) 784 if (ev_is_active (w))
708 return; 785 return;
709 786
710 w->at += now; 787 w->at += now;
716 timers [timercnt - 1] = w; 793 timers [timercnt - 1] = w;
717 upheap ((WT *)timers, timercnt - 1); 794 upheap ((WT *)timers, timercnt - 1);
718} 795}
719 796
720void 797void
721evtimer_stop (struct ev_timer *w) 798ev_timer_stop (struct ev_timer *w)
722{ 799{
723 ev_clear ((W)w); 800 ev_clear ((W)w);
724 if (!ev_is_active (w)) 801 if (!ev_is_active (w))
725 return; 802 return;
726 803
734 811
735 ev_stop ((W)w); 812 ev_stop ((W)w);
736} 813}
737 814
738void 815void
739evtimer_again (struct ev_timer *w) 816ev_timer_again (struct ev_timer *w)
740{ 817{
741 if (ev_is_active (w)) 818 if (ev_is_active (w))
742 { 819 {
743 if (w->repeat) 820 if (w->repeat)
744 { 821 {
745 w->at = now + w->repeat; 822 w->at = now + w->repeat;
746 downheap ((WT *)timers, timercnt, w->active - 1); 823 downheap ((WT *)timers, timercnt, w->active - 1);
747 } 824 }
748 else 825 else
749 evtimer_stop (w); 826 ev_timer_stop (w);
750 } 827 }
751 else if (w->repeat) 828 else if (w->repeat)
752 evtimer_start (w); 829 ev_timer_start (w);
753} 830}
754 831
755void 832void
756evperiodic_start (struct ev_periodic *w) 833ev_periodic_start (struct ev_periodic *w)
757{ 834{
758 if (ev_is_active (w)) 835 if (ev_is_active (w))
759 return; 836 return;
760 837
761 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 838 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
769 periodics [periodiccnt - 1] = w; 846 periodics [periodiccnt - 1] = w;
770 upheap ((WT *)periodics, periodiccnt - 1); 847 upheap ((WT *)periodics, periodiccnt - 1);
771} 848}
772 849
773void 850void
774evperiodic_stop (struct ev_periodic *w) 851ev_periodic_stop (struct ev_periodic *w)
775{ 852{
776 ev_clear ((W)w); 853 ev_clear ((W)w);
777 if (!ev_is_active (w)) 854 if (!ev_is_active (w))
778 return; 855 return;
779 856
785 862
786 ev_stop ((W)w); 863 ev_stop ((W)w);
787} 864}
788 865
789void 866void
790evsignal_start (struct ev_signal *w) 867ev_signal_start (struct ev_signal *w)
791{ 868{
792 if (ev_is_active (w)) 869 if (ev_is_active (w))
793 return; 870 return;
794 871
795 ev_start ((W)w, 1); 872 ev_start ((W)w, 1);
805 sigaction (w->signum, &sa, 0); 882 sigaction (w->signum, &sa, 0);
806 } 883 }
807} 884}
808 885
809void 886void
810evsignal_stop (struct ev_signal *w) 887ev_signal_stop (struct ev_signal *w)
811{ 888{
812 ev_clear ((W)w); 889 ev_clear ((W)w);
813 if (!ev_is_active (w)) 890 if (!ev_is_active (w))
814 return; 891 return;
815 892
818 895
819 if (!signals [w->signum - 1].head) 896 if (!signals [w->signum - 1].head)
820 signal (w->signum, SIG_DFL); 897 signal (w->signum, SIG_DFL);
821} 898}
822 899
900void
823void evidle_start (struct ev_idle *w) 901ev_idle_start (struct ev_idle *w)
824{ 902{
825 if (ev_is_active (w)) 903 if (ev_is_active (w))
826 return; 904 return;
827 905
828 ev_start ((W)w, ++idlecnt); 906 ev_start ((W)w, ++idlecnt);
829 array_needsize (idles, idlemax, idlecnt, ); 907 array_needsize (idles, idlemax, idlecnt, );
830 idles [idlecnt - 1] = w; 908 idles [idlecnt - 1] = w;
831} 909}
832 910
911void
833void evidle_stop (struct ev_idle *w) 912ev_idle_stop (struct ev_idle *w)
834{ 913{
835 ev_clear ((W)w); 914 ev_clear ((W)w);
836 if (ev_is_active (w)) 915 if (ev_is_active (w))
837 return; 916 return;
838 917
839 idles [w->active - 1] = idles [--idlecnt]; 918 idles [w->active - 1] = idles [--idlecnt];
840 ev_stop ((W)w); 919 ev_stop ((W)w);
841} 920}
842 921
922void
843void evprepare_start (struct ev_prepare *w) 923ev_prepare_start (struct ev_prepare *w)
844{ 924{
845 if (ev_is_active (w)) 925 if (ev_is_active (w))
846 return; 926 return;
847 927
848 ev_start ((W)w, ++preparecnt); 928 ev_start ((W)w, ++preparecnt);
849 array_needsize (prepares, preparemax, preparecnt, ); 929 array_needsize (prepares, preparemax, preparecnt, );
850 prepares [preparecnt - 1] = w; 930 prepares [preparecnt - 1] = w;
851} 931}
852 932
933void
853void evprepare_stop (struct ev_prepare *w) 934ev_prepare_stop (struct ev_prepare *w)
854{ 935{
855 ev_clear ((W)w); 936 ev_clear ((W)w);
856 if (ev_is_active (w)) 937 if (ev_is_active (w))
857 return; 938 return;
858 939
859 prepares [w->active - 1] = prepares [--preparecnt]; 940 prepares [w->active - 1] = prepares [--preparecnt];
860 ev_stop ((W)w); 941 ev_stop ((W)w);
861} 942}
862 943
944void
863void evcheck_start (struct ev_check *w) 945ev_check_start (struct ev_check *w)
864{ 946{
865 if (ev_is_active (w)) 947 if (ev_is_active (w))
866 return; 948 return;
867 949
868 ev_start ((W)w, ++checkcnt); 950 ev_start ((W)w, ++checkcnt);
869 array_needsize (checks, checkmax, checkcnt, ); 951 array_needsize (checks, checkmax, checkcnt, );
870 checks [checkcnt - 1] = w; 952 checks [checkcnt - 1] = w;
871} 953}
872 954
955void
873void evcheck_stop (struct ev_check *w) 956ev_check_stop (struct ev_check *w)
874{ 957{
875 ev_clear ((W)w); 958 ev_clear ((W)w);
876 if (ev_is_active (w)) 959 if (ev_is_active (w))
877 return; 960 return;
878 961
879 checks [w->active - 1] = checks [--checkcnt]; 962 checks [w->active - 1] = checks [--checkcnt];
963 ev_stop ((W)w);
964}
965
966void
967ev_child_start (struct ev_child *w)
968{
969 if (ev_is_active (w))
970 return;
971
972 ev_start ((W)w, 1);
973 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
974}
975
976void
977ev_child_stop (struct ev_child *w)
978{
979 ev_clear ((W)w);
980 if (ev_is_active (w))
981 return;
982
983 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
880 ev_stop ((W)w); 984 ev_stop ((W)w);
881} 985}
882 986
883/*****************************************************************************/ 987/*****************************************************************************/
884 988
894once_cb (struct ev_once *once, int revents) 998once_cb (struct ev_once *once, int revents)
895{ 999{
896 void (*cb)(int revents, void *arg) = once->cb; 1000 void (*cb)(int revents, void *arg) = once->cb;
897 void *arg = once->arg; 1001 void *arg = once->arg;
898 1002
899 evio_stop (&once->io); 1003 ev_io_stop (&once->io);
900 evtimer_stop (&once->to); 1004 ev_timer_stop (&once->to);
901 free (once); 1005 free (once);
902 1006
903 cb (revents, arg); 1007 cb (revents, arg);
904} 1008}
905 1009
919ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1023ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
920{ 1024{
921 struct ev_once *once = malloc (sizeof (struct ev_once)); 1025 struct ev_once *once = malloc (sizeof (struct ev_once));
922 1026
923 if (!once) 1027 if (!once)
924 cb (EV_ERROR, arg); 1028 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
925 else 1029 else
926 { 1030 {
927 once->cb = cb; 1031 once->cb = cb;
928 once->arg = arg; 1032 once->arg = arg;
929 1033
930 evw_init (&once->io, once_cb_io); 1034 ev_watcher_init (&once->io, once_cb_io);
931
932 if (fd >= 0) 1035 if (fd >= 0)
933 { 1036 {
934 evio_set (&once->io, fd, events); 1037 ev_io_set (&once->io, fd, events);
935 evio_start (&once->io); 1038 ev_io_start (&once->io);
936 } 1039 }
937 1040
938 evw_init (&once->to, once_cb_to); 1041 ev_watcher_init (&once->to, once_cb_to);
939
940 if (timeout >= 0.) 1042 if (timeout >= 0.)
941 { 1043 {
942 evtimer_set (&once->to, timeout, 0.); 1044 ev_timer_set (&once->to, timeout, 0.);
943 evtimer_start (&once->to); 1045 ev_timer_start (&once->to);
944 } 1046 }
945 } 1047 }
946} 1048}
947 1049
948/*****************************************************************************/ 1050/*****************************************************************************/
959 1061
960static void 1062static void
961ocb (struct ev_timer *w, int revents) 1063ocb (struct ev_timer *w, int revents)
962{ 1064{
963 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 1065 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
964 evtimer_stop (w); 1066 ev_timer_stop (w);
965 evtimer_start (w); 1067 ev_timer_start (w);
966} 1068}
967 1069
968static void 1070static void
969scb (struct ev_signal *w, int revents) 1071scb (struct ev_signal *w, int revents)
970{ 1072{
971 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1073 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
972 evio_stop (&wio); 1074 ev_io_stop (&wio);
973 evio_start (&wio); 1075 ev_io_start (&wio);
974} 1076}
975 1077
976static void 1078static void
977gcb (struct ev_signal *w, int revents) 1079gcb (struct ev_signal *w, int revents)
978{ 1080{
982 1084
983int main (void) 1085int main (void)
984{ 1086{
985 ev_init (0); 1087 ev_init (0);
986 1088
987 evio_init (&wio, sin_cb, 0, EV_READ); 1089 ev_io_init (&wio, sin_cb, 0, EV_READ);
988 evio_start (&wio); 1090 ev_io_start (&wio);
989 1091
990 struct ev_timer t[10000]; 1092 struct ev_timer t[10000];
991 1093
992#if 0 1094#if 0
993 int i; 1095 int i;
994 for (i = 0; i < 10000; ++i) 1096 for (i = 0; i < 10000; ++i)
995 { 1097 {
996 struct ev_timer *w = t + i; 1098 struct ev_timer *w = t + i;
997 evw_init (w, ocb, i); 1099 ev_watcher_init (w, ocb, i);
998 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1100 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
999 evtimer_start (w); 1101 ev_timer_start (w);
1000 if (drand48 () < 0.5) 1102 if (drand48 () < 0.5)
1001 evtimer_stop (w); 1103 ev_timer_stop (w);
1002 } 1104 }
1003#endif 1105#endif
1004 1106
1005 struct ev_timer t1; 1107 struct ev_timer t1;
1006 evtimer_init (&t1, ocb, 5, 10); 1108 ev_timer_init (&t1, ocb, 5, 10);
1007 evtimer_start (&t1); 1109 ev_timer_start (&t1);
1008 1110
1009 struct ev_signal sig; 1111 struct ev_signal sig;
1010 evsignal_init (&sig, scb, SIGQUIT); 1112 ev_signal_init (&sig, scb, SIGQUIT);
1011 evsignal_start (&sig); 1113 ev_signal_start (&sig);
1012 1114
1013 struct ev_check cw; 1115 struct ev_check cw;
1014 evcheck_init (&cw, gcb); 1116 ev_check_init (&cw, gcb);
1015 evcheck_start (&cw); 1117 ev_check_start (&cw);
1016 1118
1017 struct ev_idle iw; 1119 struct ev_idle iw;
1018 evidle_init (&iw, gcb); 1120 ev_idle_init (&iw, gcb);
1019 evidle_start (&iw); 1121 ev_idle_start (&iw);
1020 1122
1021 ev_loop (0); 1123 ev_loop (0);
1022 1124
1023 return 0; 1125 return 0;
1024} 1126}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines