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

Comparing libev/ev.c (file contents):
Revision 1.134 by root, Fri Nov 23 19:13:33 2007 UTC vs.
Revision 1.149 by root, Tue Nov 27 19:23:31 2007 UTC

111#include <time.h> 111#include <time.h>
112 112
113#include <signal.h> 113#include <signal.h>
114 114
115#ifndef _WIN32 115#ifndef _WIN32
116# include <unistd.h>
117# include <sys/time.h> 116# include <sys/time.h>
118# include <sys/wait.h> 117# include <sys/wait.h>
118# include <unistd.h>
119#else 119#else
120# define WIN32_LEAN_AND_MEAN 120# define WIN32_LEAN_AND_MEAN
121# include <windows.h> 121# include <windows.h>
122# ifndef EV_SELECT_IS_WINSOCKET 122# ifndef EV_SELECT_IS_WINSOCKET
123# define EV_SELECT_IS_WINSOCKET 1 123# define EV_SELECT_IS_WINSOCKET 1
156 156
157#ifndef EV_USE_PORT 157#ifndef EV_USE_PORT
158# define EV_USE_PORT 0 158# define EV_USE_PORT 0
159#endif 159#endif
160 160
161#ifndef EV_PID_HASHSIZE
162# if EV_MINIMAL
163# define EV_PID_HASHSIZE 1
164# else
165# define EV_PID_HASHSIZE 16
166# endif
167#endif
168
161/**/ 169/**/
162 170
163#ifndef CLOCK_MONOTONIC 171#ifndef CLOCK_MONOTONIC
164# undef EV_USE_MONOTONIC 172# undef EV_USE_MONOTONIC
165# define EV_USE_MONOTONIC 0 173# define EV_USE_MONOTONIC 0
176 184
177/**/ 185/**/
178 186
179#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 187#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
180#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 188#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
181#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
182/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ 189/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
183 190
184#ifdef EV_H 191#ifdef EV_H
185# include EV_H 192# include EV_H
186#else 193#else
187# include "ev.h" 194# include "ev.h"
188#endif 195#endif
189 196
190#if __GNUC__ >= 3 197#if __GNUC__ >= 3
191# define expect(expr,value) __builtin_expect ((expr),(value)) 198# define expect(expr,value) __builtin_expect ((expr),(value))
199# define inline_size static inline /* inline for codesize */
200# if EV_MINIMAL
201# define noinline __attribute__ ((noinline))
202# define inline_speed static noinline
203# else
204# define noinline
192# define inline static inline 205# define inline_speed static inline
206# endif
193#else 207#else
194# define expect(expr,value) (expr) 208# define expect(expr,value) (expr)
209# define inline_speed static
195# define inline static 210# define inline_size static
211# define noinline
196#endif 212#endif
197 213
198#define expect_false(expr) expect ((expr) != 0, 0) 214#define expect_false(expr) expect ((expr) != 0, 0)
199#define expect_true(expr) expect ((expr) != 0, 1) 215#define expect_true(expr) expect ((expr) != 0, 1)
200 216
202#define ABSPRI(w) ((w)->priority - EV_MINPRI) 218#define ABSPRI(w) ((w)->priority - EV_MINPRI)
203 219
204#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 220#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
205#define EMPTY2(a,b) /* used to suppress some warnings */ 221#define EMPTY2(a,b) /* used to suppress some warnings */
206 222
207typedef struct ev_watcher *W; 223typedef ev_watcher *W;
208typedef struct ev_watcher_list *WL; 224typedef ev_watcher_list *WL;
209typedef struct ev_watcher_time *WT; 225typedef ev_watcher_time *WT;
210 226
211static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 227static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
212 228
213#ifdef _WIN32 229#ifdef _WIN32
214# include "ev_win32.c" 230# include "ev_win32.c"
216 232
217/*****************************************************************************/ 233/*****************************************************************************/
218 234
219static void (*syserr_cb)(const char *msg); 235static void (*syserr_cb)(const char *msg);
220 236
237void
221void ev_set_syserr_cb (void (*cb)(const char *msg)) 238ev_set_syserr_cb (void (*cb)(const char *msg))
222{ 239{
223 syserr_cb = cb; 240 syserr_cb = cb;
224} 241}
225 242
226static void 243static void noinline
227syserr (const char *msg) 244syserr (const char *msg)
228{ 245{
229 if (!msg) 246 if (!msg)
230 msg = "(libev) system error"; 247 msg = "(libev) system error";
231 248
238 } 255 }
239} 256}
240 257
241static void *(*alloc)(void *ptr, long size); 258static void *(*alloc)(void *ptr, long size);
242 259
260void
243void ev_set_allocator (void *(*cb)(void *ptr, long size)) 261ev_set_allocator (void *(*cb)(void *ptr, long size))
244{ 262{
245 alloc = cb; 263 alloc = cb;
246} 264}
247 265
248static void * 266static void *
320 gettimeofday (&tv, 0); 338 gettimeofday (&tv, 0);
321 return tv.tv_sec + tv.tv_usec * 1e-6; 339 return tv.tv_sec + tv.tv_usec * 1e-6;
322#endif 340#endif
323} 341}
324 342
325inline ev_tstamp 343ev_tstamp inline_size
326get_clock (void) 344get_clock (void)
327{ 345{
328#if EV_USE_MONOTONIC 346#if EV_USE_MONOTONIC
329 if (expect_true (have_monotonic)) 347 if (expect_true (have_monotonic))
330 { 348 {
373#define array_free(stem, idx) \ 391#define array_free(stem, idx) \
374 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 392 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
375 393
376/*****************************************************************************/ 394/*****************************************************************************/
377 395
378static void 396void noinline
379anfds_init (ANFD *base, int count)
380{
381 while (count--)
382 {
383 base->head = 0;
384 base->events = EV_NONE;
385 base->reify = 0;
386
387 ++base;
388 }
389}
390
391void
392ev_feed_event (EV_P_ void *w, int revents) 397ev_feed_event (EV_P_ void *w, int revents)
393{ 398{
394 W w_ = (W)w; 399 W w_ = (W)w;
395 400
396 if (expect_false (w_->pending)) 401 if (expect_false (w_->pending))
397 { 402 {
398 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 403 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
399 return; 404 return;
400 } 405 }
401
402 if (expect_false (!w_->cb))
403 return;
404 406
405 w_->pending = ++pendingcnt [ABSPRI (w_)]; 407 w_->pending = ++pendingcnt [ABSPRI (w_)];
406 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); 408 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2);
407 pendings [ABSPRI (w_)][w_->pending - 1].w = w_; 409 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
408 pendings [ABSPRI (w_)][w_->pending - 1].events = revents; 410 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
409} 411}
410 412
411static void 413void inline_size
412queue_events (EV_P_ W *events, int eventcnt, int type) 414queue_events (EV_P_ W *events, int eventcnt, int type)
413{ 415{
414 int i; 416 int i;
415 417
416 for (i = 0; i < eventcnt; ++i) 418 for (i = 0; i < eventcnt; ++i)
417 ev_feed_event (EV_A_ events [i], type); 419 ev_feed_event (EV_A_ events [i], type);
418} 420}
419 421
420inline void 422/*****************************************************************************/
423
424void inline_size
425anfds_init (ANFD *base, int count)
426{
427 while (count--)
428 {
429 base->head = 0;
430 base->events = EV_NONE;
431 base->reify = 0;
432
433 ++base;
434 }
435}
436
437void inline_speed
421fd_event (EV_P_ int fd, int revents) 438fd_event (EV_P_ int fd, int revents)
422{ 439{
423 ANFD *anfd = anfds + fd; 440 ANFD *anfd = anfds + fd;
424 struct ev_io *w; 441 ev_io *w;
425 442
426 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 443 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
427 { 444 {
428 int ev = w->events & revents; 445 int ev = w->events & revents;
429 446
430 if (ev) 447 if (ev)
431 ev_feed_event (EV_A_ (W)w, ev); 448 ev_feed_event (EV_A_ (W)w, ev);
436ev_feed_fd_event (EV_P_ int fd, int revents) 453ev_feed_fd_event (EV_P_ int fd, int revents)
437{ 454{
438 fd_event (EV_A_ fd, revents); 455 fd_event (EV_A_ fd, revents);
439} 456}
440 457
441/*****************************************************************************/ 458void inline_size
442
443inline void
444fd_reify (EV_P) 459fd_reify (EV_P)
445{ 460{
446 int i; 461 int i;
447 462
448 for (i = 0; i < fdchangecnt; ++i) 463 for (i = 0; i < fdchangecnt; ++i)
449 { 464 {
450 int fd = fdchanges [i]; 465 int fd = fdchanges [i];
451 ANFD *anfd = anfds + fd; 466 ANFD *anfd = anfds + fd;
452 struct ev_io *w; 467 ev_io *w;
453 468
454 int events = 0; 469 int events = 0;
455 470
456 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 471 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
457 events |= w->events; 472 events |= w->events;
458 473
459#if EV_SELECT_IS_WINSOCKET 474#if EV_SELECT_IS_WINSOCKET
460 if (events) 475 if (events)
461 { 476 {
472 } 487 }
473 488
474 fdchangecnt = 0; 489 fdchangecnt = 0;
475} 490}
476 491
477static void 492void inline_size
478fd_change (EV_P_ int fd) 493fd_change (EV_P_ int fd)
479{ 494{
480 if (expect_false (anfds [fd].reify)) 495 if (expect_false (anfds [fd].reify))
481 return; 496 return;
482 497
485 ++fdchangecnt; 500 ++fdchangecnt;
486 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 501 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
487 fdchanges [fdchangecnt - 1] = fd; 502 fdchanges [fdchangecnt - 1] = fd;
488} 503}
489 504
490static void 505void inline_speed
491fd_kill (EV_P_ int fd) 506fd_kill (EV_P_ int fd)
492{ 507{
493 struct ev_io *w; 508 ev_io *w;
494 509
495 while ((w = (struct ev_io *)anfds [fd].head)) 510 while ((w = (ev_io *)anfds [fd].head))
496 { 511 {
497 ev_io_stop (EV_A_ w); 512 ev_io_stop (EV_A_ w);
498 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 513 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
499 } 514 }
500} 515}
501 516
502inline int 517int inline_size
503fd_valid (int fd) 518fd_valid (int fd)
504{ 519{
505#ifdef _WIN32 520#ifdef _WIN32
506 return _get_osfhandle (fd) != -1; 521 return _get_osfhandle (fd) != -1;
507#else 522#else
508 return fcntl (fd, F_GETFD) != -1; 523 return fcntl (fd, F_GETFD) != -1;
509#endif 524#endif
510} 525}
511 526
512/* called on EBADF to verify fds */ 527/* called on EBADF to verify fds */
513static void 528static void noinline
514fd_ebadf (EV_P) 529fd_ebadf (EV_P)
515{ 530{
516 int fd; 531 int fd;
517 532
518 for (fd = 0; fd < anfdmax; ++fd) 533 for (fd = 0; fd < anfdmax; ++fd)
520 if (!fd_valid (fd) == -1 && errno == EBADF) 535 if (!fd_valid (fd) == -1 && errno == EBADF)
521 fd_kill (EV_A_ fd); 536 fd_kill (EV_A_ fd);
522} 537}
523 538
524/* called on ENOMEM in select/poll to kill some fds and retry */ 539/* called on ENOMEM in select/poll to kill some fds and retry */
525static void 540static void noinline
526fd_enomem (EV_P) 541fd_enomem (EV_P)
527{ 542{
528 int fd; 543 int fd;
529 544
530 for (fd = anfdmax; fd--; ) 545 for (fd = anfdmax; fd--; )
534 return; 549 return;
535 } 550 }
536} 551}
537 552
538/* usually called after fork if backend needs to re-arm all fds from scratch */ 553/* usually called after fork if backend needs to re-arm all fds from scratch */
539static void 554static void noinline
540fd_rearm_all (EV_P) 555fd_rearm_all (EV_P)
541{ 556{
542 int fd; 557 int fd;
543 558
544 /* this should be highly optimised to not do anything but set a flag */ 559 /* this should be highly optimised to not do anything but set a flag */
550 } 565 }
551} 566}
552 567
553/*****************************************************************************/ 568/*****************************************************************************/
554 569
555static void 570void inline_speed
556upheap (WT *heap, int k) 571upheap (WT *heap, int k)
557{ 572{
558 WT w = heap [k]; 573 WT w = heap [k];
559 574
560 while (k && heap [k >> 1]->at > w->at) 575 while (k && heap [k >> 1]->at > w->at)
567 heap [k] = w; 582 heap [k] = w;
568 ((W)heap [k])->active = k + 1; 583 ((W)heap [k])->active = k + 1;
569 584
570} 585}
571 586
572static void 587void inline_speed
573downheap (WT *heap, int N, int k) 588downheap (WT *heap, int N, int k)
574{ 589{
575 WT w = heap [k]; 590 WT w = heap [k];
576 591
577 while (k < (N >> 1)) 592 while (k < (N >> 1))
591 606
592 heap [k] = w; 607 heap [k] = w;
593 ((W)heap [k])->active = k + 1; 608 ((W)heap [k])->active = k + 1;
594} 609}
595 610
596inline void 611void inline_size
597adjustheap (WT *heap, int N, int k) 612adjustheap (WT *heap, int N, int k)
598{ 613{
599 upheap (heap, k); 614 upheap (heap, k);
600 downheap (heap, N, k); 615 downheap (heap, N, k);
601} 616}
611static ANSIG *signals; 626static ANSIG *signals;
612static int signalmax; 627static int signalmax;
613 628
614static int sigpipe [2]; 629static int sigpipe [2];
615static sig_atomic_t volatile gotsig; 630static sig_atomic_t volatile gotsig;
616static struct ev_io sigev; 631static ev_io sigev;
617 632
618static void 633void inline_size
619signals_init (ANSIG *base, int count) 634signals_init (ANSIG *base, int count)
620{ 635{
621 while (count--) 636 while (count--)
622 { 637 {
623 base->head = 0; 638 base->head = 0;
643 write (sigpipe [1], &signum, 1); 658 write (sigpipe [1], &signum, 1);
644 errno = old_errno; 659 errno = old_errno;
645 } 660 }
646} 661}
647 662
648void 663void noinline
649ev_feed_signal_event (EV_P_ int signum) 664ev_feed_signal_event (EV_P_ int signum)
650{ 665{
651 WL w; 666 WL w;
652 667
653#if EV_MULTIPLICITY 668#if EV_MULTIPLICITY
664 for (w = signals [signum].head; w; w = w->next) 679 for (w = signals [signum].head; w; w = w->next)
665 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 680 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
666} 681}
667 682
668static void 683static void
669sigcb (EV_P_ struct ev_io *iow, int revents) 684sigcb (EV_P_ ev_io *iow, int revents)
670{ 685{
671 int signum; 686 int signum;
672 687
673 read (sigpipe [0], &revents, 1); 688 read (sigpipe [0], &revents, 1);
674 gotsig = 0; 689 gotsig = 0;
676 for (signum = signalmax; signum--; ) 691 for (signum = signalmax; signum--; )
677 if (signals [signum].gotsig) 692 if (signals [signum].gotsig)
678 ev_feed_signal_event (EV_A_ signum + 1); 693 ev_feed_signal_event (EV_A_ signum + 1);
679} 694}
680 695
681static void 696void inline_size
682fd_intern (int fd) 697fd_intern (int fd)
683{ 698{
684#ifdef _WIN32 699#ifdef _WIN32
685 int arg = 1; 700 int arg = 1;
686 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 701 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
688 fcntl (fd, F_SETFD, FD_CLOEXEC); 703 fcntl (fd, F_SETFD, FD_CLOEXEC);
689 fcntl (fd, F_SETFL, O_NONBLOCK); 704 fcntl (fd, F_SETFL, O_NONBLOCK);
690#endif 705#endif
691} 706}
692 707
693static void 708static void noinline
694siginit (EV_P) 709siginit (EV_P)
695{ 710{
696 fd_intern (sigpipe [0]); 711 fd_intern (sigpipe [0]);
697 fd_intern (sigpipe [1]); 712 fd_intern (sigpipe [1]);
698 713
701 ev_unref (EV_A); /* child watcher should not keep loop alive */ 716 ev_unref (EV_A); /* child watcher should not keep loop alive */
702} 717}
703 718
704/*****************************************************************************/ 719/*****************************************************************************/
705 720
706static struct ev_child *childs [PID_HASHSIZE]; 721static ev_child *childs [EV_PID_HASHSIZE];
707 722
708#ifndef _WIN32 723#ifndef _WIN32
709 724
710static struct ev_signal childev; 725static ev_signal childev;
711 726
712#ifndef WCONTINUED 727void inline_speed
713# define WCONTINUED 0
714#endif
715
716static void
717child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) 728child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status)
718{ 729{
719 struct ev_child *w; 730 ev_child *w;
720 731
721 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 732 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
722 if (w->pid == pid || !w->pid) 733 if (w->pid == pid || !w->pid)
723 { 734 {
724 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 735 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
725 w->rpid = pid; 736 w->rpid = pid;
726 w->rstatus = status; 737 w->rstatus = status;
727 ev_feed_event (EV_A_ (W)w, EV_CHILD); 738 ev_feed_event (EV_A_ (W)w, EV_CHILD);
728 } 739 }
729} 740}
730 741
742#ifndef WCONTINUED
743# define WCONTINUED 0
744#endif
745
731static void 746static void
732childcb (EV_P_ struct ev_signal *sw, int revents) 747childcb (EV_P_ ev_signal *sw, int revents)
733{ 748{
734 int pid, status; 749 int pid, status;
735 750
751 /* some systems define WCONTINUED but then fail to support it (linux 2.4) */
736 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 752 if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
737 { 753 if (!WCONTINUED
754 || errno != EINVAL
755 || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED)))
756 return;
757
738 /* make sure we are called again until all childs have been reaped */ 758 /* make sure we are called again until all childs have been reaped */
739 /* we need to do it this way so that the callback gets called before we continue */ 759 /* we need to do it this way so that the callback gets called before we continue */
740 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); 760 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
741 761
742 child_reap (EV_A_ sw, pid, pid, status); 762 child_reap (EV_A_ sw, pid, pid, status);
763 if (EV_PID_HASHSIZE > 1)
743 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ 764 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
744 }
745} 765}
746 766
747#endif 767#endif
748 768
749/*****************************************************************************/ 769/*****************************************************************************/
775{ 795{
776 return EV_VERSION_MINOR; 796 return EV_VERSION_MINOR;
777} 797}
778 798
779/* return true if we are running with elevated privileges and should ignore env variables */ 799/* return true if we are running with elevated privileges and should ignore env variables */
780static int 800int inline_size
781enable_secure (void) 801enable_secure (void)
782{ 802{
783#ifdef _WIN32 803#ifdef _WIN32
784 return 0; 804 return 0;
785#else 805#else
907 array_free (pending, [i]); 927 array_free (pending, [i]);
908 928
909 /* have to use the microsoft-never-gets-it-right macro */ 929 /* have to use the microsoft-never-gets-it-right macro */
910 array_free (fdchange, EMPTY0); 930 array_free (fdchange, EMPTY0);
911 array_free (timer, EMPTY0); 931 array_free (timer, EMPTY0);
912#if EV_PERIODICS 932#if EV_PERIODIC_ENABLE
913 array_free (periodic, EMPTY0); 933 array_free (periodic, EMPTY0);
914#endif 934#endif
915 array_free (idle, EMPTY0); 935 array_free (idle, EMPTY0);
916 array_free (prepare, EMPTY0); 936 array_free (prepare, EMPTY0);
917 array_free (check, EMPTY0); 937 array_free (check, EMPTY0);
1053 postfork = 1; 1073 postfork = 1;
1054} 1074}
1055 1075
1056/*****************************************************************************/ 1076/*****************************************************************************/
1057 1077
1058static int 1078int inline_size
1059any_pending (EV_P) 1079any_pending (EV_P)
1060{ 1080{
1061 int pri; 1081 int pri;
1062 1082
1063 for (pri = NUMPRI; pri--; ) 1083 for (pri = NUMPRI; pri--; )
1065 return 1; 1085 return 1;
1066 1086
1067 return 0; 1087 return 0;
1068} 1088}
1069 1089
1070inline void 1090void inline_speed
1071call_pending (EV_P) 1091call_pending (EV_P)
1072{ 1092{
1073 int pri; 1093 int pri;
1074 1094
1075 for (pri = NUMPRI; pri--; ) 1095 for (pri = NUMPRI; pri--; )
1077 { 1097 {
1078 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1098 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1079 1099
1080 if (expect_true (p->w)) 1100 if (expect_true (p->w))
1081 { 1101 {
1102 assert (("non-pending watcher on pending list", p->w->pending));
1103
1082 p->w->pending = 0; 1104 p->w->pending = 0;
1083 EV_CB_INVOKE (p->w, p->events); 1105 EV_CB_INVOKE (p->w, p->events);
1084 } 1106 }
1085 } 1107 }
1086} 1108}
1087 1109
1088inline void 1110void inline_size
1089timers_reify (EV_P) 1111timers_reify (EV_P)
1090{ 1112{
1091 while (timercnt && ((WT)timers [0])->at <= mn_now) 1113 while (timercnt && ((WT)timers [0])->at <= mn_now)
1092 { 1114 {
1093 struct ev_timer *w = timers [0]; 1115 ev_timer *w = timers [0];
1094 1116
1095 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1117 assert (("inactive timer on timer heap detected", ev_is_active (w)));
1096 1118
1097 /* first reschedule or stop timer */ 1119 /* first reschedule or stop timer */
1098 if (w->repeat) 1120 if (w->repeat)
1110 1132
1111 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1133 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1112 } 1134 }
1113} 1135}
1114 1136
1115#if EV_PERIODICS 1137#if EV_PERIODIC_ENABLE
1116inline void 1138void inline_size
1117periodics_reify (EV_P) 1139periodics_reify (EV_P)
1118{ 1140{
1119 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1141 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1120 { 1142 {
1121 struct ev_periodic *w = periodics [0]; 1143 ev_periodic *w = periodics [0];
1122 1144
1123 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1145 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1124 1146
1125 /* first reschedule or stop timer */ 1147 /* first reschedule or stop timer */
1126 if (w->reschedule_cb) 1148 if (w->reschedule_cb)
1140 1162
1141 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1163 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1142 } 1164 }
1143} 1165}
1144 1166
1145static void 1167static void noinline
1146periodics_reschedule (EV_P) 1168periodics_reschedule (EV_P)
1147{ 1169{
1148 int i; 1170 int i;
1149 1171
1150 /* adjust periodics after time jump */ 1172 /* adjust periodics after time jump */
1151 for (i = 0; i < periodiccnt; ++i) 1173 for (i = 0; i < periodiccnt; ++i)
1152 { 1174 {
1153 struct ev_periodic *w = periodics [i]; 1175 ev_periodic *w = periodics [i];
1154 1176
1155 if (w->reschedule_cb) 1177 if (w->reschedule_cb)
1156 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1178 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1157 else if (w->interval) 1179 else if (w->interval)
1158 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1180 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1162 for (i = periodiccnt >> 1; i--; ) 1184 for (i = periodiccnt >> 1; i--; )
1163 downheap ((WT *)periodics, periodiccnt, i); 1185 downheap ((WT *)periodics, periodiccnt, i);
1164} 1186}
1165#endif 1187#endif
1166 1188
1167inline int 1189int inline_size
1168time_update_monotonic (EV_P) 1190time_update_monotonic (EV_P)
1169{ 1191{
1170 mn_now = get_clock (); 1192 mn_now = get_clock ();
1171 1193
1172 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1194 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1180 ev_rt_now = ev_time (); 1202 ev_rt_now = ev_time ();
1181 return 1; 1203 return 1;
1182 } 1204 }
1183} 1205}
1184 1206
1185inline void 1207void inline_size
1186time_update (EV_P) 1208time_update (EV_P)
1187{ 1209{
1188 int i; 1210 int i;
1189 1211
1190#if EV_USE_MONOTONIC 1212#if EV_USE_MONOTONIC
1192 { 1214 {
1193 if (time_update_monotonic (EV_A)) 1215 if (time_update_monotonic (EV_A))
1194 { 1216 {
1195 ev_tstamp odiff = rtmn_diff; 1217 ev_tstamp odiff = rtmn_diff;
1196 1218
1197 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1219 /* loop a few times, before making important decisions.
1220 * on the choice of "4": one iteration isn't enough,
1221 * in case we get preempted during the calls to
1222 * ev_time and get_clock. a second call is almost guarenteed
1223 * to succeed in that case, though. and looping a few more times
1224 * doesn't hurt either as we only do this on time-jumps or
1225 * in the unlikely event of getting preempted here.
1226 */
1227 for (i = 4; --i; )
1198 { 1228 {
1199 rtmn_diff = ev_rt_now - mn_now; 1229 rtmn_diff = ev_rt_now - mn_now;
1200 1230
1201 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1231 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1202 return; /* all is well */ 1232 return; /* all is well */
1204 ev_rt_now = ev_time (); 1234 ev_rt_now = ev_time ();
1205 mn_now = get_clock (); 1235 mn_now = get_clock ();
1206 now_floor = mn_now; 1236 now_floor = mn_now;
1207 } 1237 }
1208 1238
1209# if EV_PERIODICS 1239# if EV_PERIODIC_ENABLE
1210 periodics_reschedule (EV_A); 1240 periodics_reschedule (EV_A);
1211# endif 1241# endif
1212 /* no timer adjustment, as the monotonic clock doesn't jump */ 1242 /* no timer adjustment, as the monotonic clock doesn't jump */
1213 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1243 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1214 } 1244 }
1218 { 1248 {
1219 ev_rt_now = ev_time (); 1249 ev_rt_now = ev_time ();
1220 1250
1221 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1251 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1222 { 1252 {
1223#if EV_PERIODICS 1253#if EV_PERIODIC_ENABLE
1224 periodics_reschedule (EV_A); 1254 periodics_reschedule (EV_A);
1225#endif 1255#endif
1226 1256
1227 /* adjust timers. this is easy, as the offset is the same for all */ 1257 /* adjust timers. this is easy, as the offset is the same for all */
1228 for (i = 0; i < timercnt; ++i) 1258 for (i = 0; i < timercnt; ++i)
1248static int loop_done; 1278static int loop_done;
1249 1279
1250void 1280void
1251ev_loop (EV_P_ int flags) 1281ev_loop (EV_P_ int flags)
1252{ 1282{
1253 double block;
1254 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1283 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1284 ? EVUNLOOP_ONE
1285 : EVUNLOOP_CANCEL;
1255 1286
1256 while (activecnt) 1287 while (activecnt)
1257 { 1288 {
1289 /* we might have forked, so reify kernel state if necessary */
1290 #if EV_FORK_ENABLE
1291 if (expect_false (postfork))
1292 if (forkcnt)
1293 {
1294 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1295 call_pending (EV_A);
1296 }
1297 #endif
1298
1258 /* queue check watchers (and execute them) */ 1299 /* queue check watchers (and execute them) */
1259 if (expect_false (preparecnt)) 1300 if (expect_false (preparecnt))
1260 { 1301 {
1261 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1302 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1262 call_pending (EV_A); 1303 call_pending (EV_A);
1268 1309
1269 /* update fd-related kernel structures */ 1310 /* update fd-related kernel structures */
1270 fd_reify (EV_A); 1311 fd_reify (EV_A);
1271 1312
1272 /* calculate blocking time */ 1313 /* calculate blocking time */
1314 {
1315 double block;
1273 1316
1274 /* we only need this for !monotonic clock or timers, but as we basically 1317 if (flags & EVLOOP_NONBLOCK || idlecnt)
1275 always have timers, we just calculate it always */ 1318 block = 0.; /* do not block at all */
1319 else
1320 {
1321 /* update time to cancel out callback processing overhead */
1276#if EV_USE_MONOTONIC 1322#if EV_USE_MONOTONIC
1277 if (expect_true (have_monotonic)) 1323 if (expect_true (have_monotonic))
1278 time_update_monotonic (EV_A); 1324 time_update_monotonic (EV_A);
1279 else 1325 else
1280#endif 1326#endif
1281 { 1327 {
1282 ev_rt_now = ev_time (); 1328 ev_rt_now = ev_time ();
1283 mn_now = ev_rt_now; 1329 mn_now = ev_rt_now;
1284 } 1330 }
1285 1331
1286 if (flags & EVLOOP_NONBLOCK || idlecnt)
1287 block = 0.;
1288 else
1289 {
1290 block = MAX_BLOCKTIME; 1332 block = MAX_BLOCKTIME;
1291 1333
1292 if (timercnt) 1334 if (timercnt)
1293 { 1335 {
1294 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1336 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1295 if (block > to) block = to; 1337 if (block > to) block = to;
1296 } 1338 }
1297 1339
1298#if EV_PERIODICS 1340#if EV_PERIODIC_ENABLE
1299 if (periodiccnt) 1341 if (periodiccnt)
1300 { 1342 {
1301 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; 1343 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
1302 if (block > to) block = to; 1344 if (block > to) block = to;
1303 } 1345 }
1304#endif 1346#endif
1305 1347
1306 if (expect_false (block < 0.)) block = 0.; 1348 if (expect_false (block < 0.)) block = 0.;
1307 } 1349 }
1308 1350
1309 backend_poll (EV_A_ block); 1351 backend_poll (EV_A_ block);
1352 }
1310 1353
1311 /* update ev_rt_now, do magic */ 1354 /* update ev_rt_now, do magic */
1312 time_update (EV_A); 1355 time_update (EV_A);
1313 1356
1314 /* queue pending timers and reschedule them */ 1357 /* queue pending timers and reschedule them */
1315 timers_reify (EV_A); /* relative timers called last */ 1358 timers_reify (EV_A); /* relative timers called last */
1316#if EV_PERIODICS 1359#if EV_PERIODIC_ENABLE
1317 periodics_reify (EV_A); /* absolute timers called first */ 1360 periodics_reify (EV_A); /* absolute timers called first */
1318#endif 1361#endif
1319 1362
1320 /* queue idle watchers unless io or timers are pending */ 1363 /* queue idle watchers unless other events are pending */
1321 if (idlecnt && !any_pending (EV_A)) 1364 if (idlecnt && !any_pending (EV_A))
1322 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1365 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1323 1366
1324 /* queue check watchers, to be executed first */ 1367 /* queue check watchers, to be executed first */
1325 if (expect_false (checkcnt)) 1368 if (expect_false (checkcnt))
1329 1372
1330 if (expect_false (loop_done)) 1373 if (expect_false (loop_done))
1331 break; 1374 break;
1332 } 1375 }
1333 1376
1334 if (loop_done != 2) 1377 if (loop_done == EVUNLOOP_ONE)
1335 loop_done = 0; 1378 loop_done = EVUNLOOP_CANCEL;
1336} 1379}
1337 1380
1338void 1381void
1339ev_unloop (EV_P_ int how) 1382ev_unloop (EV_P_ int how)
1340{ 1383{
1341 loop_done = how; 1384 loop_done = how;
1342} 1385}
1343 1386
1344/*****************************************************************************/ 1387/*****************************************************************************/
1345 1388
1346inline void 1389void inline_size
1347wlist_add (WL *head, WL elem) 1390wlist_add (WL *head, WL elem)
1348{ 1391{
1349 elem->next = *head; 1392 elem->next = *head;
1350 *head = elem; 1393 *head = elem;
1351} 1394}
1352 1395
1353inline void 1396void inline_size
1354wlist_del (WL *head, WL elem) 1397wlist_del (WL *head, WL elem)
1355{ 1398{
1356 while (*head) 1399 while (*head)
1357 { 1400 {
1358 if (*head == elem) 1401 if (*head == elem)
1363 1406
1364 head = &(*head)->next; 1407 head = &(*head)->next;
1365 } 1408 }
1366} 1409}
1367 1410
1368inline void 1411void inline_speed
1369ev_clear_pending (EV_P_ W w) 1412ev_clear_pending (EV_P_ W w)
1370{ 1413{
1371 if (w->pending) 1414 if (w->pending)
1372 { 1415 {
1373 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1416 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1374 w->pending = 0; 1417 w->pending = 0;
1375 } 1418 }
1376} 1419}
1377 1420
1378inline void 1421void inline_speed
1379ev_start (EV_P_ W w, int active) 1422ev_start (EV_P_ W w, int active)
1380{ 1423{
1381 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1424 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
1382 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; 1425 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1383 1426
1384 w->active = active; 1427 w->active = active;
1385 ev_ref (EV_A); 1428 ev_ref (EV_A);
1386} 1429}
1387 1430
1388inline void 1431void inline_size
1389ev_stop (EV_P_ W w) 1432ev_stop (EV_P_ W w)
1390{ 1433{
1391 ev_unref (EV_A); 1434 ev_unref (EV_A);
1392 w->active = 0; 1435 w->active = 0;
1393} 1436}
1394 1437
1395/*****************************************************************************/ 1438/*****************************************************************************/
1396 1439
1397void 1440void
1398ev_io_start (EV_P_ struct ev_io *w) 1441ev_io_start (EV_P_ ev_io *w)
1399{ 1442{
1400 int fd = w->fd; 1443 int fd = w->fd;
1401 1444
1402 if (expect_false (ev_is_active (w))) 1445 if (expect_false (ev_is_active (w)))
1403 return; 1446 return;
1410 1453
1411 fd_change (EV_A_ fd); 1454 fd_change (EV_A_ fd);
1412} 1455}
1413 1456
1414void 1457void
1415ev_io_stop (EV_P_ struct ev_io *w) 1458ev_io_stop (EV_P_ ev_io *w)
1416{ 1459{
1417 ev_clear_pending (EV_A_ (W)w); 1460 ev_clear_pending (EV_A_ (W)w);
1418 if (expect_false (!ev_is_active (w))) 1461 if (expect_false (!ev_is_active (w)))
1419 return; 1462 return;
1420 1463
1425 1468
1426 fd_change (EV_A_ w->fd); 1469 fd_change (EV_A_ w->fd);
1427} 1470}
1428 1471
1429void 1472void
1430ev_timer_start (EV_P_ struct ev_timer *w) 1473ev_timer_start (EV_P_ ev_timer *w)
1431{ 1474{
1432 if (expect_false (ev_is_active (w))) 1475 if (expect_false (ev_is_active (w)))
1433 return; 1476 return;
1434 1477
1435 ((WT)w)->at += mn_now; 1478 ((WT)w)->at += mn_now;
1436 1479
1437 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1480 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1438 1481
1439 ev_start (EV_A_ (W)w, ++timercnt); 1482 ev_start (EV_A_ (W)w, ++timercnt);
1440 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); 1483 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1441 timers [timercnt - 1] = w; 1484 timers [timercnt - 1] = w;
1442 upheap ((WT *)timers, timercnt - 1); 1485 upheap ((WT *)timers, timercnt - 1);
1443 1486
1444 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1487 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1445} 1488}
1446 1489
1447void 1490void
1448ev_timer_stop (EV_P_ struct ev_timer *w) 1491ev_timer_stop (EV_P_ ev_timer *w)
1449{ 1492{
1450 ev_clear_pending (EV_A_ (W)w); 1493 ev_clear_pending (EV_A_ (W)w);
1451 if (expect_false (!ev_is_active (w))) 1494 if (expect_false (!ev_is_active (w)))
1452 return; 1495 return;
1453 1496
1463 1506
1464 ev_stop (EV_A_ (W)w); 1507 ev_stop (EV_A_ (W)w);
1465} 1508}
1466 1509
1467void 1510void
1468ev_timer_again (EV_P_ struct ev_timer *w) 1511ev_timer_again (EV_P_ ev_timer *w)
1469{ 1512{
1470 if (ev_is_active (w)) 1513 if (ev_is_active (w))
1471 { 1514 {
1472 if (w->repeat) 1515 if (w->repeat)
1473 { 1516 {
1482 w->at = w->repeat; 1525 w->at = w->repeat;
1483 ev_timer_start (EV_A_ w); 1526 ev_timer_start (EV_A_ w);
1484 } 1527 }
1485} 1528}
1486 1529
1487#if EV_PERIODICS 1530#if EV_PERIODIC_ENABLE
1488void 1531void
1489ev_periodic_start (EV_P_ struct ev_periodic *w) 1532ev_periodic_start (EV_P_ ev_periodic *w)
1490{ 1533{
1491 if (expect_false (ev_is_active (w))) 1534 if (expect_false (ev_is_active (w)))
1492 return; 1535 return;
1493 1536
1494 if (w->reschedule_cb) 1537 if (w->reschedule_cb)
1499 /* this formula differs from the one in periodic_reify because we do not always round up */ 1542 /* this formula differs from the one in periodic_reify because we do not always round up */
1500 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1543 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1501 } 1544 }
1502 1545
1503 ev_start (EV_A_ (W)w, ++periodiccnt); 1546 ev_start (EV_A_ (W)w, ++periodiccnt);
1504 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1547 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1505 periodics [periodiccnt - 1] = w; 1548 periodics [periodiccnt - 1] = w;
1506 upheap ((WT *)periodics, periodiccnt - 1); 1549 upheap ((WT *)periodics, periodiccnt - 1);
1507 1550
1508 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1551 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1509} 1552}
1510 1553
1511void 1554void
1512ev_periodic_stop (EV_P_ struct ev_periodic *w) 1555ev_periodic_stop (EV_P_ ev_periodic *w)
1513{ 1556{
1514 ev_clear_pending (EV_A_ (W)w); 1557 ev_clear_pending (EV_A_ (W)w);
1515 if (expect_false (!ev_is_active (w))) 1558 if (expect_false (!ev_is_active (w)))
1516 return; 1559 return;
1517 1560
1525 1568
1526 ev_stop (EV_A_ (W)w); 1569 ev_stop (EV_A_ (W)w);
1527} 1570}
1528 1571
1529void 1572void
1530ev_periodic_again (EV_P_ struct ev_periodic *w) 1573ev_periodic_again (EV_P_ ev_periodic *w)
1531{ 1574{
1532 /* TODO: use adjustheap and recalculation */ 1575 /* TODO: use adjustheap and recalculation */
1533 ev_periodic_stop (EV_A_ w); 1576 ev_periodic_stop (EV_A_ w);
1534 ev_periodic_start (EV_A_ w); 1577 ev_periodic_start (EV_A_ w);
1535} 1578}
1536#endif 1579#endif
1537 1580
1538void
1539ev_idle_start (EV_P_ struct ev_idle *w)
1540{
1541 if (expect_false (ev_is_active (w)))
1542 return;
1543
1544 ev_start (EV_A_ (W)w, ++idlecnt);
1545 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1546 idles [idlecnt - 1] = w;
1547}
1548
1549void
1550ev_idle_stop (EV_P_ struct ev_idle *w)
1551{
1552 ev_clear_pending (EV_A_ (W)w);
1553 if (expect_false (!ev_is_active (w)))
1554 return;
1555
1556 idles [((W)w)->active - 1] = idles [--idlecnt];
1557 ev_stop (EV_A_ (W)w);
1558}
1559
1560void
1561ev_prepare_start (EV_P_ struct ev_prepare *w)
1562{
1563 if (expect_false (ev_is_active (w)))
1564 return;
1565
1566 ev_start (EV_A_ (W)w, ++preparecnt);
1567 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1568 prepares [preparecnt - 1] = w;
1569}
1570
1571void
1572ev_prepare_stop (EV_P_ struct ev_prepare *w)
1573{
1574 ev_clear_pending (EV_A_ (W)w);
1575 if (expect_false (!ev_is_active (w)))
1576 return;
1577
1578 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1579 ev_stop (EV_A_ (W)w);
1580}
1581
1582void
1583ev_check_start (EV_P_ struct ev_check *w)
1584{
1585 if (expect_false (ev_is_active (w)))
1586 return;
1587
1588 ev_start (EV_A_ (W)w, ++checkcnt);
1589 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1590 checks [checkcnt - 1] = w;
1591}
1592
1593void
1594ev_check_stop (EV_P_ struct ev_check *w)
1595{
1596 ev_clear_pending (EV_A_ (W)w);
1597 if (expect_false (!ev_is_active (w)))
1598 return;
1599
1600 checks [((W)w)->active - 1] = checks [--checkcnt];
1601 ev_stop (EV_A_ (W)w);
1602}
1603
1604#ifndef SA_RESTART 1581#ifndef SA_RESTART
1605# define SA_RESTART 0 1582# define SA_RESTART 0
1606#endif 1583#endif
1607 1584
1608void 1585void
1609ev_signal_start (EV_P_ struct ev_signal *w) 1586ev_signal_start (EV_P_ ev_signal *w)
1610{ 1587{
1611#if EV_MULTIPLICITY 1588#if EV_MULTIPLICITY
1612 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1589 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1613#endif 1590#endif
1614 if (expect_false (ev_is_active (w))) 1591 if (expect_false (ev_is_active (w)))
1633#endif 1610#endif
1634 } 1611 }
1635} 1612}
1636 1613
1637void 1614void
1638ev_signal_stop (EV_P_ struct ev_signal *w) 1615ev_signal_stop (EV_P_ ev_signal *w)
1639{ 1616{
1640 ev_clear_pending (EV_A_ (W)w); 1617 ev_clear_pending (EV_A_ (W)w);
1641 if (expect_false (!ev_is_active (w))) 1618 if (expect_false (!ev_is_active (w)))
1642 return; 1619 return;
1643 1620
1647 if (!signals [w->signum - 1].head) 1624 if (!signals [w->signum - 1].head)
1648 signal (w->signum, SIG_DFL); 1625 signal (w->signum, SIG_DFL);
1649} 1626}
1650 1627
1651void 1628void
1652ev_child_start (EV_P_ struct ev_child *w) 1629ev_child_start (EV_P_ ev_child *w)
1653{ 1630{
1654#if EV_MULTIPLICITY 1631#if EV_MULTIPLICITY
1655 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1632 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1656#endif 1633#endif
1657 if (expect_false (ev_is_active (w))) 1634 if (expect_false (ev_is_active (w)))
1658 return; 1635 return;
1659 1636
1660 ev_start (EV_A_ (W)w, 1); 1637 ev_start (EV_A_ (W)w, 1);
1661 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1638 wlist_add ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1662} 1639}
1663 1640
1664void 1641void
1665ev_child_stop (EV_P_ struct ev_child *w) 1642ev_child_stop (EV_P_ ev_child *w)
1666{ 1643{
1667 ev_clear_pending (EV_A_ (W)w); 1644 ev_clear_pending (EV_A_ (W)w);
1668 if (expect_false (!ev_is_active (w))) 1645 if (expect_false (!ev_is_active (w)))
1669 return; 1646 return;
1670 1647
1671 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1648 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1672 ev_stop (EV_A_ (W)w); 1649 ev_stop (EV_A_ (W)w);
1673} 1650}
1674 1651
1675#if EV_MULTIPLICITY 1652#if EV_STAT_ENABLE
1653
1654# ifdef _WIN32
1655# undef lstat
1656# define lstat(a,b) _stati64 (a,b)
1657# endif
1658
1659#define DEF_STAT_INTERVAL 5.0074891
1660#define MIN_STAT_INTERVAL 0.1074891
1661
1662void
1663ev_stat_stat (EV_P_ ev_stat *w)
1664{
1665 if (lstat (w->path, &w->attr) < 0)
1666 w->attr.st_nlink = 0;
1667 else if (!w->attr.st_nlink)
1668 w->attr.st_nlink = 1;
1669}
1670
1676static void 1671static void
1677embed_cb (EV_P_ struct ev_io *io, int revents) 1672stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1678{ 1673{
1679 struct ev_embed *w = (struct ev_embed *)(((char *)io) - offsetof (struct ev_embed, io)); 1674 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1680 1675
1676 /* we copy this here each the time so that */
1677 /* prev has the old value when the callback gets invoked */
1678 w->prev = w->attr;
1679 ev_stat_stat (EV_A_ w);
1680
1681 if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata)))
1681 ev_feed_event (EV_A_ (W)w, EV_EMBED); 1682 ev_feed_event (EV_A_ w, EV_STAT);
1683}
1684
1685void
1686ev_stat_start (EV_P_ ev_stat *w)
1687{
1688 if (expect_false (ev_is_active (w)))
1689 return;
1690
1691 /* since we use memcmp, we need to clear any padding data etc. */
1692 memset (&w->prev, 0, sizeof (ev_statdata));
1693 memset (&w->attr, 0, sizeof (ev_statdata));
1694
1695 ev_stat_stat (EV_A_ w);
1696
1697 if (w->interval < MIN_STAT_INTERVAL)
1698 w->interval = w->interval ? MIN_STAT_INTERVAL : DEF_STAT_INTERVAL;
1699
1700 ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval);
1701 ev_set_priority (&w->timer, ev_priority (w));
1702 ev_timer_start (EV_A_ &w->timer);
1703
1704 ev_start (EV_A_ (W)w, 1);
1705}
1706
1707void
1708ev_stat_stop (EV_P_ ev_stat *w)
1709{
1710 ev_clear_pending (EV_A_ (W)w);
1711 if (expect_false (!ev_is_active (w)))
1712 return;
1713
1714 ev_timer_stop (EV_A_ &w->timer);
1715
1716 ev_stop (EV_A_ (W)w);
1717}
1718#endif
1719
1720void
1721ev_idle_start (EV_P_ ev_idle *w)
1722{
1723 if (expect_false (ev_is_active (w)))
1724 return;
1725
1726 ev_start (EV_A_ (W)w, ++idlecnt);
1727 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1728 idles [idlecnt - 1] = w;
1729}
1730
1731void
1732ev_idle_stop (EV_P_ ev_idle *w)
1733{
1734 ev_clear_pending (EV_A_ (W)w);
1735 if (expect_false (!ev_is_active (w)))
1736 return;
1737
1738 {
1739 int active = ((W)w)->active;
1740 idles [active - 1] = idles [--idlecnt];
1741 ((W)idles [active - 1])->active = active;
1742 }
1743
1744 ev_stop (EV_A_ (W)w);
1745}
1746
1747void
1748ev_prepare_start (EV_P_ ev_prepare *w)
1749{
1750 if (expect_false (ev_is_active (w)))
1751 return;
1752
1753 ev_start (EV_A_ (W)w, ++preparecnt);
1754 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1755 prepares [preparecnt - 1] = w;
1756}
1757
1758void
1759ev_prepare_stop (EV_P_ ev_prepare *w)
1760{
1761 ev_clear_pending (EV_A_ (W)w);
1762 if (expect_false (!ev_is_active (w)))
1763 return;
1764
1765 {
1766 int active = ((W)w)->active;
1767 prepares [active - 1] = prepares [--preparecnt];
1768 ((W)prepares [active - 1])->active = active;
1769 }
1770
1771 ev_stop (EV_A_ (W)w);
1772}
1773
1774void
1775ev_check_start (EV_P_ ev_check *w)
1776{
1777 if (expect_false (ev_is_active (w)))
1778 return;
1779
1780 ev_start (EV_A_ (W)w, ++checkcnt);
1781 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1782 checks [checkcnt - 1] = w;
1783}
1784
1785void
1786ev_check_stop (EV_P_ ev_check *w)
1787{
1788 ev_clear_pending (EV_A_ (W)w);
1789 if (expect_false (!ev_is_active (w)))
1790 return;
1791
1792 {
1793 int active = ((W)w)->active;
1794 checks [active - 1] = checks [--checkcnt];
1795 ((W)checks [active - 1])->active = active;
1796 }
1797
1798 ev_stop (EV_A_ (W)w);
1799}
1800
1801#if EV_EMBED_ENABLE
1802void noinline
1803ev_embed_sweep (EV_P_ ev_embed *w)
1804{
1682 ev_loop (w->loop, EVLOOP_NONBLOCK); 1805 ev_loop (w->loop, EVLOOP_NONBLOCK);
1683} 1806}
1684 1807
1808static void
1809embed_cb (EV_P_ ev_io *io, int revents)
1810{
1811 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
1812
1813 if (ev_cb (w))
1814 ev_feed_event (EV_A_ (W)w, EV_EMBED);
1815 else
1816 ev_embed_sweep (loop, w);
1817}
1818
1685void 1819void
1686ev_embed_start (EV_P_ struct ev_embed *w) 1820ev_embed_start (EV_P_ ev_embed *w)
1687{ 1821{
1688 if (expect_false (ev_is_active (w))) 1822 if (expect_false (ev_is_active (w)))
1689 return; 1823 return;
1690 1824
1691 { 1825 {
1692 struct ev_loop *loop = w->loop; 1826 struct ev_loop *loop = w->loop;
1693 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); 1827 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
1694 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); 1828 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ);
1695 } 1829 }
1696 1830
1831 ev_set_priority (&w->io, ev_priority (w));
1697 ev_io_start (EV_A_ &w->io); 1832 ev_io_start (EV_A_ &w->io);
1833
1698 ev_start (EV_A_ (W)w, 1); 1834 ev_start (EV_A_ (W)w, 1);
1699} 1835}
1700 1836
1701void 1837void
1702ev_embed_stop (EV_P_ struct ev_embed *w) 1838ev_embed_stop (EV_P_ ev_embed *w)
1703{ 1839{
1704 ev_clear_pending (EV_A_ (W)w); 1840 ev_clear_pending (EV_A_ (W)w);
1705 if (expect_false (!ev_is_active (w))) 1841 if (expect_false (!ev_is_active (w)))
1706 return; 1842 return;
1707 1843
1708 ev_io_stop (EV_A_ &w->io); 1844 ev_io_stop (EV_A_ &w->io);
1845
1709 ev_stop (EV_A_ (W)w); 1846 ev_stop (EV_A_ (W)w);
1710} 1847}
1711#endif 1848#endif
1712 1849
1850#if EV_FORK_ENABLE
1851void
1852ev_fork_start (EV_P_ ev_fork *w)
1853{
1854 if (expect_false (ev_is_active (w)))
1855 return;
1856
1857 ev_start (EV_A_ (W)w, ++forkcnt);
1858 array_needsize (ev_fork *, forks, forkmax, forkcnt, EMPTY2);
1859 forks [forkcnt - 1] = w;
1860}
1861
1862void
1863ev_fork_stop (EV_P_ ev_fork *w)
1864{
1865 ev_clear_pending (EV_A_ (W)w);
1866 if (expect_false (!ev_is_active (w)))
1867 return;
1868
1869 {
1870 int active = ((W)w)->active;
1871 forks [active - 1] = forks [--forkcnt];
1872 ((W)forks [active - 1])->active = active;
1873 }
1874
1875 ev_stop (EV_A_ (W)w);
1876}
1877#endif
1878
1713/*****************************************************************************/ 1879/*****************************************************************************/
1714 1880
1715struct ev_once 1881struct ev_once
1716{ 1882{
1717 struct ev_io io; 1883 ev_io io;
1718 struct ev_timer to; 1884 ev_timer to;
1719 void (*cb)(int revents, void *arg); 1885 void (*cb)(int revents, void *arg);
1720 void *arg; 1886 void *arg;
1721}; 1887};
1722 1888
1723static void 1889static void
1732 1898
1733 cb (revents, arg); 1899 cb (revents, arg);
1734} 1900}
1735 1901
1736static void 1902static void
1737once_cb_io (EV_P_ struct ev_io *w, int revents) 1903once_cb_io (EV_P_ ev_io *w, int revents)
1738{ 1904{
1739 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); 1905 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1740} 1906}
1741 1907
1742static void 1908static void
1743once_cb_to (EV_P_ struct ev_timer *w, int revents) 1909once_cb_to (EV_P_ ev_timer *w, int revents)
1744{ 1910{
1745 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); 1911 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1746} 1912}
1747 1913
1748void 1914void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines