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

Comparing libev/ev.c (file contents):
Revision 1.34 by root, Thu Nov 1 11:43:11 2007 UTC vs.
Revision 1.67 by root, Mon Nov 5 16:42:15 2007 UTC

1/* 1/*
2 * libev event processing core, watcher management
3 *
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved. 5 * All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 8 * modification, are permitted provided that the following conditions are
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (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. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 30 */
29#if EV_USE_CONFIG_H 31#ifndef EV_STANDALONE
30# include "config.h" 32# include "config.h"
33
34# if HAVE_CLOCK_GETTIME
35# define EV_USE_MONOTONIC 1
36# define EV_USE_REALTIME 1
37# endif
38
39# if HAVE_SELECT && HAVE_SYS_SELECT_H
40# define EV_USE_SELECT 1
41# endif
42
43# if HAVE_POLL && HAVE_POLL_H
44# define EV_USE_POLL 1
45# endif
46
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
48# define EV_USE_EPOLL 1
49# endif
50
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
52# define EV_USE_KQUEUE 1
53# endif
54
31#endif 55#endif
32 56
33#include <math.h> 57#include <math.h>
34#include <stdlib.h> 58#include <stdlib.h>
35#include <unistd.h> 59#include <unistd.h>
40#include <stdio.h> 64#include <stdio.h>
41 65
42#include <assert.h> 66#include <assert.h>
43#include <errno.h> 67#include <errno.h>
44#include <sys/types.h> 68#include <sys/types.h>
69#ifndef WIN32
45#include <sys/wait.h> 70# include <sys/wait.h>
71#endif
46#include <sys/time.h> 72#include <sys/time.h>
47#include <time.h> 73#include <time.h>
48 74
75/**/
76
49#ifndef EV_USE_MONOTONIC 77#ifndef EV_USE_MONOTONIC
50# ifdef CLOCK_MONOTONIC
51# define EV_USE_MONOTONIC 1 78# define EV_USE_MONOTONIC 1
52# endif
53#endif 79#endif
54 80
55#ifndef EV_USE_SELECT 81#ifndef EV_USE_SELECT
56# define EV_USE_SELECT 1 82# define EV_USE_SELECT 1
57#endif 83#endif
58 84
85#ifndef EV_USE_POLL
86# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
87#endif
88
59#ifndef EV_USE_EPOLL 89#ifndef EV_USE_EPOLL
60# define EV_USE_EPOLL 0 90# define EV_USE_EPOLL 0
61#endif 91#endif
62 92
93#ifndef EV_USE_KQUEUE
94# define EV_USE_KQUEUE 0
95#endif
96
97#ifndef EV_USE_WIN32
98# ifdef WIN32
99# define EV_USE_WIN32 1
100# else
101# define EV_USE_WIN32 0
102# endif
103#endif
104
105#ifndef EV_USE_REALTIME
106# define EV_USE_REALTIME 1
107#endif
108
109/**/
110
111#ifndef CLOCK_MONOTONIC
112# undef EV_USE_MONOTONIC
113# define EV_USE_MONOTONIC 0
114#endif
115
63#ifndef CLOCK_REALTIME 116#ifndef CLOCK_REALTIME
117# undef EV_USE_REALTIME
64# define EV_USE_REALTIME 0 118# define EV_USE_REALTIME 0
65#endif 119#endif
66#ifndef EV_USE_REALTIME 120
67# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ 121/**/
68#endif
69 122
70#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 123#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
71#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ 124#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
72#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 125#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 */ 126/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
74 127
75#include "ev.h" 128#include "ev.h"
129
130#if __GNUC__ >= 3
131# define expect(expr,value) __builtin_expect ((expr),(value))
132# define inline inline
133#else
134# define expect(expr,value) (expr)
135# define inline static
136#endif
137
138#define expect_false(expr) expect ((expr) != 0, 0)
139#define expect_true(expr) expect ((expr) != 0, 1)
140
141#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
142#define ABSPRI(w) ((w)->priority - EV_MINPRI)
76 143
77typedef struct ev_watcher *W; 144typedef struct ev_watcher *W;
78typedef struct ev_watcher_list *WL; 145typedef struct ev_watcher_list *WL;
79typedef struct ev_watcher_time *WT; 146typedef struct ev_watcher_time *WT;
80 147
81static ev_tstamp now, diff; /* monotonic clock */ 148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
82ev_tstamp ev_now;
83int ev_method;
84 149
85static int have_monotonic; /* runtime */ 150#if WIN32
86 151/* note: the comment below could not be substantiated, but what would I care */
87static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 152/* MSDN says this is required to handle SIGFPE */
88static void (*method_modify)(int fd, int oev, int nev); 153volatile double SIGFPE_REQ = 0.0f;
89static void (*method_poll)(ev_tstamp timeout); 154#endif
90 155
91/*****************************************************************************/ 156/*****************************************************************************/
92 157
93ev_tstamp 158typedef struct
159{
160 struct ev_watcher_list *head;
161 unsigned char events;
162 unsigned char reify;
163} ANFD;
164
165typedef struct
166{
167 W w;
168 int events;
169} ANPENDING;
170
171#if EV_MULTIPLICITY
172
173struct ev_loop
174{
175# define VAR(name,decl) decl;
176# include "ev_vars.h"
177};
178# undef VAR
179# include "ev_wrap.h"
180
181#else
182
183# define VAR(name,decl) static decl;
184# include "ev_vars.h"
185# undef VAR
186
187#endif
188
189/*****************************************************************************/
190
191inline ev_tstamp
94ev_time (void) 192ev_time (void)
95{ 193{
96#if EV_USE_REALTIME 194#if EV_USE_REALTIME
97 struct timespec ts; 195 struct timespec ts;
98 clock_gettime (CLOCK_REALTIME, &ts); 196 clock_gettime (CLOCK_REALTIME, &ts);
102 gettimeofday (&tv, 0); 200 gettimeofday (&tv, 0);
103 return tv.tv_sec + tv.tv_usec * 1e-6; 201 return tv.tv_sec + tv.tv_usec * 1e-6;
104#endif 202#endif
105} 203}
106 204
107static ev_tstamp 205inline ev_tstamp
108get_clock (void) 206get_clock (void)
109{ 207{
110#if EV_USE_MONOTONIC 208#if EV_USE_MONOTONIC
111 if (have_monotonic) 209 if (expect_true (have_monotonic))
112 { 210 {
113 struct timespec ts; 211 struct timespec ts;
114 clock_gettime (CLOCK_MONOTONIC, &ts); 212 clock_gettime (CLOCK_MONOTONIC, &ts);
115 return ts.tv_sec + ts.tv_nsec * 1e-9; 213 return ts.tv_sec + ts.tv_nsec * 1e-9;
116 } 214 }
117#endif 215#endif
118 216
119 return ev_time (); 217 return ev_time ();
120} 218}
121 219
220ev_tstamp
221ev_now (EV_P)
222{
223 return rt_now;
224}
225
122#define array_roundsize(base,n) ((n) | 4 & ~3) 226#define array_roundsize(base,n) ((n) | 4 & ~3)
123 227
124#define array_needsize(base,cur,cnt,init) \ 228#define array_needsize(base,cur,cnt,init) \
125 if ((cnt) > cur) \ 229 if (expect_false ((cnt) > cur)) \
126 { \ 230 { \
127 int newcnt = cur; \ 231 int newcnt = cur; \
128 do \ 232 do \
129 { \ 233 { \
130 newcnt = array_roundsize (base, newcnt << 1); \ 234 newcnt = array_roundsize (base, newcnt << 1); \
134 base = realloc (base, sizeof (*base) * (newcnt)); \ 238 base = realloc (base, sizeof (*base) * (newcnt)); \
135 init (base + cur, newcnt - cur); \ 239 init (base + cur, newcnt - cur); \
136 cur = newcnt; \ 240 cur = newcnt; \
137 } 241 }
138 242
243#define array_slim(stem) \
244 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
245 { \
246 stem ## max = array_roundsize (stem ## cnt >> 1); \
247 base = realloc (base, sizeof (*base) * (stem ## max)); \
248 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
249 }
250
251#define array_free(stem, idx) \
252 free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
253
139/*****************************************************************************/ 254/*****************************************************************************/
140
141typedef struct
142{
143 struct ev_io *head;
144 unsigned char events;
145 unsigned char reify;
146} ANFD;
147
148static ANFD *anfds;
149static int anfdmax;
150 255
151static void 256static void
152anfds_init (ANFD *base, int count) 257anfds_init (ANFD *base, int count)
153{ 258{
154 while (count--) 259 while (count--)
159 264
160 ++base; 265 ++base;
161 } 266 }
162} 267}
163 268
164typedef struct
165{
166 W w;
167 int events;
168} ANPENDING;
169
170static ANPENDING *pendings;
171static int pendingmax, pendingcnt;
172
173static void 269static void
174event (W w, int events) 270event (EV_P_ W w, int events)
175{ 271{
176 if (w->pending) 272 if (w->pending)
177 { 273 {
178 pendings [w->pending - 1].events |= events; 274 pendings [ABSPRI (w)][w->pending - 1].events |= events;
179 return; 275 return;
180 } 276 }
181 277
182 w->pending = ++pendingcnt; 278 w->pending = ++pendingcnt [ABSPRI (w)];
183 array_needsize (pendings, pendingmax, pendingcnt, ); 279 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
184 pendings [pendingcnt - 1].w = w; 280 pendings [ABSPRI (w)][w->pending - 1].w = w;
185 pendings [pendingcnt - 1].events = events; 281 pendings [ABSPRI (w)][w->pending - 1].events = events;
186} 282}
187 283
188static void 284static void
189queue_events (W *events, int eventcnt, int type) 285queue_events (EV_P_ W *events, int eventcnt, int type)
190{ 286{
191 int i; 287 int i;
192 288
193 for (i = 0; i < eventcnt; ++i) 289 for (i = 0; i < eventcnt; ++i)
194 event (events [i], type); 290 event (EV_A_ events [i], type);
195} 291}
196 292
197static void 293static void
198fd_event (int fd, int events) 294fd_event (EV_P_ int fd, int events)
199{ 295{
200 ANFD *anfd = anfds + fd; 296 ANFD *anfd = anfds + fd;
201 struct ev_io *w; 297 struct ev_io *w;
202 298
203 for (w = anfd->head; w; w = w->next) 299 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
204 { 300 {
205 int ev = w->events & events; 301 int ev = w->events & events;
206 302
207 if (ev) 303 if (ev)
208 event ((W)w, ev); 304 event (EV_A_ (W)w, ev);
209 } 305 }
210} 306}
211 307
212/*****************************************************************************/ 308/*****************************************************************************/
213 309
214static int *fdchanges;
215static int fdchangemax, fdchangecnt;
216
217static void 310static void
218fd_reify (void) 311fd_reify (EV_P)
219{ 312{
220 int i; 313 int i;
221 314
222 for (i = 0; i < fdchangecnt; ++i) 315 for (i = 0; i < fdchangecnt; ++i)
223 { 316 {
225 ANFD *anfd = anfds + fd; 318 ANFD *anfd = anfds + fd;
226 struct ev_io *w; 319 struct ev_io *w;
227 320
228 int events = 0; 321 int events = 0;
229 322
230 for (w = anfd->head; w; w = w->next) 323 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
231 events |= w->events; 324 events |= w->events;
232 325
233 anfd->reify = 0; 326 anfd->reify = 0;
234 327
235 if (anfd->events != events)
236 {
237 method_modify (fd, anfd->events, events); 328 method_modify (EV_A_ fd, anfd->events, events);
238 anfd->events = events; 329 anfd->events = events;
239 }
240 } 330 }
241 331
242 fdchangecnt = 0; 332 fdchangecnt = 0;
243} 333}
244 334
245static void 335static void
246fd_change (int fd) 336fd_change (EV_P_ int fd)
247{ 337{
248 if (anfds [fd].reify || fdchangecnt < 0) 338 if (anfds [fd].reify || fdchangecnt < 0)
249 return; 339 return;
250 340
251 anfds [fd].reify = 1; 341 anfds [fd].reify = 1;
253 ++fdchangecnt; 343 ++fdchangecnt;
254 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 344 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
255 fdchanges [fdchangecnt - 1] = fd; 345 fdchanges [fdchangecnt - 1] = fd;
256} 346}
257 347
348static void
349fd_kill (EV_P_ int fd)
350{
351 struct ev_io *w;
352
353 while ((w = (struct ev_io *)anfds [fd].head))
354 {
355 ev_io_stop (EV_A_ w);
356 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
357 }
358}
359
258/* called on EBADF to verify fds */ 360/* called on EBADF to verify fds */
259static void 361static void
260fd_recheck (void) 362fd_ebadf (EV_P)
261{ 363{
262 int fd; 364 int fd;
263 365
264 for (fd = 0; fd < anfdmax; ++fd) 366 for (fd = 0; fd < anfdmax; ++fd)
265 if (anfds [fd].events) 367 if (anfds [fd].events)
266 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 368 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
267 while (anfds [fd].head) 369 fd_kill (EV_A_ fd);
370}
371
372/* called on ENOMEM in select/poll to kill some fds and retry */
373static void
374fd_enomem (EV_P)
375{
376 int fd;
377
378 for (fd = anfdmax; fd--; )
379 if (anfds [fd].events)
268 { 380 {
269 ev_io_stop (anfds [fd].head); 381 close (fd);
270 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); 382 fd_kill (EV_A_ fd);
383 return;
271 } 384 }
385}
386
387/* susually called after fork if method needs to re-arm all fds from scratch */
388static void
389fd_rearm_all (EV_P)
390{
391 int fd;
392
393 /* this should be highly optimised to not do anything but set a flag */
394 for (fd = 0; fd < anfdmax; ++fd)
395 if (anfds [fd].events)
396 {
397 anfds [fd].events = 0;
398 fd_change (EV_A_ fd);
399 }
272} 400}
273 401
274/*****************************************************************************/ 402/*****************************************************************************/
275 403
276static struct ev_timer **timers;
277static int timermax, timercnt;
278
279static struct ev_periodic **periodics;
280static int periodicmax, periodiccnt;
281
282static void 404static void
283upheap (WT *timers, int k) 405upheap (WT *heap, int k)
284{ 406{
285 WT w = timers [k]; 407 WT w = heap [k];
286 408
287 while (k && timers [k >> 1]->at > w->at) 409 while (k && heap [k >> 1]->at > w->at)
288 { 410 {
289 timers [k] = timers [k >> 1]; 411 heap [k] = heap [k >> 1];
290 timers [k]->active = k + 1; 412 ((W)heap [k])->active = k + 1;
291 k >>= 1; 413 k >>= 1;
292 } 414 }
293 415
294 timers [k] = w; 416 heap [k] = w;
295 timers [k]->active = k + 1; 417 ((W)heap [k])->active = k + 1;
296 418
297} 419}
298 420
299static void 421static void
300downheap (WT *timers, int N, int k) 422downheap (WT *heap, int N, int k)
301{ 423{
302 WT w = timers [k]; 424 WT w = heap [k];
303 425
304 while (k < (N >> 1)) 426 while (k < (N >> 1))
305 { 427 {
306 int j = k << 1; 428 int j = k << 1;
307 429
308 if (j + 1 < N && timers [j]->at > timers [j + 1]->at) 430 if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
309 ++j; 431 ++j;
310 432
311 if (w->at <= timers [j]->at) 433 if (w->at <= heap [j]->at)
312 break; 434 break;
313 435
314 timers [k] = timers [j]; 436 heap [k] = heap [j];
315 timers [k]->active = k + 1; 437 ((W)heap [k])->active = k + 1;
316 k = j; 438 k = j;
317 } 439 }
318 440
319 timers [k] = w; 441 heap [k] = w;
320 timers [k]->active = k + 1; 442 ((W)heap [k])->active = k + 1;
321} 443}
322 444
323/*****************************************************************************/ 445/*****************************************************************************/
324 446
325typedef struct 447typedef struct
326{ 448{
327 struct ev_signal *head; 449 struct ev_watcher_list *head;
328 sig_atomic_t volatile gotsig; 450 sig_atomic_t volatile gotsig;
329} ANSIG; 451} ANSIG;
330 452
331static ANSIG *signals; 453static ANSIG *signals;
332static int signalmax; 454static int signalmax;
348} 470}
349 471
350static void 472static void
351sighandler (int signum) 473sighandler (int signum)
352{ 474{
475#if WIN32
476 signal (signum, sighandler);
477#endif
478
353 signals [signum - 1].gotsig = 1; 479 signals [signum - 1].gotsig = 1;
354 480
355 if (!gotsig) 481 if (!gotsig)
356 { 482 {
483 int old_errno = errno;
357 gotsig = 1; 484 gotsig = 1;
358 write (sigpipe [1], &signum, 1); 485 write (sigpipe [1], &signum, 1);
486 errno = old_errno;
359 } 487 }
360} 488}
361 489
362static void 490static void
363sigcb (struct ev_io *iow, int revents) 491sigcb (EV_P_ struct ev_io *iow, int revents)
364{ 492{
365 struct ev_signal *w; 493 struct ev_watcher_list *w;
366 int sig; 494 int signum;
367 495
368 read (sigpipe [0], &revents, 1); 496 read (sigpipe [0], &revents, 1);
369 gotsig = 0; 497 gotsig = 0;
370 498
371 for (sig = signalmax; sig--; ) 499 for (signum = signalmax; signum--; )
372 if (signals [sig].gotsig) 500 if (signals [signum].gotsig)
373 { 501 {
374 signals [sig].gotsig = 0; 502 signals [signum].gotsig = 0;
375 503
376 for (w = signals [sig].head; w; w = w->next) 504 for (w = signals [signum].head; w; w = w->next)
377 event ((W)w, EV_SIGNAL); 505 event (EV_A_ (W)w, EV_SIGNAL);
378 } 506 }
379} 507}
380 508
381static void 509static void
382siginit (void) 510siginit (EV_P)
383{ 511{
512#ifndef WIN32
384 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 513 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
385 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 514 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
386 515
387 /* rather than sort out wether we really need nb, set it */ 516 /* rather than sort out wether we really need nb, set it */
388 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 517 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
389 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 518 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
519#endif
390 520
391 ev_io_set (&sigev, sigpipe [0], EV_READ); 521 ev_io_set (&sigev, sigpipe [0], EV_READ);
392 ev_io_start (&sigev); 522 ev_io_start (EV_A_ &sigev);
523 ev_unref (EV_A); /* child watcher should not keep loop alive */
393} 524}
394 525
395/*****************************************************************************/ 526/*****************************************************************************/
396 527
397static struct ev_idle **idles; 528#ifndef WIN32
398static int idlemax, idlecnt;
399
400static struct ev_prepare **prepares;
401static int preparemax, preparecnt;
402
403static struct ev_check **checks;
404static int checkmax, checkcnt;
405
406/*****************************************************************************/
407 529
408static struct ev_child *childs [PID_HASHSIZE]; 530static struct ev_child *childs [PID_HASHSIZE];
409static struct ev_signal childev; 531static struct ev_signal childev;
410 532
411#ifndef WCONTINUED 533#ifndef WCONTINUED
412# define WCONTINUED 0 534# define WCONTINUED 0
413#endif 535#endif
414 536
415static void 537static void
416childcb (struct ev_signal *sw, int revents) 538child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status)
417{ 539{
418 struct ev_child *w; 540 struct ev_child *w;
541
542 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
543 if (w->pid == pid || !w->pid)
544 {
545 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
546 w->rpid = pid;
547 w->rstatus = status;
548 event (EV_A_ (W)w, EV_CHILD);
549 }
550}
551
552static void
553childcb (EV_P_ struct ev_signal *sw, int revents)
554{
419 int pid, status; 555 int pid, status;
420 556
421 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 557 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
422 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 558 {
423 if (w->pid == pid || w->pid == -1) 559 /* make sure we are called again until all childs have been reaped */
424 { 560 event (EV_A_ (W)sw, EV_SIGNAL);
425 w->status = status; 561
426 event ((W)w, EV_CHILD); 562 child_reap (EV_A_ sw, pid, pid, status);
427 } 563 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
564 }
428} 565}
566
567#endif
429 568
430/*****************************************************************************/ 569/*****************************************************************************/
431 570
571#if EV_USE_KQUEUE
572# include "ev_kqueue.c"
573#endif
432#if EV_USE_EPOLL 574#if EV_USE_EPOLL
433# include "ev_epoll.c" 575# include "ev_epoll.c"
434#endif 576#endif
577#if EV_USE_POLL
578# include "ev_poll.c"
579#endif
435#if EV_USE_SELECT 580#if EV_USE_SELECT
436# include "ev_select.c" 581# include "ev_select.c"
437#endif 582#endif
438 583
439int 584int
446ev_version_minor (void) 591ev_version_minor (void)
447{ 592{
448 return EV_VERSION_MINOR; 593 return EV_VERSION_MINOR;
449} 594}
450 595
451int ev_init (int flags) 596/* return true if we are running with elevated privileges and should ignore env variables */
597static int
598enable_secure (void)
452{ 599{
600#ifdef WIN32
601 return 0;
602#else
603 return getuid () != geteuid ()
604 || getgid () != getegid ();
605#endif
606}
607
608int
609ev_method (EV_P)
610{
611 return method;
612}
613
614static void
615loop_init (EV_P_ int methods)
616{
453 if (!ev_method) 617 if (!method)
454 { 618 {
455#if EV_USE_MONOTONIC 619#if EV_USE_MONOTONIC
456 { 620 {
457 struct timespec ts; 621 struct timespec ts;
458 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 622 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
459 have_monotonic = 1; 623 have_monotonic = 1;
460 } 624 }
461#endif 625#endif
462 626
463 ev_now = ev_time (); 627 rt_now = ev_time ();
464 now = get_clock (); 628 mn_now = get_clock ();
629 now_floor = mn_now;
465 diff = ev_now - now; 630 rtmn_diff = rt_now - mn_now;
466 631
467 if (pipe (sigpipe)) 632 if (methods == EVMETHOD_AUTO)
468 return 0; 633 if (!enable_secure () && getenv ("LIBEV_METHODS"))
469 634 methods = atoi (getenv ("LIBEV_METHODS"));
635 else
470 ev_method = EVMETHOD_NONE; 636 methods = EVMETHOD_ANY;
637
638 method = 0;
639#if EV_USE_WIN32
640 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
641#endif
642#if EV_USE_KQUEUE
643 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
644#endif
471#if EV_USE_EPOLL 645#if EV_USE_EPOLL
472 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 646 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
647#endif
648#if EV_USE_POLL
649 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
473#endif 650#endif
474#if EV_USE_SELECT 651#if EV_USE_SELECT
475 if (ev_method == EVMETHOD_NONE) select_init (flags); 652 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
476#endif 653#endif
654 }
655}
477 656
657void
658loop_destroy (EV_P)
659{
660 int i;
661
662#if EV_USE_WIN32
663 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
664#endif
665#if EV_USE_KQUEUE
666 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
667#endif
668#if EV_USE_EPOLL
669 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
670#endif
671#if EV_USE_POLL
672 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
673#endif
674#if EV_USE_SELECT
675 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
676#endif
677
678 for (i = NUMPRI; i--; )
679 array_free (pending, [i]);
680
681 array_free (fdchange, );
682 array_free (timer, );
683 array_free (periodic, );
684 array_free (idle, );
685 array_free (prepare, );
686 array_free (check, );
687
688 method = 0;
689 /*TODO*/
690}
691
692void
693loop_fork (EV_P)
694{
695 /*TODO*/
696#if EV_USE_EPOLL
697 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
698#endif
699#if EV_USE_KQUEUE
700 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
701#endif
702}
703
704#if EV_MULTIPLICITY
705struct ev_loop *
706ev_loop_new (int methods)
707{
708 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
709
710 loop_init (EV_A_ methods);
711
712 if (ev_method (EV_A))
713 return loop;
714
715 return 0;
716}
717
718void
719ev_loop_destroy (EV_P)
720{
721 loop_destroy (EV_A);
722 free (loop);
723}
724
725void
726ev_loop_fork (EV_P)
727{
728 loop_fork (EV_A);
729}
730
731#endif
732
733#if EV_MULTIPLICITY
734struct ev_loop default_loop_struct;
735static struct ev_loop *default_loop;
736
737struct ev_loop *
738#else
739static int default_loop;
740
741int
742#endif
743ev_default_loop (int methods)
744{
745 if (sigpipe [0] == sigpipe [1])
746 if (pipe (sigpipe))
747 return 0;
748
749 if (!default_loop)
750 {
751#if EV_MULTIPLICITY
752 struct ev_loop *loop = default_loop = &default_loop_struct;
753#else
754 default_loop = 1;
755#endif
756
757 loop_init (EV_A_ methods);
758
478 if (ev_method) 759 if (ev_method (EV_A))
479 { 760 {
480 ev_watcher_init (&sigev, sigcb); 761 ev_watcher_init (&sigev, sigcb);
762 ev_set_priority (&sigev, EV_MAXPRI);
481 siginit (); 763 siginit (EV_A);
482 764
765#ifndef WIN32
483 ev_signal_init (&childev, childcb, SIGCHLD); 766 ev_signal_init (&childev, childcb, SIGCHLD);
767 ev_set_priority (&childev, EV_MAXPRI);
484 ev_signal_start (&childev); 768 ev_signal_start (EV_A_ &childev);
769 ev_unref (EV_A); /* child watcher should not keep loop alive */
770#endif
485 } 771 }
772 else
773 default_loop = 0;
486 } 774 }
487 775
488 return ev_method; 776 return default_loop;
489} 777}
490 778
491/*****************************************************************************/
492
493void 779void
494ev_prefork (void) 780ev_default_destroy (void)
495{ 781{
496 /* nop */ 782#if EV_MULTIPLICITY
497} 783 struct ev_loop *loop = default_loop;
498
499void
500ev_postfork_parent (void)
501{
502 /* nop */
503}
504
505void
506ev_postfork_child (void)
507{
508#if EV_USE_EPOLL
509 if (ev_method == EVMETHOD_EPOLL)
510 epoll_postfork_child ();
511#endif 784#endif
512 785
786 ev_ref (EV_A); /* child watcher */
787 ev_signal_stop (EV_A_ &childev);
788
789 ev_ref (EV_A); /* signal watcher */
513 ev_io_stop (&sigev); 790 ev_io_stop (EV_A_ &sigev);
791
792 close (sigpipe [0]); sigpipe [0] = 0;
793 close (sigpipe [1]); sigpipe [1] = 0;
794
795 loop_destroy (EV_A);
796}
797
798void
799ev_default_fork (void)
800{
801#if EV_MULTIPLICITY
802 struct ev_loop *loop = default_loop;
803#endif
804
805 loop_fork (EV_A);
806
807 ev_io_stop (EV_A_ &sigev);
514 close (sigpipe [0]); 808 close (sigpipe [0]);
515 close (sigpipe [1]); 809 close (sigpipe [1]);
516 pipe (sigpipe); 810 pipe (sigpipe);
811
812 ev_ref (EV_A); /* signal watcher */
517 siginit (); 813 siginit (EV_A);
518} 814}
519 815
520/*****************************************************************************/ 816/*****************************************************************************/
521 817
522static void 818static void
523call_pending (void) 819call_pending (EV_P)
524{ 820{
821 int pri;
822
823 for (pri = NUMPRI; pri--; )
525 while (pendingcnt) 824 while (pendingcnt [pri])
526 { 825 {
527 ANPENDING *p = pendings + --pendingcnt; 826 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
528 827
529 if (p->w) 828 if (p->w)
530 { 829 {
531 p->w->pending = 0; 830 p->w->pending = 0;
532 p->w->cb (p->w, p->events); 831 p->w->cb (EV_A_ p->w, p->events);
533 } 832 }
534 } 833 }
535} 834}
536 835
537static void 836static void
538timers_reify (void) 837timers_reify (EV_P)
539{ 838{
540 while (timercnt && timers [0]->at <= now) 839 while (timercnt && ((WT)timers [0])->at <= mn_now)
541 { 840 {
542 struct ev_timer *w = timers [0]; 841 struct ev_timer *w = timers [0];
842
843 assert (("inactive timer on timer heap detected", ev_is_active (w)));
543 844
544 /* first reschedule or stop timer */ 845 /* first reschedule or stop timer */
545 if (w->repeat) 846 if (w->repeat)
546 { 847 {
547 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 848 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
548 w->at = now + w->repeat; 849 ((WT)w)->at = mn_now + w->repeat;
549 downheap ((WT *)timers, timercnt, 0); 850 downheap ((WT *)timers, timercnt, 0);
550 } 851 }
551 else 852 else
552 ev_timer_stop (w); /* nonrepeating: stop timer */ 853 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
553 854
554 event ((W)w, EV_TIMEOUT); 855 event (EV_A_ (W)w, EV_TIMEOUT);
555 } 856 }
556} 857}
557 858
558static void 859static void
559periodics_reify (void) 860periodics_reify (EV_P)
560{ 861{
561 while (periodiccnt && periodics [0]->at <= ev_now) 862 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
562 { 863 {
563 struct ev_periodic *w = periodics [0]; 864 struct ev_periodic *w = periodics [0];
865
866 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
564 867
565 /* first reschedule or stop timer */ 868 /* first reschedule or stop timer */
566 if (w->interval) 869 if (w->interval)
567 { 870 {
568 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 871 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
569 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); 872 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
570 downheap ((WT *)periodics, periodiccnt, 0); 873 downheap ((WT *)periodics, periodiccnt, 0);
571 } 874 }
572 else 875 else
573 ev_periodic_stop (w); /* nonrepeating: stop timer */ 876 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
574 877
575 event ((W)w, EV_PERIODIC); 878 event (EV_A_ (W)w, EV_PERIODIC);
576 } 879 }
577} 880}
578 881
579static void 882static void
580periodics_reschedule (ev_tstamp diff) 883periodics_reschedule (EV_P)
581{ 884{
582 int i; 885 int i;
583 886
584 /* adjust periodics after time jump */ 887 /* adjust periodics after time jump */
585 for (i = 0; i < periodiccnt; ++i) 888 for (i = 0; i < periodiccnt; ++i)
586 { 889 {
587 struct ev_periodic *w = periodics [i]; 890 struct ev_periodic *w = periodics [i];
588 891
589 if (w->interval) 892 if (w->interval)
590 { 893 {
591 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; 894 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
592 895
593 if (fabs (diff) >= 1e-4) 896 if (fabs (diff) >= 1e-4)
594 { 897 {
595 ev_periodic_stop (w); 898 ev_periodic_stop (EV_A_ w);
596 ev_periodic_start (w); 899 ev_periodic_start (EV_A_ w);
597 900
598 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 901 i = 0; /* restart loop, inefficient, but time jumps should be rare */
599 } 902 }
600 } 903 }
601 } 904 }
602} 905}
603 906
907inline int
908time_update_monotonic (EV_P)
909{
910 mn_now = get_clock ();
911
912 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
913 {
914 rt_now = rtmn_diff + mn_now;
915 return 0;
916 }
917 else
918 {
919 now_floor = mn_now;
920 rt_now = ev_time ();
921 return 1;
922 }
923}
924
604static void 925static void
605time_update (void) 926time_update (EV_P)
606{ 927{
607 int i; 928 int i;
608 929
609 ev_now = ev_time (); 930#if EV_USE_MONOTONIC
610
611 if (have_monotonic) 931 if (expect_true (have_monotonic))
612 { 932 {
613 ev_tstamp odiff = diff; 933 if (time_update_monotonic (EV_A))
614
615 for (i = 4; --i; ) /* loop a few times, before making important decisions */
616 { 934 {
617 now = get_clock (); 935 ev_tstamp odiff = rtmn_diff;
936
937 for (i = 4; --i; ) /* loop a few times, before making important decisions */
938 {
618 diff = ev_now - now; 939 rtmn_diff = rt_now - mn_now;
619 940
620 if (fabs (odiff - diff) < MIN_TIMEJUMP) 941 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
621 return; /* all is well */ 942 return; /* all is well */
622 943
623 ev_now = ev_time (); 944 rt_now = ev_time ();
945 mn_now = get_clock ();
946 now_floor = mn_now;
947 }
948
949 periodics_reschedule (EV_A);
950 /* no timer adjustment, as the monotonic clock doesn't jump */
951 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
624 } 952 }
625
626 periodics_reschedule (diff - odiff);
627 /* no timer adjustment, as the monotonic clock doesn't jump */
628 } 953 }
629 else 954 else
955#endif
630 { 956 {
631 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 957 rt_now = ev_time ();
958
959 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
632 { 960 {
633 periodics_reschedule (ev_now - now); 961 periodics_reschedule (EV_A);
634 962
635 /* adjust timers. this is easy, as the offset is the same for all */ 963 /* adjust timers. this is easy, as the offset is the same for all */
636 for (i = 0; i < timercnt; ++i) 964 for (i = 0; i < timercnt; ++i)
637 timers [i]->at += diff; 965 ((WT)timers [i])->at += rt_now - mn_now;
638 } 966 }
639 967
640 now = ev_now; 968 mn_now = rt_now;
641 } 969 }
642} 970}
643 971
644int ev_loop_done; 972void
973ev_ref (EV_P)
974{
975 ++activecnt;
976}
645 977
978void
979ev_unref (EV_P)
980{
981 --activecnt;
982}
983
984static int loop_done;
985
986void
646void ev_loop (int flags) 987ev_loop (EV_P_ int flags)
647{ 988{
648 double block; 989 double block;
649 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 990 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
650 991
651 do 992 do
652 { 993 {
653 /* queue check watchers (and execute them) */ 994 /* queue check watchers (and execute them) */
654 if (preparecnt) 995 if (expect_false (preparecnt))
655 { 996 {
656 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 997 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
657 call_pending (); 998 call_pending (EV_A);
658 } 999 }
659 1000
660 /* update fd-related kernel structures */ 1001 /* update fd-related kernel structures */
661 fd_reify (); 1002 fd_reify (EV_A);
662 1003
663 /* calculate blocking time */ 1004 /* calculate blocking time */
664 1005
665 /* we only need this for !monotonic clockor timers, but as we basically 1006 /* we only need this for !monotonic clockor timers, but as we basically
666 always have timers, we just calculate it always */ 1007 always have timers, we just calculate it always */
1008#if EV_USE_MONOTONIC
1009 if (expect_true (have_monotonic))
1010 time_update_monotonic (EV_A);
1011 else
1012#endif
1013 {
667 ev_now = ev_time (); 1014 rt_now = ev_time ();
1015 mn_now = rt_now;
1016 }
668 1017
669 if (flags & EVLOOP_NONBLOCK || idlecnt) 1018 if (flags & EVLOOP_NONBLOCK || idlecnt)
670 block = 0.; 1019 block = 0.;
671 else 1020 else
672 { 1021 {
673 block = MAX_BLOCKTIME; 1022 block = MAX_BLOCKTIME;
674 1023
675 if (timercnt) 1024 if (timercnt)
676 { 1025 {
677 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 1026 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
678 if (block > to) block = to; 1027 if (block > to) block = to;
679 } 1028 }
680 1029
681 if (periodiccnt) 1030 if (periodiccnt)
682 { 1031 {
683 ev_tstamp to = periodics [0]->at - ev_now + method_fudge; 1032 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
684 if (block > to) block = to; 1033 if (block > to) block = to;
685 } 1034 }
686 1035
687 if (block < 0.) block = 0.; 1036 if (block < 0.) block = 0.;
688 } 1037 }
689 1038
690 method_poll (block); 1039 method_poll (EV_A_ block);
691 1040
692 /* update ev_now, do magic */ 1041 /* update rt_now, do magic */
693 time_update (); 1042 time_update (EV_A);
694 1043
695 /* queue pending timers and reschedule them */ 1044 /* queue pending timers and reschedule them */
696 timers_reify (); /* relative timers called last */ 1045 timers_reify (EV_A); /* relative timers called last */
697 periodics_reify (); /* absolute timers called first */ 1046 periodics_reify (EV_A); /* absolute timers called first */
698 1047
699 /* queue idle watchers unless io or timers are pending */ 1048 /* queue idle watchers unless io or timers are pending */
700 if (!pendingcnt) 1049 if (!pendingcnt)
701 queue_events ((W *)idles, idlecnt, EV_IDLE); 1050 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
702 1051
703 /* queue check watchers, to be executed first */ 1052 /* queue check watchers, to be executed first */
704 if (checkcnt) 1053 if (checkcnt)
705 queue_events ((W *)checks, checkcnt, EV_CHECK); 1054 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
706 1055
707 call_pending (); 1056 call_pending (EV_A);
708 } 1057 }
709 while (!ev_loop_done); 1058 while (activecnt && !loop_done);
710 1059
711 if (ev_loop_done != 2) 1060 if (loop_done != 2)
712 ev_loop_done = 0; 1061 loop_done = 0;
1062}
1063
1064void
1065ev_unloop (EV_P_ int how)
1066{
1067 loop_done = how;
713} 1068}
714 1069
715/*****************************************************************************/ 1070/*****************************************************************************/
716 1071
717static void 1072inline void
718wlist_add (WL *head, WL elem) 1073wlist_add (WL *head, WL elem)
719{ 1074{
720 elem->next = *head; 1075 elem->next = *head;
721 *head = elem; 1076 *head = elem;
722} 1077}
723 1078
724static void 1079inline void
725wlist_del (WL *head, WL elem) 1080wlist_del (WL *head, WL elem)
726{ 1081{
727 while (*head) 1082 while (*head)
728 { 1083 {
729 if (*head == elem) 1084 if (*head == elem)
734 1089
735 head = &(*head)->next; 1090 head = &(*head)->next;
736 } 1091 }
737} 1092}
738 1093
739static void 1094inline void
740ev_clear_pending (W w) 1095ev_clear_pending (EV_P_ W w)
741{ 1096{
742 if (w->pending) 1097 if (w->pending)
743 { 1098 {
744 pendings [w->pending - 1].w = 0; 1099 pendings [ABSPRI (w)][w->pending - 1].w = 0;
745 w->pending = 0; 1100 w->pending = 0;
746 } 1101 }
747} 1102}
748 1103
749static void 1104inline void
750ev_start (W w, int active) 1105ev_start (EV_P_ W w, int active)
751{ 1106{
1107 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
1108 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1109
752 w->active = active; 1110 w->active = active;
1111 ev_ref (EV_A);
753} 1112}
754 1113
755static void 1114inline void
756ev_stop (W w) 1115ev_stop (EV_P_ W w)
757{ 1116{
1117 ev_unref (EV_A);
758 w->active = 0; 1118 w->active = 0;
759} 1119}
760 1120
761/*****************************************************************************/ 1121/*****************************************************************************/
762 1122
763void 1123void
764ev_io_start (struct ev_io *w) 1124ev_io_start (EV_P_ struct ev_io *w)
765{ 1125{
1126 int fd = w->fd;
1127
766 if (ev_is_active (w)) 1128 if (ev_is_active (w))
767 return; 1129 return;
768 1130
769 int fd = w->fd;
770
771 assert (("ev_io_start called with negative fd", fd >= 0)); 1131 assert (("ev_io_start called with negative fd", fd >= 0));
772 1132
773 ev_start ((W)w, 1); 1133 ev_start (EV_A_ (W)w, 1);
774 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 1134 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
775 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1135 wlist_add ((WL *)&anfds[fd].head, (WL)w);
776 1136
777 fd_change (fd); 1137 fd_change (EV_A_ fd);
778} 1138}
779 1139
780void 1140void
781ev_io_stop (struct ev_io *w) 1141ev_io_stop (EV_P_ struct ev_io *w)
782{ 1142{
783 ev_clear_pending ((W)w); 1143 ev_clear_pending (EV_A_ (W)w);
784 if (!ev_is_active (w)) 1144 if (!ev_is_active (w))
785 return; 1145 return;
786 1146
787 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1147 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
788 ev_stop ((W)w); 1148 ev_stop (EV_A_ (W)w);
789 1149
790 fd_change (w->fd); 1150 fd_change (EV_A_ w->fd);
791} 1151}
792 1152
793void 1153void
794ev_timer_start (struct ev_timer *w) 1154ev_timer_start (EV_P_ struct ev_timer *w)
795{ 1155{
796 if (ev_is_active (w)) 1156 if (ev_is_active (w))
797 return; 1157 return;
798 1158
799 w->at += now; 1159 ((WT)w)->at += mn_now;
800 1160
801 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1161 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
802 1162
803 ev_start ((W)w, ++timercnt); 1163 ev_start (EV_A_ (W)w, ++timercnt);
804 array_needsize (timers, timermax, timercnt, ); 1164 array_needsize (timers, timermax, timercnt, );
805 timers [timercnt - 1] = w; 1165 timers [timercnt - 1] = w;
806 upheap ((WT *)timers, timercnt - 1); 1166 upheap ((WT *)timers, timercnt - 1);
807}
808 1167
1168 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1169}
1170
809void 1171void
810ev_timer_stop (struct ev_timer *w) 1172ev_timer_stop (EV_P_ struct ev_timer *w)
811{ 1173{
812 ev_clear_pending ((W)w); 1174 ev_clear_pending (EV_A_ (W)w);
813 if (!ev_is_active (w)) 1175 if (!ev_is_active (w))
814 return; 1176 return;
815 1177
1178 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1179
816 if (w->active < timercnt--) 1180 if (((W)w)->active < timercnt--)
817 { 1181 {
818 timers [w->active - 1] = timers [timercnt]; 1182 timers [((W)w)->active - 1] = timers [timercnt];
819 downheap ((WT *)timers, timercnt, w->active - 1); 1183 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
820 } 1184 }
821 1185
822 w->at = w->repeat; 1186 ((WT)w)->at = w->repeat;
823 1187
824 ev_stop ((W)w); 1188 ev_stop (EV_A_ (W)w);
825} 1189}
826 1190
827void 1191void
828ev_timer_again (struct ev_timer *w) 1192ev_timer_again (EV_P_ struct ev_timer *w)
829{ 1193{
830 if (ev_is_active (w)) 1194 if (ev_is_active (w))
831 { 1195 {
832 if (w->repeat) 1196 if (w->repeat)
833 { 1197 {
834 w->at = now + w->repeat; 1198 ((WT)w)->at = mn_now + w->repeat;
835 downheap ((WT *)timers, timercnt, w->active - 1); 1199 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
836 } 1200 }
837 else 1201 else
838 ev_timer_stop (w); 1202 ev_timer_stop (EV_A_ w);
839 } 1203 }
840 else if (w->repeat) 1204 else if (w->repeat)
841 ev_timer_start (w); 1205 ev_timer_start (EV_A_ w);
842} 1206}
843 1207
844void 1208void
845ev_periodic_start (struct ev_periodic *w) 1209ev_periodic_start (EV_P_ struct ev_periodic *w)
846{ 1210{
847 if (ev_is_active (w)) 1211 if (ev_is_active (w))
848 return; 1212 return;
849 1213
850 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1214 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
851 1215
852 /* this formula differs from the one in periodic_reify because we do not always round up */ 1216 /* this formula differs from the one in periodic_reify because we do not always round up */
853 if (w->interval) 1217 if (w->interval)
854 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 1218 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
855 1219
856 ev_start ((W)w, ++periodiccnt); 1220 ev_start (EV_A_ (W)w, ++periodiccnt);
857 array_needsize (periodics, periodicmax, periodiccnt, ); 1221 array_needsize (periodics, periodicmax, periodiccnt, );
858 periodics [periodiccnt - 1] = w; 1222 periodics [periodiccnt - 1] = w;
859 upheap ((WT *)periodics, periodiccnt - 1); 1223 upheap ((WT *)periodics, periodiccnt - 1);
860}
861 1224
1225 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1226}
1227
862void 1228void
863ev_periodic_stop (struct ev_periodic *w) 1229ev_periodic_stop (EV_P_ struct ev_periodic *w)
864{ 1230{
865 ev_clear_pending ((W)w); 1231 ev_clear_pending (EV_A_ (W)w);
866 if (!ev_is_active (w)) 1232 if (!ev_is_active (w))
867 return; 1233 return;
868 1234
1235 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1236
869 if (w->active < periodiccnt--) 1237 if (((W)w)->active < periodiccnt--)
870 { 1238 {
871 periodics [w->active - 1] = periodics [periodiccnt]; 1239 periodics [((W)w)->active - 1] = periodics [periodiccnt];
872 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1240 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
873 } 1241 }
874 1242
875 ev_stop ((W)w); 1243 ev_stop (EV_A_ (W)w);
876} 1244}
877 1245
878void 1246void
879ev_signal_start (struct ev_signal *w) 1247ev_idle_start (EV_P_ struct ev_idle *w)
880{ 1248{
881 if (ev_is_active (w)) 1249 if (ev_is_active (w))
882 return; 1250 return;
883 1251
1252 ev_start (EV_A_ (W)w, ++idlecnt);
1253 array_needsize (idles, idlemax, idlecnt, );
1254 idles [idlecnt - 1] = w;
1255}
1256
1257void
1258ev_idle_stop (EV_P_ struct ev_idle *w)
1259{
1260 ev_clear_pending (EV_A_ (W)w);
1261 if (ev_is_active (w))
1262 return;
1263
1264 idles [((W)w)->active - 1] = idles [--idlecnt];
1265 ev_stop (EV_A_ (W)w);
1266}
1267
1268void
1269ev_prepare_start (EV_P_ struct ev_prepare *w)
1270{
1271 if (ev_is_active (w))
1272 return;
1273
1274 ev_start (EV_A_ (W)w, ++preparecnt);
1275 array_needsize (prepares, preparemax, preparecnt, );
1276 prepares [preparecnt - 1] = w;
1277}
1278
1279void
1280ev_prepare_stop (EV_P_ struct ev_prepare *w)
1281{
1282 ev_clear_pending (EV_A_ (W)w);
1283 if (ev_is_active (w))
1284 return;
1285
1286 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1287 ev_stop (EV_A_ (W)w);
1288}
1289
1290void
1291ev_check_start (EV_P_ struct ev_check *w)
1292{
1293 if (ev_is_active (w))
1294 return;
1295
1296 ev_start (EV_A_ (W)w, ++checkcnt);
1297 array_needsize (checks, checkmax, checkcnt, );
1298 checks [checkcnt - 1] = w;
1299}
1300
1301void
1302ev_check_stop (EV_P_ struct ev_check *w)
1303{
1304 ev_clear_pending (EV_A_ (W)w);
1305 if (ev_is_active (w))
1306 return;
1307
1308 checks [((W)w)->active - 1] = checks [--checkcnt];
1309 ev_stop (EV_A_ (W)w);
1310}
1311
1312#ifndef SA_RESTART
1313# define SA_RESTART 0
1314#endif
1315
1316void
1317ev_signal_start (EV_P_ struct ev_signal *w)
1318{
1319#if EV_MULTIPLICITY
1320 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1321#endif
1322 if (ev_is_active (w))
1323 return;
1324
884 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1325 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
885 1326
886 ev_start ((W)w, 1); 1327 ev_start (EV_A_ (W)w, 1);
887 array_needsize (signals, signalmax, w->signum, signals_init); 1328 array_needsize (signals, signalmax, w->signum, signals_init);
888 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1329 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
889 1330
890 if (!w->next) 1331 if (!((WL)w)->next)
891 { 1332 {
1333#if WIN32
1334 signal (w->signum, sighandler);
1335#else
892 struct sigaction sa; 1336 struct sigaction sa;
893 sa.sa_handler = sighandler; 1337 sa.sa_handler = sighandler;
894 sigfillset (&sa.sa_mask); 1338 sigfillset (&sa.sa_mask);
895 sa.sa_flags = 0; 1339 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
896 sigaction (w->signum, &sa, 0); 1340 sigaction (w->signum, &sa, 0);
1341#endif
897 } 1342 }
898} 1343}
899 1344
900void 1345void
901ev_signal_stop (struct ev_signal *w) 1346ev_signal_stop (EV_P_ struct ev_signal *w)
902{ 1347{
903 ev_clear_pending ((W)w); 1348 ev_clear_pending (EV_A_ (W)w);
904 if (!ev_is_active (w)) 1349 if (!ev_is_active (w))
905 return; 1350 return;
906 1351
907 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1352 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
908 ev_stop ((W)w); 1353 ev_stop (EV_A_ (W)w);
909 1354
910 if (!signals [w->signum - 1].head) 1355 if (!signals [w->signum - 1].head)
911 signal (w->signum, SIG_DFL); 1356 signal (w->signum, SIG_DFL);
912} 1357}
913 1358
914void 1359void
915ev_idle_start (struct ev_idle *w) 1360ev_child_start (EV_P_ struct ev_child *w)
916{ 1361{
1362#if EV_MULTIPLICITY
1363 assert (("child watchers are only supported in the default loop", loop == default_loop));
1364#endif
917 if (ev_is_active (w)) 1365 if (ev_is_active (w))
918 return; 1366 return;
919 1367
920 ev_start ((W)w, ++idlecnt); 1368 ev_start (EV_A_ (W)w, 1);
921 array_needsize (idles, idlemax, idlecnt, ); 1369 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
922 idles [idlecnt - 1] = w;
923} 1370}
924 1371
925void 1372void
926ev_idle_stop (struct ev_idle *w) 1373ev_child_stop (EV_P_ struct ev_child *w)
927{ 1374{
928 ev_clear_pending ((W)w); 1375 ev_clear_pending (EV_A_ (W)w);
929 if (ev_is_active (w)) 1376 if (ev_is_active (w))
930 return; 1377 return;
931 1378
932 idles [w->active - 1] = idles [--idlecnt];
933 ev_stop ((W)w);
934}
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
959ev_check_start (struct ev_check *w)
960{
961 if (ev_is_active (w))
962 return;
963
964 ev_start ((W)w, ++checkcnt);
965 array_needsize (checks, checkmax, checkcnt, );
966 checks [checkcnt - 1] = w;
967}
968
969void
970ev_check_stop (struct ev_check *w)
971{
972 ev_clear_pending ((W)w);
973 if (ev_is_active (w))
974 return;
975
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); 1379 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
998 ev_stop ((W)w); 1380 ev_stop (EV_A_ (W)w);
999} 1381}
1000 1382
1001/*****************************************************************************/ 1383/*****************************************************************************/
1002 1384
1003struct ev_once 1385struct ev_once
1007 void (*cb)(int revents, void *arg); 1389 void (*cb)(int revents, void *arg);
1008 void *arg; 1390 void *arg;
1009}; 1391};
1010 1392
1011static void 1393static void
1012once_cb (struct ev_once *once, int revents) 1394once_cb (EV_P_ struct ev_once *once, int revents)
1013{ 1395{
1014 void (*cb)(int revents, void *arg) = once->cb; 1396 void (*cb)(int revents, void *arg) = once->cb;
1015 void *arg = once->arg; 1397 void *arg = once->arg;
1016 1398
1017 ev_io_stop (&once->io); 1399 ev_io_stop (EV_A_ &once->io);
1018 ev_timer_stop (&once->to); 1400 ev_timer_stop (EV_A_ &once->to);
1019 free (once); 1401 free (once);
1020 1402
1021 cb (revents, arg); 1403 cb (revents, arg);
1022} 1404}
1023 1405
1024static void 1406static void
1025once_cb_io (struct ev_io *w, int revents) 1407once_cb_io (EV_P_ struct ev_io *w, int revents)
1026{ 1408{
1027 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); 1409 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1028} 1410}
1029 1411
1030static void 1412static void
1031once_cb_to (struct ev_timer *w, int revents) 1413once_cb_to (EV_P_ struct ev_timer *w, int revents)
1032{ 1414{
1033 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); 1415 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1034} 1416}
1035 1417
1036void 1418void
1037ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1419ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1038{ 1420{
1039 struct ev_once *once = malloc (sizeof (struct ev_once)); 1421 struct ev_once *once = malloc (sizeof (struct ev_once));
1040 1422
1041 if (!once) 1423 if (!once)
1042 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1424 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1047 1429
1048 ev_watcher_init (&once->io, once_cb_io); 1430 ev_watcher_init (&once->io, once_cb_io);
1049 if (fd >= 0) 1431 if (fd >= 0)
1050 { 1432 {
1051 ev_io_set (&once->io, fd, events); 1433 ev_io_set (&once->io, fd, events);
1052 ev_io_start (&once->io); 1434 ev_io_start (EV_A_ &once->io);
1053 } 1435 }
1054 1436
1055 ev_watcher_init (&once->to, once_cb_to); 1437 ev_watcher_init (&once->to, once_cb_to);
1056 if (timeout >= 0.) 1438 if (timeout >= 0.)
1057 { 1439 {
1058 ev_timer_set (&once->to, timeout, 0.); 1440 ev_timer_set (&once->to, timeout, 0.);
1059 ev_timer_start (&once->to); 1441 ev_timer_start (EV_A_ &once->to);
1060 } 1442 }
1061 } 1443 }
1062} 1444}
1063 1445
1064/*****************************************************************************/
1065
1066#if 0
1067
1068struct ev_io wio;
1069
1070static void
1071sin_cb (struct ev_io *w, int revents)
1072{
1073 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1074}
1075
1076static void
1077ocb (struct ev_timer *w, int revents)
1078{
1079 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1080 ev_timer_stop (w);
1081 ev_timer_start (w);
1082}
1083
1084static void
1085scb (struct ev_signal *w, int revents)
1086{
1087 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1088 ev_io_stop (&wio);
1089 ev_io_start (&wio);
1090}
1091
1092static void
1093gcb (struct ev_signal *w, int revents)
1094{
1095 fprintf (stderr, "generic %x\n", revents);
1096
1097}
1098
1099int main (void)
1100{
1101 ev_init (0);
1102
1103 ev_io_init (&wio, sin_cb, 0, EV_READ);
1104 ev_io_start (&wio);
1105
1106 struct ev_timer t[10000];
1107
1108#if 0
1109 int i;
1110 for (i = 0; i < 10000; ++i)
1111 {
1112 struct ev_timer *w = t + i;
1113 ev_watcher_init (w, ocb, i);
1114 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1115 ev_timer_start (w);
1116 if (drand48 () < 0.5)
1117 ev_timer_stop (w);
1118 }
1119#endif
1120
1121 struct ev_timer t1;
1122 ev_timer_init (&t1, ocb, 5, 10);
1123 ev_timer_start (&t1);
1124
1125 struct ev_signal sig;
1126 ev_signal_init (&sig, scb, SIGQUIT);
1127 ev_signal_start (&sig);
1128
1129 struct ev_check cw;
1130 ev_check_init (&cw, gcb);
1131 ev_check_start (&cw);
1132
1133 struct ev_idle iw;
1134 ev_idle_init (&iw, gcb);
1135 ev_idle_start (&iw);
1136
1137 ev_loop (0);
1138
1139 return 0;
1140}
1141
1142#endif
1143
1144
1145
1146

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines