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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines