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

Comparing libev/ev.c (file contents):
Revision 1.21 by root, Wed Oct 31 18:37:38 2007 UTC vs.
Revision 1.35 by root, Thu Nov 1 11:55:54 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;
253/*****************************************************************************/ 323/*****************************************************************************/
254 324
255typedef struct 325typedef struct
256{ 326{
257 struct ev_signal *head; 327 struct ev_signal *head;
258 sig_atomic_t gotsig; 328 sig_atomic_t volatile gotsig;
259} ANSIG; 329} ANSIG;
260 330
261static ANSIG *signals; 331static ANSIG *signals;
262static int signalmax; 332static int signalmax;
263 333
264static int sigpipe [2]; 334static int sigpipe [2];
265static sig_atomic_t gotsig; 335static sig_atomic_t volatile gotsig;
266static struct ev_io sigev; 336static struct ev_io sigev;
267 337
268static void 338static void
269signals_init (ANSIG *base, int count) 339signals_init (ANSIG *base, int count)
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
282 signals [signum - 1].gotsig = 1; 353 signals [signum - 1].gotsig = 1;
283 354
284 if (!gotsig) 355 if (!gotsig)
285 { 356 {
286 gotsig = 1; 357 gotsig = 1;
287 write (sigpipe [1], &gotsig, 1); 358 write (sigpipe [1], &signum, 1);
288 } 359 }
289} 360}
290 361
291static void 362static void
292sigcb (struct ev_io *iow, int revents) 363sigcb (struct ev_io *iow, int revents)
293{ 364{
294 struct ev_signal *w; 365 struct ev_signal *w;
295 int sig; 366 int sig;
296 367
368 read (sigpipe [0], &revents, 1);
297 gotsig = 0; 369 gotsig = 0;
298 read (sigpipe [0], &revents, 1);
299 370
300 for (sig = signalmax; sig--; ) 371 for (sig = signalmax; sig--; )
301 if (signals [sig].gotsig) 372 if (signals [sig].gotsig)
302 { 373 {
303 signals [sig].gotsig = 0; 374 signals [sig].gotsig = 0;
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;
332static struct ev_check **checks; 403static struct ev_check **checks;
333static int checkmax, checkcnt; 404static int checkmax, checkcnt;
334 405
335/*****************************************************************************/ 406/*****************************************************************************/
336 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
337#if HAVE_EPOLL 432#if EV_USE_EPOLL
338# include "ev_epoll.c" 433# include "ev_epoll.c"
339#endif 434#endif
340#if HAVE_SELECT 435#if EV_USE_SELECT
341# include "ev_select.c" 436# include "ev_select.c"
342#endif 437#endif
343 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
344int ev_init (int flags) 451int ev_init (int flags)
345{ 452{
346#if HAVE_MONOTONIC
347 {
348 struct timespec ts;
349 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
350 have_monotonic = 1;
351 }
352#endif
353
354 ev_now = ev_time ();
355 now = get_clock ();
356 diff = ev_now - now;
357
358 if (pipe (sigpipe))
359 return 0;
360
361 ev_method = EVMETHOD_NONE;
362#if HAVE_EPOLL
363 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
364#endif
365#if HAVE_SELECT
366 if (ev_method == EVMETHOD_NONE) select_init (flags);
367#endif
368
369 if (ev_method) 453 if (!ev_method)
454 {
455#if EV_USE_MONOTONIC
370 { 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 {
371 evw_init (&sigev, sigcb); 480 ev_watcher_init (&sigev, sigcb);
372 siginit (); 481 siginit ();
482
483 ev_signal_init (&childev, childcb, SIGCHLD);
484 ev_signal_start (&childev);
485 }
373 } 486 }
374 487
375 return ev_method; 488 return ev_method;
376} 489}
377 490
378/*****************************************************************************/ 491/*****************************************************************************/
379 492
380void ev_prefork (void) 493void
494ev_fork_prepare (void)
381{ 495{
382 /* nop */ 496 /* nop */
383} 497}
384 498
499void
385void ev_postfork_parent (void) 500ev_fork_parent (void)
386{ 501{
387 /* nop */ 502 /* nop */
388} 503}
389 504
505void
390void ev_postfork_child (void) 506ev_fork_child (void)
391{ 507{
392#if HAVE_EPOLL 508#if EV_USE_EPOLL
393 if (ev_method == EVMETHOD_EPOLL) 509 if (ev_method == EVMETHOD_EPOLL)
394 epoll_postfork_child (); 510 epoll_postfork_child ();
395#endif 511#endif
396 512
397 evio_stop (&sigev); 513 ev_io_stop (&sigev);
398 close (sigpipe [0]); 514 close (sigpipe [0]);
399 close (sigpipe [1]); 515 close (sigpipe [1]);
400 pipe (sigpipe); 516 pipe (sigpipe);
401 siginit (); 517 siginit ();
402} 518}
403 519
404/*****************************************************************************/ 520/*****************************************************************************/
405 521
406static void 522static void
407fd_reify (void)
408{
409 int i;
410
411 for (i = 0; i < fdchangecnt; ++i)
412 {
413 int fd = fdchanges [i];
414 ANFD *anfd = anfds + fd;
415 struct ev_io *w;
416
417 int wev = 0;
418
419 for (w = anfd->head; w; w = w->next)
420 wev |= w->events;
421
422 if (anfd->wev != wev)
423 {
424 method_modify (fd, anfd->wev, wev);
425 anfd->wev = wev;
426 }
427 }
428
429 fdchangecnt = 0;
430}
431
432static void
433call_pending () 523call_pending (void)
434{ 524{
435 while (pendingcnt) 525 while (pendingcnt)
436 { 526 {
437 ANPENDING *p = pendings + --pendingcnt; 527 ANPENDING *p = pendings + --pendingcnt;
438 528
443 } 533 }
444 } 534 }
445} 535}
446 536
447static void 537static void
448timers_reify () 538timers_reify (void)
449{ 539{
450 while (timercnt && timers [0]->at <= now) 540 while (timercnt && timers [0]->at <= now)
451 { 541 {
452 struct ev_timer *w = timers [0]; 542 struct ev_timer *w = timers [0];
453
454 event ((W)w, EV_TIMEOUT);
455 543
456 /* first reschedule or stop timer */ 544 /* first reschedule or stop timer */
457 if (w->repeat) 545 if (w->repeat)
458 { 546 {
547 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
459 w->at = now + w->repeat; 548 w->at = now + w->repeat;
460 assert (("timer timeout in the past, negative repeat?", w->at > now));
461 downheap ((WT *)timers, timercnt, 0); 549 downheap ((WT *)timers, timercnt, 0);
462 } 550 }
463 else 551 else
464 evtimer_stop (w); /* nonrepeating: stop timer */ 552 ev_timer_stop (w); /* nonrepeating: stop timer */
465 }
466}
467 553
554 event ((W)w, EV_TIMEOUT);
555 }
556}
557
468static void 558static void
469periodics_reify () 559periodics_reify (void)
470{ 560{
471 while (periodiccnt && periodics [0]->at <= ev_now) 561 while (periodiccnt && periodics [0]->at <= ev_now)
472 { 562 {
473 struct ev_periodic *w = periodics [0]; 563 struct ev_periodic *w = periodics [0];
474 564
475 /* first reschedule or stop timer */ 565 /* first reschedule or stop timer */
476 if (w->interval) 566 if (w->interval)
477 { 567 {
478 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;
479 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));
480 downheap ((WT *)periodics, periodiccnt, 0); 570 downheap ((WT *)periodics, periodiccnt, 0);
481 } 571 }
482 else 572 else
483 evperiodic_stop (w); /* nonrepeating: stop timer */ 573 ev_periodic_stop (w); /* nonrepeating: stop timer */
484 574
485 event ((W)w, EV_TIMEOUT); 575 event ((W)w, EV_PERIODIC);
486 } 576 }
487} 577}
488 578
489static void 579static void
490periodics_reschedule (ev_tstamp diff) 580periodics_reschedule (ev_tstamp diff)
500 { 590 {
501 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;
502 592
503 if (fabs (diff) >= 1e-4) 593 if (fabs (diff) >= 1e-4)
504 { 594 {
505 evperiodic_stop (w); 595 ev_periodic_stop (w);
506 evperiodic_start (w); 596 ev_periodic_start (w);
507 597
508 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 598 i = 0; /* restart loop, inefficient, but time jumps should be rare */
509 } 599 }
510 } 600 }
511 } 601 }
512} 602}
513 603
514static void 604static void
515time_update () 605time_update (void)
516{ 606{
517 int i; 607 int i;
518 608
519 ev_now = ev_time (); 609 ev_now = ev_time ();
520 610
554int ev_loop_done; 644int ev_loop_done;
555 645
556void ev_loop (int flags) 646void ev_loop (int flags)
557{ 647{
558 double block; 648 double block;
559 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 649 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
560 650
561 do 651 do
562 { 652 {
563 /* queue check watchers (and execute them) */ 653 /* queue check watchers (and execute them) */
564 if (preparecnt) 654 if (preparecnt)
645 head = &(*head)->next; 735 head = &(*head)->next;
646 } 736 }
647} 737}
648 738
649static void 739static void
650ev_clear (W w) 740ev_clear_pending (W w)
651{ 741{
652 if (w->pending) 742 if (w->pending)
653 { 743 {
654 pendings [w->pending - 1].w = 0; 744 pendings [w->pending - 1].w = 0;
655 w->pending = 0; 745 w->pending = 0;
669} 759}
670 760
671/*****************************************************************************/ 761/*****************************************************************************/
672 762
673void 763void
674evio_start (struct ev_io *w) 764ev_io_start (struct ev_io *w)
675{ 765{
676 if (ev_is_active (w)) 766 if (ev_is_active (w))
677 return; 767 return;
678 768
679 int fd = w->fd; 769 int fd = w->fd;
770
771 assert (("ev_io_start called with negative fd", fd >= 0));
680 772
681 ev_start ((W)w, 1); 773 ev_start ((W)w, 1);
682 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 774 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
683 wlist_add ((WL *)&anfds[fd].head, (WL)w); 775 wlist_add ((WL *)&anfds[fd].head, (WL)w);
684 776
685 ++fdchangecnt; 777 fd_change (fd);
686 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
687 fdchanges [fdchangecnt - 1] = fd;
688} 778}
689 779
690void 780void
691evio_stop (struct ev_io *w) 781ev_io_stop (struct ev_io *w)
692{ 782{
693 ev_clear ((W)w); 783 ev_clear_pending ((W)w);
694 if (!ev_is_active (w)) 784 if (!ev_is_active (w))
695 return; 785 return;
696 786
697 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 787 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
698 ev_stop ((W)w); 788 ev_stop ((W)w);
699 789
700 ++fdchangecnt; 790 fd_change (w->fd);
701 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
702 fdchanges [fdchangecnt - 1] = w->fd;
703} 791}
704 792
705void 793void
706evtimer_start (struct ev_timer *w) 794ev_timer_start (struct ev_timer *w)
707{ 795{
708 if (ev_is_active (w)) 796 if (ev_is_active (w))
709 return; 797 return;
710 798
711 w->at += now; 799 w->at += now;
712 800
713 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.));
714 802
715 ev_start ((W)w, ++timercnt); 803 ev_start ((W)w, ++timercnt);
716 array_needsize (timers, timermax, timercnt, ); 804 array_needsize (timers, timermax, timercnt, );
717 timers [timercnt - 1] = w; 805 timers [timercnt - 1] = w;
718 upheap ((WT *)timers, timercnt - 1); 806 upheap ((WT *)timers, timercnt - 1);
719} 807}
720 808
721void 809void
722evtimer_stop (struct ev_timer *w) 810ev_timer_stop (struct ev_timer *w)
723{ 811{
724 ev_clear ((W)w); 812 ev_clear_pending ((W)w);
725 if (!ev_is_active (w)) 813 if (!ev_is_active (w))
726 return; 814 return;
727 815
728 if (w->active < timercnt--) 816 if (w->active < timercnt--)
729 { 817 {
735 823
736 ev_stop ((W)w); 824 ev_stop ((W)w);
737} 825}
738 826
739void 827void
740evtimer_again (struct ev_timer *w) 828ev_timer_again (struct ev_timer *w)
741{ 829{
742 if (ev_is_active (w)) 830 if (ev_is_active (w))
743 { 831 {
744 if (w->repeat) 832 if (w->repeat)
745 { 833 {
746 w->at = now + w->repeat; 834 w->at = now + w->repeat;
747 downheap ((WT *)timers, timercnt, w->active - 1); 835 downheap ((WT *)timers, timercnt, w->active - 1);
748 } 836 }
749 else 837 else
750 evtimer_stop (w); 838 ev_timer_stop (w);
751 } 839 }
752 else if (w->repeat) 840 else if (w->repeat)
753 evtimer_start (w); 841 ev_timer_start (w);
754} 842}
755 843
756void 844void
757evperiodic_start (struct ev_periodic *w) 845ev_periodic_start (struct ev_periodic *w)
758{ 846{
759 if (ev_is_active (w)) 847 if (ev_is_active (w))
760 return; 848 return;
761 849
762 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.));
763 851
764 /* 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 */
765 if (w->interval) 853 if (w->interval)
766 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 854 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
767 855
770 periodics [periodiccnt - 1] = w; 858 periodics [periodiccnt - 1] = w;
771 upheap ((WT *)periodics, periodiccnt - 1); 859 upheap ((WT *)periodics, periodiccnt - 1);
772} 860}
773 861
774void 862void
775evperiodic_stop (struct ev_periodic *w) 863ev_periodic_stop (struct ev_periodic *w)
776{ 864{
777 ev_clear ((W)w); 865 ev_clear_pending ((W)w);
778 if (!ev_is_active (w)) 866 if (!ev_is_active (w))
779 return; 867 return;
780 868
781 if (w->active < periodiccnt--) 869 if (w->active < periodiccnt--)
782 { 870 {
786 874
787 ev_stop ((W)w); 875 ev_stop ((W)w);
788} 876}
789 877
790void 878void
791evsignal_start (struct ev_signal *w) 879ev_signal_start (struct ev_signal *w)
792{ 880{
793 if (ev_is_active (w)) 881 if (ev_is_active (w))
794 return; 882 return;
883
884 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
795 885
796 ev_start ((W)w, 1); 886 ev_start ((W)w, 1);
797 array_needsize (signals, signalmax, w->signum, signals_init); 887 array_needsize (signals, signalmax, w->signum, signals_init);
798 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 888 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
799 889
806 sigaction (w->signum, &sa, 0); 896 sigaction (w->signum, &sa, 0);
807 } 897 }
808} 898}
809 899
810void 900void
811evsignal_stop (struct ev_signal *w) 901ev_signal_stop (struct ev_signal *w)
812{ 902{
813 ev_clear ((W)w); 903 ev_clear_pending ((W)w);
814 if (!ev_is_active (w)) 904 if (!ev_is_active (w))
815 return; 905 return;
816 906
817 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 907 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
818 ev_stop ((W)w); 908 ev_stop ((W)w);
819 909
820 if (!signals [w->signum - 1].head) 910 if (!signals [w->signum - 1].head)
821 signal (w->signum, SIG_DFL); 911 signal (w->signum, SIG_DFL);
822} 912}
823 913
914void
824void evidle_start (struct ev_idle *w) 915ev_idle_start (struct ev_idle *w)
825{ 916{
826 if (ev_is_active (w)) 917 if (ev_is_active (w))
827 return; 918 return;
828 919
829 ev_start ((W)w, ++idlecnt); 920 ev_start ((W)w, ++idlecnt);
830 array_needsize (idles, idlemax, idlecnt, ); 921 array_needsize (idles, idlemax, idlecnt, );
831 idles [idlecnt - 1] = w; 922 idles [idlecnt - 1] = w;
832} 923}
833 924
925void
834void evidle_stop (struct ev_idle *w) 926ev_idle_stop (struct ev_idle *w)
835{ 927{
836 ev_clear ((W)w); 928 ev_clear_pending ((W)w);
837 if (ev_is_active (w)) 929 if (ev_is_active (w))
838 return; 930 return;
839 931
840 idles [w->active - 1] = idles [--idlecnt]; 932 idles [w->active - 1] = idles [--idlecnt];
841 ev_stop ((W)w); 933 ev_stop ((W)w);
842} 934}
843 935
936void
844void evprepare_start (struct ev_prepare *w) 937ev_prepare_start (struct ev_prepare *w)
845{ 938{
846 if (ev_is_active (w)) 939 if (ev_is_active (w))
847 return; 940 return;
848 941
849 ev_start ((W)w, ++preparecnt); 942 ev_start ((W)w, ++preparecnt);
850 array_needsize (prepares, preparemax, preparecnt, ); 943 array_needsize (prepares, preparemax, preparecnt, );
851 prepares [preparecnt - 1] = w; 944 prepares [preparecnt - 1] = w;
852} 945}
853 946
947void
854void evprepare_stop (struct ev_prepare *w) 948ev_prepare_stop (struct ev_prepare *w)
855{ 949{
856 ev_clear ((W)w); 950 ev_clear_pending ((W)w);
857 if (ev_is_active (w)) 951 if (ev_is_active (w))
858 return; 952 return;
859 953
860 prepares [w->active - 1] = prepares [--preparecnt]; 954 prepares [w->active - 1] = prepares [--preparecnt];
861 ev_stop ((W)w); 955 ev_stop ((W)w);
862} 956}
863 957
958void
864void evcheck_start (struct ev_check *w) 959ev_check_start (struct ev_check *w)
865{ 960{
866 if (ev_is_active (w)) 961 if (ev_is_active (w))
867 return; 962 return;
868 963
869 ev_start ((W)w, ++checkcnt); 964 ev_start ((W)w, ++checkcnt);
870 array_needsize (checks, checkmax, checkcnt, ); 965 array_needsize (checks, checkmax, checkcnt, );
871 checks [checkcnt - 1] = w; 966 checks [checkcnt - 1] = w;
872} 967}
873 968
969void
874void evcheck_stop (struct ev_check *w) 970ev_check_stop (struct ev_check *w)
875{ 971{
876 ev_clear ((W)w); 972 ev_clear_pending ((W)w);
877 if (ev_is_active (w)) 973 if (ev_is_active (w))
878 return; 974 return;
879 975
880 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);
881 ev_stop ((W)w); 998 ev_stop ((W)w);
882} 999}
883 1000
884/*****************************************************************************/ 1001/*****************************************************************************/
885 1002
895once_cb (struct ev_once *once, int revents) 1012once_cb (struct ev_once *once, int revents)
896{ 1013{
897 void (*cb)(int revents, void *arg) = once->cb; 1014 void (*cb)(int revents, void *arg) = once->cb;
898 void *arg = once->arg; 1015 void *arg = once->arg;
899 1016
900 evio_stop (&once->io); 1017 ev_io_stop (&once->io);
901 evtimer_stop (&once->to); 1018 ev_timer_stop (&once->to);
902 free (once); 1019 free (once);
903 1020
904 cb (revents, arg); 1021 cb (revents, arg);
905} 1022}
906 1023
920ev_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)
921{ 1038{
922 struct ev_once *once = malloc (sizeof (struct ev_once)); 1039 struct ev_once *once = malloc (sizeof (struct ev_once));
923 1040
924 if (!once) 1041 if (!once)
925 cb (EV_ERROR, arg); 1042 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
926 else 1043 else
927 { 1044 {
928 once->cb = cb; 1045 once->cb = cb;
929 once->arg = arg; 1046 once->arg = arg;
930 1047
931 evw_init (&once->io, once_cb_io); 1048 ev_watcher_init (&once->io, once_cb_io);
932
933 if (fd >= 0) 1049 if (fd >= 0)
934 { 1050 {
935 evio_set (&once->io, fd, events); 1051 ev_io_set (&once->io, fd, events);
936 evio_start (&once->io); 1052 ev_io_start (&once->io);
937 } 1053 }
938 1054
939 evw_init (&once->to, once_cb_to); 1055 ev_watcher_init (&once->to, once_cb_to);
940
941 if (timeout >= 0.) 1056 if (timeout >= 0.)
942 { 1057 {
943 evtimer_set (&once->to, timeout, 0.); 1058 ev_timer_set (&once->to, timeout, 0.);
944 evtimer_start (&once->to); 1059 ev_timer_start (&once->to);
945 } 1060 }
946 } 1061 }
947} 1062}
948 1063
949/*****************************************************************************/ 1064/*****************************************************************************/
960 1075
961static void 1076static void
962ocb (struct ev_timer *w, int revents) 1077ocb (struct ev_timer *w, int revents)
963{ 1078{
964 //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);
965 evtimer_stop (w); 1080 ev_timer_stop (w);
966 evtimer_start (w); 1081 ev_timer_start (w);
967} 1082}
968 1083
969static void 1084static void
970scb (struct ev_signal *w, int revents) 1085scb (struct ev_signal *w, int revents)
971{ 1086{
972 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1087 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
973 evio_stop (&wio); 1088 ev_io_stop (&wio);
974 evio_start (&wio); 1089 ev_io_start (&wio);
975} 1090}
976 1091
977static void 1092static void
978gcb (struct ev_signal *w, int revents) 1093gcb (struct ev_signal *w, int revents)
979{ 1094{
983 1098
984int main (void) 1099int main (void)
985{ 1100{
986 ev_init (0); 1101 ev_init (0);
987 1102
988 evio_init (&wio, sin_cb, 0, EV_READ); 1103 ev_io_init (&wio, sin_cb, 0, EV_READ);
989 evio_start (&wio); 1104 ev_io_start (&wio);
990 1105
991 struct ev_timer t[10000]; 1106 struct ev_timer t[10000];
992 1107
993#if 0 1108#if 0
994 int i; 1109 int i;
995 for (i = 0; i < 10000; ++i) 1110 for (i = 0; i < 10000; ++i)
996 { 1111 {
997 struct ev_timer *w = t + i; 1112 struct ev_timer *w = t + i;
998 evw_init (w, ocb, i); 1113 ev_watcher_init (w, ocb, i);
999 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1114 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1000 evtimer_start (w); 1115 ev_timer_start (w);
1001 if (drand48 () < 0.5) 1116 if (drand48 () < 0.5)
1002 evtimer_stop (w); 1117 ev_timer_stop (w);
1003 } 1118 }
1004#endif 1119#endif
1005 1120
1006 struct ev_timer t1; 1121 struct ev_timer t1;
1007 evtimer_init (&t1, ocb, 5, 10); 1122 ev_timer_init (&t1, ocb, 5, 10);
1008 evtimer_start (&t1); 1123 ev_timer_start (&t1);
1009 1124
1010 struct ev_signal sig; 1125 struct ev_signal sig;
1011 evsignal_init (&sig, scb, SIGQUIT); 1126 ev_signal_init (&sig, scb, SIGQUIT);
1012 evsignal_start (&sig); 1127 ev_signal_start (&sig);
1013 1128
1014 struct ev_check cw; 1129 struct ev_check cw;
1015 evcheck_init (&cw, gcb); 1130 ev_check_init (&cw, gcb);
1016 evcheck_start (&cw); 1131 ev_check_start (&cw);
1017 1132
1018 struct ev_idle iw; 1133 struct ev_idle iw;
1019 evidle_init (&iw, gcb); 1134 ev_idle_init (&iw, gcb);
1020 evidle_start (&iw); 1135 ev_idle_start (&iw);
1021 1136
1022 ev_loop (0); 1137 ev_loop (0);
1023 1138
1024 return 0; 1139 return 0;
1025} 1140}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines