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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines