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

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