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

Comparing libev/ev.c (file contents):
Revision 1.16 by root, Wed Oct 31 13:57:34 2007 UTC vs.
Revision 1.32 by root, Thu Nov 1 09:21:51 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines