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.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;
327static int idlemax, idlecnt; 388static int idlemax, idlecnt;
328 389
390static struct ev_prepare **prepares;
391static int preparemax, preparecnt;
392
329static struct ev_check **checks; 393static struct ev_check **checks;
330static int checkmax, checkcnt; 394static int checkmax, checkcnt;
331 395
332/*****************************************************************************/ 396/*****************************************************************************/
333 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
334#if HAVE_EPOLL 422#if EV_USE_EPOLL
335# include "ev_epoll.c" 423# include "ev_epoll.c"
336#endif 424#endif
337#if HAVE_SELECT 425#if EV_USE_SELECT
338# include "ev_select.c" 426# include "ev_select.c"
339#endif 427#endif
340 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
341int ev_init (int flags) 441int ev_init (int flags)
342{ 442{
343#if HAVE_MONOTONIC
344 {
345 struct timespec ts;
346 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
347 have_monotonic = 1;
348 }
349#endif
350
351 ev_now = ev_time ();
352 now = get_clock ();
353 diff = ev_now - now;
354
355 if (pipe (sigpipe))
356 return 0;
357
358 ev_method = EVMETHOD_NONE;
359#if HAVE_EPOLL
360 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
361#endif
362#if HAVE_SELECT
363 if (ev_method == EVMETHOD_NONE) select_init (flags);
364#endif
365
366 if (ev_method) 443 if (!ev_method)
444 {
445#if EV_USE_MONOTONIC
367 { 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 {
368 evw_init (&sigev, sigcb); 470 ev_watcher_init (&sigev, sigcb);
369 siginit (); 471 siginit ();
472
473 ev_signal_init (&childev, childcb, SIGCHLD);
474 ev_signal_start (&childev);
475 }
370 } 476 }
371 477
372 return ev_method; 478 return ev_method;
373} 479}
374 480
375/*****************************************************************************/ 481/*****************************************************************************/
376 482
483void
377void ev_prefork (void) 484ev_prefork (void)
378{ 485{
379 /* nop */ 486 /* nop */
380} 487}
381 488
489void
382void ev_postfork_parent (void) 490ev_postfork_parent (void)
383{ 491{
384 /* nop */ 492 /* nop */
385} 493}
386 494
495void
387void ev_postfork_child (void) 496ev_postfork_child (void)
388{ 497{
389#if HAVE_EPOLL 498#if EV_USE_EPOLL
390 if (ev_method == EVMETHOD_EPOLL) 499 if (ev_method == EVMETHOD_EPOLL)
391 epoll_postfork_child (); 500 epoll_postfork_child ();
392#endif 501#endif
393 502
394 evio_stop (&sigev); 503 ev_io_stop (&sigev);
395 close (sigpipe [0]); 504 close (sigpipe [0]);
396 close (sigpipe [1]); 505 close (sigpipe [1]);
397 pipe (sigpipe); 506 pipe (sigpipe);
398 siginit (); 507 siginit ();
399} 508}
400 509
401/*****************************************************************************/ 510/*****************************************************************************/
402 511
403static void 512static void
404fd_reify (void)
405{
406 int i;
407
408 for (i = 0; i < fdchangecnt; ++i)
409 {
410 int fd = fdchanges [i];
411 ANFD *anfd = anfds + fd;
412 struct ev_io *w;
413
414 int wev = 0;
415
416 for (w = anfd->head; w; w = w->next)
417 wev |= w->events;
418
419 if (anfd->wev != wev)
420 {
421 method_modify (fd, anfd->wev, wev);
422 anfd->wev = wev;
423 }
424 }
425
426 fdchangecnt = 0;
427}
428
429static void
430call_pending () 513call_pending (void)
431{ 514{
432 while (pendingcnt) 515 while (pendingcnt)
433 { 516 {
434 ANPENDING *p = pendings + --pendingcnt; 517 ANPENDING *p = pendings + --pendingcnt;
435 518
440 } 523 }
441 } 524 }
442} 525}
443 526
444static void 527static void
445timers_reify () 528timers_reify (void)
446{ 529{
447 while (timercnt && timers [0]->at <= now) 530 while (timercnt && timers [0]->at <= now)
448 { 531 {
449 struct ev_timer *w = timers [0]; 532 struct ev_timer *w = timers [0];
450
451 event ((W)w, EV_TIMEOUT);
452 533
453 /* first reschedule or stop timer */ 534 /* first reschedule or stop timer */
454 if (w->repeat) 535 if (w->repeat)
455 { 536 {
456 w->at = now + w->repeat; 537 w->at = now + w->repeat;
457 assert (("timer timeout in the past, negative repeat?", w->at > now)); 538 assert (("timer timeout in the past, negative repeat?", w->at > now));
458 downheap ((WT *)timers, timercnt, 0); 539 downheap ((WT *)timers, timercnt, 0);
459 } 540 }
460 else 541 else
461 evtimer_stop (w); /* nonrepeating: stop timer */ 542 ev_timer_stop (w); /* nonrepeating: stop timer */
462 }
463}
464 543
544 event ((W)w, EV_TIMEOUT);
545 }
546}
547
465static void 548static void
466periodics_reify () 549periodics_reify (void)
467{ 550{
468 while (periodiccnt && periodics [0]->at <= ev_now) 551 while (periodiccnt && periodics [0]->at <= ev_now)
469 { 552 {
470 struct ev_periodic *w = periodics [0]; 553 struct ev_periodic *w = periodics [0];
471 554
475 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;
476 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));
477 downheap ((WT *)periodics, periodiccnt, 0); 560 downheap ((WT *)periodics, periodiccnt, 0);
478 } 561 }
479 else 562 else
480 evperiodic_stop (w); /* nonrepeating: stop timer */ 563 ev_periodic_stop (w); /* nonrepeating: stop timer */
481 564
482 event ((W)w, EV_TIMEOUT); 565 event ((W)w, EV_TIMEOUT);
483 } 566 }
484} 567}
485 568
497 { 580 {
498 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;
499 582
500 if (fabs (diff) >= 1e-4) 583 if (fabs (diff) >= 1e-4)
501 { 584 {
502 evperiodic_stop (w); 585 ev_periodic_stop (w);
503 evperiodic_start (w); 586 ev_periodic_start (w);
504 587
505 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 588 i = 0; /* restart loop, inefficient, but time jumps should be rare */
506 } 589 }
507 } 590 }
508 } 591 }
509} 592}
510 593
511static void 594static void
512time_update () 595time_update (void)
513{ 596{
514 int i; 597 int i;
515 598
516 ev_now = ev_time (); 599 ev_now = ev_time ();
517 600
551int ev_loop_done; 634int ev_loop_done;
552 635
553void ev_loop (int flags) 636void ev_loop (int flags)
554{ 637{
555 double block; 638 double block;
556 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 639 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
557
558 if (checkcnt)
559 {
560 queue_events ((W *)checks, checkcnt, EV_CHECK);
561 call_pending ();
562 }
563 640
564 do 641 do
565 { 642 {
643 /* queue check watchers (and execute them) */
644 if (preparecnt)
645 {
646 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
647 call_pending ();
648 }
649
566 /* update fd-related kernel structures */ 650 /* update fd-related kernel structures */
567 fd_reify (); 651 fd_reify ();
568 652
569 /* calculate blocking time */ 653 /* calculate blocking time */
570 654
571 /* 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 */
572 ev_now = ev_time (); 657 ev_now = ev_time ();
573 658
574 if (flags & EVLOOP_NONBLOCK || idlecnt) 659 if (flags & EVLOOP_NONBLOCK || idlecnt)
575 block = 0.; 660 block = 0.;
576 else 661 else
596 681
597 /* update ev_now, do magic */ 682 /* update ev_now, do magic */
598 time_update (); 683 time_update ();
599 684
600 /* queue pending timers and reschedule them */ 685 /* queue pending timers and reschedule them */
686 timers_reify (); /* relative timers called last */
601 periodics_reify (); /* absolute timers first */ 687 periodics_reify (); /* absolute timers called first */
602 timers_reify (); /* relative timers second */
603 688
604 /* queue idle watchers unless io or timers are pending */ 689 /* queue idle watchers unless io or timers are pending */
605 if (!pendingcnt) 690 if (!pendingcnt)
606 queue_events ((W *)idles, idlecnt, EV_IDLE); 691 queue_events ((W *)idles, idlecnt, EV_IDLE);
607 692
608 /* queue check and possibly idle watchers */ 693 /* queue check watchers, to be executed first */
694 if (checkcnt)
609 queue_events ((W *)checks, checkcnt, EV_CHECK); 695 queue_events ((W *)checks, checkcnt, EV_CHECK);
610 696
611 call_pending (); 697 call_pending ();
612 } 698 }
613 while (!ev_loop_done); 699 while (!ev_loop_done);
614 700
663} 749}
664 750
665/*****************************************************************************/ 751/*****************************************************************************/
666 752
667void 753void
668evio_start (struct ev_io *w) 754ev_io_start (struct ev_io *w)
669{ 755{
670 if (ev_is_active (w)) 756 if (ev_is_active (w))
671 return; 757 return;
672 758
673 int fd = w->fd; 759 int fd = w->fd;
674 760
675 ev_start ((W)w, 1); 761 ev_start ((W)w, 1);
676 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 762 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
677 wlist_add ((WL *)&anfds[fd].head, (WL)w); 763 wlist_add ((WL *)&anfds[fd].head, (WL)w);
678 764
679 ++fdchangecnt; 765 fd_change (fd);
680 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
681 fdchanges [fdchangecnt - 1] = fd;
682} 766}
683 767
684void 768void
685evio_stop (struct ev_io *w) 769ev_io_stop (struct ev_io *w)
686{ 770{
687 ev_clear ((W)w); 771 ev_clear ((W)w);
688 if (!ev_is_active (w)) 772 if (!ev_is_active (w))
689 return; 773 return;
690 774
691 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 775 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
692 ev_stop ((W)w); 776 ev_stop ((W)w);
693 777
694 ++fdchangecnt; 778 fd_change (w->fd);
695 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
696 fdchanges [fdchangecnt - 1] = w->fd;
697} 779}
698 780
699void 781void
700evtimer_start (struct ev_timer *w) 782ev_timer_start (struct ev_timer *w)
701{ 783{
702 if (ev_is_active (w)) 784 if (ev_is_active (w))
703 return; 785 return;
704 786
705 w->at += now; 787 w->at += now;
711 timers [timercnt - 1] = w; 793 timers [timercnt - 1] = w;
712 upheap ((WT *)timers, timercnt - 1); 794 upheap ((WT *)timers, timercnt - 1);
713} 795}
714 796
715void 797void
716evtimer_stop (struct ev_timer *w) 798ev_timer_stop (struct ev_timer *w)
717{ 799{
718 ev_clear ((W)w); 800 ev_clear ((W)w);
719 if (!ev_is_active (w)) 801 if (!ev_is_active (w))
720 return; 802 return;
721 803
729 811
730 ev_stop ((W)w); 812 ev_stop ((W)w);
731} 813}
732 814
733void 815void
734evtimer_again (struct ev_timer *w) 816ev_timer_again (struct ev_timer *w)
735{ 817{
736 if (ev_is_active (w)) 818 if (ev_is_active (w))
737 { 819 {
738 if (w->repeat) 820 if (w->repeat)
739 { 821 {
740 w->at = now + w->repeat; 822 w->at = now + w->repeat;
741 downheap ((WT *)timers, timercnt, w->active - 1); 823 downheap ((WT *)timers, timercnt, w->active - 1);
742 } 824 }
743 else 825 else
744 evtimer_stop (w); 826 ev_timer_stop (w);
745 } 827 }
746 else if (w->repeat) 828 else if (w->repeat)
747 evtimer_start (w); 829 ev_timer_start (w);
748} 830}
749 831
750void 832void
751evperiodic_start (struct ev_periodic *w) 833ev_periodic_start (struct ev_periodic *w)
752{ 834{
753 if (ev_is_active (w)) 835 if (ev_is_active (w))
754 return; 836 return;
755 837
756 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.));
764 periodics [periodiccnt - 1] = w; 846 periodics [periodiccnt - 1] = w;
765 upheap ((WT *)periodics, periodiccnt - 1); 847 upheap ((WT *)periodics, periodiccnt - 1);
766} 848}
767 849
768void 850void
769evperiodic_stop (struct ev_periodic *w) 851ev_periodic_stop (struct ev_periodic *w)
770{ 852{
771 ev_clear ((W)w); 853 ev_clear ((W)w);
772 if (!ev_is_active (w)) 854 if (!ev_is_active (w))
773 return; 855 return;
774 856
780 862
781 ev_stop ((W)w); 863 ev_stop ((W)w);
782} 864}
783 865
784void 866void
785evsignal_start (struct ev_signal *w) 867ev_signal_start (struct ev_signal *w)
786{ 868{
787 if (ev_is_active (w)) 869 if (ev_is_active (w))
788 return; 870 return;
789 871
790 ev_start ((W)w, 1); 872 ev_start ((W)w, 1);
800 sigaction (w->signum, &sa, 0); 882 sigaction (w->signum, &sa, 0);
801 } 883 }
802} 884}
803 885
804void 886void
805evsignal_stop (struct ev_signal *w) 887ev_signal_stop (struct ev_signal *w)
806{ 888{
807 ev_clear ((W)w); 889 ev_clear ((W)w);
808 if (!ev_is_active (w)) 890 if (!ev_is_active (w))
809 return; 891 return;
810 892
813 895
814 if (!signals [w->signum - 1].head) 896 if (!signals [w->signum - 1].head)
815 signal (w->signum, SIG_DFL); 897 signal (w->signum, SIG_DFL);
816} 898}
817 899
900void
818void evidle_start (struct ev_idle *w) 901ev_idle_start (struct ev_idle *w)
819{ 902{
820 if (ev_is_active (w)) 903 if (ev_is_active (w))
821 return; 904 return;
822 905
823 ev_start ((W)w, ++idlecnt); 906 ev_start ((W)w, ++idlecnt);
824 array_needsize (idles, idlemax, idlecnt, ); 907 array_needsize (idles, idlemax, idlecnt, );
825 idles [idlecnt - 1] = w; 908 idles [idlecnt - 1] = w;
826} 909}
827 910
911void
828void evidle_stop (struct ev_idle *w) 912ev_idle_stop (struct ev_idle *w)
829{ 913{
830 ev_clear ((W)w); 914 ev_clear ((W)w);
831 if (ev_is_active (w)) 915 if (ev_is_active (w))
832 return; 916 return;
833 917
834 idles [w->active - 1] = idles [--idlecnt]; 918 idles [w->active - 1] = idles [--idlecnt];
835 ev_stop ((W)w); 919 ev_stop ((W)w);
836} 920}
837 921
922void
923ev_prepare_start (struct ev_prepare *w)
924{
925 if (ev_is_active (w))
926 return;
927
928 ev_start ((W)w, ++preparecnt);
929 array_needsize (prepares, preparemax, preparecnt, );
930 prepares [preparecnt - 1] = w;
931}
932
933void
934ev_prepare_stop (struct ev_prepare *w)
935{
936 ev_clear ((W)w);
937 if (ev_is_active (w))
938 return;
939
940 prepares [w->active - 1] = prepares [--preparecnt];
941 ev_stop ((W)w);
942}
943
944void
838void evcheck_start (struct ev_check *w) 945ev_check_start (struct ev_check *w)
839{ 946{
840 if (ev_is_active (w)) 947 if (ev_is_active (w))
841 return; 948 return;
842 949
843 ev_start ((W)w, ++checkcnt); 950 ev_start ((W)w, ++checkcnt);
844 array_needsize (checks, checkmax, checkcnt, ); 951 array_needsize (checks, checkmax, checkcnt, );
845 checks [checkcnt - 1] = w; 952 checks [checkcnt - 1] = w;
846} 953}
847 954
955void
848void evcheck_stop (struct ev_check *w) 956ev_check_stop (struct ev_check *w)
849{ 957{
850 ev_clear ((W)w); 958 ev_clear ((W)w);
851 if (ev_is_active (w)) 959 if (ev_is_active (w))
852 return; 960 return;
853 961
854 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);
855 ev_stop ((W)w); 984 ev_stop ((W)w);
856} 985}
857 986
858/*****************************************************************************/ 987/*****************************************************************************/
859 988
869once_cb (struct ev_once *once, int revents) 998once_cb (struct ev_once *once, int revents)
870{ 999{
871 void (*cb)(int revents, void *arg) = once->cb; 1000 void (*cb)(int revents, void *arg) = once->cb;
872 void *arg = once->arg; 1001 void *arg = once->arg;
873 1002
874 evio_stop (&once->io); 1003 ev_io_stop (&once->io);
875 evtimer_stop (&once->to); 1004 ev_timer_stop (&once->to);
876 free (once); 1005 free (once);
877 1006
878 cb (revents, arg); 1007 cb (revents, arg);
879} 1008}
880 1009
894ev_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)
895{ 1024{
896 struct ev_once *once = malloc (sizeof (struct ev_once)); 1025 struct ev_once *once = malloc (sizeof (struct ev_once));
897 1026
898 if (!once) 1027 if (!once)
899 cb (EV_ERROR, arg); 1028 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
900 else 1029 else
901 { 1030 {
902 once->cb = cb; 1031 once->cb = cb;
903 once->arg = arg; 1032 once->arg = arg;
904 1033
905 evw_init (&once->io, once_cb_io); 1034 ev_watcher_init (&once->io, once_cb_io);
906
907 if (fd >= 0) 1035 if (fd >= 0)
908 { 1036 {
909 evio_set (&once->io, fd, events); 1037 ev_io_set (&once->io, fd, events);
910 evio_start (&once->io); 1038 ev_io_start (&once->io);
911 } 1039 }
912 1040
913 evw_init (&once->to, once_cb_to); 1041 ev_watcher_init (&once->to, once_cb_to);
914
915 if (timeout >= 0.) 1042 if (timeout >= 0.)
916 { 1043 {
917 evtimer_set (&once->to, timeout, 0.); 1044 ev_timer_set (&once->to, timeout, 0.);
918 evtimer_start (&once->to); 1045 ev_timer_start (&once->to);
919 } 1046 }
920 } 1047 }
921} 1048}
922 1049
923/*****************************************************************************/ 1050/*****************************************************************************/
934 1061
935static void 1062static void
936ocb (struct ev_timer *w, int revents) 1063ocb (struct ev_timer *w, int revents)
937{ 1064{
938 //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);
939 evtimer_stop (w); 1066 ev_timer_stop (w);
940 evtimer_start (w); 1067 ev_timer_start (w);
941} 1068}
942 1069
943static void 1070static void
944scb (struct ev_signal *w, int revents) 1071scb (struct ev_signal *w, int revents)
945{ 1072{
946 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1073 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
947 evio_stop (&wio); 1074 ev_io_stop (&wio);
948 evio_start (&wio); 1075 ev_io_start (&wio);
949} 1076}
950 1077
951static void 1078static void
952gcb (struct ev_signal *w, int revents) 1079gcb (struct ev_signal *w, int revents)
953{ 1080{
957 1084
958int main (void) 1085int main (void)
959{ 1086{
960 ev_init (0); 1087 ev_init (0);
961 1088
962 evio_init (&wio, sin_cb, 0, EV_READ); 1089 ev_io_init (&wio, sin_cb, 0, EV_READ);
963 evio_start (&wio); 1090 ev_io_start (&wio);
964 1091
965 struct ev_timer t[10000]; 1092 struct ev_timer t[10000];
966 1093
967#if 0 1094#if 0
968 int i; 1095 int i;
969 for (i = 0; i < 10000; ++i) 1096 for (i = 0; i < 10000; ++i)
970 { 1097 {
971 struct ev_timer *w = t + i; 1098 struct ev_timer *w = t + i;
972 evw_init (w, ocb, i); 1099 ev_watcher_init (w, ocb, i);
973 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1100 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
974 evtimer_start (w); 1101 ev_timer_start (w);
975 if (drand48 () < 0.5) 1102 if (drand48 () < 0.5)
976 evtimer_stop (w); 1103 ev_timer_stop (w);
977 } 1104 }
978#endif 1105#endif
979 1106
980 struct ev_timer t1; 1107 struct ev_timer t1;
981 evtimer_init (&t1, ocb, 5, 10); 1108 ev_timer_init (&t1, ocb, 5, 10);
982 evtimer_start (&t1); 1109 ev_timer_start (&t1);
983 1110
984 struct ev_signal sig; 1111 struct ev_signal sig;
985 evsignal_init (&sig, scb, SIGQUIT); 1112 ev_signal_init (&sig, scb, SIGQUIT);
986 evsignal_start (&sig); 1113 ev_signal_start (&sig);
987 1114
988 struct ev_check cw; 1115 struct ev_check cw;
989 evcheck_init (&cw, gcb); 1116 ev_check_init (&cw, gcb);
990 evcheck_start (&cw); 1117 ev_check_start (&cw);
991 1118
992 struct ev_idle iw; 1119 struct ev_idle iw;
993 evidle_init (&iw, gcb); 1120 ev_idle_init (&iw, gcb);
994 evidle_start (&iw); 1121 ev_idle_start (&iw);
995 1122
996 ev_loop (0); 1123 ev_loop (0);
997 1124
998 return 0; 1125 return 0;
999} 1126}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines