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

Comparing libev/ev.c (file contents):
Revision 1.51 by root, Sat Nov 3 21:58:51 2007 UTC vs.
Revision 1.71 by root, Tue Nov 6 13:17:55 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines