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.30 by root, Thu Nov 1 08:28: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#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
60#ifndef HAVE_REALTIME 63#ifndef EV_USE_REALTIME
61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 64# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */
62#endif 65#endif
63 66
64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 67#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
65#define MAX_BLOCKTIME 60. 68#define MAX_BLOCKTIME 59.731
69#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
66 70
67#include "ev.h" 71#include "ev.h"
68 72
69typedef struct ev_watcher *W; 73typedef struct ev_watcher *W;
70typedef struct ev_watcher_list *WL; 74typedef struct ev_watcher_list *WL;
83/*****************************************************************************/ 87/*****************************************************************************/
84 88
85ev_tstamp 89ev_tstamp
86ev_time (void) 90ev_time (void)
87{ 91{
88#if HAVE_REALTIME 92#if EV_USE_REALTIME
89 struct timespec ts; 93 struct timespec ts;
90 clock_gettime (CLOCK_REALTIME, &ts); 94 clock_gettime (CLOCK_REALTIME, &ts);
91 return ts.tv_sec + ts.tv_nsec * 1e-9; 95 return ts.tv_sec + ts.tv_nsec * 1e-9;
92#else 96#else
93 struct timeval tv; 97 struct timeval tv;
97} 101}
98 102
99static ev_tstamp 103static ev_tstamp
100get_clock (void) 104get_clock (void)
101{ 105{
102#if HAVE_MONOTONIC 106#if EV_USE_MONOTONIC
103 if (have_monotonic) 107 if (have_monotonic)
104 { 108 {
105 struct timespec ts; 109 struct timespec ts;
106 clock_gettime (CLOCK_MONOTONIC, &ts); 110 clock_gettime (CLOCK_MONOTONIC, &ts);
107 return ts.tv_sec + ts.tv_nsec * 1e-9; 111 return ts.tv_sec + ts.tv_nsec * 1e-9;
109#endif 113#endif
110 114
111 return ev_time (); 115 return ev_time ();
112} 116}
113 117
118#define array_roundsize(base,n) ((n) | 4 & ~3)
119
114#define array_needsize(base,cur,cnt,init) \ 120#define array_needsize(base,cur,cnt,init) \
115 if ((cnt) > cur) \ 121 if ((cnt) > cur) \
116 { \ 122 { \
117 int newcnt = cur ? cur << 1 : 16; \ 123 int newcnt = cur; \
124 do \
125 { \
126 newcnt = array_roundsize (base, newcnt << 1); \
127 } \
128 while ((cnt) > newcnt); \
129 \
118 base = realloc (base, sizeof (*base) * (newcnt)); \ 130 base = realloc (base, sizeof (*base) * (newcnt)); \
119 init (base + cur, newcnt - cur); \ 131 init (base + cur, newcnt - cur); \
120 cur = newcnt; \ 132 cur = newcnt; \
121 } 133 }
122 134
123/*****************************************************************************/ 135/*****************************************************************************/
124 136
125typedef struct 137typedef struct
126{ 138{
127 struct ev_io *head; 139 struct ev_io *head;
128 unsigned char wev, rev; /* want, received event set */ 140 int events;
129} ANFD; 141} ANFD;
130 142
131static ANFD *anfds; 143static ANFD *anfds;
132static int anfdmax; 144static int anfdmax;
133 145
134static int *fdchanges;
135static int fdchangemax, fdchangecnt;
136
137static void 146static void
138anfds_init (ANFD *base, int count) 147anfds_init (ANFD *base, int count)
139{ 148{
140 while (count--) 149 while (count--)
141 { 150 {
142 base->head = 0; 151 base->head = 0;
143 base->wev = base->rev = EV_NONE; 152 base->events = EV_NONE;
144 ++base; 153 ++base;
145 } 154 }
146} 155}
147 156
148typedef struct 157typedef struct
155static int pendingmax, pendingcnt; 164static int pendingmax, pendingcnt;
156 165
157static void 166static void
158event (W w, int events) 167event (W w, int events)
159{ 168{
160 if (w->active)
161 {
162 w->pending = ++pendingcnt; 169 w->pending = ++pendingcnt;
163 array_needsize (pendings, pendingmax, pendingcnt, ); 170 array_needsize (pendings, pendingmax, pendingcnt, );
164 pendings [pendingcnt - 1].w = w; 171 pendings [pendingcnt - 1].w = w;
165 pendings [pendingcnt - 1].events = events; 172 pendings [pendingcnt - 1].events = events;
166 } 173}
174
175static void
176queue_events (W *events, int eventcnt, int type)
177{
178 int i;
179
180 for (i = 0; i < eventcnt; ++i)
181 event (events [i], type);
167} 182}
168 183
169static void 184static void
170fd_event (int fd, int events) 185fd_event (int fd, int events)
171{ 186{
179 if (ev) 194 if (ev)
180 event ((W)w, ev); 195 event ((W)w, ev);
181 } 196 }
182} 197}
183 198
199/*****************************************************************************/
200
201static int *fdchanges;
202static int fdchangemax, fdchangecnt;
203
184static void 204static void
185queue_events (W *events, int eventcnt, int type) 205fd_reify (void)
186{ 206{
187 int i; 207 int i;
188 208
189 for (i = 0; i < eventcnt; ++i) 209 for (i = 0; i < fdchangecnt; ++i)
190 event (events [i], type); 210 {
211 int fd = fdchanges [i];
212 ANFD *anfd = anfds + fd;
213 struct ev_io *w;
214
215 int events = 0;
216
217 for (w = anfd->head; w; w = w->next)
218 events |= w->events;
219
220 anfd->events &= ~EV_REIFY;
221
222 if (anfd->events != events)
223 {
224 method_modify (fd, anfd->events, events);
225 anfd->events = events;
226 }
227 }
228
229 fdchangecnt = 0;
230}
231
232static void
233fd_change (int fd)
234{
235 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0)
236 return;
237
238 anfds [fd].events |= EV_REIFY;
239
240 ++fdchangecnt;
241 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
242 fdchanges [fdchangecnt - 1] = fd;
243}
244
245/* called on EBADF to verify fds */
246static void
247fd_recheck (void)
248{
249 int fd;
250
251 for (fd = 0; fd < anfdmax; ++fd)
252 if (anfds [fd].events)
253 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
254 while (anfds [fd].head)
255 {
256 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT);
257 ev_io_stop (anfds [fd].head);
258 }
191} 259}
192 260
193/*****************************************************************************/ 261/*****************************************************************************/
194 262
195static struct ev_timer **timers; 263static struct ev_timer **timers;
304 372
305 /* rather than sort out wether we really need nb, set it */ 373 /* rather than sort out wether we really need nb, set it */
306 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 374 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
307 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 375 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
308 376
309 evio_set (&sigev, sigpipe [0], EV_READ); 377 ev_io_set (&sigev, sigpipe [0], EV_READ);
310 evio_start (&sigev); 378 ev_io_start (&sigev);
311} 379}
312 380
313/*****************************************************************************/ 381/*****************************************************************************/
314 382
315static struct ev_idle **idles; 383static struct ev_idle **idles;
316static int idlemax, idlecnt; 384static int idlemax, idlecnt;
317 385
386static struct ev_prepare **prepares;
387static int preparemax, preparecnt;
388
318static struct ev_check **checks; 389static struct ev_check **checks;
319static int checkmax, checkcnt; 390static int checkmax, checkcnt;
320 391
321/*****************************************************************************/ 392/*****************************************************************************/
322 393
394static struct ev_child *childs [PID_HASHSIZE];
395static struct ev_signal childev;
396
397#ifndef WCONTINUED
398# define WCONTINUED 0
399#endif
400
401static void
402childcb (struct ev_signal *sw, int revents)
403{
404 struct ev_child *w;
405 int pid, status;
406
407 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
408 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
409 if (w->pid == pid || w->pid == -1)
410 {
411 w->status = status;
412 event ((W)w, EV_CHILD);
413 }
414}
415
416/*****************************************************************************/
417
323#if HAVE_EPOLL 418#if EV_USE_EPOLL
324# include "ev_epoll.c" 419# include "ev_epoll.c"
325#endif 420#endif
326#if HAVE_SELECT 421#if EV_USE_SELECT
327# include "ev_select.c" 422# include "ev_select.c"
328#endif 423#endif
329 424
425int
426ev_version_major (void)
427{
428 return EV_VERSION_MAJOR;
429}
430
431int
432ev_version_minor (void)
433{
434 return EV_VERSION_MINOR;
435}
436
330int ev_init (int flags) 437int ev_init (int flags)
331{ 438{
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) 439 if (!ev_method)
440 {
441#if EV_USE_MONOTONIC
356 { 442 {
443 struct timespec ts;
444 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
445 have_monotonic = 1;
446 }
447#endif
448
449 ev_now = ev_time ();
450 now = get_clock ();
451 diff = ev_now - now;
452
453 if (pipe (sigpipe))
454 return 0;
455
456 ev_method = EVMETHOD_NONE;
457#if EV_USE_EPOLL
458 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
459#endif
460#if EV_USE_SELECT
461 if (ev_method == EVMETHOD_NONE) select_init (flags);
462#endif
463
464 if (ev_method)
465 {
357 evw_init (&sigev, sigcb); 466 ev_watcher_init (&sigev, sigcb);
358 siginit (); 467 siginit ();
468
469 ev_signal_init (&childev, childcb, SIGCHLD);
470 ev_signal_start (&childev);
471 }
359 } 472 }
360 473
361 return ev_method; 474 return ev_method;
362} 475}
363 476
364/*****************************************************************************/ 477/*****************************************************************************/
365 478
479void
366void ev_prefork (void) 480ev_prefork (void)
367{ 481{
368 /* nop */ 482 /* nop */
369} 483}
370 484
485void
371void ev_postfork_parent (void) 486ev_postfork_parent (void)
372{ 487{
373 /* nop */ 488 /* nop */
374} 489}
375 490
491void
376void ev_postfork_child (void) 492ev_postfork_child (void)
377{ 493{
378#if HAVE_EPOLL 494#if EV_USE_EPOLL
379 if (ev_method == EVMETHOD_EPOLL) 495 if (ev_method == EVMETHOD_EPOLL)
380 epoll_postfork_child (); 496 epoll_postfork_child ();
381#endif 497#endif
382 498
383 evio_stop (&sigev); 499 ev_io_stop (&sigev);
384 close (sigpipe [0]); 500 close (sigpipe [0]);
385 close (sigpipe [1]); 501 close (sigpipe [1]);
386 pipe (sigpipe); 502 pipe (sigpipe);
387 siginit (); 503 siginit ();
388} 504}
389 505
390/*****************************************************************************/ 506/*****************************************************************************/
391 507
392static void 508static 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 () 509call_pending (void)
420{ 510{
421 int i; 511 while (pendingcnt)
422
423 for (i = 0; i < pendingcnt; ++i)
424 { 512 {
425 ANPENDING *p = pendings + i; 513 ANPENDING *p = pendings + --pendingcnt;
426 514
427 if (p->w) 515 if (p->w)
428 { 516 {
429 p->w->pending = 0; 517 p->w->pending = 0;
430 p->w->cb (p->w, p->events); 518 p->w->cb (p->w, p->events);
431 } 519 }
432 } 520 }
433
434 pendingcnt = 0;
435} 521}
436 522
437static void 523static void
438timers_reify () 524timers_reify (void)
439{ 525{
440 while (timercnt && timers [0]->at <= now) 526 while (timercnt && timers [0]->at <= now)
441 { 527 {
442 struct ev_timer *w = timers [0]; 528 struct ev_timer *w = timers [0];
443
444 event ((W)w, EV_TIMEOUT);
445 529
446 /* first reschedule or stop timer */ 530 /* first reschedule or stop timer */
447 if (w->repeat) 531 if (w->repeat)
448 { 532 {
449 w->at = now + w->repeat; 533 w->at = now + w->repeat;
450 assert (("timer timeout in the past, negative repeat?", w->at > now)); 534 assert (("timer timeout in the past, negative repeat?", w->at > now));
451 downheap ((WT *)timers, timercnt, 0); 535 downheap ((WT *)timers, timercnt, 0);
452 } 536 }
453 else 537 else
454 evtimer_stop (w); /* nonrepeating: stop timer */ 538 ev_timer_stop (w); /* nonrepeating: stop timer */
455 }
456}
457 539
540 event ((W)w, EV_TIMEOUT);
541 }
542}
543
458static void 544static void
459periodics_reify () 545periodics_reify (void)
460{ 546{
461 while (periodiccnt && periodics [0]->at <= ev_now) 547 while (periodiccnt && periodics [0]->at <= ev_now)
462 { 548 {
463 struct ev_periodic *w = periodics [0]; 549 struct ev_periodic *w = periodics [0];
464 550
468 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 554 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)); 555 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
470 downheap ((WT *)periodics, periodiccnt, 0); 556 downheap ((WT *)periodics, periodiccnt, 0);
471 } 557 }
472 else 558 else
473 evperiodic_stop (w); /* nonrepeating: stop timer */ 559 ev_periodic_stop (w); /* nonrepeating: stop timer */
474 560
475 event ((W)w, EV_TIMEOUT); 561 event ((W)w, EV_TIMEOUT);
476 } 562 }
477} 563}
478 564
490 { 576 {
491 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; 577 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
492 578
493 if (fabs (diff) >= 1e-4) 579 if (fabs (diff) >= 1e-4)
494 { 580 {
495 evperiodic_stop (w); 581 ev_periodic_stop (w);
496 evperiodic_start (w); 582 ev_periodic_start (w);
497 583
498 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 584 i = 0; /* restart loop, inefficient, but time jumps should be rare */
499 } 585 }
500 } 586 }
501 } 587 }
502} 588}
503 589
504static void 590static void
505time_update () 591time_update (void)
506{ 592{
507 int i; 593 int i;
508 594
509 ev_now = ev_time (); 595 ev_now = ev_time ();
510 596
544int ev_loop_done; 630int ev_loop_done;
545 631
546void ev_loop (int flags) 632void ev_loop (int flags)
547{ 633{
548 double block; 634 double block;
549 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 635 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 636
557 do 637 do
558 { 638 {
639 /* queue check watchers (and execute them) */
640 if (preparecnt)
641 {
642 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
643 call_pending ();
644 }
645
559 /* update fd-related kernel structures */ 646 /* update fd-related kernel structures */
560 fd_reify (); 647 fd_reify ();
561 648
562 /* calculate blocking time */ 649 /* calculate blocking time */
563 650
564 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ 651 /* we only need this for !monotonic clockor timers, but as we basically
652 always have timers, we just calculate it always */
565 ev_now = ev_time (); 653 ev_now = ev_time ();
566 654
567 if (flags & EVLOOP_NONBLOCK || idlecnt) 655 if (flags & EVLOOP_NONBLOCK || idlecnt)
568 block = 0.; 656 block = 0.;
569 else 657 else
589 677
590 /* update ev_now, do magic */ 678 /* update ev_now, do magic */
591 time_update (); 679 time_update ();
592 680
593 /* queue pending timers and reschedule them */ 681 /* queue pending timers and reschedule them */
682 timers_reify (); /* relative timers called last */
594 periodics_reify (); /* absolute timers first */ 683 periodics_reify (); /* absolute timers called first */
595 timers_reify (); /* relative timers second */
596 684
597 /* queue idle watchers unless io or timers are pending */ 685 /* queue idle watchers unless io or timers are pending */
598 if (!pendingcnt) 686 if (!pendingcnt)
599 queue_events ((W *)idles, idlecnt, EV_IDLE); 687 queue_events ((W *)idles, idlecnt, EV_IDLE);
600 688
601 /* queue check and possibly idle watchers */ 689 /* queue check watchers, to be executed first */
690 if (checkcnt)
602 queue_events ((W *)checks, checkcnt, EV_CHECK); 691 queue_events ((W *)checks, checkcnt, EV_CHECK);
603 692
604 call_pending (); 693 call_pending ();
605 } 694 }
606 while (!ev_loop_done); 695 while (!ev_loop_done);
607 696
656} 745}
657 746
658/*****************************************************************************/ 747/*****************************************************************************/
659 748
660void 749void
661evio_start (struct ev_io *w) 750ev_io_start (struct ev_io *w)
662{ 751{
663 if (ev_is_active (w)) 752 if (ev_is_active (w))
664 return; 753 return;
665 754
666 int fd = w->fd; 755 int fd = w->fd;
667 756
668 ev_start ((W)w, 1); 757 ev_start ((W)w, 1);
669 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 758 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
670 wlist_add ((WL *)&anfds[fd].head, (WL)w); 759 wlist_add ((WL *)&anfds[fd].head, (WL)w);
671 760
672 ++fdchangecnt; 761 fd_change (fd);
673 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
674 fdchanges [fdchangecnt - 1] = fd;
675} 762}
676 763
677void 764void
678evio_stop (struct ev_io *w) 765ev_io_stop (struct ev_io *w)
679{ 766{
680 ev_clear ((W)w); 767 ev_clear ((W)w);
681 if (!ev_is_active (w)) 768 if (!ev_is_active (w))
682 return; 769 return;
683 770
684 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 771 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
685 ev_stop ((W)w); 772 ev_stop ((W)w);
686 773
687 ++fdchangecnt; 774 fd_change (w->fd);
688 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
689 fdchanges [fdchangecnt - 1] = w->fd;
690} 775}
691 776
692void 777void
693evtimer_start (struct ev_timer *w) 778ev_timer_start (struct ev_timer *w)
694{ 779{
695 if (ev_is_active (w)) 780 if (ev_is_active (w))
696 return; 781 return;
697 782
698 w->at += now; 783 w->at += now;
704 timers [timercnt - 1] = w; 789 timers [timercnt - 1] = w;
705 upheap ((WT *)timers, timercnt - 1); 790 upheap ((WT *)timers, timercnt - 1);
706} 791}
707 792
708void 793void
709evtimer_stop (struct ev_timer *w) 794ev_timer_stop (struct ev_timer *w)
710{ 795{
711 ev_clear ((W)w); 796 ev_clear ((W)w);
712 if (!ev_is_active (w)) 797 if (!ev_is_active (w))
713 return; 798 return;
714 799
722 807
723 ev_stop ((W)w); 808 ev_stop ((W)w);
724} 809}
725 810
726void 811void
727evtimer_again (struct ev_timer *w) 812ev_timer_again (struct ev_timer *w)
728{ 813{
729 if (ev_is_active (w)) 814 if (ev_is_active (w))
730 { 815 {
731 if (w->repeat) 816 if (w->repeat)
732 { 817 {
733 w->at = now + w->repeat; 818 w->at = now + w->repeat;
734 downheap ((WT *)timers, timercnt, w->active - 1); 819 downheap ((WT *)timers, timercnt, w->active - 1);
735 } 820 }
736 else 821 else
737 evtimer_stop (w); 822 ev_timer_stop (w);
738 } 823 }
739 else if (w->repeat) 824 else if (w->repeat)
740 evtimer_start (w); 825 ev_timer_start (w);
741} 826}
742 827
743void 828void
744evperiodic_start (struct ev_periodic *w) 829ev_periodic_start (struct ev_periodic *w)
745{ 830{
746 if (ev_is_active (w)) 831 if (ev_is_active (w))
747 return; 832 return;
748 833
749 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 834 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
757 periodics [periodiccnt - 1] = w; 842 periodics [periodiccnt - 1] = w;
758 upheap ((WT *)periodics, periodiccnt - 1); 843 upheap ((WT *)periodics, periodiccnt - 1);
759} 844}
760 845
761void 846void
762evperiodic_stop (struct ev_periodic *w) 847ev_periodic_stop (struct ev_periodic *w)
763{ 848{
764 ev_clear ((W)w); 849 ev_clear ((W)w);
765 if (!ev_is_active (w)) 850 if (!ev_is_active (w))
766 return; 851 return;
767 852
773 858
774 ev_stop ((W)w); 859 ev_stop ((W)w);
775} 860}
776 861
777void 862void
778evsignal_start (struct ev_signal *w) 863ev_signal_start (struct ev_signal *w)
779{ 864{
780 if (ev_is_active (w)) 865 if (ev_is_active (w))
781 return; 866 return;
782 867
783 ev_start ((W)w, 1); 868 ev_start ((W)w, 1);
793 sigaction (w->signum, &sa, 0); 878 sigaction (w->signum, &sa, 0);
794 } 879 }
795} 880}
796 881
797void 882void
798evsignal_stop (struct ev_signal *w) 883ev_signal_stop (struct ev_signal *w)
799{ 884{
800 ev_clear ((W)w); 885 ev_clear ((W)w);
801 if (!ev_is_active (w)) 886 if (!ev_is_active (w))
802 return; 887 return;
803 888
806 891
807 if (!signals [w->signum - 1].head) 892 if (!signals [w->signum - 1].head)
808 signal (w->signum, SIG_DFL); 893 signal (w->signum, SIG_DFL);
809} 894}
810 895
896void
811void evidle_start (struct ev_idle *w) 897ev_idle_start (struct ev_idle *w)
812{ 898{
813 if (ev_is_active (w)) 899 if (ev_is_active (w))
814 return; 900 return;
815 901
816 ev_start ((W)w, ++idlecnt); 902 ev_start ((W)w, ++idlecnt);
817 array_needsize (idles, idlemax, idlecnt, ); 903 array_needsize (idles, idlemax, idlecnt, );
818 idles [idlecnt - 1] = w; 904 idles [idlecnt - 1] = w;
819} 905}
820 906
907void
821void evidle_stop (struct ev_idle *w) 908ev_idle_stop (struct ev_idle *w)
822{ 909{
823 ev_clear ((W)w); 910 ev_clear ((W)w);
824 if (ev_is_active (w)) 911 if (ev_is_active (w))
825 return; 912 return;
826 913
827 idles [w->active - 1] = idles [--idlecnt]; 914 idles [w->active - 1] = idles [--idlecnt];
828 ev_stop ((W)w); 915 ev_stop ((W)w);
829} 916}
830 917
918void
919ev_prepare_start (struct ev_prepare *w)
920{
921 if (ev_is_active (w))
922 return;
923
924 ev_start ((W)w, ++preparecnt);
925 array_needsize (prepares, preparemax, preparecnt, );
926 prepares [preparecnt - 1] = w;
927}
928
929void
930ev_prepare_stop (struct ev_prepare *w)
931{
932 ev_clear ((W)w);
933 if (ev_is_active (w))
934 return;
935
936 prepares [w->active - 1] = prepares [--preparecnt];
937 ev_stop ((W)w);
938}
939
940void
831void evcheck_start (struct ev_check *w) 941ev_check_start (struct ev_check *w)
832{ 942{
833 if (ev_is_active (w)) 943 if (ev_is_active (w))
834 return; 944 return;
835 945
836 ev_start ((W)w, ++checkcnt); 946 ev_start ((W)w, ++checkcnt);
837 array_needsize (checks, checkmax, checkcnt, ); 947 array_needsize (checks, checkmax, checkcnt, );
838 checks [checkcnt - 1] = w; 948 checks [checkcnt - 1] = w;
839} 949}
840 950
951void
841void evcheck_stop (struct ev_check *w) 952ev_check_stop (struct ev_check *w)
842{ 953{
843 ev_clear ((W)w); 954 ev_clear ((W)w);
844 if (ev_is_active (w)) 955 if (ev_is_active (w))
845 return; 956 return;
846 957
847 checks [w->active - 1] = checks [--checkcnt]; 958 checks [w->active - 1] = checks [--checkcnt];
959 ev_stop ((W)w);
960}
961
962void
963ev_child_start (struct ev_child *w)
964{
965 if (ev_is_active (w))
966 return;
967
968 ev_start ((W)w, 1);
969 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
970}
971
972void
973ev_child_stop (struct ev_child *w)
974{
975 ev_clear ((W)w);
976 if (ev_is_active (w))
977 return;
978
979 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
848 ev_stop ((W)w); 980 ev_stop ((W)w);
849} 981}
850 982
851/*****************************************************************************/ 983/*****************************************************************************/
852 984
862once_cb (struct ev_once *once, int revents) 994once_cb (struct ev_once *once, int revents)
863{ 995{
864 void (*cb)(int revents, void *arg) = once->cb; 996 void (*cb)(int revents, void *arg) = once->cb;
865 void *arg = once->arg; 997 void *arg = once->arg;
866 998
867 evio_stop (&once->io); 999 ev_io_stop (&once->io);
868 evtimer_stop (&once->to); 1000 ev_timer_stop (&once->to);
869 free (once); 1001 free (once);
870 1002
871 cb (revents, arg); 1003 cb (revents, arg);
872} 1004}
873 1005
887ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1019ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
888{ 1020{
889 struct ev_once *once = malloc (sizeof (struct ev_once)); 1021 struct ev_once *once = malloc (sizeof (struct ev_once));
890 1022
891 if (!once) 1023 if (!once)
892 cb (EV_ERROR, arg); 1024 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
893 else 1025 else
894 { 1026 {
895 once->cb = cb; 1027 once->cb = cb;
896 once->arg = arg; 1028 once->arg = arg;
897 1029
898 evw_init (&once->io, once_cb_io); 1030 ev_watcher_init (&once->io, once_cb_io);
899
900 if (fd >= 0) 1031 if (fd >= 0)
901 { 1032 {
902 evio_set (&once->io, fd, events); 1033 ev_io_set (&once->io, fd, events);
903 evio_start (&once->io); 1034 ev_io_start (&once->io);
904 } 1035 }
905 1036
906 evw_init (&once->to, once_cb_to); 1037 ev_watcher_init (&once->to, once_cb_to);
907
908 if (timeout >= 0.) 1038 if (timeout >= 0.)
909 { 1039 {
910 evtimer_set (&once->to, timeout, 0.); 1040 ev_timer_set (&once->to, timeout, 0.);
911 evtimer_start (&once->to); 1041 ev_timer_start (&once->to);
912 } 1042 }
913 } 1043 }
914} 1044}
915 1045
916/*****************************************************************************/ 1046/*****************************************************************************/
927 1057
928static void 1058static void
929ocb (struct ev_timer *w, int revents) 1059ocb (struct ev_timer *w, int revents)
930{ 1060{
931 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 1061 //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); 1062 ev_timer_stop (w);
933 evtimer_start (w); 1063 ev_timer_start (w);
934} 1064}
935 1065
936static void 1066static void
937scb (struct ev_signal *w, int revents) 1067scb (struct ev_signal *w, int revents)
938{ 1068{
939 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1069 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
940 evio_stop (&wio); 1070 ev_io_stop (&wio);
941 evio_start (&wio); 1071 ev_io_start (&wio);
942} 1072}
943 1073
944static void 1074static void
945gcb (struct ev_signal *w, int revents) 1075gcb (struct ev_signal *w, int revents)
946{ 1076{
950 1080
951int main (void) 1081int main (void)
952{ 1082{
953 ev_init (0); 1083 ev_init (0);
954 1084
955 evio_init (&wio, sin_cb, 0, EV_READ); 1085 ev_io_init (&wio, sin_cb, 0, EV_READ);
956 evio_start (&wio); 1086 ev_io_start (&wio);
957 1087
958 struct ev_timer t[10000]; 1088 struct ev_timer t[10000];
959 1089
960#if 0 1090#if 0
961 int i; 1091 int i;
962 for (i = 0; i < 10000; ++i) 1092 for (i = 0; i < 10000; ++i)
963 { 1093 {
964 struct ev_timer *w = t + i; 1094 struct ev_timer *w = t + i;
965 evw_init (w, ocb, i); 1095 ev_watcher_init (w, ocb, i);
966 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1096 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
967 evtimer_start (w); 1097 ev_timer_start (w);
968 if (drand48 () < 0.5) 1098 if (drand48 () < 0.5)
969 evtimer_stop (w); 1099 ev_timer_stop (w);
970 } 1100 }
971#endif 1101#endif
972 1102
973 struct ev_timer t1; 1103 struct ev_timer t1;
974 evtimer_init (&t1, ocb, 5, 10); 1104 ev_timer_init (&t1, ocb, 5, 10);
975 evtimer_start (&t1); 1105 ev_timer_start (&t1);
976 1106
977 struct ev_signal sig; 1107 struct ev_signal sig;
978 evsignal_init (&sig, scb, SIGQUIT); 1108 ev_signal_init (&sig, scb, SIGQUIT);
979 evsignal_start (&sig); 1109 ev_signal_start (&sig);
980 1110
981 struct ev_check cw; 1111 struct ev_check cw;
982 evcheck_init (&cw, gcb); 1112 ev_check_init (&cw, gcb);
983 evcheck_start (&cw); 1113 ev_check_start (&cw);
984 1114
985 struct ev_idle iw; 1115 struct ev_idle iw;
986 evidle_init (&iw, gcb); 1116 ev_idle_init (&iw, gcb);
987 evidle_start (&iw); 1117 ev_idle_start (&iw);
988 1118
989 ev_loop (0); 1119 ev_loop (0);
990 1120
991 return 0; 1121 return 0;
992} 1122}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines