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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines