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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines