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

Comparing libev/ev.c (file contents):
Revision 1.65 by root, Sun Nov 4 23:29:48 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;
236 307
237#define array_free(stem, idx) \ 308#define array_free(stem, idx) \
238 free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 309 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
239 310
240/*****************************************************************************/ 311/*****************************************************************************/
241 312
242static void 313static void
243anfds_init (ANFD *base, int count) 314anfds_init (ANFD *base, int count)
250 321
251 ++base; 322 ++base;
252 } 323 }
253} 324}
254 325
255static void 326void
256event (EV_P_ W w, int events) 327ev_feed_event (EV_P_ void *w, int revents)
257{ 328{
329 W w_ = (W)w;
330
258 if (w->pending) 331 if (w_->pending)
259 { 332 {
260 pendings [ABSPRI (w)][w->pending - 1].events |= events; 333 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
261 return; 334 return;
262 } 335 }
263 336
264 w->pending = ++pendingcnt [ABSPRI (w)]; 337 w_->pending = ++pendingcnt [ABSPRI (w_)];
265 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));
266 pendings [ABSPRI (w)][w->pending - 1].w = w; 339 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
267 pendings [ABSPRI (w)][w->pending - 1].events = events; 340 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
268} 341}
269 342
270static void 343static void
271queue_events (EV_P_ W *events, int eventcnt, int type) 344queue_events (EV_P_ W *events, int eventcnt, int type)
272{ 345{
273 int i; 346 int i;
274 347
275 for (i = 0; i < eventcnt; ++i) 348 for (i = 0; i < eventcnt; ++i)
276 event (EV_A_ events [i], type); 349 ev_feed_event (EV_A_ events [i], type);
277} 350}
278 351
279static void 352inline void
280fd_event (EV_P_ int fd, int events) 353fd_event (EV_P_ int fd, int revents)
281{ 354{
282 ANFD *anfd = anfds + fd; 355 ANFD *anfd = anfds + fd;
283 struct ev_io *w; 356 struct ev_io *w;
284 357
285 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)
286 { 359 {
287 int ev = w->events & events; 360 int ev = w->events & revents;
288 361
289 if (ev) 362 if (ev)
290 event (EV_A_ (W)w, ev); 363 ev_feed_event (EV_A_ (W)w, ev);
291 } 364 }
365}
366
367void
368ev_feed_fd_event (EV_P_ int fd, int revents)
369{
370 fd_event (EV_A_ fd, revents);
292} 371}
293 372
294/*****************************************************************************/ 373/*****************************************************************************/
295 374
296static void 375static void
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)
658#endif 776#endif
659 777
660 for (i = NUMPRI; i--; ) 778 for (i = NUMPRI; i--; )
661 array_free (pending, [i]); 779 array_free (pending, [i]);
662 780
781 /* have to use the microsoft-never-gets-it-right macro */
663 array_free (fdchange, ); 782 array_free_microshit (fdchange);
664 array_free (timer, ); 783 array_free_microshit (timer);
665 array_free (periodic, ); 784 array_free_microshit (periodic);
666 array_free (idle, ); 785 array_free_microshit (idle);
667 array_free (prepare, ); 786 array_free_microshit (prepare);
668 array_free (check, ); 787 array_free_microshit (check);
669 788
670 method = 0; 789 method = 0;
671 /*TODO*/
672} 790}
673 791
674void 792static void
675loop_fork (EV_P) 793loop_fork (EV_P)
676{ 794{
677 /*TODO*/
678#if EV_USE_EPOLL 795#if EV_USE_EPOLL
679 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 796 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
680#endif 797#endif
681#if EV_USE_KQUEUE 798#if EV_USE_KQUEUE
682 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 799 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
683#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;
684} 818}
685 819
686#if EV_MULTIPLICITY 820#if EV_MULTIPLICITY
687struct ev_loop * 821struct ev_loop *
688ev_loop_new (int methods) 822ev_loop_new (int methods)
689{ 823{
690 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));
691 827
692 loop_init (EV_A_ methods); 828 loop_init (EV_A_ methods);
693 829
694 if (ev_method (EV_A)) 830 if (ev_method (EV_A))
695 return loop; 831 return loop;
699 835
700void 836void
701ev_loop_destroy (EV_P) 837ev_loop_destroy (EV_P)
702{ 838{
703 loop_destroy (EV_A); 839 loop_destroy (EV_A);
704 free (loop); 840 ev_free (loop);
705} 841}
706 842
707void 843void
708ev_loop_fork (EV_P) 844ev_loop_fork (EV_P)
709{ 845{
710 loop_fork (EV_A); 846 postfork = 1;
711} 847}
712 848
713#endif 849#endif
714 850
715#if EV_MULTIPLICITY 851#if EV_MULTIPLICITY
716struct ev_loop default_loop_struct;
717static struct ev_loop *default_loop;
718
719struct ev_loop * 852struct ev_loop *
720#else 853#else
721static int default_loop;
722
723int 854int
724#endif 855#endif
725ev_default_loop (int methods) 856ev_default_loop (int methods)
726{ 857{
727 if (sigpipe [0] == sigpipe [1]) 858 if (sigpipe [0] == sigpipe [1])
738 869
739 loop_init (EV_A_ methods); 870 loop_init (EV_A_ methods);
740 871
741 if (ev_method (EV_A)) 872 if (ev_method (EV_A))
742 { 873 {
743 ev_watcher_init (&sigev, sigcb);
744 ev_set_priority (&sigev, EV_MAXPRI);
745 siginit (EV_A); 874 siginit (EV_A);
746 875
747#ifndef WIN32 876#ifndef WIN32
748 ev_signal_init (&childev, childcb, SIGCHLD); 877 ev_signal_init (&childev, childcb, SIGCHLD);
749 ev_set_priority (&childev, EV_MAXPRI); 878 ev_set_priority (&childev, EV_MAXPRI);
763{ 892{
764#if EV_MULTIPLICITY 893#if EV_MULTIPLICITY
765 struct ev_loop *loop = default_loop; 894 struct ev_loop *loop = default_loop;
766#endif 895#endif
767 896
897#ifndef WIN32
768 ev_ref (EV_A); /* child watcher */ 898 ev_ref (EV_A); /* child watcher */
769 ev_signal_stop (EV_A_ &childev); 899 ev_signal_stop (EV_A_ &childev);
900#endif
770 901
771 ev_ref (EV_A); /* signal watcher */ 902 ev_ref (EV_A); /* signal watcher */
772 ev_io_stop (EV_A_ &sigev); 903 ev_io_stop (EV_A_ &sigev);
773 904
774 close (sigpipe [0]); sigpipe [0] = 0; 905 close (sigpipe [0]); sigpipe [0] = 0;
782{ 913{
783#if EV_MULTIPLICITY 914#if EV_MULTIPLICITY
784 struct ev_loop *loop = default_loop; 915 struct ev_loop *loop = default_loop;
785#endif 916#endif
786 917
787 loop_fork (EV_A); 918 if (method)
788 919 postfork = 1;
789 ev_io_stop (EV_A_ &sigev);
790 close (sigpipe [0]);
791 close (sigpipe [1]);
792 pipe (sigpipe);
793
794 ev_ref (EV_A); /* signal watcher */
795 siginit (EV_A);
796} 920}
797 921
798/*****************************************************************************/ 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}
799 935
800static void 936static void
801call_pending (EV_P) 937call_pending (EV_P)
802{ 938{
803 int pri; 939 int pri;
808 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 944 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
809 945
810 if (p->w) 946 if (p->w)
811 { 947 {
812 p->w->pending = 0; 948 p->w->pending = 0;
813
814 ((void (*)(EV_P_ W, int))p->w->cb) (EV_A_ p->w, p->events); 949 p->w->cb (EV_A_ p->w, p->events);
815 } 950 }
816 } 951 }
817} 952}
818 953
819static void 954static void
833 downheap ((WT *)timers, timercnt, 0); 968 downheap ((WT *)timers, timercnt, 0);
834 } 969 }
835 else 970 else
836 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 971 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
837 972
838 event (EV_A_ (W)w, EV_TIMEOUT); 973 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
839 } 974 }
840} 975}
841 976
842static void 977static void
843periodics_reify (EV_P) 978periodics_reify (EV_P)
847 struct ev_periodic *w = periodics [0]; 982 struct ev_periodic *w = periodics [0];
848 983
849 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 984 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
850 985
851 /* 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 }
852 if (w->interval) 994 else if (w->interval)
853 { 995 {
854 ((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;
855 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));
856 downheap ((WT *)periodics, periodiccnt, 0); 998 downheap ((WT *)periodics, periodiccnt, 0);
857 } 999 }
858 else 1000 else
859 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1001 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
860 1002
861 event (EV_A_ (W)w, EV_PERIODIC); 1003 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
862 } 1004 }
863} 1005}
864 1006
865static void 1007static void
866periodics_reschedule (EV_P) 1008periodics_reschedule (EV_P)
870 /* adjust periodics after time jump */ 1012 /* adjust periodics after time jump */
871 for (i = 0; i < periodiccnt; ++i) 1013 for (i = 0; i < periodiccnt; ++i)
872 { 1014 {
873 struct ev_periodic *w = periodics [i]; 1015 struct ev_periodic *w = periodics [i];
874 1016
1017 if (w->reschedule_cb)
1018 ((WT)w)->at = w->reschedule_cb (w, rt_now);
875 if (w->interval) 1019 else if (w->interval)
876 {
877 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;
878
879 if (fabs (diff) >= 1e-4)
880 {
881 ev_periodic_stop (EV_A_ w);
882 ev_periodic_start (EV_A_ w);
883
884 i = 0; /* restart loop, inefficient, but time jumps should be rare */
885 }
886 }
887 } 1021 }
1022
1023 /* now rebuild the heap */
1024 for (i = periodiccnt >> 1; i--; )
1025 downheap ((WT *)periodics, periodiccnt, i);
888} 1026}
889 1027
890inline int 1028inline int
891time_update_monotonic (EV_P) 1029time_update_monotonic (EV_P)
892{ 1030{
979 { 1117 {
980 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1118 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
981 call_pending (EV_A); 1119 call_pending (EV_A);
982 } 1120 }
983 1121
1122 /* we might have forked, so reify kernel state if necessary */
1123 if (expect_false (postfork))
1124 loop_fork (EV_A);
1125
984 /* update fd-related kernel structures */ 1126 /* update fd-related kernel structures */
985 fd_reify (EV_A); 1127 fd_reify (EV_A);
986 1128
987 /* calculate blocking time */ 1129 /* calculate blocking time */
988 1130
989 /* 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
990 always have timers, we just calculate it always */ 1132 always have timers, we just calculate it always */
991#if EV_USE_MONOTONIC 1133#if EV_USE_MONOTONIC
992 if (expect_true (have_monotonic)) 1134 if (expect_true (have_monotonic))
993 time_update_monotonic (EV_A); 1135 time_update_monotonic (EV_A);
994 else 1136 else
1027 /* queue pending timers and reschedule them */ 1169 /* queue pending timers and reschedule them */
1028 timers_reify (EV_A); /* relative timers called last */ 1170 timers_reify (EV_A); /* relative timers called last */
1029 periodics_reify (EV_A); /* absolute timers called first */ 1171 periodics_reify (EV_A); /* absolute timers called first */
1030 1172
1031 /* queue idle watchers unless io or timers are pending */ 1173 /* queue idle watchers unless io or timers are pending */
1032 if (!pendingcnt) 1174 if (idlecnt && !any_pending (EV_A))
1033 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1175 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1034 1176
1035 /* queue check watchers, to be executed first */ 1177 /* queue check watchers, to be executed first */
1036 if (checkcnt) 1178 if (checkcnt)
1037 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1179 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1112 return; 1254 return;
1113 1255
1114 assert (("ev_io_start called with negative fd", fd >= 0)); 1256 assert (("ev_io_start called with negative fd", fd >= 0));
1115 1257
1116 ev_start (EV_A_ (W)w, 1); 1258 ev_start (EV_A_ (W)w, 1);
1117 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 1259 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1118 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1260 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1119 1261
1120 fd_change (EV_A_ fd); 1262 fd_change (EV_A_ fd);
1121} 1263}
1122 1264
1142 ((WT)w)->at += mn_now; 1284 ((WT)w)->at += mn_now;
1143 1285
1144 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.));
1145 1287
1146 ev_start (EV_A_ (W)w, ++timercnt); 1288 ev_start (EV_A_ (W)w, ++timercnt);
1147 array_needsize (timers, timermax, timercnt, ); 1289 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void));
1148 timers [timercnt - 1] = w; 1290 timers [timercnt - 1] = w;
1149 upheap ((WT *)timers, timercnt - 1); 1291 upheap ((WT *)timers, timercnt - 1);
1150 1292
1151 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1293 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1152} 1294}
1192ev_periodic_start (EV_P_ struct ev_periodic *w) 1334ev_periodic_start (EV_P_ struct ev_periodic *w)
1193{ 1335{
1194 if (ev_is_active (w)) 1336 if (ev_is_active (w))
1195 return; 1337 return;
1196 1338
1339 if (w->reschedule_cb)
1340 ((WT)w)->at = w->reschedule_cb (w, rt_now);
1341 else if (w->interval)
1342 {
1197 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.));
1198
1199 /* 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 */
1200 if (w->interval)
1201 ((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 }
1202 1347
1203 ev_start (EV_A_ (W)w, ++periodiccnt); 1348 ev_start (EV_A_ (W)w, ++periodiccnt);
1204 array_needsize (periodics, periodicmax, periodiccnt, ); 1349 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1205 periodics [periodiccnt - 1] = w; 1350 periodics [periodiccnt - 1] = w;
1206 upheap ((WT *)periodics, periodiccnt - 1); 1351 upheap ((WT *)periodics, periodiccnt - 1);
1207 1352
1208 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1353 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1209} 1354}
1225 1370
1226 ev_stop (EV_A_ (W)w); 1371 ev_stop (EV_A_ (W)w);
1227} 1372}
1228 1373
1229void 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
1230ev_idle_start (EV_P_ struct ev_idle *w) 1382ev_idle_start (EV_P_ struct ev_idle *w)
1231{ 1383{
1232 if (ev_is_active (w)) 1384 if (ev_is_active (w))
1233 return; 1385 return;
1234 1386
1235 ev_start (EV_A_ (W)w, ++idlecnt); 1387 ev_start (EV_A_ (W)w, ++idlecnt);
1236 array_needsize (idles, idlemax, idlecnt, ); 1388 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void));
1237 idles [idlecnt - 1] = w; 1389 idles [idlecnt - 1] = w;
1238} 1390}
1239 1391
1240void 1392void
1241ev_idle_stop (EV_P_ struct ev_idle *w) 1393ev_idle_stop (EV_P_ struct ev_idle *w)
1253{ 1405{
1254 if (ev_is_active (w)) 1406 if (ev_is_active (w))
1255 return; 1407 return;
1256 1408
1257 ev_start (EV_A_ (W)w, ++preparecnt); 1409 ev_start (EV_A_ (W)w, ++preparecnt);
1258 array_needsize (prepares, preparemax, preparecnt, ); 1410 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void));
1259 prepares [preparecnt - 1] = w; 1411 prepares [preparecnt - 1] = w;
1260} 1412}
1261 1413
1262void 1414void
1263ev_prepare_stop (EV_P_ struct ev_prepare *w) 1415ev_prepare_stop (EV_P_ struct ev_prepare *w)
1275{ 1427{
1276 if (ev_is_active (w)) 1428 if (ev_is_active (w))
1277 return; 1429 return;
1278 1430
1279 ev_start (EV_A_ (W)w, ++checkcnt); 1431 ev_start (EV_A_ (W)w, ++checkcnt);
1280 array_needsize (checks, checkmax, checkcnt, ); 1432 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void));
1281 checks [checkcnt - 1] = w; 1433 checks [checkcnt - 1] = w;
1282} 1434}
1283 1435
1284void 1436void
1285ev_check_stop (EV_P_ struct ev_check *w) 1437ev_check_stop (EV_P_ struct ev_check *w)
1306 return; 1458 return;
1307 1459
1308 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));
1309 1461
1310 ev_start (EV_A_ (W)w, 1); 1462 ev_start (EV_A_ (W)w, 1);
1311 array_needsize (signals, signalmax, w->signum, signals_init); 1463 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1312 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1464 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1313 1465
1314 if (!((WL)w)->next) 1466 if (!((WL)w)->next)
1315 { 1467 {
1468#if WIN32
1469 signal (w->signum, sighandler);
1470#else
1316 struct sigaction sa; 1471 struct sigaction sa;
1317 sa.sa_handler = sighandler; 1472 sa.sa_handler = sighandler;
1318 sigfillset (&sa.sa_mask); 1473 sigfillset (&sa.sa_mask);
1319 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 */
1320 sigaction (w->signum, &sa, 0); 1475 sigaction (w->signum, &sa, 0);
1476#endif
1321 } 1477 }
1322} 1478}
1323 1479
1324void 1480void
1325ev_signal_stop (EV_P_ struct ev_signal *w) 1481ev_signal_stop (EV_P_ struct ev_signal *w)
1375 void (*cb)(int revents, void *arg) = once->cb; 1531 void (*cb)(int revents, void *arg) = once->cb;
1376 void *arg = once->arg; 1532 void *arg = once->arg;
1377 1533
1378 ev_io_stop (EV_A_ &once->io); 1534 ev_io_stop (EV_A_ &once->io);
1379 ev_timer_stop (EV_A_ &once->to); 1535 ev_timer_stop (EV_A_ &once->to);
1380 free (once); 1536 ev_free (once);
1381 1537
1382 cb (revents, arg); 1538 cb (revents, arg);
1383} 1539}
1384 1540
1385static void 1541static void
1395} 1551}
1396 1552
1397void 1553void
1398ev_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)
1399{ 1555{
1400 struct ev_once *once = malloc (sizeof (struct ev_once)); 1556 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1401 1557
1402 if (!once) 1558 if (!once)
1403 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1559 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1404 else 1560 else
1405 { 1561 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines