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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines