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

Comparing libev/ev.c (file contents):
Revision 1.63 by root, Sun Nov 4 22:03:17 2007 UTC vs.
Revision 1.80 by root, Fri Nov 9 15:30:59 2007 UTC

54 54
55#endif 55#endif
56 56
57#include <math.h> 57#include <math.h>
58#include <stdlib.h> 58#include <stdlib.h>
59#include <unistd.h>
60#include <fcntl.h> 59#include <fcntl.h>
61#include <signal.h>
62#include <stddef.h> 60#include <stddef.h>
63 61
64#include <stdio.h> 62#include <stdio.h>
65 63
66#include <assert.h> 64#include <assert.h>
67#include <errno.h> 65#include <errno.h>
68#include <sys/types.h> 66#include <sys/types.h>
67#include <time.h>
68
69#include <signal.h>
70
69#ifndef WIN32 71#ifndef WIN32
72# include <unistd.h>
73# include <sys/time.h>
70# include <sys/wait.h> 74# include <sys/wait.h>
71#endif 75#endif
72#include <sys/time.h>
73#include <time.h>
74
75/**/ 76/**/
76 77
77#ifndef EV_USE_MONOTONIC 78#ifndef EV_USE_MONOTONIC
78# define EV_USE_MONOTONIC 1 79# define EV_USE_MONOTONIC 1
79#endif 80#endif
94# define EV_USE_KQUEUE 0 95# define EV_USE_KQUEUE 0
95#endif 96#endif
96 97
97#ifndef EV_USE_WIN32 98#ifndef EV_USE_WIN32
98# ifdef WIN32 99# ifdef WIN32
100# define EV_USE_WIN32 0 /* it does not exist, use select */
101# undef EV_USE_SELECT
99# define EV_USE_WIN32 1 102# define EV_USE_SELECT 1
100# else 103# else
101# define EV_USE_WIN32 0 104# define EV_USE_WIN32 0
102# endif 105# endif
103#endif 106#endif
104 107
145typedef struct ev_watcher_list *WL; 148typedef struct ev_watcher_list *WL;
146typedef struct ev_watcher_time *WT; 149typedef struct ev_watcher_time *WT;
147 150
148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 151static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
149 152
153#include "ev_win32.c"
154
150/*****************************************************************************/ 155/*****************************************************************************/
151 156
157static void (*syserr_cb)(const char *msg);
158
159void ev_set_syserr_cb (void (*cb)(const char *msg))
160{
161 syserr_cb = cb;
162}
163
164static void
165syserr (const char *msg)
166{
167 if (!msg)
168 msg = "(libev) system error";
169
170 if (syserr_cb)
171 syserr_cb (msg);
172 else
173 {
174 perror (msg);
175 abort ();
176 }
177}
178
179static void *(*alloc)(void *ptr, long size);
180
181void ev_set_allocator (void *(*cb)(void *ptr, long size))
182{
183 alloc = cb;
184}
185
186static void *
187ev_realloc (void *ptr, long size)
188{
189 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
190
191 if (!ptr && size)
192 {
193 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
194 abort ();
195 }
196
197 return ptr;
198}
199
200#define ev_malloc(size) ev_realloc (0, (size))
201#define ev_free(ptr) ev_realloc ((ptr), 0)
202
203/*****************************************************************************/
204
152typedef struct 205typedef struct
153{ 206{
154 struct ev_watcher_list *head; 207 WL head;
155 unsigned char events; 208 unsigned char events;
156 unsigned char reify; 209 unsigned char reify;
157} ANFD; 210} ANFD;
158 211
159typedef struct 212typedef struct
162 int events; 215 int events;
163} ANPENDING; 216} ANPENDING;
164 217
165#if EV_MULTIPLICITY 218#if EV_MULTIPLICITY
166 219
167struct ev_loop 220 struct ev_loop
168{ 221 {
169# define VAR(name,decl) decl; 222 #define VAR(name,decl) decl;
170# include "ev_vars.h" 223 #include "ev_vars.h"
171};
172# undef VAR 224 #undef VAR
225 };
173# include "ev_wrap.h" 226 #include "ev_wrap.h"
227
228 struct ev_loop default_loop_struct;
229 static struct ev_loop *default_loop;
174 230
175#else 231#else
176 232
177# define VAR(name,decl) static decl; 233 #define VAR(name,decl) static decl;
178# include "ev_vars.h" 234 #include "ev_vars.h"
179# undef VAR 235 #undef VAR
236
237 static int default_loop;
180 238
181#endif 239#endif
182 240
183/*****************************************************************************/ 241/*****************************************************************************/
184 242
215ev_now (EV_P) 273ev_now (EV_P)
216{ 274{
217 return rt_now; 275 return rt_now;
218} 276}
219 277
220#define array_roundsize(base,n) ((n) | 4 & ~3) 278#define array_roundsize(type,n) ((n) | 4 & ~3)
221 279
222#define array_needsize(base,cur,cnt,init) \ 280#define array_needsize(type,base,cur,cnt,init) \
223 if (expect_false ((cnt) > cur)) \ 281 if (expect_false ((cnt) > cur)) \
224 { \ 282 { \
225 int newcnt = cur; \ 283 int newcnt = cur; \
226 do \ 284 do \
227 { \ 285 { \
228 newcnt = array_roundsize (base, newcnt << 1); \ 286 newcnt = array_roundsize (type, newcnt << 1); \
229 } \ 287 } \
230 while ((cnt) > newcnt); \ 288 while ((cnt) > newcnt); \
231 \ 289 \
232 base = realloc (base, sizeof (*base) * (newcnt)); \ 290 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
233 init (base + cur, newcnt - cur); \ 291 init (base + cur, newcnt - cur); \
234 cur = newcnt; \ 292 cur = newcnt; \
235 } 293 }
294
295#define array_slim(type,stem) \
296 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
297 { \
298 stem ## max = array_roundsize (stem ## cnt >> 1); \
299 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
300 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
301 }
302
303/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
304/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
305#define array_free_microshit(stem) \
306 ev_free (stem ## s); stem ## cnt = stem ## max = 0;
307
308#define array_free(stem, idx) \
309 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
236 310
237/*****************************************************************************/ 311/*****************************************************************************/
238 312
239static void 313static void
240anfds_init (ANFD *base, int count) 314anfds_init (ANFD *base, int count)
247 321
248 ++base; 322 ++base;
249 } 323 }
250} 324}
251 325
252static void 326void
253event (EV_P_ W w, int events) 327ev_feed_event (EV_P_ void *w, int revents)
254{ 328{
329 W w_ = (W)w;
330
255 if (w->pending) 331 if (w_->pending)
256 { 332 {
257 pendings [ABSPRI (w)][w->pending - 1].events |= events; 333 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
258 return; 334 return;
259 } 335 }
260 336
261 w->pending = ++pendingcnt [ABSPRI (w)]; 337 w_->pending = ++pendingcnt [ABSPRI (w_)];
262 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); 338 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void));
263 pendings [ABSPRI (w)][w->pending - 1].w = w; 339 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
264 pendings [ABSPRI (w)][w->pending - 1].events = events; 340 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
265} 341}
266 342
267static void 343static void
268queue_events (EV_P_ W *events, int eventcnt, int type) 344queue_events (EV_P_ W *events, int eventcnt, int type)
269{ 345{
270 int i; 346 int i;
271 347
272 for (i = 0; i < eventcnt; ++i) 348 for (i = 0; i < eventcnt; ++i)
273 event (EV_A_ events [i], type); 349 ev_feed_event (EV_A_ events [i], type);
274} 350}
275 351
276static void 352inline void
277fd_event (EV_P_ int fd, int events) 353fd_event (EV_P_ int fd, int revents)
278{ 354{
279 ANFD *anfd = anfds + fd; 355 ANFD *anfd = anfds + fd;
280 struct ev_io *w; 356 struct ev_io *w;
281 357
282 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 358 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
283 { 359 {
284 int ev = w->events & events; 360 int ev = w->events & revents;
285 361
286 if (ev) 362 if (ev)
287 event (EV_A_ (W)w, ev); 363 ev_feed_event (EV_A_ (W)w, ev);
288 } 364 }
365}
366
367void
368ev_feed_fd_event (EV_P_ int fd, int revents)
369{
370 fd_event (EV_A_ fd, revents);
289} 371}
290 372
291/*****************************************************************************/ 373/*****************************************************************************/
292 374
293static void 375static void
306 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 388 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
307 events |= w->events; 389 events |= w->events;
308 390
309 anfd->reify = 0; 391 anfd->reify = 0;
310 392
311 if (anfd->events != events)
312 {
313 method_modify (EV_A_ fd, anfd->events, events); 393 method_modify (EV_A_ fd, anfd->events, events);
314 anfd->events = events; 394 anfd->events = events;
315 }
316 } 395 }
317 396
318 fdchangecnt = 0; 397 fdchangecnt = 0;
319} 398}
320 399
321static void 400static void
322fd_change (EV_P_ int fd) 401fd_change (EV_P_ int fd)
323{ 402{
324 if (anfds [fd].reify || fdchangecnt < 0) 403 if (anfds [fd].reify)
325 return; 404 return;
326 405
327 anfds [fd].reify = 1; 406 anfds [fd].reify = 1;
328 407
329 ++fdchangecnt; 408 ++fdchangecnt;
330 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 409 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void));
331 fdchanges [fdchangecnt - 1] = fd; 410 fdchanges [fdchangecnt - 1] = fd;
332} 411}
333 412
334static void 413static void
335fd_kill (EV_P_ int fd) 414fd_kill (EV_P_ int fd)
337 struct ev_io *w; 416 struct ev_io *w;
338 417
339 while ((w = (struct ev_io *)anfds [fd].head)) 418 while ((w = (struct ev_io *)anfds [fd].head))
340 { 419 {
341 ev_io_stop (EV_A_ w); 420 ev_io_stop (EV_A_ w);
342 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 421 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
343 } 422 }
423}
424
425static int
426fd_valid (int fd)
427{
428#ifdef WIN32
429 return !!win32_get_osfhandle (fd);
430#else
431 return fcntl (fd, F_GETFD) != -1;
432#endif
344} 433}
345 434
346/* called on EBADF to verify fds */ 435/* called on EBADF to verify fds */
347static void 436static void
348fd_ebadf (EV_P) 437fd_ebadf (EV_P)
349{ 438{
350 int fd; 439 int fd;
351 440
352 for (fd = 0; fd < anfdmax; ++fd) 441 for (fd = 0; fd < anfdmax; ++fd)
353 if (anfds [fd].events) 442 if (anfds [fd].events)
354 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 443 if (!fd_valid (fd) == -1 && errno == EBADF)
355 fd_kill (EV_A_ fd); 444 fd_kill (EV_A_ fd);
356} 445}
357 446
358/* called on ENOMEM in select/poll to kill some fds and retry */ 447/* called on ENOMEM in select/poll to kill some fds and retry */
359static void 448static void
362 int fd; 451 int fd;
363 452
364 for (fd = anfdmax; fd--; ) 453 for (fd = anfdmax; fd--; )
365 if (anfds [fd].events) 454 if (anfds [fd].events)
366 { 455 {
367 close (fd);
368 fd_kill (EV_A_ fd); 456 fd_kill (EV_A_ fd);
369 return; 457 return;
370 } 458 }
371} 459}
372 460
373/* susually called after fork if method needs to re-arm all fds from scratch */ 461/* usually called after fork if method needs to re-arm all fds from scratch */
374static void 462static void
375fd_rearm_all (EV_P) 463fd_rearm_all (EV_P)
376{ 464{
377 int fd; 465 int fd;
378 466
430 518
431/*****************************************************************************/ 519/*****************************************************************************/
432 520
433typedef struct 521typedef struct
434{ 522{
435 struct ev_watcher_list *head; 523 WL head;
436 sig_atomic_t volatile gotsig; 524 sig_atomic_t volatile gotsig;
437} ANSIG; 525} ANSIG;
438 526
439static ANSIG *signals; 527static ANSIG *signals;
440static int signalmax; 528static int signalmax;
456} 544}
457 545
458static void 546static void
459sighandler (int signum) 547sighandler (int signum)
460{ 548{
549#if WIN32
550 signal (signum, sighandler);
551#endif
552
461 signals [signum - 1].gotsig = 1; 553 signals [signum - 1].gotsig = 1;
462 554
463 if (!gotsig) 555 if (!gotsig)
464 { 556 {
465 int old_errno = errno; 557 int old_errno = errno;
466 gotsig = 1; 558 gotsig = 1;
559#ifdef WIN32
560 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
561#else
467 write (sigpipe [1], &signum, 1); 562 write (sigpipe [1], &signum, 1);
563#endif
468 errno = old_errno; 564 errno = old_errno;
469 } 565 }
470} 566}
471 567
568void
569ev_feed_signal_event (EV_P_ int signum)
570{
571 WL w;
572
573#if EV_MULTIPLICITY
574 assert (("feeding signal events is only supported in the default loop", loop == default_loop));
575#endif
576
577 --signum;
578
579 if (signum < 0 || signum >= signalmax)
580 return;
581
582 signals [signum].gotsig = 0;
583
584 for (w = signals [signum].head; w; w = w->next)
585 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
586}
587
472static void 588static void
473sigcb (EV_P_ struct ev_io *iow, int revents) 589sigcb (EV_P_ struct ev_io *iow, int revents)
474{ 590{
475 struct ev_watcher_list *w;
476 int signum; 591 int signum;
477 592
593#ifdef WIN32
594 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
595#else
478 read (sigpipe [0], &revents, 1); 596 read (sigpipe [0], &revents, 1);
597#endif
479 gotsig = 0; 598 gotsig = 0;
480 599
481 for (signum = signalmax; signum--; ) 600 for (signum = signalmax; signum--; )
482 if (signals [signum].gotsig) 601 if (signals [signum].gotsig)
483 { 602 ev_feed_signal_event (EV_A_ signum + 1);
484 signals [signum].gotsig = 0;
485
486 for (w = signals [signum].head; w; w = w->next)
487 event (EV_A_ (W)w, EV_SIGNAL);
488 }
489} 603}
490 604
491static void 605static void
492siginit (EV_P) 606siginit (EV_P)
493{ 607{
505 ev_unref (EV_A); /* child watcher should not keep loop alive */ 619 ev_unref (EV_A); /* child watcher should not keep loop alive */
506} 620}
507 621
508/*****************************************************************************/ 622/*****************************************************************************/
509 623
624static struct ev_child *childs [PID_HASHSIZE];
625
510#ifndef WIN32 626#ifndef WIN32
511 627
512static struct ev_child *childs [PID_HASHSIZE];
513static struct ev_signal childev; 628static struct ev_signal childev;
514 629
515#ifndef WCONTINUED 630#ifndef WCONTINUED
516# define WCONTINUED 0 631# define WCONTINUED 0
517#endif 632#endif
525 if (w->pid == pid || !w->pid) 640 if (w->pid == pid || !w->pid)
526 { 641 {
527 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 642 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
528 w->rpid = pid; 643 w->rpid = pid;
529 w->rstatus = status; 644 w->rstatus = status;
530 event (EV_A_ (W)w, EV_CHILD); 645 ev_feed_event (EV_A_ (W)w, EV_CHILD);
531 } 646 }
532} 647}
533 648
534static void 649static void
535childcb (EV_P_ struct ev_signal *sw, int revents) 650childcb (EV_P_ struct ev_signal *sw, int revents)
537 int pid, status; 652 int pid, status;
538 653
539 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 654 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
540 { 655 {
541 /* make sure we are called again until all childs have been reaped */ 656 /* make sure we are called again until all childs have been reaped */
542 event (EV_A_ (W)sw, EV_SIGNAL); 657 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
543 658
544 child_reap (EV_A_ sw, pid, pid, status); 659 child_reap (EV_A_ sw, pid, pid, status);
545 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ 660 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
546 } 661 }
547} 662}
631 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 746 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
632#endif 747#endif
633#if EV_USE_SELECT 748#if EV_USE_SELECT
634 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 749 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
635#endif 750#endif
751
752 ev_watcher_init (&sigev, sigcb);
753 ev_set_priority (&sigev, EV_MAXPRI);
636 } 754 }
637} 755}
638 756
639void 757void
640loop_destroy (EV_P) 758loop_destroy (EV_P)
641{ 759{
760 int i;
761
642#if EV_USE_WIN32 762#if EV_USE_WIN32
643 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); 763 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
644#endif 764#endif
645#if EV_USE_KQUEUE 765#if EV_USE_KQUEUE
646 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 766 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
653#endif 773#endif
654#if EV_USE_SELECT 774#if EV_USE_SELECT
655 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 775 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
656#endif 776#endif
657 777
778 for (i = NUMPRI; i--; )
779 array_free (pending, [i]);
780
781 /* have to use the microsoft-never-gets-it-right macro */
782 array_free_microshit (fdchange);
783 array_free_microshit (timer);
784 array_free_microshit (periodic);
785 array_free_microshit (idle);
786 array_free_microshit (prepare);
787 array_free_microshit (check);
788
658 method = 0; 789 method = 0;
659 /*TODO*/
660} 790}
661 791
662void 792static void
663loop_fork (EV_P) 793loop_fork (EV_P)
664{ 794{
665 /*TODO*/
666#if EV_USE_EPOLL 795#if EV_USE_EPOLL
667 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 796 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
668#endif 797#endif
669#if EV_USE_KQUEUE 798#if EV_USE_KQUEUE
670 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 799 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
671#endif 800#endif
801
802 if (ev_is_active (&sigev))
803 {
804 /* default loop */
805
806 ev_ref (EV_A);
807 ev_io_stop (EV_A_ &sigev);
808 close (sigpipe [0]);
809 close (sigpipe [1]);
810
811 while (pipe (sigpipe))
812 syserr ("(libev) error creating pipe");
813
814 siginit (EV_A);
815 }
816
817 postfork = 0;
672} 818}
673 819
674#if EV_MULTIPLICITY 820#if EV_MULTIPLICITY
675struct ev_loop * 821struct ev_loop *
676ev_loop_new (int methods) 822ev_loop_new (int methods)
677{ 823{
678 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); 824 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
825
826 memset (loop, 0, sizeof (struct ev_loop));
679 827
680 loop_init (EV_A_ methods); 828 loop_init (EV_A_ methods);
681 829
682 if (ev_method (EV_A)) 830 if (ev_method (EV_A))
683 return loop; 831 return loop;
687 835
688void 836void
689ev_loop_destroy (EV_P) 837ev_loop_destroy (EV_P)
690{ 838{
691 loop_destroy (EV_A); 839 loop_destroy (EV_A);
692 free (loop); 840 ev_free (loop);
693} 841}
694 842
695void 843void
696ev_loop_fork (EV_P) 844ev_loop_fork (EV_P)
697{ 845{
698 loop_fork (EV_A); 846 postfork = 1;
699} 847}
700 848
701#endif 849#endif
702 850
703#if EV_MULTIPLICITY 851#if EV_MULTIPLICITY
704struct ev_loop default_loop_struct;
705static struct ev_loop *default_loop;
706
707struct ev_loop * 852struct ev_loop *
708#else 853#else
709static int default_loop;
710
711int 854int
712#endif 855#endif
713ev_default_loop (int methods) 856ev_default_loop (int methods)
714{ 857{
715 if (sigpipe [0] == sigpipe [1]) 858 if (sigpipe [0] == sigpipe [1])
726 869
727 loop_init (EV_A_ methods); 870 loop_init (EV_A_ methods);
728 871
729 if (ev_method (EV_A)) 872 if (ev_method (EV_A))
730 { 873 {
731 ev_watcher_init (&sigev, sigcb);
732 ev_set_priority (&sigev, EV_MAXPRI);
733 siginit (EV_A); 874 siginit (EV_A);
734 875
735#ifndef WIN32 876#ifndef WIN32
736 ev_signal_init (&childev, childcb, SIGCHLD); 877 ev_signal_init (&childev, childcb, SIGCHLD);
737 ev_set_priority (&childev, EV_MAXPRI); 878 ev_set_priority (&childev, EV_MAXPRI);
751{ 892{
752#if EV_MULTIPLICITY 893#if EV_MULTIPLICITY
753 struct ev_loop *loop = default_loop; 894 struct ev_loop *loop = default_loop;
754#endif 895#endif
755 896
897#ifndef WIN32
756 ev_ref (EV_A); /* child watcher */ 898 ev_ref (EV_A); /* child watcher */
757 ev_signal_stop (EV_A_ &childev); 899 ev_signal_stop (EV_A_ &childev);
900#endif
758 901
759 ev_ref (EV_A); /* signal watcher */ 902 ev_ref (EV_A); /* signal watcher */
760 ev_io_stop (EV_A_ &sigev); 903 ev_io_stop (EV_A_ &sigev);
761 904
762 close (sigpipe [0]); sigpipe [0] = 0; 905 close (sigpipe [0]); sigpipe [0] = 0;
770{ 913{
771#if EV_MULTIPLICITY 914#if EV_MULTIPLICITY
772 struct ev_loop *loop = default_loop; 915 struct ev_loop *loop = default_loop;
773#endif 916#endif
774 917
775 loop_fork (EV_A); 918 if (method)
776 919 postfork = 1;
777 ev_io_stop (EV_A_ &sigev);
778 close (sigpipe [0]);
779 close (sigpipe [1]);
780 pipe (sigpipe);
781
782 ev_ref (EV_A); /* signal watcher */
783 siginit (EV_A);
784} 920}
785 921
786/*****************************************************************************/ 922/*****************************************************************************/
923
924static int
925any_pending (EV_P)
926{
927 int pri;
928
929 for (pri = NUMPRI; pri--; )
930 if (pendingcnt [pri])
931 return 1;
932
933 return 0;
934}
787 935
788static void 936static void
789call_pending (EV_P) 937call_pending (EV_P)
790{ 938{
791 int pri; 939 int pri;
796 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 944 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
797 945
798 if (p->w) 946 if (p->w)
799 { 947 {
800 p->w->pending = 0; 948 p->w->pending = 0;
801
802 (*(void (**)(EV_P_ W, int))&p->w->cb) (EV_A_ p->w, p->events); 949 p->w->cb (EV_A_ p->w, p->events);
803 } 950 }
804 } 951 }
805} 952}
806 953
807static void 954static void
821 downheap ((WT *)timers, timercnt, 0); 968 downheap ((WT *)timers, timercnt, 0);
822 } 969 }
823 else 970 else
824 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 971 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
825 972
826 event (EV_A_ (W)w, EV_TIMEOUT); 973 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
827 } 974 }
828} 975}
829 976
830static void 977static void
831periodics_reify (EV_P) 978periodics_reify (EV_P)
835 struct ev_periodic *w = periodics [0]; 982 struct ev_periodic *w = periodics [0];
836 983
837 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 984 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
838 985
839 /* first reschedule or stop timer */ 986 /* first reschedule or stop timer */
987 if (w->reschedule_cb)
988 {
989 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001);
990
991 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now));
992 downheap ((WT *)periodics, periodiccnt, 0);
993 }
840 if (w->interval) 994 else if (w->interval)
841 { 995 {
842 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 996 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
843 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); 997 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
844 downheap ((WT *)periodics, periodiccnt, 0); 998 downheap ((WT *)periodics, periodiccnt, 0);
845 } 999 }
846 else 1000 else
847 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1001 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
848 1002
849 event (EV_A_ (W)w, EV_PERIODIC); 1003 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
850 } 1004 }
851} 1005}
852 1006
853static void 1007static void
854periodics_reschedule (EV_P) 1008periodics_reschedule (EV_P)
858 /* adjust periodics after time jump */ 1012 /* adjust periodics after time jump */
859 for (i = 0; i < periodiccnt; ++i) 1013 for (i = 0; i < periodiccnt; ++i)
860 { 1014 {
861 struct ev_periodic *w = periodics [i]; 1015 struct ev_periodic *w = periodics [i];
862 1016
1017 if (w->reschedule_cb)
1018 ((WT)w)->at = w->reschedule_cb (w, rt_now);
863 if (w->interval) 1019 else if (w->interval)
864 {
865 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1020 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
866
867 if (fabs (diff) >= 1e-4)
868 {
869 ev_periodic_stop (EV_A_ w);
870 ev_periodic_start (EV_A_ w);
871
872 i = 0; /* restart loop, inefficient, but time jumps should be rare */
873 }
874 }
875 } 1021 }
1022
1023 /* now rebuild the heap */
1024 for (i = periodiccnt >> 1; i--; )
1025 downheap ((WT *)periodics, periodiccnt, i);
876} 1026}
877 1027
878inline int 1028inline int
879time_update_monotonic (EV_P) 1029time_update_monotonic (EV_P)
880{ 1030{
967 { 1117 {
968 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1118 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
969 call_pending (EV_A); 1119 call_pending (EV_A);
970 } 1120 }
971 1121
1122 /* we might have forked, so reify kernel state if necessary */
1123 if (expect_false (postfork))
1124 loop_fork (EV_A);
1125
972 /* update fd-related kernel structures */ 1126 /* update fd-related kernel structures */
973 fd_reify (EV_A); 1127 fd_reify (EV_A);
974 1128
975 /* calculate blocking time */ 1129 /* calculate blocking time */
976 1130
977 /* we only need this for !monotonic clockor timers, but as we basically 1131 /* we only need this for !monotonic clock or timers, but as we basically
978 always have timers, we just calculate it always */ 1132 always have timers, we just calculate it always */
979#if EV_USE_MONOTONIC 1133#if EV_USE_MONOTONIC
980 if (expect_true (have_monotonic)) 1134 if (expect_true (have_monotonic))
981 time_update_monotonic (EV_A); 1135 time_update_monotonic (EV_A);
982 else 1136 else
1015 /* queue pending timers and reschedule them */ 1169 /* queue pending timers and reschedule them */
1016 timers_reify (EV_A); /* relative timers called last */ 1170 timers_reify (EV_A); /* relative timers called last */
1017 periodics_reify (EV_A); /* absolute timers called first */ 1171 periodics_reify (EV_A); /* absolute timers called first */
1018 1172
1019 /* queue idle watchers unless io or timers are pending */ 1173 /* queue idle watchers unless io or timers are pending */
1020 if (!pendingcnt) 1174 if (idlecnt && !any_pending (EV_A))
1021 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1175 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1022 1176
1023 /* queue check watchers, to be executed first */ 1177 /* queue check watchers, to be executed first */
1024 if (checkcnt) 1178 if (checkcnt)
1025 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1179 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1100 return; 1254 return;
1101 1255
1102 assert (("ev_io_start called with negative fd", fd >= 0)); 1256 assert (("ev_io_start called with negative fd", fd >= 0));
1103 1257
1104 ev_start (EV_A_ (W)w, 1); 1258 ev_start (EV_A_ (W)w, 1);
1105 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 1259 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1106 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1260 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1107 1261
1108 fd_change (EV_A_ fd); 1262 fd_change (EV_A_ fd);
1109} 1263}
1110 1264
1130 ((WT)w)->at += mn_now; 1284 ((WT)w)->at += mn_now;
1131 1285
1132 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1286 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1133 1287
1134 ev_start (EV_A_ (W)w, ++timercnt); 1288 ev_start (EV_A_ (W)w, ++timercnt);
1135 array_needsize (timers, timermax, timercnt, ); 1289 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void));
1136 timers [timercnt - 1] = w; 1290 timers [timercnt - 1] = w;
1137 upheap ((WT *)timers, timercnt - 1); 1291 upheap ((WT *)timers, timercnt - 1);
1138 1292
1139 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1293 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1140} 1294}
1180ev_periodic_start (EV_P_ struct ev_periodic *w) 1334ev_periodic_start (EV_P_ struct ev_periodic *w)
1181{ 1335{
1182 if (ev_is_active (w)) 1336 if (ev_is_active (w))
1183 return; 1337 return;
1184 1338
1339 if (w->reschedule_cb)
1340 ((WT)w)->at = w->reschedule_cb (w, rt_now);
1341 else if (w->interval)
1342 {
1185 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1343 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1186
1187 /* this formula differs from the one in periodic_reify because we do not always round up */ 1344 /* this formula differs from the one in periodic_reify because we do not always round up */
1188 if (w->interval)
1189 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1345 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1346 }
1190 1347
1191 ev_start (EV_A_ (W)w, ++periodiccnt); 1348 ev_start (EV_A_ (W)w, ++periodiccnt);
1192 array_needsize (periodics, periodicmax, periodiccnt, ); 1349 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1193 periodics [periodiccnt - 1] = w; 1350 periodics [periodiccnt - 1] = w;
1194 upheap ((WT *)periodics, periodiccnt - 1); 1351 upheap ((WT *)periodics, periodiccnt - 1);
1195 1352
1196 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1353 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1197} 1354}
1213 1370
1214 ev_stop (EV_A_ (W)w); 1371 ev_stop (EV_A_ (W)w);
1215} 1372}
1216 1373
1217void 1374void
1375ev_periodic_again (EV_P_ struct ev_periodic *w)
1376{
1377 ev_periodic_stop (EV_A_ w);
1378 ev_periodic_start (EV_A_ w);
1379}
1380
1381void
1218ev_idle_start (EV_P_ struct ev_idle *w) 1382ev_idle_start (EV_P_ struct ev_idle *w)
1219{ 1383{
1220 if (ev_is_active (w)) 1384 if (ev_is_active (w))
1221 return; 1385 return;
1222 1386
1223 ev_start (EV_A_ (W)w, ++idlecnt); 1387 ev_start (EV_A_ (W)w, ++idlecnt);
1224 array_needsize (idles, idlemax, idlecnt, ); 1388 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void));
1225 idles [idlecnt - 1] = w; 1389 idles [idlecnt - 1] = w;
1226} 1390}
1227 1391
1228void 1392void
1229ev_idle_stop (EV_P_ struct ev_idle *w) 1393ev_idle_stop (EV_P_ struct ev_idle *w)
1241{ 1405{
1242 if (ev_is_active (w)) 1406 if (ev_is_active (w))
1243 return; 1407 return;
1244 1408
1245 ev_start (EV_A_ (W)w, ++preparecnt); 1409 ev_start (EV_A_ (W)w, ++preparecnt);
1246 array_needsize (prepares, preparemax, preparecnt, ); 1410 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void));
1247 prepares [preparecnt - 1] = w; 1411 prepares [preparecnt - 1] = w;
1248} 1412}
1249 1413
1250void 1414void
1251ev_prepare_stop (EV_P_ struct ev_prepare *w) 1415ev_prepare_stop (EV_P_ struct ev_prepare *w)
1263{ 1427{
1264 if (ev_is_active (w)) 1428 if (ev_is_active (w))
1265 return; 1429 return;
1266 1430
1267 ev_start (EV_A_ (W)w, ++checkcnt); 1431 ev_start (EV_A_ (W)w, ++checkcnt);
1268 array_needsize (checks, checkmax, checkcnt, ); 1432 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void));
1269 checks [checkcnt - 1] = w; 1433 checks [checkcnt - 1] = w;
1270} 1434}
1271 1435
1272void 1436void
1273ev_check_stop (EV_P_ struct ev_check *w) 1437ev_check_stop (EV_P_ struct ev_check *w)
1294 return; 1458 return;
1295 1459
1296 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1460 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1297 1461
1298 ev_start (EV_A_ (W)w, 1); 1462 ev_start (EV_A_ (W)w, 1);
1299 array_needsize (signals, signalmax, w->signum, signals_init); 1463 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1300 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1464 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1301 1465
1302 if (!((WL)w)->next) 1466 if (!((WL)w)->next)
1303 { 1467 {
1468#if WIN32
1469 signal (w->signum, sighandler);
1470#else
1304 struct sigaction sa; 1471 struct sigaction sa;
1305 sa.sa_handler = sighandler; 1472 sa.sa_handler = sighandler;
1306 sigfillset (&sa.sa_mask); 1473 sigfillset (&sa.sa_mask);
1307 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1474 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1308 sigaction (w->signum, &sa, 0); 1475 sigaction (w->signum, &sa, 0);
1476#endif
1309 } 1477 }
1310} 1478}
1311 1479
1312void 1480void
1313ev_signal_stop (EV_P_ struct ev_signal *w) 1481ev_signal_stop (EV_P_ struct ev_signal *w)
1363 void (*cb)(int revents, void *arg) = once->cb; 1531 void (*cb)(int revents, void *arg) = once->cb;
1364 void *arg = once->arg; 1532 void *arg = once->arg;
1365 1533
1366 ev_io_stop (EV_A_ &once->io); 1534 ev_io_stop (EV_A_ &once->io);
1367 ev_timer_stop (EV_A_ &once->to); 1535 ev_timer_stop (EV_A_ &once->to);
1368 free (once); 1536 ev_free (once);
1369 1537
1370 cb (revents, arg); 1538 cb (revents, arg);
1371} 1539}
1372 1540
1373static void 1541static void
1383} 1551}
1384 1552
1385void 1553void
1386ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1554ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1387{ 1555{
1388 struct ev_once *once = malloc (sizeof (struct ev_once)); 1556 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1389 1557
1390 if (!once) 1558 if (!once)
1391 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1559 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1392 else 1560 else
1393 { 1561 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines