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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines