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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines