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

Comparing libev/ev.c (file contents):
Revision 1.15 by root, Wed Oct 31 11:56:34 2007 UTC vs.
Revision 1.37 by root, Thu Nov 1 13:33:12 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines