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

Comparing libev/ev.c (file contents):
Revision 1.36 by root, Thu Nov 1 13:11:11 2007 UTC vs.
Revision 1.68 by root, Mon Nov 5 20:19:00 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines