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

Comparing libev/ev.c (file contents):
Revision 1.19 by root, Wed Oct 31 17:55:55 2007 UTC vs.
Revision 1.33 by root, Thu Nov 1 11:11:22 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines