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

Comparing libev/ev.c (file contents):
Revision 1.26 by root, Wed Oct 31 21:50:15 2007 UTC vs.
Revision 1.36 by root, Thu Nov 1 13:11:11 2007 UTC

1/* 1/*
2 * libev event processing core, watcher management
3 *
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved. 5 * All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 8 * modification, are permitted provided that the following conditions are
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (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. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 30 */
31#if EV_USE_CONFIG_H
32# include "config.h"
33#endif
29 34
30#include <math.h> 35#include <math.h>
31#include <stdlib.h> 36#include <stdlib.h>
32#include <unistd.h> 37#include <unistd.h>
33#include <fcntl.h> 38#include <fcntl.h>
41#include <sys/types.h> 46#include <sys/types.h>
42#include <sys/wait.h> 47#include <sys/wait.h>
43#include <sys/time.h> 48#include <sys/time.h>
44#include <time.h> 49#include <time.h>
45 50
46#ifndef HAVE_MONOTONIC 51#ifndef EV_USE_MONOTONIC
47# ifdef CLOCK_MONOTONIC 52# ifdef CLOCK_MONOTONIC
48# define HAVE_MONOTONIC 1 53# define EV_USE_MONOTONIC 1
49# endif 54# endif
50#endif 55#endif
51 56
52#ifndef HAVE_SELECT 57#ifndef EV_USE_SELECT
53# define HAVE_SELECT 1 58# define EV_USE_SELECT 1
54#endif 59#endif
55 60
56#ifndef HAVE_EPOLL 61#ifndef EV_USE_EPOLL
57# define HAVE_EPOLL 0 62# define EV_USE_EPOLL 0
58#endif 63#endif
59 64
65#ifndef CLOCK_REALTIME
66# define EV_USE_REALTIME 0
67#endif
60#ifndef HAVE_REALTIME 68#ifndef EV_USE_REALTIME
61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 69# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */
62#endif 70#endif
63 71
64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 72#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
65#define MAX_BLOCKTIME 60. 73#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ 74#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
75#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
67 76
68#include "ev.h" 77#include "ev.h"
69 78
70typedef struct ev_watcher *W; 79typedef struct ev_watcher *W;
71typedef struct ev_watcher_list *WL; 80typedef struct ev_watcher_list *WL;
84/*****************************************************************************/ 93/*****************************************************************************/
85 94
86ev_tstamp 95ev_tstamp
87ev_time (void) 96ev_time (void)
88{ 97{
89#if HAVE_REALTIME 98#if EV_USE_REALTIME
90 struct timespec ts; 99 struct timespec ts;
91 clock_gettime (CLOCK_REALTIME, &ts); 100 clock_gettime (CLOCK_REALTIME, &ts);
92 return ts.tv_sec + ts.tv_nsec * 1e-9; 101 return ts.tv_sec + ts.tv_nsec * 1e-9;
93#else 102#else
94 struct timeval tv; 103 struct timeval tv;
98} 107}
99 108
100static ev_tstamp 109static ev_tstamp
101get_clock (void) 110get_clock (void)
102{ 111{
103#if HAVE_MONOTONIC 112#if EV_USE_MONOTONIC
104 if (have_monotonic) 113 if (have_monotonic)
105 { 114 {
106 struct timespec ts; 115 struct timespec ts;
107 clock_gettime (CLOCK_MONOTONIC, &ts); 116 clock_gettime (CLOCK_MONOTONIC, &ts);
108 return ts.tv_sec + ts.tv_nsec * 1e-9; 117 return ts.tv_sec + ts.tv_nsec * 1e-9;
109 } 118 }
110#endif 119#endif
111 120
112 return ev_time (); 121 return ev_time ();
113} 122}
123
124#define array_roundsize(base,n) ((n) | 4 & ~3)
114 125
115#define array_needsize(base,cur,cnt,init) \ 126#define array_needsize(base,cur,cnt,init) \
116 if ((cnt) > cur) \ 127 if ((cnt) > cur) \
117 { \ 128 { \
118 int newcnt = cur; \ 129 int newcnt = cur; \
119 do \ 130 do \
120 { \ 131 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \ 132 newcnt = array_roundsize (base, newcnt << 1); \
122 } \ 133 } \
123 while ((cnt) > newcnt); \ 134 while ((cnt) > newcnt); \
124 \ 135 \
125 base = realloc (base, sizeof (*base) * (newcnt)); \ 136 base = realloc (base, sizeof (*base) * (newcnt)); \
126 init (base + cur, newcnt - cur); \ 137 init (base + cur, newcnt - cur); \
130/*****************************************************************************/ 141/*****************************************************************************/
131 142
132typedef struct 143typedef struct
133{ 144{
134 struct ev_io *head; 145 struct ev_io *head;
135 unsigned char wev, rev; /* want, received event set */ 146 unsigned char events;
147 unsigned char reify;
136} ANFD; 148} ANFD;
137 149
138static ANFD *anfds; 150static ANFD *anfds;
139static int anfdmax; 151static int anfdmax;
140 152
141static int *fdchanges;
142static int fdchangemax, fdchangecnt;
143
144static void 153static void
145anfds_init (ANFD *base, int count) 154anfds_init (ANFD *base, int count)
146{ 155{
147 while (count--) 156 while (count--)
148 { 157 {
149 base->head = 0; 158 base->head = 0;
150 base->wev = base->rev = EV_NONE; 159 base->events = EV_NONE;
160 base->reify = 0;
161
151 ++base; 162 ++base;
152 } 163 }
153} 164}
154 165
155typedef struct 166typedef struct
162static int pendingmax, pendingcnt; 173static int pendingmax, pendingcnt;
163 174
164static void 175static void
165event (W w, int events) 176event (W w, int events)
166{ 177{
167 if (w->active) 178 if (w->pending)
179 {
180 pendings [w->pending - 1].events |= events;
181 return;
168 { 182 }
183
169 w->pending = ++pendingcnt; 184 w->pending = ++pendingcnt;
170 array_needsize (pendings, pendingmax, pendingcnt, ); 185 array_needsize (pendings, pendingmax, pendingcnt, );
171 pendings [pendingcnt - 1].w = w; 186 pendings [pendingcnt - 1].w = w;
172 pendings [pendingcnt - 1].events = events; 187 pendings [pendingcnt - 1].events = events;
173 } 188}
189
190static void
191queue_events (W *events, int eventcnt, int type)
192{
193 int i;
194
195 for (i = 0; i < eventcnt; ++i)
196 event (events [i], type);
174} 197}
175 198
176static void 199static void
177fd_event (int fd, int events) 200fd_event (int fd, int events)
178{ 201{
186 if (ev) 209 if (ev)
187 event ((W)w, ev); 210 event ((W)w, ev);
188 } 211 }
189} 212}
190 213
214/*****************************************************************************/
215
216static int *fdchanges;
217static int fdchangemax, fdchangecnt;
218
191static void 219static void
192queue_events (W *events, int eventcnt, int type) 220fd_reify (void)
193{ 221{
194 int i; 222 int i;
195 223
196 for (i = 0; i < eventcnt; ++i) 224 for (i = 0; i < fdchangecnt; ++i)
197 event (events [i], type); 225 {
226 int fd = fdchanges [i];
227 ANFD *anfd = anfds + fd;
228 struct ev_io *w;
229
230 int events = 0;
231
232 for (w = anfd->head; w; w = w->next)
233 events |= w->events;
234
235 anfd->reify = 0;
236
237 if (anfd->events != events)
238 {
239 method_modify (fd, anfd->events, events);
240 anfd->events = events;
241 }
242 }
243
244 fdchangecnt = 0;
245}
246
247static void
248fd_change (int fd)
249{
250 if (anfds [fd].reify || fdchangecnt < 0)
251 return;
252
253 anfds [fd].reify = 1;
254
255 ++fdchangecnt;
256 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
257 fdchanges [fdchangecnt - 1] = fd;
198} 258}
199 259
200/* called on EBADF to verify fds */ 260/* called on EBADF to verify fds */
201static void 261static void
202fd_recheck (void) 262fd_recheck (void)
203{ 263{
204 int fd; 264 int fd;
205 265
206 for (fd = 0; fd < anfdmax; ++fd) 266 for (fd = 0; fd < anfdmax; ++fd)
207 if (anfds [fd].wev) 267 if (anfds [fd].events)
208 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 268 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
209 while (anfds [fd].head) 269 while (anfds [fd].head)
210 { 270 {
211 event ((W)anfds [fd].head, EV_ERROR);
212 evio_stop (anfds [fd].head); 271 ev_io_stop (anfds [fd].head);
272 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE);
213 } 273 }
214} 274}
215 275
216/*****************************************************************************/ 276/*****************************************************************************/
217 277
265/*****************************************************************************/ 325/*****************************************************************************/
266 326
267typedef struct 327typedef struct
268{ 328{
269 struct ev_signal *head; 329 struct ev_signal *head;
270 sig_atomic_t gotsig; 330 sig_atomic_t volatile gotsig;
271} ANSIG; 331} ANSIG;
272 332
273static ANSIG *signals; 333static ANSIG *signals;
274static int signalmax; 334static int signalmax;
275 335
276static int sigpipe [2]; 336static int sigpipe [2];
277static sig_atomic_t gotsig; 337static sig_atomic_t volatile gotsig;
278static struct ev_io sigev; 338static struct ev_io sigev;
279 339
280static void 340static void
281signals_init (ANSIG *base, int count) 341signals_init (ANSIG *base, int count)
282{ 342{
283 while (count--) 343 while (count--)
284 { 344 {
285 base->head = 0; 345 base->head = 0;
286 base->gotsig = 0; 346 base->gotsig = 0;
347
287 ++base; 348 ++base;
288 } 349 }
289} 350}
290 351
291static void 352static void
294 signals [signum - 1].gotsig = 1; 355 signals [signum - 1].gotsig = 1;
295 356
296 if (!gotsig) 357 if (!gotsig)
297 { 358 {
298 gotsig = 1; 359 gotsig = 1;
299 write (sigpipe [1], &gotsig, 1); 360 write (sigpipe [1], &signum, 1);
300 } 361 }
301} 362}
302 363
303static void 364static void
304sigcb (struct ev_io *iow, int revents) 365sigcb (struct ev_io *iow, int revents)
305{ 366{
306 struct ev_signal *w; 367 struct ev_signal *w;
307 int sig; 368 int sig;
308 369
370 read (sigpipe [0], &revents, 1);
309 gotsig = 0; 371 gotsig = 0;
310 read (sigpipe [0], &revents, 1);
311 372
312 for (sig = signalmax; sig--; ) 373 for (sig = signalmax; sig--; )
313 if (signals [sig].gotsig) 374 if (signals [sig].gotsig)
314 { 375 {
315 signals [sig].gotsig = 0; 376 signals [sig].gotsig = 0;
327 388
328 /* rather than sort out wether we really need nb, set it */ 389 /* rather than sort out wether we really need nb, set it */
329 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 390 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
330 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 391 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
331 392
332 evio_set (&sigev, sigpipe [0], EV_READ); 393 ev_io_set (&sigev, sigpipe [0], EV_READ);
333 evio_start (&sigev); 394 ev_io_start (&sigev);
334} 395}
335 396
336/*****************************************************************************/ 397/*****************************************************************************/
337 398
338static struct ev_idle **idles; 399static struct ev_idle **idles;
368 } 429 }
369} 430}
370 431
371/*****************************************************************************/ 432/*****************************************************************************/
372 433
373#if HAVE_EPOLL 434#if EV_USE_EPOLL
374# include "ev_epoll.c" 435# include "ev_epoll.c"
375#endif 436#endif
376#if HAVE_SELECT 437#if EV_USE_SELECT
377# include "ev_select.c" 438# include "ev_select.c"
378#endif 439#endif
379 440
380int 441int
381ev_version_major (void) 442ev_version_major (void)
391 452
392int ev_init (int flags) 453int ev_init (int flags)
393{ 454{
394 if (!ev_method) 455 if (!ev_method)
395 { 456 {
396#if HAVE_MONOTONIC 457#if EV_USE_MONOTONIC
397 { 458 {
398 struct timespec ts; 459 struct timespec ts;
399 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 460 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
400 have_monotonic = 1; 461 have_monotonic = 1;
401 } 462 }
407 468
408 if (pipe (sigpipe)) 469 if (pipe (sigpipe))
409 return 0; 470 return 0;
410 471
411 ev_method = EVMETHOD_NONE; 472 ev_method = EVMETHOD_NONE;
412#if HAVE_EPOLL 473#if EV_USE_EPOLL
413 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 474 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
414#endif 475#endif
415#if HAVE_SELECT 476#if EV_USE_SELECT
416 if (ev_method == EVMETHOD_NONE) select_init (flags); 477 if (ev_method == EVMETHOD_NONE) select_init (flags);
417#endif 478#endif
418 479
419 if (ev_method) 480 if (ev_method)
420 { 481 {
421 evw_init (&sigev, sigcb); 482 ev_watcher_init (&sigev, sigcb);
422 siginit (); 483 siginit ();
423 484
424 evsignal_init (&childev, childcb, SIGCHLD); 485 ev_signal_init (&childev, childcb, SIGCHLD);
425 evsignal_start (&childev); 486 ev_signal_start (&childev);
426 } 487 }
427 } 488 }
428 489
429 return ev_method; 490 return ev_method;
430} 491}
431 492
432/*****************************************************************************/ 493/*****************************************************************************/
433 494
434void 495void
435ev_prefork (void) 496ev_fork_prepare (void)
436{ 497{
437 /* nop */ 498 /* nop */
438} 499}
439 500
440void 501void
441ev_postfork_parent (void) 502ev_fork_parent (void)
442{ 503{
443 /* nop */ 504 /* nop */
444} 505}
445 506
446void 507void
447ev_postfork_child (void) 508ev_fork_child (void)
448{ 509{
449#if HAVE_EPOLL 510#if EV_USE_EPOLL
450 if (ev_method == EVMETHOD_EPOLL) 511 if (ev_method == EVMETHOD_EPOLL)
451 epoll_postfork_child (); 512 epoll_postfork_child ();
452#endif 513#endif
453 514
454 evio_stop (&sigev); 515 ev_io_stop (&sigev);
455 close (sigpipe [0]); 516 close (sigpipe [0]);
456 close (sigpipe [1]); 517 close (sigpipe [1]);
457 pipe (sigpipe); 518 pipe (sigpipe);
458 siginit (); 519 siginit ();
459} 520}
460 521
461/*****************************************************************************/ 522/*****************************************************************************/
462 523
463static void 524static void
464fd_reify (void)
465{
466 int i;
467
468 for (i = 0; i < fdchangecnt; ++i)
469 {
470 int fd = fdchanges [i];
471 ANFD *anfd = anfds + fd;
472 struct ev_io *w;
473
474 int wev = 0;
475
476 for (w = anfd->head; w; w = w->next)
477 wev |= w->events;
478
479 if (anfd->wev != wev)
480 {
481 method_modify (fd, anfd->wev, wev);
482 anfd->wev = wev;
483 }
484 }
485
486 fdchangecnt = 0;
487}
488
489static void
490call_pending (void) 525call_pending (void)
491{ 526{
492 while (pendingcnt) 527 while (pendingcnt)
493 { 528 {
494 ANPENDING *p = pendings + --pendingcnt; 529 ANPENDING *p = pendings + --pendingcnt;
506{ 541{
507 while (timercnt && timers [0]->at <= now) 542 while (timercnt && timers [0]->at <= now)
508 { 543 {
509 struct ev_timer *w = timers [0]; 544 struct ev_timer *w = timers [0];
510 545
511 event ((W)w, EV_TIMEOUT);
512
513 /* first reschedule or stop timer */ 546 /* first reschedule or stop timer */
514 if (w->repeat) 547 if (w->repeat)
515 { 548 {
549 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
516 w->at = now + w->repeat; 550 w->at = now + w->repeat;
517 assert (("timer timeout in the past, negative repeat?", w->at > now));
518 downheap ((WT *)timers, timercnt, 0); 551 downheap ((WT *)timers, timercnt, 0);
519 } 552 }
520 else 553 else
521 evtimer_stop (w); /* nonrepeating: stop timer */ 554 ev_timer_stop (w); /* nonrepeating: stop timer */
555
556 event ((W)w, EV_TIMEOUT);
522 } 557 }
523} 558}
524 559
525static void 560static void
526periodics_reify (void) 561periodics_reify (void)
531 566
532 /* first reschedule or stop timer */ 567 /* first reschedule or stop timer */
533 if (w->interval) 568 if (w->interval)
534 { 569 {
535 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 570 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
536 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 571 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
537 downheap ((WT *)periodics, periodiccnt, 0); 572 downheap ((WT *)periodics, periodiccnt, 0);
538 } 573 }
539 else 574 else
540 evperiodic_stop (w); /* nonrepeating: stop timer */ 575 ev_periodic_stop (w); /* nonrepeating: stop timer */
541 576
542 event ((W)w, EV_TIMEOUT); 577 event ((W)w, EV_PERIODIC);
543 } 578 }
544} 579}
545 580
546static void 581static void
547periodics_reschedule (ev_tstamp diff) 582periodics_reschedule (ev_tstamp diff)
557 { 592 {
558 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; 593 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
559 594
560 if (fabs (diff) >= 1e-4) 595 if (fabs (diff) >= 1e-4)
561 { 596 {
562 evperiodic_stop (w); 597 ev_periodic_stop (w);
563 evperiodic_start (w); 598 ev_periodic_start (w);
564 599
565 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 600 i = 0; /* restart loop, inefficient, but time jumps should be rare */
566 } 601 }
567 } 602 }
568 } 603 }
702 head = &(*head)->next; 737 head = &(*head)->next;
703 } 738 }
704} 739}
705 740
706static void 741static void
707ev_clear (W w) 742ev_clear_pending (W w)
708{ 743{
709 if (w->pending) 744 if (w->pending)
710 { 745 {
711 pendings [w->pending - 1].w = 0; 746 pendings [w->pending - 1].w = 0;
712 w->pending = 0; 747 w->pending = 0;
726} 761}
727 762
728/*****************************************************************************/ 763/*****************************************************************************/
729 764
730void 765void
731evio_start (struct ev_io *w) 766ev_io_start (struct ev_io *w)
732{ 767{
733 if (ev_is_active (w)) 768 if (ev_is_active (w))
734 return; 769 return;
735 770
736 int fd = w->fd; 771 int fd = w->fd;
772
773 assert (("ev_io_start called with negative fd", fd >= 0));
737 774
738 ev_start ((W)w, 1); 775 ev_start ((W)w, 1);
739 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 776 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
740 wlist_add ((WL *)&anfds[fd].head, (WL)w); 777 wlist_add ((WL *)&anfds[fd].head, (WL)w);
741 778
742 ++fdchangecnt; 779 fd_change (fd);
743 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
744 fdchanges [fdchangecnt - 1] = fd;
745
746 if (w->fd == 9)
747 printf ("start %p:%x\n", w, w->events);//D
748} 780}
749 781
750void 782void
751evio_stop (struct ev_io *w) 783ev_io_stop (struct ev_io *w)
752{ 784{
753 if (w->fd == 9)
754 printf ("stop %p:%x\n", w, w->events);//D
755 ev_clear ((W)w); 785 ev_clear_pending ((W)w);
756 if (!ev_is_active (w)) 786 if (!ev_is_active (w))
757 return; 787 return;
758 788
759 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 789 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
760 ev_stop ((W)w); 790 ev_stop ((W)w);
761 791
762 ++fdchangecnt; 792 fd_change (w->fd);
763 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
764 fdchanges [fdchangecnt - 1] = w->fd;
765} 793}
766 794
767void 795void
768evtimer_start (struct ev_timer *w) 796ev_timer_start (struct ev_timer *w)
769{ 797{
770 if (ev_is_active (w)) 798 if (ev_is_active (w))
771 return; 799 return;
772 800
773 w->at += now; 801 w->at += now;
774 802
775 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 803 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
776 804
777 ev_start ((W)w, ++timercnt); 805 ev_start ((W)w, ++timercnt);
778 array_needsize (timers, timermax, timercnt, ); 806 array_needsize (timers, timermax, timercnt, );
779 timers [timercnt - 1] = w; 807 timers [timercnt - 1] = w;
780 upheap ((WT *)timers, timercnt - 1); 808 upheap ((WT *)timers, timercnt - 1);
781} 809}
782 810
783void 811void
784evtimer_stop (struct ev_timer *w) 812ev_timer_stop (struct ev_timer *w)
785{ 813{
786 ev_clear ((W)w); 814 ev_clear_pending ((W)w);
787 if (!ev_is_active (w)) 815 if (!ev_is_active (w))
788 return; 816 return;
789 817
790 if (w->active < timercnt--) 818 if (w->active < timercnt--)
791 { 819 {
797 825
798 ev_stop ((W)w); 826 ev_stop ((W)w);
799} 827}
800 828
801void 829void
802evtimer_again (struct ev_timer *w) 830ev_timer_again (struct ev_timer *w)
803{ 831{
804 if (ev_is_active (w)) 832 if (ev_is_active (w))
805 { 833 {
806 if (w->repeat) 834 if (w->repeat)
807 { 835 {
808 w->at = now + w->repeat; 836 w->at = now + w->repeat;
809 downheap ((WT *)timers, timercnt, w->active - 1); 837 downheap ((WT *)timers, timercnt, w->active - 1);
810 } 838 }
811 else 839 else
812 evtimer_stop (w); 840 ev_timer_stop (w);
813 } 841 }
814 else if (w->repeat) 842 else if (w->repeat)
815 evtimer_start (w); 843 ev_timer_start (w);
816} 844}
817 845
818void 846void
819evperiodic_start (struct ev_periodic *w) 847ev_periodic_start (struct ev_periodic *w)
820{ 848{
821 if (ev_is_active (w)) 849 if (ev_is_active (w))
822 return; 850 return;
823 851
824 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 852 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
825 853
826 /* this formula differs from the one in periodic_reify because we do not always round up */ 854 /* this formula differs from the one in periodic_reify because we do not always round up */
827 if (w->interval) 855 if (w->interval)
828 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 856 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
829 857
832 periodics [periodiccnt - 1] = w; 860 periodics [periodiccnt - 1] = w;
833 upheap ((WT *)periodics, periodiccnt - 1); 861 upheap ((WT *)periodics, periodiccnt - 1);
834} 862}
835 863
836void 864void
837evperiodic_stop (struct ev_periodic *w) 865ev_periodic_stop (struct ev_periodic *w)
838{ 866{
839 ev_clear ((W)w); 867 ev_clear_pending ((W)w);
840 if (!ev_is_active (w)) 868 if (!ev_is_active (w))
841 return; 869 return;
842 870
843 if (w->active < periodiccnt--) 871 if (w->active < periodiccnt--)
844 { 872 {
848 876
849 ev_stop ((W)w); 877 ev_stop ((W)w);
850} 878}
851 879
852void 880void
853evsignal_start (struct ev_signal *w) 881ev_signal_start (struct ev_signal *w)
854{ 882{
855 if (ev_is_active (w)) 883 if (ev_is_active (w))
856 return; 884 return;
885
886 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
857 887
858 ev_start ((W)w, 1); 888 ev_start ((W)w, 1);
859 array_needsize (signals, signalmax, w->signum, signals_init); 889 array_needsize (signals, signalmax, w->signum, signals_init);
860 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 890 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
861 891
868 sigaction (w->signum, &sa, 0); 898 sigaction (w->signum, &sa, 0);
869 } 899 }
870} 900}
871 901
872void 902void
873evsignal_stop (struct ev_signal *w) 903ev_signal_stop (struct ev_signal *w)
874{ 904{
875 ev_clear ((W)w); 905 ev_clear_pending ((W)w);
876 if (!ev_is_active (w)) 906 if (!ev_is_active (w))
877 return; 907 return;
878 908
879 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 909 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
880 ev_stop ((W)w); 910 ev_stop ((W)w);
881 911
882 if (!signals [w->signum - 1].head) 912 if (!signals [w->signum - 1].head)
883 signal (w->signum, SIG_DFL); 913 signal (w->signum, SIG_DFL);
884} 914}
885 915
916void
886void evidle_start (struct ev_idle *w) 917ev_idle_start (struct ev_idle *w)
887{ 918{
888 if (ev_is_active (w)) 919 if (ev_is_active (w))
889 return; 920 return;
890 921
891 ev_start ((W)w, ++idlecnt); 922 ev_start ((W)w, ++idlecnt);
892 array_needsize (idles, idlemax, idlecnt, ); 923 array_needsize (idles, idlemax, idlecnt, );
893 idles [idlecnt - 1] = w; 924 idles [idlecnt - 1] = w;
894} 925}
895 926
927void
896void evidle_stop (struct ev_idle *w) 928ev_idle_stop (struct ev_idle *w)
897{ 929{
898 ev_clear ((W)w); 930 ev_clear_pending ((W)w);
899 if (ev_is_active (w)) 931 if (ev_is_active (w))
900 return; 932 return;
901 933
902 idles [w->active - 1] = idles [--idlecnt]; 934 idles [w->active - 1] = idles [--idlecnt];
903 ev_stop ((W)w); 935 ev_stop ((W)w);
904} 936}
905 937
938void
906void evprepare_start (struct ev_prepare *w) 939ev_prepare_start (struct ev_prepare *w)
907{ 940{
908 if (ev_is_active (w)) 941 if (ev_is_active (w))
909 return; 942 return;
910 943
911 ev_start ((W)w, ++preparecnt); 944 ev_start ((W)w, ++preparecnt);
912 array_needsize (prepares, preparemax, preparecnt, ); 945 array_needsize (prepares, preparemax, preparecnt, );
913 prepares [preparecnt - 1] = w; 946 prepares [preparecnt - 1] = w;
914} 947}
915 948
949void
916void evprepare_stop (struct ev_prepare *w) 950ev_prepare_stop (struct ev_prepare *w)
917{ 951{
918 ev_clear ((W)w); 952 ev_clear_pending ((W)w);
919 if (ev_is_active (w)) 953 if (ev_is_active (w))
920 return; 954 return;
921 955
922 prepares [w->active - 1] = prepares [--preparecnt]; 956 prepares [w->active - 1] = prepares [--preparecnt];
923 ev_stop ((W)w); 957 ev_stop ((W)w);
924} 958}
925 959
960void
926void evcheck_start (struct ev_check *w) 961ev_check_start (struct ev_check *w)
927{ 962{
928 if (ev_is_active (w)) 963 if (ev_is_active (w))
929 return; 964 return;
930 965
931 ev_start ((W)w, ++checkcnt); 966 ev_start ((W)w, ++checkcnt);
932 array_needsize (checks, checkmax, checkcnt, ); 967 array_needsize (checks, checkmax, checkcnt, );
933 checks [checkcnt - 1] = w; 968 checks [checkcnt - 1] = w;
934} 969}
935 970
971void
936void evcheck_stop (struct ev_check *w) 972ev_check_stop (struct ev_check *w)
937{ 973{
938 ev_clear ((W)w); 974 ev_clear_pending ((W)w);
939 if (ev_is_active (w)) 975 if (ev_is_active (w))
940 return; 976 return;
941 977
942 checks [w->active - 1] = checks [--checkcnt]; 978 checks [w->active - 1] = checks [--checkcnt];
943 ev_stop ((W)w); 979 ev_stop ((W)w);
944} 980}
945 981
982void
946void evchild_start (struct ev_child *w) 983ev_child_start (struct ev_child *w)
947{ 984{
948 if (ev_is_active (w)) 985 if (ev_is_active (w))
949 return; 986 return;
950 987
951 ev_start ((W)w, 1); 988 ev_start ((W)w, 1);
952 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 989 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
953} 990}
954 991
992void
955void evchild_stop (struct ev_child *w) 993ev_child_stop (struct ev_child *w)
956{ 994{
957 ev_clear ((W)w); 995 ev_clear_pending ((W)w);
958 if (ev_is_active (w)) 996 if (ev_is_active (w))
959 return; 997 return;
960 998
961 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 999 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
962 ev_stop ((W)w); 1000 ev_stop ((W)w);
976once_cb (struct ev_once *once, int revents) 1014once_cb (struct ev_once *once, int revents)
977{ 1015{
978 void (*cb)(int revents, void *arg) = once->cb; 1016 void (*cb)(int revents, void *arg) = once->cb;
979 void *arg = once->arg; 1017 void *arg = once->arg;
980 1018
981 evio_stop (&once->io); 1019 ev_io_stop (&once->io);
982 evtimer_stop (&once->to); 1020 ev_timer_stop (&once->to);
983 free (once); 1021 free (once);
984 1022
985 cb (revents, arg); 1023 cb (revents, arg);
986} 1024}
987 1025
1001ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1039ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1002{ 1040{
1003 struct ev_once *once = malloc (sizeof (struct ev_once)); 1041 struct ev_once *once = malloc (sizeof (struct ev_once));
1004 1042
1005 if (!once) 1043 if (!once)
1006 cb (EV_ERROR, arg); 1044 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1007 else 1045 else
1008 { 1046 {
1009 once->cb = cb; 1047 once->cb = cb;
1010 once->arg = arg; 1048 once->arg = arg;
1011 1049
1012 evw_init (&once->io, once_cb_io); 1050 ev_watcher_init (&once->io, once_cb_io);
1013
1014 if (fd >= 0) 1051 if (fd >= 0)
1015 { 1052 {
1016 evio_set (&once->io, fd, events); 1053 ev_io_set (&once->io, fd, events);
1017 evio_start (&once->io); 1054 ev_io_start (&once->io);
1018 } 1055 }
1019 1056
1020 evw_init (&once->to, once_cb_to); 1057 ev_watcher_init (&once->to, once_cb_to);
1021
1022 if (timeout >= 0.) 1058 if (timeout >= 0.)
1023 { 1059 {
1024 evtimer_set (&once->to, timeout, 0.); 1060 ev_timer_set (&once->to, timeout, 0.);
1025 evtimer_start (&once->to); 1061 ev_timer_start (&once->to);
1026 } 1062 }
1027 } 1063 }
1028} 1064}
1029 1065
1030/*****************************************************************************/ 1066/*****************************************************************************/
1041 1077
1042static void 1078static void
1043ocb (struct ev_timer *w, int revents) 1079ocb (struct ev_timer *w, int revents)
1044{ 1080{
1045 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 1081 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1046 evtimer_stop (w); 1082 ev_timer_stop (w);
1047 evtimer_start (w); 1083 ev_timer_start (w);
1048} 1084}
1049 1085
1050static void 1086static void
1051scb (struct ev_signal *w, int revents) 1087scb (struct ev_signal *w, int revents)
1052{ 1088{
1053 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1089 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1054 evio_stop (&wio); 1090 ev_io_stop (&wio);
1055 evio_start (&wio); 1091 ev_io_start (&wio);
1056} 1092}
1057 1093
1058static void 1094static void
1059gcb (struct ev_signal *w, int revents) 1095gcb (struct ev_signal *w, int revents)
1060{ 1096{
1064 1100
1065int main (void) 1101int main (void)
1066{ 1102{
1067 ev_init (0); 1103 ev_init (0);
1068 1104
1069 evio_init (&wio, sin_cb, 0, EV_READ); 1105 ev_io_init (&wio, sin_cb, 0, EV_READ);
1070 evio_start (&wio); 1106 ev_io_start (&wio);
1071 1107
1072 struct ev_timer t[10000]; 1108 struct ev_timer t[10000];
1073 1109
1074#if 0 1110#if 0
1075 int i; 1111 int i;
1076 for (i = 0; i < 10000; ++i) 1112 for (i = 0; i < 10000; ++i)
1077 { 1113 {
1078 struct ev_timer *w = t + i; 1114 struct ev_timer *w = t + i;
1079 evw_init (w, ocb, i); 1115 ev_watcher_init (w, ocb, i);
1080 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1116 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1081 evtimer_start (w); 1117 ev_timer_start (w);
1082 if (drand48 () < 0.5) 1118 if (drand48 () < 0.5)
1083 evtimer_stop (w); 1119 ev_timer_stop (w);
1084 } 1120 }
1085#endif 1121#endif
1086 1122
1087 struct ev_timer t1; 1123 struct ev_timer t1;
1088 evtimer_init (&t1, ocb, 5, 10); 1124 ev_timer_init (&t1, ocb, 5, 10);
1089 evtimer_start (&t1); 1125 ev_timer_start (&t1);
1090 1126
1091 struct ev_signal sig; 1127 struct ev_signal sig;
1092 evsignal_init (&sig, scb, SIGQUIT); 1128 ev_signal_init (&sig, scb, SIGQUIT);
1093 evsignal_start (&sig); 1129 ev_signal_start (&sig);
1094 1130
1095 struct ev_check cw; 1131 struct ev_check cw;
1096 evcheck_init (&cw, gcb); 1132 ev_check_init (&cw, gcb);
1097 evcheck_start (&cw); 1133 ev_check_start (&cw);
1098 1134
1099 struct ev_idle iw; 1135 struct ev_idle iw;
1100 evidle_init (&iw, gcb); 1136 ev_idle_init (&iw, gcb);
1101 evidle_start (&iw); 1137 ev_idle_start (&iw);
1102 1138
1103 ev_loop (0); 1139 ev_loop (0);
1104 1140
1105 return 0; 1141 return 0;
1106} 1142}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines