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.39 by root, Thu Nov 1 17:17:32 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;
109 } 121 }
110#endif 122#endif
111 123
112 return ev_time (); 124 return ev_time ();
113} 125}
126
127#define array_roundsize(base,n) ((n) | 4 & ~3)
114 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; \ 132 int newcnt = cur; \
119 do \ 133 do \
120 { \ 134 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \ 135 newcnt = array_roundsize (base, newcnt << 1); \
122 } \ 136 } \
123 while ((cnt) > newcnt); \ 137 while ((cnt) > newcnt); \
124 \ 138 \
125 base = realloc (base, sizeof (*base) * (newcnt)); \ 139 base = realloc (base, sizeof (*base) * (newcnt)); \
126 init (base + cur, newcnt - cur); \ 140 init (base + cur, newcnt - cur); \
130/*****************************************************************************/ 144/*****************************************************************************/
131 145
132typedef struct 146typedef struct
133{ 147{
134 struct ev_io *head; 148 struct ev_io *head;
135 unsigned char wev, rev; /* want, received event set */ 149 unsigned char events;
150 unsigned char reify;
136} ANFD; 151} ANFD;
137 152
138static ANFD *anfds; 153static ANFD *anfds;
139static int anfdmax; 154static int anfdmax;
140 155
141static int *fdchanges;
142static int fdchangemax, fdchangecnt;
143
144static void 156static void
145anfds_init (ANFD *base, int count) 157anfds_init (ANFD *base, int count)
146{ 158{
147 while (count--) 159 while (count--)
148 { 160 {
149 base->head = 0; 161 base->head = 0;
150 base->wev = base->rev = EV_NONE; 162 base->events = EV_NONE;
163 base->reify = 0;
164
151 ++base; 165 ++base;
152 } 166 }
153} 167}
154 168
155typedef struct 169typedef struct
162static int pendingmax, pendingcnt; 176static int pendingmax, pendingcnt;
163 177
164static void 178static void
165event (W w, int events) 179event (W w, int events)
166{ 180{
167 if (w->active) 181 if (w->pending)
182 {
183 pendings [w->pending - 1].events |= events;
184 return;
168 { 185 }
186
169 w->pending = ++pendingcnt; 187 w->pending = ++pendingcnt;
170 array_needsize (pendings, pendingmax, pendingcnt, ); 188 array_needsize (pendings, pendingmax, pendingcnt, );
171 pendings [pendingcnt - 1].w = w; 189 pendings [pendingcnt - 1].w = w;
172 pendings [pendingcnt - 1].events = events; 190 pendings [pendingcnt - 1].events = events;
173 } 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);
174} 200}
175 201
176static void 202static void
177fd_event (int fd, int events) 203fd_event (int fd, int events)
178{ 204{
186 if (ev) 212 if (ev)
187 event ((W)w, ev); 213 event ((W)w, ev);
188 } 214 }
189} 215}
190 216
217/*****************************************************************************/
218
219static int *fdchanges;
220static int fdchangemax, fdchangecnt;
221
191static void 222static void
192queue_events (W *events, int eventcnt, int type) 223fd_reify (void)
193{ 224{
194 int i; 225 int i;
195 226
196 for (i = 0; i < eventcnt; ++i) 227 for (i = 0; i < fdchangecnt; ++i)
197 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;
198} 261}
199 262
200/* called on EBADF to verify fds */ 263/* called on EBADF to verify fds */
201static void 264static void
202fd_recheck (void) 265fd_recheck (void)
203{ 266{
204 int fd; 267 int fd;
205 268
206 for (fd = 0; fd < anfdmax; ++fd) 269 for (fd = 0; fd < anfdmax; ++fd)
207 if (anfds [fd].wev) 270 if (anfds [fd].events)
208 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 271 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
209 while (anfds [fd].head) 272 while (anfds [fd].head)
210 { 273 {
211 event ((W)anfds [fd].head, EV_ERROR);
212 evio_stop (anfds [fd].head); 274 ev_io_stop (anfds [fd].head);
275 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE);
213 } 276 }
214} 277}
215 278
216/*****************************************************************************/ 279/*****************************************************************************/
217 280
265/*****************************************************************************/ 328/*****************************************************************************/
266 329
267typedef struct 330typedef struct
268{ 331{
269 struct ev_signal *head; 332 struct ev_signal *head;
270 sig_atomic_t gotsig; 333 sig_atomic_t volatile gotsig;
271} ANSIG; 334} ANSIG;
272 335
273static ANSIG *signals; 336static ANSIG *signals;
274static int signalmax; 337static int signalmax;
275 338
276static int sigpipe [2]; 339static int sigpipe [2];
277static sig_atomic_t gotsig; 340static sig_atomic_t volatile gotsig;
278static struct ev_io sigev; 341static struct ev_io sigev;
279 342
280static void 343static void
281signals_init (ANSIG *base, int count) 344signals_init (ANSIG *base, int count)
282{ 345{
283 while (count--) 346 while (count--)
284 { 347 {
285 base->head = 0; 348 base->head = 0;
286 base->gotsig = 0; 349 base->gotsig = 0;
350
287 ++base; 351 ++base;
288 } 352 }
289} 353}
290 354
291static void 355static void
294 signals [signum - 1].gotsig = 1; 358 signals [signum - 1].gotsig = 1;
295 359
296 if (!gotsig) 360 if (!gotsig)
297 { 361 {
298 gotsig = 1; 362 gotsig = 1;
299 write (sigpipe [1], &gotsig, 1); 363 write (sigpipe [1], &signum, 1);
300 } 364 }
301} 365}
302 366
303static void 367static void
304sigcb (struct ev_io *iow, int revents) 368sigcb (struct ev_io *iow, int revents)
305{ 369{
306 struct ev_signal *w; 370 struct ev_signal *w;
307 int sig; 371 int signum;
308 372
373 read (sigpipe [0], &revents, 1);
309 gotsig = 0; 374 gotsig = 0;
310 read (sigpipe [0], &revents, 1);
311 375
312 for (sig = signalmax; sig--; ) 376 for (signum = signalmax; signum--; )
313 if (signals [sig].gotsig) 377 if (signals [signum].gotsig)
314 { 378 {
315 signals [sig].gotsig = 0; 379 signals [signum].gotsig = 0;
316 380
317 for (w = signals [sig].head; w; w = w->next) 381 for (w = signals [signum].head; w; w = w->next)
318 event ((W)w, EV_SIGNAL); 382 event ((W)w, EV_SIGNAL);
319 } 383 }
320} 384}
321 385
322static void 386static void
327 391
328 /* rather than sort out wether we really need nb, set it */ 392 /* rather than sort out wether we really need nb, set it */
329 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 393 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
330 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 394 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
331 395
332 evio_set (&sigev, sigpipe [0], EV_READ); 396 ev_io_set (&sigev, sigpipe [0], EV_READ);
333 evio_start (&sigev); 397 ev_io_start (&sigev);
334} 398}
335 399
336/*****************************************************************************/ 400/*****************************************************************************/
337 401
338static struct ev_idle **idles; 402static struct ev_idle **idles;
359 struct ev_child *w; 423 struct ev_child *w;
360 int pid, status; 424 int pid, status;
361 425
362 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 426 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
363 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 427 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
364 if (w->pid == pid || w->pid == -1) 428 if (w->pid == pid || !w->pid)
365 { 429 {
366 w->status = status; 430 w->status = status;
367 event ((W)w, EV_CHILD); 431 event ((W)w, EV_CHILD);
368 } 432 }
369} 433}
370 434
371/*****************************************************************************/ 435/*****************************************************************************/
372 436
373#if HAVE_EPOLL 437#if EV_USE_EPOLL
374# include "ev_epoll.c" 438# include "ev_epoll.c"
375#endif 439#endif
376#if HAVE_SELECT 440#if EV_USE_SELECT
377# include "ev_select.c" 441# include "ev_select.c"
378#endif 442#endif
379 443
380int 444int
381ev_version_major (void) 445ev_version_major (void)
391 455
392int ev_init (int flags) 456int ev_init (int flags)
393{ 457{
394 if (!ev_method) 458 if (!ev_method)
395 { 459 {
396#if HAVE_MONOTONIC 460#if EV_USE_MONOTONIC
397 { 461 {
398 struct timespec ts; 462 struct timespec ts;
399 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 463 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
400 have_monotonic = 1; 464 have_monotonic = 1;
401 } 465 }
407 471
408 if (pipe (sigpipe)) 472 if (pipe (sigpipe))
409 return 0; 473 return 0;
410 474
411 ev_method = EVMETHOD_NONE; 475 ev_method = EVMETHOD_NONE;
412#if HAVE_EPOLL 476#if EV_USE_EPOLL
413 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 477 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
414#endif 478#endif
415#if HAVE_SELECT 479#if EV_USE_SELECT
416 if (ev_method == EVMETHOD_NONE) select_init (flags); 480 if (ev_method == EVMETHOD_NONE) select_init (flags);
417#endif 481#endif
418 482
419 if (ev_method) 483 if (ev_method)
420 { 484 {
421 evw_init (&sigev, sigcb); 485 ev_watcher_init (&sigev, sigcb);
422 siginit (); 486 siginit ();
423 487
424 evsignal_init (&childev, childcb, SIGCHLD); 488 ev_signal_init (&childev, childcb, SIGCHLD);
425 evsignal_start (&childev); 489 ev_signal_start (&childev);
426 } 490 }
427 } 491 }
428 492
429 return ev_method; 493 return ev_method;
430} 494}
431 495
432/*****************************************************************************/ 496/*****************************************************************************/
433 497
434void 498void
435ev_prefork (void) 499ev_fork_prepare (void)
436{ 500{
437 /* nop */ 501 /* nop */
438} 502}
439 503
440void 504void
441ev_postfork_parent (void) 505ev_fork_parent (void)
442{ 506{
443 /* nop */ 507 /* nop */
444} 508}
445 509
446void 510void
447ev_postfork_child (void) 511ev_fork_child (void)
448{ 512{
449#if HAVE_EPOLL 513#if EV_USE_EPOLL
450 if (ev_method == EVMETHOD_EPOLL) 514 if (ev_method == EVMETHOD_EPOLL)
451 epoll_postfork_child (); 515 epoll_postfork_child ();
452#endif 516#endif
453 517
454 evio_stop (&sigev); 518 ev_io_stop (&sigev);
455 close (sigpipe [0]); 519 close (sigpipe [0]);
456 close (sigpipe [1]); 520 close (sigpipe [1]);
457 pipe (sigpipe); 521 pipe (sigpipe);
458 siginit (); 522 siginit ();
459} 523}
460 524
461/*****************************************************************************/ 525/*****************************************************************************/
462 526
463static void 527static 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) 528call_pending (void)
491{ 529{
492 while (pendingcnt) 530 while (pendingcnt)
493 { 531 {
494 ANPENDING *p = pendings + --pendingcnt; 532 ANPENDING *p = pendings + --pendingcnt;
506{ 544{
507 while (timercnt && timers [0]->at <= now) 545 while (timercnt && timers [0]->at <= now)
508 { 546 {
509 struct ev_timer *w = timers [0]; 547 struct ev_timer *w = timers [0];
510 548
511 event ((W)w, EV_TIMEOUT);
512
513 /* first reschedule or stop timer */ 549 /* first reschedule or stop timer */
514 if (w->repeat) 550 if (w->repeat)
515 { 551 {
552 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
516 w->at = now + w->repeat; 553 w->at = now + w->repeat;
517 assert (("timer timeout in the past, negative repeat?", w->at > now));
518 downheap ((WT *)timers, timercnt, 0); 554 downheap ((WT *)timers, timercnt, 0);
519 } 555 }
520 else 556 else
521 evtimer_stop (w); /* nonrepeating: stop timer */ 557 ev_timer_stop (w); /* nonrepeating: stop timer */
558
559 event ((W)w, EV_TIMEOUT);
522 } 560 }
523} 561}
524 562
525static void 563static void
526periodics_reify (void) 564periodics_reify (void)
531 569
532 /* first reschedule or stop timer */ 570 /* first reschedule or stop timer */
533 if (w->interval) 571 if (w->interval)
534 { 572 {
535 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;
536 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));
537 downheap ((WT *)periodics, periodiccnt, 0); 575 downheap ((WT *)periodics, periodiccnt, 0);
538 } 576 }
539 else 577 else
540 evperiodic_stop (w); /* nonrepeating: stop timer */ 578 ev_periodic_stop (w); /* nonrepeating: stop timer */
541 579
542 event ((W)w, EV_TIMEOUT); 580 event ((W)w, EV_PERIODIC);
543 } 581 }
544} 582}
545 583
546static void 584static void
547periodics_reschedule (ev_tstamp diff) 585periodics_reschedule (ev_tstamp diff)
557 { 595 {
558 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;
559 597
560 if (fabs (diff) >= 1e-4) 598 if (fabs (diff) >= 1e-4)
561 { 599 {
562 evperiodic_stop (w); 600 ev_periodic_stop (w);
563 evperiodic_start (w); 601 ev_periodic_start (w);
564 602
565 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 603 i = 0; /* restart loop, inefficient, but time jumps should be rare */
566 } 604 }
567 } 605 }
568 } 606 }
702 head = &(*head)->next; 740 head = &(*head)->next;
703 } 741 }
704} 742}
705 743
706static void 744static void
707ev_clear (W w) 745ev_clear_pending (W w)
708{ 746{
709 if (w->pending) 747 if (w->pending)
710 { 748 {
711 pendings [w->pending - 1].w = 0; 749 pendings [w->pending - 1].w = 0;
712 w->pending = 0; 750 w->pending = 0;
726} 764}
727 765
728/*****************************************************************************/ 766/*****************************************************************************/
729 767
730void 768void
731evio_start (struct ev_io *w) 769ev_io_start (struct ev_io *w)
732{ 770{
733 if (ev_is_active (w))
734 return;
735
736 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));
737 777
738 ev_start ((W)w, 1); 778 ev_start ((W)w, 1);
739 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 779 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
740 wlist_add ((WL *)&anfds[fd].head, (WL)w); 780 wlist_add ((WL *)&anfds[fd].head, (WL)w);
741 781
742 ++fdchangecnt; 782 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} 783}
749 784
750void 785void
751evio_stop (struct ev_io *w) 786ev_io_stop (struct ev_io *w)
752{ 787{
753 if (w->fd == 9)
754 printf ("stop %p:%x\n", w, w->events);//D
755 ev_clear ((W)w); 788 ev_clear_pending ((W)w);
756 if (!ev_is_active (w)) 789 if (!ev_is_active (w))
757 return; 790 return;
758 791
759 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 792 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
760 ev_stop ((W)w); 793 ev_stop ((W)w);
761 794
762 ++fdchangecnt; 795 fd_change (w->fd);
763 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
764 fdchanges [fdchangecnt - 1] = w->fd;
765} 796}
766 797
767void 798void
768evtimer_start (struct ev_timer *w) 799ev_timer_start (struct ev_timer *w)
769{ 800{
770 if (ev_is_active (w)) 801 if (ev_is_active (w))
771 return; 802 return;
772 803
773 w->at += now; 804 w->at += now;
774 805
775 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.));
776 807
777 ev_start ((W)w, ++timercnt); 808 ev_start ((W)w, ++timercnt);
778 array_needsize (timers, timermax, timercnt, ); 809 array_needsize (timers, timermax, timercnt, );
779 timers [timercnt - 1] = w; 810 timers [timercnt - 1] = w;
780 upheap ((WT *)timers, timercnt - 1); 811 upheap ((WT *)timers, timercnt - 1);
781} 812}
782 813
783void 814void
784evtimer_stop (struct ev_timer *w) 815ev_timer_stop (struct ev_timer *w)
785{ 816{
786 ev_clear ((W)w); 817 ev_clear_pending ((W)w);
787 if (!ev_is_active (w)) 818 if (!ev_is_active (w))
788 return; 819 return;
789 820
790 if (w->active < timercnt--) 821 if (w->active < timercnt--)
791 { 822 {
797 828
798 ev_stop ((W)w); 829 ev_stop ((W)w);
799} 830}
800 831
801void 832void
802evtimer_again (struct ev_timer *w) 833ev_timer_again (struct ev_timer *w)
803{ 834{
804 if (ev_is_active (w)) 835 if (ev_is_active (w))
805 { 836 {
806 if (w->repeat) 837 if (w->repeat)
807 { 838 {
808 w->at = now + w->repeat; 839 w->at = now + w->repeat;
809 downheap ((WT *)timers, timercnt, w->active - 1); 840 downheap ((WT *)timers, timercnt, w->active - 1);
810 } 841 }
811 else 842 else
812 evtimer_stop (w); 843 ev_timer_stop (w);
813 } 844 }
814 else if (w->repeat) 845 else if (w->repeat)
815 evtimer_start (w); 846 ev_timer_start (w);
816} 847}
817 848
818void 849void
819evperiodic_start (struct ev_periodic *w) 850ev_periodic_start (struct ev_periodic *w)
820{ 851{
821 if (ev_is_active (w)) 852 if (ev_is_active (w))
822 return; 853 return;
823 854
824 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.));
825 856
826 /* 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 */
827 if (w->interval) 858 if (w->interval)
828 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 859 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
829 860
832 periodics [periodiccnt - 1] = w; 863 periodics [periodiccnt - 1] = w;
833 upheap ((WT *)periodics, periodiccnt - 1); 864 upheap ((WT *)periodics, periodiccnt - 1);
834} 865}
835 866
836void 867void
837evperiodic_stop (struct ev_periodic *w) 868ev_periodic_stop (struct ev_periodic *w)
838{ 869{
839 ev_clear ((W)w); 870 ev_clear_pending ((W)w);
840 if (!ev_is_active (w)) 871 if (!ev_is_active (w))
841 return; 872 return;
842 873
843 if (w->active < periodiccnt--) 874 if (w->active < periodiccnt--)
844 { 875 {
848 879
849 ev_stop ((W)w); 880 ev_stop ((W)w);
850} 881}
851 882
852void 883void
853evsignal_start (struct ev_signal *w) 884ev_signal_start (struct ev_signal *w)
854{ 885{
855 if (ev_is_active (w)) 886 if (ev_is_active (w))
856 return; 887 return;
888
889 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
857 890
858 ev_start ((W)w, 1); 891 ev_start ((W)w, 1);
859 array_needsize (signals, signalmax, w->signum, signals_init); 892 array_needsize (signals, signalmax, w->signum, signals_init);
860 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 893 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
861 894
868 sigaction (w->signum, &sa, 0); 901 sigaction (w->signum, &sa, 0);
869 } 902 }
870} 903}
871 904
872void 905void
873evsignal_stop (struct ev_signal *w) 906ev_signal_stop (struct ev_signal *w)
874{ 907{
875 ev_clear ((W)w); 908 ev_clear_pending ((W)w);
876 if (!ev_is_active (w)) 909 if (!ev_is_active (w))
877 return; 910 return;
878 911
879 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 912 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
880 ev_stop ((W)w); 913 ev_stop ((W)w);
881 914
882 if (!signals [w->signum - 1].head) 915 if (!signals [w->signum - 1].head)
883 signal (w->signum, SIG_DFL); 916 signal (w->signum, SIG_DFL);
884} 917}
885 918
919void
886void evidle_start (struct ev_idle *w) 920ev_idle_start (struct ev_idle *w)
887{ 921{
888 if (ev_is_active (w)) 922 if (ev_is_active (w))
889 return; 923 return;
890 924
891 ev_start ((W)w, ++idlecnt); 925 ev_start ((W)w, ++idlecnt);
892 array_needsize (idles, idlemax, idlecnt, ); 926 array_needsize (idles, idlemax, idlecnt, );
893 idles [idlecnt - 1] = w; 927 idles [idlecnt - 1] = w;
894} 928}
895 929
930void
896void evidle_stop (struct ev_idle *w) 931ev_idle_stop (struct ev_idle *w)
897{ 932{
898 ev_clear ((W)w); 933 ev_clear_pending ((W)w);
899 if (ev_is_active (w)) 934 if (ev_is_active (w))
900 return; 935 return;
901 936
902 idles [w->active - 1] = idles [--idlecnt]; 937 idles [w->active - 1] = idles [--idlecnt];
903 ev_stop ((W)w); 938 ev_stop ((W)w);
904} 939}
905 940
941void
906void evprepare_start (struct ev_prepare *w) 942ev_prepare_start (struct ev_prepare *w)
907{ 943{
908 if (ev_is_active (w)) 944 if (ev_is_active (w))
909 return; 945 return;
910 946
911 ev_start ((W)w, ++preparecnt); 947 ev_start ((W)w, ++preparecnt);
912 array_needsize (prepares, preparemax, preparecnt, ); 948 array_needsize (prepares, preparemax, preparecnt, );
913 prepares [preparecnt - 1] = w; 949 prepares [preparecnt - 1] = w;
914} 950}
915 951
952void
916void evprepare_stop (struct ev_prepare *w) 953ev_prepare_stop (struct ev_prepare *w)
917{ 954{
918 ev_clear ((W)w); 955 ev_clear_pending ((W)w);
919 if (ev_is_active (w)) 956 if (ev_is_active (w))
920 return; 957 return;
921 958
922 prepares [w->active - 1] = prepares [--preparecnt]; 959 prepares [w->active - 1] = prepares [--preparecnt];
923 ev_stop ((W)w); 960 ev_stop ((W)w);
924} 961}
925 962
963void
926void evcheck_start (struct ev_check *w) 964ev_check_start (struct ev_check *w)
927{ 965{
928 if (ev_is_active (w)) 966 if (ev_is_active (w))
929 return; 967 return;
930 968
931 ev_start ((W)w, ++checkcnt); 969 ev_start ((W)w, ++checkcnt);
932 array_needsize (checks, checkmax, checkcnt, ); 970 array_needsize (checks, checkmax, checkcnt, );
933 checks [checkcnt - 1] = w; 971 checks [checkcnt - 1] = w;
934} 972}
935 973
974void
936void evcheck_stop (struct ev_check *w) 975ev_check_stop (struct ev_check *w)
937{ 976{
938 ev_clear ((W)w); 977 ev_clear_pending ((W)w);
939 if (ev_is_active (w)) 978 if (ev_is_active (w))
940 return; 979 return;
941 980
942 checks [w->active - 1] = checks [--checkcnt]; 981 checks [w->active - 1] = checks [--checkcnt];
943 ev_stop ((W)w); 982 ev_stop ((W)w);
944} 983}
945 984
985void
946void evchild_start (struct ev_child *w) 986ev_child_start (struct ev_child *w)
947{ 987{
948 if (ev_is_active (w)) 988 if (ev_is_active (w))
949 return; 989 return;
950 990
951 ev_start ((W)w, 1); 991 ev_start ((W)w, 1);
952 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 992 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
953} 993}
954 994
995void
955void evchild_stop (struct ev_child *w) 996ev_child_stop (struct ev_child *w)
956{ 997{
957 ev_clear ((W)w); 998 ev_clear_pending ((W)w);
958 if (ev_is_active (w)) 999 if (ev_is_active (w))
959 return; 1000 return;
960 1001
961 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1002 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
962 ev_stop ((W)w); 1003 ev_stop ((W)w);
976once_cb (struct ev_once *once, int revents) 1017once_cb (struct ev_once *once, int revents)
977{ 1018{
978 void (*cb)(int revents, void *arg) = once->cb; 1019 void (*cb)(int revents, void *arg) = once->cb;
979 void *arg = once->arg; 1020 void *arg = once->arg;
980 1021
981 evio_stop (&once->io); 1022 ev_io_stop (&once->io);
982 evtimer_stop (&once->to); 1023 ev_timer_stop (&once->to);
983 free (once); 1024 free (once);
984 1025
985 cb (revents, arg); 1026 cb (revents, arg);
986} 1027}
987 1028
1001ev_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)
1002{ 1043{
1003 struct ev_once *once = malloc (sizeof (struct ev_once)); 1044 struct ev_once *once = malloc (sizeof (struct ev_once));
1004 1045
1005 if (!once) 1046 if (!once)
1006 cb (EV_ERROR, arg); 1047 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1007 else 1048 else
1008 { 1049 {
1009 once->cb = cb; 1050 once->cb = cb;
1010 once->arg = arg; 1051 once->arg = arg;
1011 1052
1012 evw_init (&once->io, once_cb_io); 1053 ev_watcher_init (&once->io, once_cb_io);
1013
1014 if (fd >= 0) 1054 if (fd >= 0)
1015 { 1055 {
1016 evio_set (&once->io, fd, events); 1056 ev_io_set (&once->io, fd, events);
1017 evio_start (&once->io); 1057 ev_io_start (&once->io);
1018 } 1058 }
1019 1059
1020 evw_init (&once->to, once_cb_to); 1060 ev_watcher_init (&once->to, once_cb_to);
1021
1022 if (timeout >= 0.) 1061 if (timeout >= 0.)
1023 { 1062 {
1024 evtimer_set (&once->to, timeout, 0.); 1063 ev_timer_set (&once->to, timeout, 0.);
1025 evtimer_start (&once->to); 1064 ev_timer_start (&once->to);
1026 } 1065 }
1027 } 1066 }
1028} 1067}
1029 1068
1030/*****************************************************************************/ 1069/*****************************************************************************/
1041 1080
1042static void 1081static void
1043ocb (struct ev_timer *w, int revents) 1082ocb (struct ev_timer *w, int revents)
1044{ 1083{
1045 //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);
1046 evtimer_stop (w); 1085 ev_timer_stop (w);
1047 evtimer_start (w); 1086 ev_timer_start (w);
1048} 1087}
1049 1088
1050static void 1089static void
1051scb (struct ev_signal *w, int revents) 1090scb (struct ev_signal *w, int revents)
1052{ 1091{
1053 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1092 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1054 evio_stop (&wio); 1093 ev_io_stop (&wio);
1055 evio_start (&wio); 1094 ev_io_start (&wio);
1056} 1095}
1057 1096
1058static void 1097static void
1059gcb (struct ev_signal *w, int revents) 1098gcb (struct ev_signal *w, int revents)
1060{ 1099{
1064 1103
1065int main (void) 1104int main (void)
1066{ 1105{
1067 ev_init (0); 1106 ev_init (0);
1068 1107
1069 evio_init (&wio, sin_cb, 0, EV_READ); 1108 ev_io_init (&wio, sin_cb, 0, EV_READ);
1070 evio_start (&wio); 1109 ev_io_start (&wio);
1071 1110
1072 struct ev_timer t[10000]; 1111 struct ev_timer t[10000];
1073 1112
1074#if 0 1113#if 0
1075 int i; 1114 int i;
1076 for (i = 0; i < 10000; ++i) 1115 for (i = 0; i < 10000; ++i)
1077 { 1116 {
1078 struct ev_timer *w = t + i; 1117 struct ev_timer *w = t + i;
1079 evw_init (w, ocb, i); 1118 ev_watcher_init (w, ocb, i);
1080 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1119 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1081 evtimer_start (w); 1120 ev_timer_start (w);
1082 if (drand48 () < 0.5) 1121 if (drand48 () < 0.5)
1083 evtimer_stop (w); 1122 ev_timer_stop (w);
1084 } 1123 }
1085#endif 1124#endif
1086 1125
1087 struct ev_timer t1; 1126 struct ev_timer t1;
1088 evtimer_init (&t1, ocb, 5, 10); 1127 ev_timer_init (&t1, ocb, 5, 10);
1089 evtimer_start (&t1); 1128 ev_timer_start (&t1);
1090 1129
1091 struct ev_signal sig; 1130 struct ev_signal sig;
1092 evsignal_init (&sig, scb, SIGQUIT); 1131 ev_signal_init (&sig, scb, SIGQUIT);
1093 evsignal_start (&sig); 1132 ev_signal_start (&sig);
1094 1133
1095 struct ev_check cw; 1134 struct ev_check cw;
1096 evcheck_init (&cw, gcb); 1135 ev_check_init (&cw, gcb);
1097 evcheck_start (&cw); 1136 ev_check_start (&cw);
1098 1137
1099 struct ev_idle iw; 1138 struct ev_idle iw;
1100 evidle_init (&iw, gcb); 1139 ev_idle_init (&iw, gcb);
1101 evidle_start (&iw); 1140 ev_idle_start (&iw);
1102 1141
1103 ev_loop (0); 1142 ev_loop (0);
1104 1143
1105 return 0; 1144 return 0;
1106} 1145}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines