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

Comparing libev/ev.c (file contents):
Revision 1.59 by root, Sun Nov 4 18:15:16 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
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
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 int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 153static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
119 154
155#if WIN32
156/* note: the comment below could not be substantiated, but what would I care */
157/* MSDN says this is required to handle SIGFPE */
158volatile double SIGFPE_REQ = 0.0f;
159#endif
160
120/*****************************************************************************/ 161/*****************************************************************************/
121 162
163static void (*syserr_cb)(const char *msg);
164
165void ev_set_syserr_cb (void (*cb)(const char *msg))
166{
167 syserr_cb = cb;
168}
169
170static void
171syserr (const char *msg)
172{
173 if (!msg)
174 msg = "(libev) system error";
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
122typedef struct 211typedef struct
123{ 212{
124 struct ev_watcher_list *head; 213 WL head;
125 unsigned char events; 214 unsigned char events;
126 unsigned char reify; 215 unsigned char reify;
127} ANFD; 216} ANFD;
128 217
129typedef struct 218typedef struct
187 return rt_now; 276 return rt_now;
188} 277}
189 278
190#define array_roundsize(base,n) ((n) | 4 & ~3) 279#define array_roundsize(base,n) ((n) | 4 & ~3)
191 280
192#define array_needsize(base,cur,cnt,init) \ 281#define array_needsize(base,cur,cnt,init) \
193 if (expect_false ((cnt) > cur)) \ 282 if (expect_false ((cnt) > cur)) \
194 { \ 283 { \
195 int newcnt = cur; \ 284 int newcnt = cur; \
196 do \ 285 do \
197 { \ 286 { \
198 newcnt = array_roundsize (base, newcnt << 1); \ 287 newcnt = array_roundsize (base, newcnt << 1); \
199 } \ 288 } \
200 while ((cnt) > newcnt); \ 289 while ((cnt) > newcnt); \
201 \ 290 \
202 base = realloc (base, sizeof (*base) * (newcnt)); \ 291 base = ev_realloc (base, sizeof (*base) * (newcnt)); \
203 init (base + cur, newcnt - cur); \ 292 init (base + cur, newcnt - cur); \
204 cur = newcnt; \ 293 cur = newcnt; \
205 } 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;
206 311
207/*****************************************************************************/ 312/*****************************************************************************/
208 313
209static void 314static void
210anfds_init (ANFD *base, int count) 315anfds_init (ANFD *base, int count)
227 pendings [ABSPRI (w)][w->pending - 1].events |= events; 332 pendings [ABSPRI (w)][w->pending - 1].events |= events;
228 return; 333 return;
229 } 334 }
230 335
231 w->pending = ++pendingcnt [ABSPRI (w)]; 336 w->pending = ++pendingcnt [ABSPRI (w)];
232 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); 337 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void));
233 pendings [ABSPRI (w)][w->pending - 1].w = w; 338 pendings [ABSPRI (w)][w->pending - 1].w = w;
234 pendings [ABSPRI (w)][w->pending - 1].events = events; 339 pendings [ABSPRI (w)][w->pending - 1].events = events;
235} 340}
236 341
237static void 342static void
276 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)
277 events |= w->events; 382 events |= w->events;
278 383
279 anfd->reify = 0; 384 anfd->reify = 0;
280 385
281 if (anfd->events != events)
282 {
283 method_modify (EV_A_ fd, anfd->events, events); 386 method_modify (EV_A_ fd, anfd->events, events);
284 anfd->events = events; 387 anfd->events = events;
285 }
286 } 388 }
287 389
288 fdchangecnt = 0; 390 fdchangecnt = 0;
289} 391}
290 392
291static void 393static void
292fd_change (EV_P_ int fd) 394fd_change (EV_P_ int fd)
293{ 395{
294 if (anfds [fd].reify || fdchangecnt < 0) 396 if (anfds [fd].reify)
295 return; 397 return;
296 398
297 anfds [fd].reify = 1; 399 anfds [fd].reify = 1;
298 400
299 ++fdchangecnt; 401 ++fdchangecnt;
300 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 402 array_needsize (fdchanges, fdchangemax, fdchangecnt, (void));
301 fdchanges [fdchangecnt - 1] = fd; 403 fdchanges [fdchangecnt - 1] = fd;
302} 404}
303 405
304static void 406static void
305fd_kill (EV_P_ int fd) 407fd_kill (EV_P_ int fd)
311 ev_io_stop (EV_A_ w); 413 ev_io_stop (EV_A_ w);
312 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 414 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
313 } 415 }
314} 416}
315 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
316/* called on EBADF to verify fds */ 428/* called on EBADF to verify fds */
317static void 429static void
318fd_ebadf (EV_P) 430fd_ebadf (EV_P)
319{ 431{
320 int fd; 432 int fd;
321 433
322 for (fd = 0; fd < anfdmax; ++fd) 434 for (fd = 0; fd < anfdmax; ++fd)
323 if (anfds [fd].events) 435 if (anfds [fd].events)
324 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 436 if (!fd_valid (fd) == -1 && errno == EBADF)
325 fd_kill (EV_A_ fd); 437 fd_kill (EV_A_ fd);
326} 438}
327 439
328/* 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 */
329static void 441static void
330fd_enomem (EV_P) 442fd_enomem (EV_P)
331{ 443{
332 int fd = anfdmax; 444 int fd;
333 445
334 while (fd--) 446 for (fd = anfdmax; fd--; )
335 if (anfds [fd].events) 447 if (anfds [fd].events)
336 { 448 {
337 close (fd);
338 fd_kill (EV_A_ fd); 449 fd_kill (EV_A_ fd);
339 return; 450 return;
340 } 451 }
341} 452}
342 453
343/* susually called after fork if method needs to re-arm all fds from scratch */ 454/* usually called after fork if method needs to re-arm all fds from scratch */
344static void 455static void
345fd_rearm_all (EV_P) 456fd_rearm_all (EV_P)
346{ 457{
347 int fd; 458 int fd;
348 459
349 /* this should be highly optimised to not do anything but set a flag */ 460 /* this should be highly optimised to not do anything but set a flag */
350 for (fd = 0; fd < anfdmax; ++fd) 461 for (fd = 0; fd < anfdmax; ++fd)
351 if (anfds [fd].events) 462 if (anfds [fd].events)
352 { 463 {
353 anfds [fd].events = 0; 464 anfds [fd].events = 0;
354 fd_change (fd); 465 fd_change (EV_A_ fd);
355 } 466 }
356} 467}
357 468
358/*****************************************************************************/ 469/*****************************************************************************/
359 470
363 WT w = heap [k]; 474 WT w = heap [k];
364 475
365 while (k && heap [k >> 1]->at > w->at) 476 while (k && heap [k >> 1]->at > w->at)
366 { 477 {
367 heap [k] = heap [k >> 1]; 478 heap [k] = heap [k >> 1];
368 heap [k]->active = k + 1; 479 ((W)heap [k])->active = k + 1;
369 k >>= 1; 480 k >>= 1;
370 } 481 }
371 482
372 heap [k] = w; 483 heap [k] = w;
373 heap [k]->active = k + 1; 484 ((W)heap [k])->active = k + 1;
374 485
375} 486}
376 487
377static void 488static void
378downheap (WT *heap, int N, int k) 489downheap (WT *heap, int N, int k)
388 499
389 if (w->at <= heap [j]->at) 500 if (w->at <= heap [j]->at)
390 break; 501 break;
391 502
392 heap [k] = heap [j]; 503 heap [k] = heap [j];
393 heap [k]->active = k + 1; 504 ((W)heap [k])->active = k + 1;
394 k = j; 505 k = j;
395 } 506 }
396 507
397 heap [k] = w; 508 heap [k] = w;
398 heap [k]->active = k + 1; 509 ((W)heap [k])->active = k + 1;
399} 510}
400 511
401/*****************************************************************************/ 512/*****************************************************************************/
402 513
403typedef struct 514typedef struct
404{ 515{
405 struct ev_watcher_list *head; 516 WL head;
406 sig_atomic_t volatile gotsig; 517 sig_atomic_t volatile gotsig;
407} ANSIG; 518} ANSIG;
408 519
409static ANSIG *signals; 520static ANSIG *signals;
410static int signalmax; 521static int signalmax;
426} 537}
427 538
428static void 539static void
429sighandler (int signum) 540sighandler (int signum)
430{ 541{
542#if WIN32
543 signal (signum, sighandler);
544#endif
545
431 signals [signum - 1].gotsig = 1; 546 signals [signum - 1].gotsig = 1;
432 547
433 if (!gotsig) 548 if (!gotsig)
434 { 549 {
435 int old_errno = errno; 550 int old_errno = errno;
440} 555}
441 556
442static void 557static void
443sigcb (EV_P_ struct ev_io *iow, int revents) 558sigcb (EV_P_ struct ev_io *iow, int revents)
444{ 559{
445 struct ev_watcher_list *w; 560 WL w;
446 int signum; 561 int signum;
447 562
448 read (sigpipe [0], &revents, 1); 563 read (sigpipe [0], &revents, 1);
449 gotsig = 0; 564 gotsig = 0;
450 565
475 ev_unref (EV_A); /* child watcher should not keep loop alive */ 590 ev_unref (EV_A); /* child watcher should not keep loop alive */
476} 591}
477 592
478/*****************************************************************************/ 593/*****************************************************************************/
479 594
595static struct ev_child *childs [PID_HASHSIZE];
596
480#ifndef WIN32 597#ifndef WIN32
481 598
482static struct ev_child *childs [PID_HASHSIZE];
483static struct ev_signal childev; 599static struct ev_signal childev;
484 600
485#ifndef WCONTINUED 601#ifndef WCONTINUED
486# define WCONTINUED 0 602# define WCONTINUED 0
487#endif 603#endif
492 struct ev_child *w; 608 struct ev_child *w;
493 609
494 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)
495 if (w->pid == pid || !w->pid) 611 if (w->pid == pid || !w->pid)
496 { 612 {
497 w->priority = sw->priority; /* need to do it *now* */ 613 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
498 w->rpid = pid; 614 w->rpid = pid;
499 w->rstatus = status; 615 w->rstatus = status;
500 event (EV_A_ (W)w, EV_CHILD); 616 event (EV_A_ (W)w, EV_CHILD);
501 } 617 }
502} 618}
503 619
504static void 620static void
586 methods = atoi (getenv ("LIBEV_METHODS")); 702 methods = atoi (getenv ("LIBEV_METHODS"));
587 else 703 else
588 methods = EVMETHOD_ANY; 704 methods = EVMETHOD_ANY;
589 705
590 method = 0; 706 method = 0;
707#if EV_USE_WIN32
708 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
709#endif
591#if EV_USE_KQUEUE 710#if EV_USE_KQUEUE
592 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 711 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
593#endif 712#endif
594#if EV_USE_EPOLL 713#if EV_USE_EPOLL
595 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 714 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
598 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 717 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
599#endif 718#endif
600#if EV_USE_SELECT 719#if EV_USE_SELECT
601 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 720 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
602#endif 721#endif
722
723 ev_watcher_init (&sigev, sigcb);
724 ev_set_priority (&sigev, EV_MAXPRI);
603 } 725 }
604} 726}
605 727
606void 728void
607loop_destroy (EV_P) 729loop_destroy (EV_P)
608{ 730{
731 int i;
732
733#if EV_USE_WIN32
734 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
735#endif
609#if EV_USE_KQUEUE 736#if EV_USE_KQUEUE
610 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 737 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
611#endif 738#endif
612#if EV_USE_EPOLL 739#if EV_USE_EPOLL
613 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 740 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
617#endif 744#endif
618#if EV_USE_SELECT 745#if EV_USE_SELECT
619 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 746 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
620#endif 747#endif
621 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
622 method = 0; 760 method = 0;
623 /*TODO*/
624} 761}
625 762
626void 763static void
627loop_fork (EV_P) 764loop_fork (EV_P)
628{ 765{
629 /*TODO*/
630#if EV_USE_EPOLL 766#if EV_USE_EPOLL
631 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 767 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
632#endif 768#endif
633#if EV_USE_KQUEUE 769#if EV_USE_KQUEUE
634 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 770 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
635#endif 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;
636} 789}
637 790
638#if EV_MULTIPLICITY 791#if EV_MULTIPLICITY
639struct ev_loop * 792struct ev_loop *
640ev_loop_new (int methods) 793ev_loop_new (int methods)
641{ 794{
642 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); 795 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
796
797 memset (loop, 0, sizeof (struct ev_loop));
643 798
644 loop_init (EV_A_ methods); 799 loop_init (EV_A_ methods);
645 800
646 if (ev_methods (EV_A)) 801 if (ev_method (EV_A))
647 return loop; 802 return loop;
648 803
649 return 0; 804 return 0;
650} 805}
651 806
652void 807void
653ev_loop_destroy (EV_P) 808ev_loop_destroy (EV_P)
654{ 809{
655 loop_destroy (EV_A); 810 loop_destroy (EV_A);
656 free (loop); 811 ev_free (loop);
657} 812}
658 813
659void 814void
660ev_loop_fork (EV_P) 815ev_loop_fork (EV_P)
661{ 816{
662 loop_fork (EV_A); 817 postfork = 1;
663} 818}
664 819
665#endif 820#endif
666 821
667#if EV_MULTIPLICITY 822#if EV_MULTIPLICITY
690 845
691 loop_init (EV_A_ methods); 846 loop_init (EV_A_ methods);
692 847
693 if (ev_method (EV_A)) 848 if (ev_method (EV_A))
694 { 849 {
695 ev_watcher_init (&sigev, sigcb);
696 ev_set_priority (&sigev, EV_MAXPRI);
697 siginit (EV_A); 850 siginit (EV_A);
698 851
699#ifndef WIN32 852#ifndef WIN32
700 ev_signal_init (&childev, childcb, SIGCHLD); 853 ev_signal_init (&childev, childcb, SIGCHLD);
701 ev_set_priority (&childev, EV_MAXPRI); 854 ev_set_priority (&childev, EV_MAXPRI);
715{ 868{
716#if EV_MULTIPLICITY 869#if EV_MULTIPLICITY
717 struct ev_loop *loop = default_loop; 870 struct ev_loop *loop = default_loop;
718#endif 871#endif
719 872
873#ifndef WIN32
720 ev_ref (EV_A); /* child watcher */ 874 ev_ref (EV_A); /* child watcher */
721 ev_signal_stop (EV_A_ &childev); 875 ev_signal_stop (EV_A_ &childev);
876#endif
722 877
723 ev_ref (EV_A); /* signal watcher */ 878 ev_ref (EV_A); /* signal watcher */
724 ev_io_stop (EV_A_ &sigev); 879 ev_io_stop (EV_A_ &sigev);
725 880
726 close (sigpipe [0]); sigpipe [0] = 0; 881 close (sigpipe [0]); sigpipe [0] = 0;
728 883
729 loop_destroy (EV_A); 884 loop_destroy (EV_A);
730} 885}
731 886
732void 887void
733ev_default_fork (EV_P) 888ev_default_fork (void)
734{ 889{
735 loop_fork (EV_A); 890#if EV_MULTIPLICITY
891 struct ev_loop *loop = default_loop;
892#endif
736 893
737 ev_io_stop (EV_A_ &sigev); 894 if (method)
738 close (sigpipe [0]); 895 postfork = 1;
739 close (sigpipe [1]);
740 pipe (sigpipe);
741
742 ev_ref (EV_A); /* signal watcher */
743 siginit (EV_A);
744} 896}
745 897
746/*****************************************************************************/ 898/*****************************************************************************/
747 899
748static void 900static void
764} 916}
765 917
766static void 918static void
767timers_reify (EV_P) 919timers_reify (EV_P)
768{ 920{
769 while (timercnt && timers [0]->at <= mn_now) 921 while (timercnt && ((WT)timers [0])->at <= mn_now)
770 { 922 {
771 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)));
772 926
773 /* first reschedule or stop timer */ 927 /* first reschedule or stop timer */
774 if (w->repeat) 928 if (w->repeat)
775 { 929 {
776 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.));
777 w->at = mn_now + w->repeat; 931 ((WT)w)->at = mn_now + w->repeat;
778 downheap ((WT *)timers, timercnt, 0); 932 downheap ((WT *)timers, timercnt, 0);
779 } 933 }
780 else 934 else
781 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 935 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
782 936
785} 939}
786 940
787static void 941static void
788periodics_reify (EV_P) 942periodics_reify (EV_P)
789{ 943{
790 while (periodiccnt && periodics [0]->at <= rt_now) 944 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
791 { 945 {
792 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)));
793 949
794 /* first reschedule or stop timer */ 950 /* first reschedule or stop timer */
795 if (w->interval) 951 if (w->interval)
796 { 952 {
797 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;
798 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));
799 downheap ((WT *)periodics, periodiccnt, 0); 955 downheap ((WT *)periodics, periodiccnt, 0);
800 } 956 }
801 else 957 else
802 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 958 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
803 959
815 { 971 {
816 struct ev_periodic *w = periodics [i]; 972 struct ev_periodic *w = periodics [i];
817 973
818 if (w->interval) 974 if (w->interval)
819 { 975 {
820 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;
821 977
822 if (fabs (diff) >= 1e-4) 978 if (fabs (diff) >= 1e-4)
823 { 979 {
824 ev_periodic_stop (EV_A_ w); 980 ev_periodic_stop (EV_A_ w);
825 ev_periodic_start (EV_A_ w); 981 ev_periodic_start (EV_A_ w);
886 { 1042 {
887 periodics_reschedule (EV_A); 1043 periodics_reschedule (EV_A);
888 1044
889 /* 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 */
890 for (i = 0; i < timercnt; ++i) 1046 for (i = 0; i < timercnt; ++i)
891 timers [i]->at += rt_now - mn_now; 1047 ((WT)timers [i])->at += rt_now - mn_now;
892 } 1048 }
893 1049
894 mn_now = rt_now; 1050 mn_now = rt_now;
895 } 1051 }
896} 1052}
922 { 1078 {
923 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1079 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
924 call_pending (EV_A); 1080 call_pending (EV_A);
925 } 1081 }
926 1082
1083 /* we might have forked, so reify kernel state if necessary */
1084 if (expect_false (postfork))
1085 loop_fork (EV_A);
1086
927 /* update fd-related kernel structures */ 1087 /* update fd-related kernel structures */
928 fd_reify (EV_A); 1088 fd_reify (EV_A);
929 1089
930 /* calculate blocking time */ 1090 /* calculate blocking time */
931 1091
947 { 1107 {
948 block = MAX_BLOCKTIME; 1108 block = MAX_BLOCKTIME;
949 1109
950 if (timercnt) 1110 if (timercnt)
951 { 1111 {
952 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1112 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
953 if (block > to) block = to; 1113 if (block > to) block = to;
954 } 1114 }
955 1115
956 if (periodiccnt) 1116 if (periodiccnt)
957 { 1117 {
958 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1118 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
959 if (block > to) block = to; 1119 if (block > to) block = to;
960 } 1120 }
961 1121
962 if (block < 0.) block = 0.; 1122 if (block < 0.) block = 0.;
963 } 1123 }
1080ev_timer_start (EV_P_ struct ev_timer *w) 1240ev_timer_start (EV_P_ struct ev_timer *w)
1081{ 1241{
1082 if (ev_is_active (w)) 1242 if (ev_is_active (w))
1083 return; 1243 return;
1084 1244
1085 w->at += mn_now; 1245 ((WT)w)->at += mn_now;
1086 1246
1087 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.));
1088 1248
1089 ev_start (EV_A_ (W)w, ++timercnt); 1249 ev_start (EV_A_ (W)w, ++timercnt);
1090 array_needsize (timers, timermax, timercnt, ); 1250 array_needsize (timers, timermax, timercnt, (void));
1091 timers [timercnt - 1] = w; 1251 timers [timercnt - 1] = w;
1092 upheap ((WT *)timers, timercnt - 1); 1252 upheap ((WT *)timers, timercnt - 1);
1253
1254 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1093} 1255}
1094 1256
1095void 1257void
1096ev_timer_stop (EV_P_ struct ev_timer *w) 1258ev_timer_stop (EV_P_ struct ev_timer *w)
1097{ 1259{
1098 ev_clear_pending (EV_A_ (W)w); 1260 ev_clear_pending (EV_A_ (W)w);
1099 if (!ev_is_active (w)) 1261 if (!ev_is_active (w))
1100 return; 1262 return;
1101 1263
1264 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1265
1102 if (w->active < timercnt--) 1266 if (((W)w)->active < timercnt--)
1103 { 1267 {
1104 timers [w->active - 1] = timers [timercnt]; 1268 timers [((W)w)->active - 1] = timers [timercnt];
1105 downheap ((WT *)timers, timercnt, w->active - 1); 1269 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1106 } 1270 }
1107 1271
1108 w->at = w->repeat; 1272 ((WT)w)->at = w->repeat;
1109 1273
1110 ev_stop (EV_A_ (W)w); 1274 ev_stop (EV_A_ (W)w);
1111} 1275}
1112 1276
1113void 1277void
1115{ 1279{
1116 if (ev_is_active (w)) 1280 if (ev_is_active (w))
1117 { 1281 {
1118 if (w->repeat) 1282 if (w->repeat)
1119 { 1283 {
1120 w->at = mn_now + w->repeat; 1284 ((WT)w)->at = mn_now + w->repeat;
1121 downheap ((WT *)timers, timercnt, w->active - 1); 1285 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1122 } 1286 }
1123 else 1287 else
1124 ev_timer_stop (EV_A_ w); 1288 ev_timer_stop (EV_A_ w);
1125 } 1289 }
1126 else if (w->repeat) 1290 else if (w->repeat)
1135 1299
1136 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.));
1137 1301
1138 /* 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 */
1139 if (w->interval) 1303 if (w->interval)
1140 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;
1141 1305
1142 ev_start (EV_A_ (W)w, ++periodiccnt); 1306 ev_start (EV_A_ (W)w, ++periodiccnt);
1143 array_needsize (periodics, periodicmax, periodiccnt, ); 1307 array_needsize (periodics, periodicmax, periodiccnt, (void));
1144 periodics [periodiccnt - 1] = w; 1308 periodics [periodiccnt - 1] = w;
1145 upheap ((WT *)periodics, periodiccnt - 1); 1309 upheap ((WT *)periodics, periodiccnt - 1);
1310
1311 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1146} 1312}
1147 1313
1148void 1314void
1149ev_periodic_stop (EV_P_ struct ev_periodic *w) 1315ev_periodic_stop (EV_P_ struct ev_periodic *w)
1150{ 1316{
1151 ev_clear_pending (EV_A_ (W)w); 1317 ev_clear_pending (EV_A_ (W)w);
1152 if (!ev_is_active (w)) 1318 if (!ev_is_active (w))
1153 return; 1319 return;
1154 1320
1321 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1322
1155 if (w->active < periodiccnt--) 1323 if (((W)w)->active < periodiccnt--)
1156 { 1324 {
1157 periodics [w->active - 1] = periodics [periodiccnt]; 1325 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1158 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1326 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1159 } 1327 }
1160 1328
1161 ev_stop (EV_A_ (W)w); 1329 ev_stop (EV_A_ (W)w);
1162} 1330}
1163 1331
1166{ 1334{
1167 if (ev_is_active (w)) 1335 if (ev_is_active (w))
1168 return; 1336 return;
1169 1337
1170 ev_start (EV_A_ (W)w, ++idlecnt); 1338 ev_start (EV_A_ (W)w, ++idlecnt);
1171 array_needsize (idles, idlemax, idlecnt, ); 1339 array_needsize (idles, idlemax, idlecnt, (void));
1172 idles [idlecnt - 1] = w; 1340 idles [idlecnt - 1] = w;
1173} 1341}
1174 1342
1175void 1343void
1176ev_idle_stop (EV_P_ struct ev_idle *w) 1344ev_idle_stop (EV_P_ struct ev_idle *w)
1177{ 1345{
1178 ev_clear_pending (EV_A_ (W)w); 1346 ev_clear_pending (EV_A_ (W)w);
1179 if (ev_is_active (w)) 1347 if (ev_is_active (w))
1180 return; 1348 return;
1181 1349
1182 idles [w->active - 1] = idles [--idlecnt]; 1350 idles [((W)w)->active - 1] = idles [--idlecnt];
1183 ev_stop (EV_A_ (W)w); 1351 ev_stop (EV_A_ (W)w);
1184} 1352}
1185 1353
1186void 1354void
1187ev_prepare_start (EV_P_ struct ev_prepare *w) 1355ev_prepare_start (EV_P_ struct ev_prepare *w)
1188{ 1356{
1189 if (ev_is_active (w)) 1357 if (ev_is_active (w))
1190 return; 1358 return;
1191 1359
1192 ev_start (EV_A_ (W)w, ++preparecnt); 1360 ev_start (EV_A_ (W)w, ++preparecnt);
1193 array_needsize (prepares, preparemax, preparecnt, ); 1361 array_needsize (prepares, preparemax, preparecnt, (void));
1194 prepares [preparecnt - 1] = w; 1362 prepares [preparecnt - 1] = w;
1195} 1363}
1196 1364
1197void 1365void
1198ev_prepare_stop (EV_P_ struct ev_prepare *w) 1366ev_prepare_stop (EV_P_ struct ev_prepare *w)
1199{ 1367{
1200 ev_clear_pending (EV_A_ (W)w); 1368 ev_clear_pending (EV_A_ (W)w);
1201 if (ev_is_active (w)) 1369 if (ev_is_active (w))
1202 return; 1370 return;
1203 1371
1204 prepares [w->active - 1] = prepares [--preparecnt]; 1372 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1205 ev_stop (EV_A_ (W)w); 1373 ev_stop (EV_A_ (W)w);
1206} 1374}
1207 1375
1208void 1376void
1209ev_check_start (EV_P_ struct ev_check *w) 1377ev_check_start (EV_P_ struct ev_check *w)
1210{ 1378{
1211 if (ev_is_active (w)) 1379 if (ev_is_active (w))
1212 return; 1380 return;
1213 1381
1214 ev_start (EV_A_ (W)w, ++checkcnt); 1382 ev_start (EV_A_ (W)w, ++checkcnt);
1215 array_needsize (checks, checkmax, checkcnt, ); 1383 array_needsize (checks, checkmax, checkcnt, (void));
1216 checks [checkcnt - 1] = w; 1384 checks [checkcnt - 1] = w;
1217} 1385}
1218 1386
1219void 1387void
1220ev_check_stop (EV_P_ struct ev_check *w) 1388ev_check_stop (EV_P_ struct ev_check *w)
1221{ 1389{
1222 ev_clear_pending (EV_A_ (W)w); 1390 ev_clear_pending (EV_A_ (W)w);
1223 if (ev_is_active (w)) 1391 if (ev_is_active (w))
1224 return; 1392 return;
1225 1393
1226 checks [w->active - 1] = checks [--checkcnt]; 1394 checks [((W)w)->active - 1] = checks [--checkcnt];
1227 ev_stop (EV_A_ (W)w); 1395 ev_stop (EV_A_ (W)w);
1228} 1396}
1229 1397
1230#ifndef SA_RESTART 1398#ifndef SA_RESTART
1231# define SA_RESTART 0 1399# define SA_RESTART 0
1244 1412
1245 ev_start (EV_A_ (W)w, 1); 1413 ev_start (EV_A_ (W)w, 1);
1246 array_needsize (signals, signalmax, w->signum, signals_init); 1414 array_needsize (signals, signalmax, w->signum, signals_init);
1247 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1415 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1248 1416
1249 if (!w->next) 1417 if (!((WL)w)->next)
1250 { 1418 {
1419#if WIN32
1420 signal (w->signum, sighandler);
1421#else
1251 struct sigaction sa; 1422 struct sigaction sa;
1252 sa.sa_handler = sighandler; 1423 sa.sa_handler = sighandler;
1253 sigfillset (&sa.sa_mask); 1424 sigfillset (&sa.sa_mask);
1254 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 */
1255 sigaction (w->signum, &sa, 0); 1426 sigaction (w->signum, &sa, 0);
1427#endif
1256 } 1428 }
1257} 1429}
1258 1430
1259void 1431void
1260ev_signal_stop (EV_P_ struct ev_signal *w) 1432ev_signal_stop (EV_P_ struct ev_signal *w)
1310 void (*cb)(int revents, void *arg) = once->cb; 1482 void (*cb)(int revents, void *arg) = once->cb;
1311 void *arg = once->arg; 1483 void *arg = once->arg;
1312 1484
1313 ev_io_stop (EV_A_ &once->io); 1485 ev_io_stop (EV_A_ &once->io);
1314 ev_timer_stop (EV_A_ &once->to); 1486 ev_timer_stop (EV_A_ &once->to);
1315 free (once); 1487 ev_free (once);
1316 1488
1317 cb (revents, arg); 1489 cb (revents, arg);
1318} 1490}
1319 1491
1320static void 1492static void
1330} 1502}
1331 1503
1332void 1504void
1333ev_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)
1334{ 1506{
1335 struct ev_once *once = malloc (sizeof (struct ev_once)); 1507 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1336 1508
1337 if (!once) 1509 if (!once)
1338 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1510 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1339 else 1511 else
1340 { 1512 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines