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.35 by root, Thu Nov 1 11:55:54 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 unsigned char events;
145 unsigned char reify;
100} ANFD; 146} ANFD;
101 147
102static ANFD *anfds; 148static ANFD *anfds;
103static int anfdmax; 149static int anfdmax;
104 150
105static int *fdchanges;
106static int fdchangemax, fdchangecnt;
107
108static void 151static void
109anfds_init (ANFD *base, int count) 152anfds_init (ANFD *base, int count)
110{ 153{
111 while (count--) 154 while (count--)
112 { 155 {
113 base->head = 0; 156 base->head = 0;
114 base->wev = base->rev = EV_NONE; 157 base->events = EV_NONE;
158 base->reify = 0;
159
115 ++base; 160 ++base;
116 } 161 }
117} 162}
118 163
119typedef struct 164typedef struct
126static int pendingmax, pendingcnt; 171static int pendingmax, pendingcnt;
127 172
128static void 173static void
129event (W w, int events) 174event (W w, int events)
130{ 175{
131 if (w->active) 176 if (w->pending)
177 {
178 pendings [w->pending - 1].events |= events;
179 return;
132 { 180 }
181
133 w->pending = ++pendingcnt; 182 w->pending = ++pendingcnt;
134 array_needsize (pendings, pendingmax, pendingcnt, ); 183 array_needsize (pendings, pendingmax, pendingcnt, );
135 pendings [pendingcnt - 1].w = w; 184 pendings [pendingcnt - 1].w = w;
136 pendings [pendingcnt - 1].events = events; 185 pendings [pendingcnt - 1].events = events;
137 } 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);
138} 195}
139 196
140static void 197static void
141fd_event (int fd, int events) 198fd_event (int fd, int events)
142{ 199{
150 if (ev) 207 if (ev)
151 event ((W)w, ev); 208 event ((W)w, ev);
152 } 209 }
153} 210}
154 211
212/*****************************************************************************/
213
214static int *fdchanges;
215static int fdchangemax, fdchangecnt;
216
155static void 217static void
156queue_events (W *events, int eventcnt, int type) 218fd_reify (void)
157{ 219{
158 int i; 220 int i;
159 221
160 for (i = 0; i < eventcnt; ++i) 222 for (i = 0; i < fdchangecnt; ++i)
161 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;
256}
257
258/* called on EBADF to verify fds */
259static void
260fd_recheck (void)
261{
262 int fd;
263
264 for (fd = 0; fd < anfdmax; ++fd)
265 if (anfds [fd].events)
266 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
267 while (anfds [fd].head)
268 {
269 ev_io_stop (anfds [fd].head);
270 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE);
271 }
162} 272}
163 273
164/*****************************************************************************/ 274/*****************************************************************************/
165 275
166static struct ev_timer **timers; 276static struct ev_timer **timers;
213/*****************************************************************************/ 323/*****************************************************************************/
214 324
215typedef struct 325typedef struct
216{ 326{
217 struct ev_signal *head; 327 struct ev_signal *head;
218 sig_atomic_t gotsig; 328 sig_atomic_t volatile gotsig;
219} ANSIG; 329} ANSIG;
220 330
221static ANSIG *signals; 331static ANSIG *signals;
222static int signalmax; 332static int signalmax;
223 333
224static int sigpipe [2]; 334static int sigpipe [2];
225static sig_atomic_t gotsig; 335static sig_atomic_t volatile gotsig;
226static struct ev_io sigev; 336static struct ev_io sigev;
227 337
228static void 338static void
229signals_init (ANSIG *base, int count) 339signals_init (ANSIG *base, int count)
230{ 340{
231 while (count--) 341 while (count--)
232 { 342 {
233 base->head = 0; 343 base->head = 0;
234 base->gotsig = 0; 344 base->gotsig = 0;
345
235 ++base; 346 ++base;
236 } 347 }
237} 348}
238 349
239static void 350static void
242 signals [signum - 1].gotsig = 1; 353 signals [signum - 1].gotsig = 1;
243 354
244 if (!gotsig) 355 if (!gotsig)
245 { 356 {
246 gotsig = 1; 357 gotsig = 1;
247 write (sigpipe [1], &gotsig, 1); 358 write (sigpipe [1], &signum, 1);
248 } 359 }
249} 360}
250 361
251static void 362static void
252sigcb (struct ev_io *iow, int revents) 363sigcb (struct ev_io *iow, int revents)
253{ 364{
254 struct ev_signal *w; 365 struct ev_signal *w;
255 int sig; 366 int sig;
256 367
368 read (sigpipe [0], &revents, 1);
257 gotsig = 0; 369 gotsig = 0;
258 read (sigpipe [0], &revents, 1);
259 370
260 for (sig = signalmax; sig--; ) 371 for (sig = signalmax; sig--; )
261 if (signals [sig].gotsig) 372 if (signals [sig].gotsig)
262 { 373 {
263 signals [sig].gotsig = 0; 374 signals [sig].gotsig = 0;
275 386
276 /* rather than sort out wether we really need nb, set it */ 387 /* rather than sort out wether we really need nb, set it */
277 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 388 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
278 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 389 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
279 390
280 evio_set (&sigev, sigpipe [0], EV_READ); 391 ev_io_set (&sigev, sigpipe [0], EV_READ);
281 evio_start (&sigev); 392 ev_io_start (&sigev);
282} 393}
283 394
284/*****************************************************************************/ 395/*****************************************************************************/
285 396
286static struct ev_idle **idles; 397static struct ev_idle **idles;
287static int idlemax, idlecnt; 398static int idlemax, idlecnt;
288 399
400static struct ev_prepare **prepares;
401static int preparemax, preparecnt;
402
289static struct ev_check **checks; 403static struct ev_check **checks;
290static int checkmax, checkcnt; 404static int checkmax, checkcnt;
291 405
292/*****************************************************************************/ 406/*****************************************************************************/
293 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
294#if HAVE_EPOLL 432#if EV_USE_EPOLL
295# include "ev_epoll.c" 433# include "ev_epoll.c"
296#endif 434#endif
297#if HAVE_SELECT 435#if EV_USE_SELECT
298# include "ev_select.c" 436# include "ev_select.c"
299#endif 437#endif
300 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
301int ev_init (int flags) 451int ev_init (int flags)
302{ 452{
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) 453 if (!ev_method)
454 {
455#if EV_USE_MONOTONIC
327 { 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 {
328 evw_init (&sigev, sigcb); 480 ev_watcher_init (&sigev, sigcb);
329 siginit (); 481 siginit ();
482
483 ev_signal_init (&childev, childcb, SIGCHLD);
484 ev_signal_start (&childev);
485 }
330 } 486 }
331 487
332 return ev_method; 488 return ev_method;
333} 489}
334 490
335/*****************************************************************************/ 491/*****************************************************************************/
336 492
337void ev_prefork (void) 493void
494ev_fork_prepare (void)
338{ 495{
339 /* nop */ 496 /* nop */
340} 497}
341 498
499void
342void ev_postfork_parent (void) 500ev_fork_parent (void)
343{ 501{
344 /* nop */ 502 /* nop */
345} 503}
346 504
505void
347void ev_postfork_child (void) 506ev_fork_child (void)
348{ 507{
349#if HAVE_EPOLL 508#if EV_USE_EPOLL
350 if (ev_method == EVMETHOD_EPOLL) 509 if (ev_method == EVMETHOD_EPOLL)
351 epoll_postfork_child (); 510 epoll_postfork_child ();
352#endif 511#endif
353 512
354 evio_stop (&sigev); 513 ev_io_stop (&sigev);
355 close (sigpipe [0]); 514 close (sigpipe [0]);
356 close (sigpipe [1]); 515 close (sigpipe [1]);
357 pipe (sigpipe); 516 pipe (sigpipe);
358 siginit (); 517 siginit ();
359} 518}
360 519
361/*****************************************************************************/ 520/*****************************************************************************/
362 521
363static void 522static 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 () 523call_pending (void)
391{ 524{
392 int i; 525 while (pendingcnt)
393
394 for (i = 0; i < pendingcnt; ++i)
395 { 526 {
396 ANPENDING *p = pendings + i; 527 ANPENDING *p = pendings + --pendingcnt;
397 528
398 if (p->w) 529 if (p->w)
399 { 530 {
400 p->w->pending = 0; 531 p->w->pending = 0;
401 p->w->cb (p->w, p->events); 532 p->w->cb (p->w, p->events);
402 } 533 }
403 } 534 }
404
405 pendingcnt = 0;
406} 535}
407 536
408static void 537static void
409timers_reify () 538timers_reify (void)
410{ 539{
411 while (timercnt && timers [0]->at <= now) 540 while (timercnt && timers [0]->at <= now)
412 { 541 {
413 struct ev_timer *w = timers [0]; 542 struct ev_timer *w = timers [0];
414
415 event ((W)w, EV_TIMEOUT);
416 543
417 /* first reschedule or stop timer */ 544 /* first reschedule or stop timer */
418 if (w->repeat) 545 if (w->repeat)
419 { 546 {
547 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
420 w->at = now + w->repeat; 548 w->at = now + w->repeat;
421 assert (("timer timeout in the past, negative repeat?", w->at > now));
422 downheap ((WT *)timers, timercnt, 0); 549 downheap ((WT *)timers, timercnt, 0);
423 } 550 }
424 else 551 else
425 evtimer_stop (w); /* nonrepeating: stop timer */ 552 ev_timer_stop (w); /* nonrepeating: stop timer */
426 }
427}
428 553
554 event ((W)w, EV_TIMEOUT);
555 }
556}
557
429static void 558static void
430periodics_reify () 559periodics_reify (void)
431{ 560{
432 while (periodiccnt && periodics [0]->at <= ev_now) 561 while (periodiccnt && periodics [0]->at <= ev_now)
433 { 562 {
434 struct ev_periodic *w = periodics [0]; 563 struct ev_periodic *w = periodics [0];
435 564
436 /* first reschedule or stop timer */ 565 /* first reschedule or stop timer */
437 if (w->interval) 566 if (w->interval)
438 { 567 {
439 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;
440 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));
441 downheap ((WT *)periodics, periodiccnt, 0); 570 downheap ((WT *)periodics, periodiccnt, 0);
442 } 571 }
443 else 572 else
444 evperiodic_stop (w); /* nonrepeating: stop timer */ 573 ev_periodic_stop (w); /* nonrepeating: stop timer */
445 574
446 event ((W)w, EV_TIMEOUT); 575 event ((W)w, EV_PERIODIC);
447 } 576 }
448} 577}
449 578
450static void 579static void
451periodics_reschedule (ev_tstamp diff) 580periodics_reschedule (ev_tstamp diff)
461 { 590 {
462 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;
463 592
464 if (fabs (diff) >= 1e-4) 593 if (fabs (diff) >= 1e-4)
465 { 594 {
466 evperiodic_stop (w); 595 ev_periodic_stop (w);
467 evperiodic_start (w); 596 ev_periodic_start (w);
468 597
469 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 598 i = 0; /* restart loop, inefficient, but time jumps should be rare */
470 } 599 }
471 } 600 }
472 } 601 }
473} 602}
474 603
475static void 604static void
476time_update () 605time_update (void)
477{ 606{
478 int i; 607 int i;
479 608
480 ev_now = ev_time (); 609 ev_now = ev_time ();
481 610
515int ev_loop_done; 644int ev_loop_done;
516 645
517void ev_loop (int flags) 646void ev_loop (int flags)
518{ 647{
519 double block; 648 double block;
520 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 649 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 650
528 do 651 do
529 { 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
530 /* update fd-related kernel structures */ 660 /* update fd-related kernel structures */
531 fd_reify (); 661 fd_reify ();
532 662
533 /* calculate blocking time */ 663 /* calculate blocking time */
534 664
535 /* 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 */
536 ev_now = ev_time (); 667 ev_now = ev_time ();
537 668
538 if (flags & EVLOOP_NONBLOCK || idlecnt) 669 if (flags & EVLOOP_NONBLOCK || idlecnt)
539 block = 0.; 670 block = 0.;
540 else 671 else
560 691
561 /* update ev_now, do magic */ 692 /* update ev_now, do magic */
562 time_update (); 693 time_update ();
563 694
564 /* queue pending timers and reschedule them */ 695 /* queue pending timers and reschedule them */
696 timers_reify (); /* relative timers called last */
565 periodics_reify (); /* absolute timers first */ 697 periodics_reify (); /* absolute timers called first */
566 timers_reify (); /* relative timers second */
567 698
568 /* queue idle watchers unless io or timers are pending */ 699 /* queue idle watchers unless io or timers are pending */
569 if (!pendingcnt) 700 if (!pendingcnt)
570 queue_events ((W *)idles, idlecnt, EV_IDLE); 701 queue_events ((W *)idles, idlecnt, EV_IDLE);
571 702
572 /* queue check and possibly idle watchers */ 703 /* queue check watchers, to be executed first */
704 if (checkcnt)
573 queue_events ((W *)checks, checkcnt, EV_CHECK); 705 queue_events ((W *)checks, checkcnt, EV_CHECK);
574 706
575 call_pending (); 707 call_pending ();
576 } 708 }
577 while (!ev_loop_done); 709 while (!ev_loop_done);
578 710
603 head = &(*head)->next; 735 head = &(*head)->next;
604 } 736 }
605} 737}
606 738
607static void 739static void
608ev_clear (W w) 740ev_clear_pending (W w)
609{ 741{
610 if (w->pending) 742 if (w->pending)
611 { 743 {
612 pendings [w->pending - 1].w = 0; 744 pendings [w->pending - 1].w = 0;
613 w->pending = 0; 745 w->pending = 0;
627} 759}
628 760
629/*****************************************************************************/ 761/*****************************************************************************/
630 762
631void 763void
632evio_start (struct ev_io *w) 764ev_io_start (struct ev_io *w)
633{ 765{
634 if (ev_is_active (w)) 766 if (ev_is_active (w))
635 return; 767 return;
636 768
637 int fd = w->fd; 769 int fd = w->fd;
770
771 assert (("ev_io_start called with negative fd", fd >= 0));
638 772
639 ev_start ((W)w, 1); 773 ev_start ((W)w, 1);
640 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 774 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
641 wlist_add ((WL *)&anfds[fd].head, (WL)w); 775 wlist_add ((WL *)&anfds[fd].head, (WL)w);
642 776
643 ++fdchangecnt; 777 fd_change (fd);
644 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
645 fdchanges [fdchangecnt - 1] = fd;
646} 778}
647 779
648void 780void
649evio_stop (struct ev_io *w) 781ev_io_stop (struct ev_io *w)
650{ 782{
651 ev_clear ((W)w); 783 ev_clear_pending ((W)w);
652 if (!ev_is_active (w)) 784 if (!ev_is_active (w))
653 return; 785 return;
654 786
655 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 787 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
656 ev_stop ((W)w); 788 ev_stop ((W)w);
657 789
658 ++fdchangecnt; 790 fd_change (w->fd);
659 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
660 fdchanges [fdchangecnt - 1] = w->fd;
661} 791}
662 792
663void 793void
664evtimer_start (struct ev_timer *w) 794ev_timer_start (struct ev_timer *w)
665{ 795{
666 if (ev_is_active (w)) 796 if (ev_is_active (w))
667 return; 797 return;
668 798
669 w->at += now; 799 w->at += now;
670 800
671 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.));
672 802
673 ev_start ((W)w, ++timercnt); 803 ev_start ((W)w, ++timercnt);
674 array_needsize (timers, timermax, timercnt, ); 804 array_needsize (timers, timermax, timercnt, );
675 timers [timercnt - 1] = w; 805 timers [timercnt - 1] = w;
676 upheap ((WT *)timers, timercnt - 1); 806 upheap ((WT *)timers, timercnt - 1);
677} 807}
678 808
679void 809void
680evtimer_stop (struct ev_timer *w) 810ev_timer_stop (struct ev_timer *w)
681{ 811{
682 ev_clear ((W)w); 812 ev_clear_pending ((W)w);
683 if (!ev_is_active (w)) 813 if (!ev_is_active (w))
684 return; 814 return;
685 815
686 if (w->active < timercnt--) 816 if (w->active < timercnt--)
687 { 817 {
693 823
694 ev_stop ((W)w); 824 ev_stop ((W)w);
695} 825}
696 826
697void 827void
698evtimer_again (struct ev_timer *w) 828ev_timer_again (struct ev_timer *w)
699{ 829{
700 if (ev_is_active (w)) 830 if (ev_is_active (w))
701 { 831 {
702 if (w->repeat) 832 if (w->repeat)
703 { 833 {
704 w->at = now + w->repeat; 834 w->at = now + w->repeat;
705 downheap ((WT *)timers, timercnt, w->active - 1); 835 downheap ((WT *)timers, timercnt, w->active - 1);
706 } 836 }
707 else 837 else
708 evtimer_stop (w); 838 ev_timer_stop (w);
709 } 839 }
710 else if (w->repeat) 840 else if (w->repeat)
711 evtimer_start (w); 841 ev_timer_start (w);
712} 842}
713 843
714void 844void
715evperiodic_start (struct ev_periodic *w) 845ev_periodic_start (struct ev_periodic *w)
716{ 846{
717 if (ev_is_active (w)) 847 if (ev_is_active (w))
718 return; 848 return;
719 849
720 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.));
721 851
722 /* 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 */
723 if (w->interval) 853 if (w->interval)
724 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 854 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
725 855
728 periodics [periodiccnt - 1] = w; 858 periodics [periodiccnt - 1] = w;
729 upheap ((WT *)periodics, periodiccnt - 1); 859 upheap ((WT *)periodics, periodiccnt - 1);
730} 860}
731 861
732void 862void
733evperiodic_stop (struct ev_periodic *w) 863ev_periodic_stop (struct ev_periodic *w)
734{ 864{
735 ev_clear ((W)w); 865 ev_clear_pending ((W)w);
736 if (!ev_is_active (w)) 866 if (!ev_is_active (w))
737 return; 867 return;
738 868
739 if (w->active < periodiccnt--) 869 if (w->active < periodiccnt--)
740 { 870 {
744 874
745 ev_stop ((W)w); 875 ev_stop ((W)w);
746} 876}
747 877
748void 878void
749evsignal_start (struct ev_signal *w) 879ev_signal_start (struct ev_signal *w)
750{ 880{
751 if (ev_is_active (w)) 881 if (ev_is_active (w))
752 return; 882 return;
883
884 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
753 885
754 ev_start ((W)w, 1); 886 ev_start ((W)w, 1);
755 array_needsize (signals, signalmax, w->signum, signals_init); 887 array_needsize (signals, signalmax, w->signum, signals_init);
756 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 888 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
757 889
764 sigaction (w->signum, &sa, 0); 896 sigaction (w->signum, &sa, 0);
765 } 897 }
766} 898}
767 899
768void 900void
769evsignal_stop (struct ev_signal *w) 901ev_signal_stop (struct ev_signal *w)
770{ 902{
771 ev_clear ((W)w); 903 ev_clear_pending ((W)w);
772 if (!ev_is_active (w)) 904 if (!ev_is_active (w))
773 return; 905 return;
774 906
775 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 907 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
776 ev_stop ((W)w); 908 ev_stop ((W)w);
777 909
778 if (!signals [w->signum - 1].head) 910 if (!signals [w->signum - 1].head)
779 signal (w->signum, SIG_DFL); 911 signal (w->signum, SIG_DFL);
780} 912}
781 913
914void
782void evidle_start (struct ev_idle *w) 915ev_idle_start (struct ev_idle *w)
783{ 916{
784 if (ev_is_active (w)) 917 if (ev_is_active (w))
785 return; 918 return;
786 919
787 ev_start ((W)w, ++idlecnt); 920 ev_start ((W)w, ++idlecnt);
788 array_needsize (idles, idlemax, idlecnt, ); 921 array_needsize (idles, idlemax, idlecnt, );
789 idles [idlecnt - 1] = w; 922 idles [idlecnt - 1] = w;
790} 923}
791 924
925void
792void evidle_stop (struct ev_idle *w) 926ev_idle_stop (struct ev_idle *w)
793{ 927{
794 ev_clear ((W)w); 928 ev_clear_pending ((W)w);
795 if (ev_is_active (w)) 929 if (ev_is_active (w))
796 return; 930 return;
797 931
798 idles [w->active - 1] = idles [--idlecnt]; 932 idles [w->active - 1] = idles [--idlecnt];
799 ev_stop ((W)w); 933 ev_stop ((W)w);
800} 934}
801 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
802void evcheck_start (struct ev_check *w) 959ev_check_start (struct ev_check *w)
803{ 960{
804 if (ev_is_active (w)) 961 if (ev_is_active (w))
805 return; 962 return;
806 963
807 ev_start ((W)w, ++checkcnt); 964 ev_start ((W)w, ++checkcnt);
808 array_needsize (checks, checkmax, checkcnt, ); 965 array_needsize (checks, checkmax, checkcnt, );
809 checks [checkcnt - 1] = w; 966 checks [checkcnt - 1] = w;
810} 967}
811 968
969void
812void evcheck_stop (struct ev_check *w) 970ev_check_stop (struct ev_check *w)
813{ 971{
814 ev_clear ((W)w); 972 ev_clear_pending ((W)w);
815 if (ev_is_active (w)) 973 if (ev_is_active (w))
816 return; 974 return;
817 975
818 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);
819 ev_stop ((W)w); 998 ev_stop ((W)w);
820} 999}
821 1000
822/*****************************************************************************/ 1001/*****************************************************************************/
823 1002
833once_cb (struct ev_once *once, int revents) 1012once_cb (struct ev_once *once, int revents)
834{ 1013{
835 void (*cb)(int revents, void *arg) = once->cb; 1014 void (*cb)(int revents, void *arg) = once->cb;
836 void *arg = once->arg; 1015 void *arg = once->arg;
837 1016
838 evio_stop (&once->io); 1017 ev_io_stop (&once->io);
839 evtimer_stop (&once->to); 1018 ev_timer_stop (&once->to);
840 free (once); 1019 free (once);
841 1020
842 cb (revents, arg); 1021 cb (revents, arg);
843} 1022}
844 1023
858ev_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)
859{ 1038{
860 struct ev_once *once = malloc (sizeof (struct ev_once)); 1039 struct ev_once *once = malloc (sizeof (struct ev_once));
861 1040
862 if (!once) 1041 if (!once)
863 cb (EV_ERROR, arg); 1042 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
864 else 1043 else
865 { 1044 {
866 once->cb = cb; 1045 once->cb = cb;
867 once->arg = arg; 1046 once->arg = arg;
868 1047
869 evw_init (&once->io, once_cb_io); 1048 ev_watcher_init (&once->io, once_cb_io);
870
871 if (fd >= 0) 1049 if (fd >= 0)
872 { 1050 {
873 evio_set (&once->io, fd, events); 1051 ev_io_set (&once->io, fd, events);
874 evio_start (&once->io); 1052 ev_io_start (&once->io);
875 } 1053 }
876 1054
877 evw_init (&once->to, once_cb_to); 1055 ev_watcher_init (&once->to, once_cb_to);
878
879 if (timeout >= 0.) 1056 if (timeout >= 0.)
880 { 1057 {
881 evtimer_set (&once->to, timeout, 0.); 1058 ev_timer_set (&once->to, timeout, 0.);
882 evtimer_start (&once->to); 1059 ev_timer_start (&once->to);
883 } 1060 }
884 } 1061 }
885} 1062}
886 1063
887/*****************************************************************************/ 1064/*****************************************************************************/
898 1075
899static void 1076static void
900ocb (struct ev_timer *w, int revents) 1077ocb (struct ev_timer *w, int revents)
901{ 1078{
902 //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);
903 evtimer_stop (w); 1080 ev_timer_stop (w);
904 evtimer_start (w); 1081 ev_timer_start (w);
905} 1082}
906 1083
907static void 1084static void
908scb (struct ev_signal *w, int revents) 1085scb (struct ev_signal *w, int revents)
909{ 1086{
910 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1087 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
911 evio_stop (&wio); 1088 ev_io_stop (&wio);
912 evio_start (&wio); 1089 ev_io_start (&wio);
913} 1090}
914 1091
915static void 1092static void
916gcb (struct ev_signal *w, int revents) 1093gcb (struct ev_signal *w, int revents)
917{ 1094{
921 1098
922int main (void) 1099int main (void)
923{ 1100{
924 ev_init (0); 1101 ev_init (0);
925 1102
926 evio_init (&wio, sin_cb, 0, EV_READ); 1103 ev_io_init (&wio, sin_cb, 0, EV_READ);
927 evio_start (&wio); 1104 ev_io_start (&wio);
928 1105
929 struct ev_timer t[10000]; 1106 struct ev_timer t[10000];
930 1107
931#if 0 1108#if 0
932 int i; 1109 int i;
933 for (i = 0; i < 10000; ++i) 1110 for (i = 0; i < 10000; ++i)
934 { 1111 {
935 struct ev_timer *w = t + i; 1112 struct ev_timer *w = t + i;
936 evw_init (w, ocb, i); 1113 ev_watcher_init (w, ocb, i);
937 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1114 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
938 evtimer_start (w); 1115 ev_timer_start (w);
939 if (drand48 () < 0.5) 1116 if (drand48 () < 0.5)
940 evtimer_stop (w); 1117 ev_timer_stop (w);
941 } 1118 }
942#endif 1119#endif
943 1120
944 struct ev_timer t1; 1121 struct ev_timer t1;
945 evtimer_init (&t1, ocb, 5, 10); 1122 ev_timer_init (&t1, ocb, 5, 10);
946 evtimer_start (&t1); 1123 ev_timer_start (&t1);
947 1124
948 struct ev_signal sig; 1125 struct ev_signal sig;
949 evsignal_init (&sig, scb, SIGQUIT); 1126 ev_signal_init (&sig, scb, SIGQUIT);
950 evsignal_start (&sig); 1127 ev_signal_start (&sig);
951 1128
952 struct ev_check cw; 1129 struct ev_check cw;
953 evcheck_init (&cw, gcb); 1130 ev_check_init (&cw, gcb);
954 evcheck_start (&cw); 1131 ev_check_start (&cw);
955 1132
956 struct ev_idle iw; 1133 struct ev_idle iw;
957 evidle_init (&iw, gcb); 1134 ev_idle_init (&iw, gcb);
958 evidle_start (&iw); 1135 ev_idle_start (&iw);
959 1136
960 ev_loop (0); 1137 ev_loop (0);
961 1138
962 return 0; 1139 return 0;
963} 1140}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines