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

Comparing libev/ev.c (file contents):
Revision 1.133 by root, Fri Nov 23 11:32:22 2007 UTC vs.
Revision 1.140 by root, Mon Nov 26 19:49:36 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
187# include "ev.h" 187# include "ev.h"
188#endif 188#endif
189 189
190#if __GNUC__ >= 3 190#if __GNUC__ >= 3
191# define expect(expr,value) __builtin_expect ((expr),(value)) 191# define expect(expr,value) __builtin_expect ((expr),(value))
192# define inline_size static inline /* inline for codesize */
193# if EV_MINIMAL
194# define noinline __attribute__ ((noinline))
195# define inline_speed static noinline
196# else
197# define noinline
192# define inline static inline 198# define inline_speed static inline
199# endif
193#else 200#else
194# define expect(expr,value) (expr) 201# define expect(expr,value) (expr)
195# define inline static 202# define inline_speed static
203# define inline_minimal static
204# define noinline
196#endif 205#endif
197 206
198#define expect_false(expr) expect ((expr) != 0, 0) 207#define expect_false(expr) expect ((expr) != 0, 0)
199#define expect_true(expr) expect ((expr) != 0, 1) 208#define expect_true(expr) expect ((expr) != 0, 1)
200 209
202#define ABSPRI(w) ((w)->priority - EV_MINPRI) 211#define ABSPRI(w) ((w)->priority - EV_MINPRI)
203 212
204#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 213#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
205#define EMPTY2(a,b) /* used to suppress some warnings */ 214#define EMPTY2(a,b) /* used to suppress some warnings */
206 215
207typedef struct ev_watcher *W; 216typedef ev_watcher *W;
208typedef struct ev_watcher_list *WL; 217typedef ev_watcher_list *WL;
209typedef struct ev_watcher_time *WT; 218typedef ev_watcher_time *WT;
210 219
211static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 220static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
212 221
213#ifdef _WIN32 222#ifdef _WIN32
214# include "ev_win32.c" 223# include "ev_win32.c"
306 315
307#endif 316#endif
308 317
309/*****************************************************************************/ 318/*****************************************************************************/
310 319
311ev_tstamp 320ev_tstamp noinline
312ev_time (void) 321ev_time (void)
313{ 322{
314#if EV_USE_REALTIME 323#if EV_USE_REALTIME
315 struct timespec ts; 324 struct timespec ts;
316 clock_gettime (CLOCK_REALTIME, &ts); 325 clock_gettime (CLOCK_REALTIME, &ts);
320 gettimeofday (&tv, 0); 329 gettimeofday (&tv, 0);
321 return tv.tv_sec + tv.tv_usec * 1e-6; 330 return tv.tv_sec + tv.tv_usec * 1e-6;
322#endif 331#endif
323} 332}
324 333
325inline ev_tstamp 334ev_tstamp inline_size
326get_clock (void) 335get_clock (void)
327{ 336{
328#if EV_USE_MONOTONIC 337#if EV_USE_MONOTONIC
329 if (expect_true (have_monotonic)) 338 if (expect_true (have_monotonic))
330 { 339 {
373#define array_free(stem, idx) \ 382#define array_free(stem, idx) \
374 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 383 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
375 384
376/*****************************************************************************/ 385/*****************************************************************************/
377 386
378static void 387void inline_size
379anfds_init (ANFD *base, int count) 388anfds_init (ANFD *base, int count)
380{ 389{
381 while (count--) 390 while (count--)
382 { 391 {
383 base->head = 0; 392 base->head = 0;
386 395
387 ++base; 396 ++base;
388 } 397 }
389} 398}
390 399
391void 400void noinline
392ev_feed_event (EV_P_ void *w, int revents) 401ev_feed_event (EV_P_ void *w, int revents)
393{ 402{
394 W w_ = (W)w; 403 W w_ = (W)w;
395 404
396 if (expect_false (w_->pending)) 405 if (expect_false (w_->pending))
412 421
413 for (i = 0; i < eventcnt; ++i) 422 for (i = 0; i < eventcnt; ++i)
414 ev_feed_event (EV_A_ events [i], type); 423 ev_feed_event (EV_A_ events [i], type);
415} 424}
416 425
417inline void 426void inline_speed
418fd_event (EV_P_ int fd, int revents) 427fd_event (EV_P_ int fd, int revents)
419{ 428{
420 ANFD *anfd = anfds + fd; 429 ANFD *anfd = anfds + fd;
421 struct ev_io *w; 430 ev_io *w;
422 431
423 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 432 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
424 { 433 {
425 int ev = w->events & revents; 434 int ev = w->events & revents;
426 435
427 if (ev) 436 if (ev)
428 ev_feed_event (EV_A_ (W)w, ev); 437 ev_feed_event (EV_A_ (W)w, ev);
435 fd_event (EV_A_ fd, revents); 444 fd_event (EV_A_ fd, revents);
436} 445}
437 446
438/*****************************************************************************/ 447/*****************************************************************************/
439 448
440inline void 449void inline_size
441fd_reify (EV_P) 450fd_reify (EV_P)
442{ 451{
443 int i; 452 int i;
444 453
445 for (i = 0; i < fdchangecnt; ++i) 454 for (i = 0; i < fdchangecnt; ++i)
446 { 455 {
447 int fd = fdchanges [i]; 456 int fd = fdchanges [i];
448 ANFD *anfd = anfds + fd; 457 ANFD *anfd = anfds + fd;
449 struct ev_io *w; 458 ev_io *w;
450 459
451 int events = 0; 460 int events = 0;
452 461
453 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 462 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
454 events |= w->events; 463 events |= w->events;
455 464
456#if EV_SELECT_IS_WINSOCKET 465#if EV_SELECT_IS_WINSOCKET
457 if (events) 466 if (events)
458 { 467 {
469 } 478 }
470 479
471 fdchangecnt = 0; 480 fdchangecnt = 0;
472} 481}
473 482
474static void 483void inline_size
475fd_change (EV_P_ int fd) 484fd_change (EV_P_ int fd)
476{ 485{
477 if (expect_false (anfds [fd].reify)) 486 if (expect_false (anfds [fd].reify))
478 return; 487 return;
479 488
482 ++fdchangecnt; 491 ++fdchangecnt;
483 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 492 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
484 fdchanges [fdchangecnt - 1] = fd; 493 fdchanges [fdchangecnt - 1] = fd;
485} 494}
486 495
487static void 496void inline_speed
488fd_kill (EV_P_ int fd) 497fd_kill (EV_P_ int fd)
489{ 498{
490 struct ev_io *w; 499 ev_io *w;
491 500
492 while ((w = (struct ev_io *)anfds [fd].head)) 501 while ((w = (ev_io *)anfds [fd].head))
493 { 502 {
494 ev_io_stop (EV_A_ w); 503 ev_io_stop (EV_A_ w);
495 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 504 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
496 } 505 }
497} 506}
498 507
499inline int 508int inline_size
500fd_valid (int fd) 509fd_valid (int fd)
501{ 510{
502#ifdef _WIN32 511#ifdef _WIN32
503 return _get_osfhandle (fd) != -1; 512 return _get_osfhandle (fd) != -1;
504#else 513#else
505 return fcntl (fd, F_GETFD) != -1; 514 return fcntl (fd, F_GETFD) != -1;
506#endif 515#endif
507} 516}
508 517
509/* called on EBADF to verify fds */ 518/* called on EBADF to verify fds */
510static void 519static void noinline
511fd_ebadf (EV_P) 520fd_ebadf (EV_P)
512{ 521{
513 int fd; 522 int fd;
514 523
515 for (fd = 0; fd < anfdmax; ++fd) 524 for (fd = 0; fd < anfdmax; ++fd)
517 if (!fd_valid (fd) == -1 && errno == EBADF) 526 if (!fd_valid (fd) == -1 && errno == EBADF)
518 fd_kill (EV_A_ fd); 527 fd_kill (EV_A_ fd);
519} 528}
520 529
521/* called on ENOMEM in select/poll to kill some fds and retry */ 530/* called on ENOMEM in select/poll to kill some fds and retry */
522static void 531static void noinline
523fd_enomem (EV_P) 532fd_enomem (EV_P)
524{ 533{
525 int fd; 534 int fd;
526 535
527 for (fd = anfdmax; fd--; ) 536 for (fd = anfdmax; fd--; )
531 return; 540 return;
532 } 541 }
533} 542}
534 543
535/* usually called after fork if backend needs to re-arm all fds from scratch */ 544/* usually called after fork if backend needs to re-arm all fds from scratch */
536static void 545static void noinline
537fd_rearm_all (EV_P) 546fd_rearm_all (EV_P)
538{ 547{
539 int fd; 548 int fd;
540 549
541 /* this should be highly optimised to not do anything but set a flag */ 550 /* this should be highly optimised to not do anything but set a flag */
547 } 556 }
548} 557}
549 558
550/*****************************************************************************/ 559/*****************************************************************************/
551 560
552static void 561void inline_speed
553upheap (WT *heap, int k) 562upheap (WT *heap, int k)
554{ 563{
555 WT w = heap [k]; 564 WT w = heap [k];
556 565
557 while (k && heap [k >> 1]->at > w->at) 566 while (k && heap [k >> 1]->at > w->at)
564 heap [k] = w; 573 heap [k] = w;
565 ((W)heap [k])->active = k + 1; 574 ((W)heap [k])->active = k + 1;
566 575
567} 576}
568 577
569static void 578void inline_speed
570downheap (WT *heap, int N, int k) 579downheap (WT *heap, int N, int k)
571{ 580{
572 WT w = heap [k]; 581 WT w = heap [k];
573 582
574 while (k < (N >> 1)) 583 while (k < (N >> 1))
588 597
589 heap [k] = w; 598 heap [k] = w;
590 ((W)heap [k])->active = k + 1; 599 ((W)heap [k])->active = k + 1;
591} 600}
592 601
593inline void 602void inline_size
594adjustheap (WT *heap, int N, int k) 603adjustheap (WT *heap, int N, int k)
595{ 604{
596 upheap (heap, k); 605 upheap (heap, k);
597 downheap (heap, N, k); 606 downheap (heap, N, k);
598} 607}
608static ANSIG *signals; 617static ANSIG *signals;
609static int signalmax; 618static int signalmax;
610 619
611static int sigpipe [2]; 620static int sigpipe [2];
612static sig_atomic_t volatile gotsig; 621static sig_atomic_t volatile gotsig;
613static struct ev_io sigev; 622static ev_io sigev;
614 623
615static void 624void inline_size
616signals_init (ANSIG *base, int count) 625signals_init (ANSIG *base, int count)
617{ 626{
618 while (count--) 627 while (count--)
619 { 628 {
620 base->head = 0; 629 base->head = 0;
640 write (sigpipe [1], &signum, 1); 649 write (sigpipe [1], &signum, 1);
641 errno = old_errno; 650 errno = old_errno;
642 } 651 }
643} 652}
644 653
645void 654void noinline
646ev_feed_signal_event (EV_P_ int signum) 655ev_feed_signal_event (EV_P_ int signum)
647{ 656{
648 WL w; 657 WL w;
649 658
650#if EV_MULTIPLICITY 659#if EV_MULTIPLICITY
661 for (w = signals [signum].head; w; w = w->next) 670 for (w = signals [signum].head; w; w = w->next)
662 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 671 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
663} 672}
664 673
665static void 674static void
666sigcb (EV_P_ struct ev_io *iow, int revents) 675sigcb (EV_P_ ev_io *iow, int revents)
667{ 676{
668 int signum; 677 int signum;
669 678
670 read (sigpipe [0], &revents, 1); 679 read (sigpipe [0], &revents, 1);
671 gotsig = 0; 680 gotsig = 0;
673 for (signum = signalmax; signum--; ) 682 for (signum = signalmax; signum--; )
674 if (signals [signum].gotsig) 683 if (signals [signum].gotsig)
675 ev_feed_signal_event (EV_A_ signum + 1); 684 ev_feed_signal_event (EV_A_ signum + 1);
676} 685}
677 686
678static void 687void inline_size
679fd_intern (int fd) 688fd_intern (int fd)
680{ 689{
681#ifdef _WIN32 690#ifdef _WIN32
682 int arg = 1; 691 int arg = 1;
683 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 692 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
685 fcntl (fd, F_SETFD, FD_CLOEXEC); 694 fcntl (fd, F_SETFD, FD_CLOEXEC);
686 fcntl (fd, F_SETFL, O_NONBLOCK); 695 fcntl (fd, F_SETFL, O_NONBLOCK);
687#endif 696#endif
688} 697}
689 698
690static void 699static void noinline
691siginit (EV_P) 700siginit (EV_P)
692{ 701{
693 fd_intern (sigpipe [0]); 702 fd_intern (sigpipe [0]);
694 fd_intern (sigpipe [1]); 703 fd_intern (sigpipe [1]);
695 704
698 ev_unref (EV_A); /* child watcher should not keep loop alive */ 707 ev_unref (EV_A); /* child watcher should not keep loop alive */
699} 708}
700 709
701/*****************************************************************************/ 710/*****************************************************************************/
702 711
703static struct ev_child *childs [PID_HASHSIZE]; 712static ev_child *childs [PID_HASHSIZE];
704 713
705#ifndef _WIN32 714#ifndef _WIN32
706 715
707static struct ev_signal childev; 716static ev_signal childev;
708 717
709#ifndef WCONTINUED 718#ifndef WCONTINUED
710# define WCONTINUED 0 719# define WCONTINUED 0
711#endif 720#endif
712 721
713static void 722void inline_speed
714child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) 723child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status)
715{ 724{
716 struct ev_child *w; 725 ev_child *w;
717 726
718 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 727 for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
719 if (w->pid == pid || !w->pid) 728 if (w->pid == pid || !w->pid)
720 { 729 {
721 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 730 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
722 w->rpid = pid; 731 w->rpid = pid;
723 w->rstatus = status; 732 w->rstatus = status;
724 ev_feed_event (EV_A_ (W)w, EV_CHILD); 733 ev_feed_event (EV_A_ (W)w, EV_CHILD);
725 } 734 }
726} 735}
727 736
728static void 737static void
729childcb (EV_P_ struct ev_signal *sw, int revents) 738childcb (EV_P_ ev_signal *sw, int revents)
730{ 739{
731 int pid, status; 740 int pid, status;
732 741
733 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 742 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
734 { 743 {
772{ 781{
773 return EV_VERSION_MINOR; 782 return EV_VERSION_MINOR;
774} 783}
775 784
776/* return true if we are running with elevated privileges and should ignore env variables */ 785/* return true if we are running with elevated privileges and should ignore env variables */
777static int 786int inline_size
778enable_secure (void) 787enable_secure (void)
779{ 788{
780#ifdef _WIN32 789#ifdef _WIN32
781 return 0; 790 return 0;
782#else 791#else
816 825
817 return flags; 826 return flags;
818} 827}
819 828
820unsigned int 829unsigned int
830ev_embeddable_backends (void)
831{
832 return EVBACKEND_EPOLL
833 | EVBACKEND_KQUEUE
834 | EVBACKEND_PORT;
835}
836
837unsigned int
821ev_backend (EV_P) 838ev_backend (EV_P)
822{ 839{
823 return backend; 840 return backend;
824} 841}
825 842
896 array_free (pending, [i]); 913 array_free (pending, [i]);
897 914
898 /* have to use the microsoft-never-gets-it-right macro */ 915 /* have to use the microsoft-never-gets-it-right macro */
899 array_free (fdchange, EMPTY0); 916 array_free (fdchange, EMPTY0);
900 array_free (timer, EMPTY0); 917 array_free (timer, EMPTY0);
901#if EV_PERIODICS 918#if EV_PERIODIC_ENABLE
902 array_free (periodic, EMPTY0); 919 array_free (periodic, EMPTY0);
903#endif 920#endif
904 array_free (idle, EMPTY0); 921 array_free (idle, EMPTY0);
905 array_free (prepare, EMPTY0); 922 array_free (prepare, EMPTY0);
906 array_free (check, EMPTY0); 923 array_free (check, EMPTY0);
1042 postfork = 1; 1059 postfork = 1;
1043} 1060}
1044 1061
1045/*****************************************************************************/ 1062/*****************************************************************************/
1046 1063
1047static int 1064int inline_size
1048any_pending (EV_P) 1065any_pending (EV_P)
1049{ 1066{
1050 int pri; 1067 int pri;
1051 1068
1052 for (pri = NUMPRI; pri--; ) 1069 for (pri = NUMPRI; pri--; )
1054 return 1; 1071 return 1;
1055 1072
1056 return 0; 1073 return 0;
1057} 1074}
1058 1075
1059inline void 1076void inline_speed
1060call_pending (EV_P) 1077call_pending (EV_P)
1061{ 1078{
1062 int pri; 1079 int pri;
1063 1080
1064 for (pri = NUMPRI; pri--; ) 1081 for (pri = NUMPRI; pri--; )
1066 { 1083 {
1067 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1084 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1068 1085
1069 if (expect_true (p->w)) 1086 if (expect_true (p->w))
1070 { 1087 {
1088 assert (("non-pending watcher on pending list", p->w->pending));
1089
1071 p->w->pending = 0; 1090 p->w->pending = 0;
1072 EV_CB_INVOKE (p->w, p->events); 1091 EV_CB_INVOKE (p->w, p->events);
1073 } 1092 }
1074 } 1093 }
1075} 1094}
1076 1095
1077inline void 1096void inline_size
1078timers_reify (EV_P) 1097timers_reify (EV_P)
1079{ 1098{
1080 while (timercnt && ((WT)timers [0])->at <= mn_now) 1099 while (timercnt && ((WT)timers [0])->at <= mn_now)
1081 { 1100 {
1082 struct ev_timer *w = timers [0]; 1101 ev_timer *w = timers [0];
1083 1102
1084 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1103 assert (("inactive timer on timer heap detected", ev_is_active (w)));
1085 1104
1086 /* first reschedule or stop timer */ 1105 /* first reschedule or stop timer */
1087 if (w->repeat) 1106 if (w->repeat)
1099 1118
1100 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1119 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1101 } 1120 }
1102} 1121}
1103 1122
1104#if EV_PERIODICS 1123#if EV_PERIODIC_ENABLE
1105inline void 1124void inline_size
1106periodics_reify (EV_P) 1125periodics_reify (EV_P)
1107{ 1126{
1108 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1127 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1109 { 1128 {
1110 struct ev_periodic *w = periodics [0]; 1129 ev_periodic *w = periodics [0];
1111 1130
1112 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1131 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1113 1132
1114 /* first reschedule or stop timer */ 1133 /* first reschedule or stop timer */
1115 if (w->reschedule_cb) 1134 if (w->reschedule_cb)
1129 1148
1130 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1149 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1131 } 1150 }
1132} 1151}
1133 1152
1134static void 1153static void noinline
1135periodics_reschedule (EV_P) 1154periodics_reschedule (EV_P)
1136{ 1155{
1137 int i; 1156 int i;
1138 1157
1139 /* adjust periodics after time jump */ 1158 /* adjust periodics after time jump */
1140 for (i = 0; i < periodiccnt; ++i) 1159 for (i = 0; i < periodiccnt; ++i)
1141 { 1160 {
1142 struct ev_periodic *w = periodics [i]; 1161 ev_periodic *w = periodics [i];
1143 1162
1144 if (w->reschedule_cb) 1163 if (w->reschedule_cb)
1145 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1164 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1146 else if (w->interval) 1165 else if (w->interval)
1147 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1166 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1151 for (i = periodiccnt >> 1; i--; ) 1170 for (i = periodiccnt >> 1; i--; )
1152 downheap ((WT *)periodics, periodiccnt, i); 1171 downheap ((WT *)periodics, periodiccnt, i);
1153} 1172}
1154#endif 1173#endif
1155 1174
1156inline int 1175int inline_size
1157time_update_monotonic (EV_P) 1176time_update_monotonic (EV_P)
1158{ 1177{
1159 mn_now = get_clock (); 1178 mn_now = get_clock ();
1160 1179
1161 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1180 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1169 ev_rt_now = ev_time (); 1188 ev_rt_now = ev_time ();
1170 return 1; 1189 return 1;
1171 } 1190 }
1172} 1191}
1173 1192
1174inline void 1193void inline_size
1175time_update (EV_P) 1194time_update (EV_P)
1176{ 1195{
1177 int i; 1196 int i;
1178 1197
1179#if EV_USE_MONOTONIC 1198#if EV_USE_MONOTONIC
1181 { 1200 {
1182 if (time_update_monotonic (EV_A)) 1201 if (time_update_monotonic (EV_A))
1183 { 1202 {
1184 ev_tstamp odiff = rtmn_diff; 1203 ev_tstamp odiff = rtmn_diff;
1185 1204
1186 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1205 /* loop a few times, before making important decisions.
1206 * on the choice of "4": one iteration isn't enough,
1207 * in case we get preempted during the calls to
1208 * ev_time and get_clock. a second call is almost guarenteed
1209 * to succeed in that case, though. and looping a few more times
1210 * doesn't hurt either as we only do this on time-jumps or
1211 * in the unlikely event of getting preempted here.
1212 */
1213 for (i = 4; --i; )
1187 { 1214 {
1188 rtmn_diff = ev_rt_now - mn_now; 1215 rtmn_diff = ev_rt_now - mn_now;
1189 1216
1190 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1217 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1191 return; /* all is well */ 1218 return; /* all is well */
1193 ev_rt_now = ev_time (); 1220 ev_rt_now = ev_time ();
1194 mn_now = get_clock (); 1221 mn_now = get_clock ();
1195 now_floor = mn_now; 1222 now_floor = mn_now;
1196 } 1223 }
1197 1224
1198# if EV_PERIODICS 1225# if EV_PERIODIC_ENABLE
1199 periodics_reschedule (EV_A); 1226 periodics_reschedule (EV_A);
1200# endif 1227# endif
1201 /* no timer adjustment, as the monotonic clock doesn't jump */ 1228 /* no timer adjustment, as the monotonic clock doesn't jump */
1202 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1229 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1203 } 1230 }
1207 { 1234 {
1208 ev_rt_now = ev_time (); 1235 ev_rt_now = ev_time ();
1209 1236
1210 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1237 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1211 { 1238 {
1212#if EV_PERIODICS 1239#if EV_PERIODIC_ENABLE
1213 periodics_reschedule (EV_A); 1240 periodics_reschedule (EV_A);
1214#endif 1241#endif
1215 1242
1216 /* adjust timers. this is easy, as the offset is the same for all */ 1243 /* adjust timers. this is easy, as the offset is the same for all */
1217 for (i = 0; i < timercnt; ++i) 1244 for (i = 0; i < timercnt; ++i)
1237static int loop_done; 1264static int loop_done;
1238 1265
1239void 1266void
1240ev_loop (EV_P_ int flags) 1267ev_loop (EV_P_ int flags)
1241{ 1268{
1242 double block;
1243 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1269 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1270 ? EVUNLOOP_ONE
1271 : EVUNLOOP_CANCEL;
1244 1272
1245 while (activecnt) 1273 while (activecnt)
1246 { 1274 {
1247 /* queue check watchers (and execute them) */ 1275 /* queue check watchers (and execute them) */
1248 if (expect_false (preparecnt)) 1276 if (expect_false (preparecnt))
1257 1285
1258 /* update fd-related kernel structures */ 1286 /* update fd-related kernel structures */
1259 fd_reify (EV_A); 1287 fd_reify (EV_A);
1260 1288
1261 /* calculate blocking time */ 1289 /* calculate blocking time */
1290 {
1291 double block;
1262 1292
1263 /* we only need this for !monotonic clock or timers, but as we basically 1293 if (flags & EVLOOP_NONBLOCK || idlecnt)
1264 always have timers, we just calculate it always */ 1294 block = 0.; /* do not block at all */
1295 else
1296 {
1297 /* update time to cancel out callback processing overhead */
1265#if EV_USE_MONOTONIC 1298#if EV_USE_MONOTONIC
1266 if (expect_true (have_monotonic)) 1299 if (expect_true (have_monotonic))
1267 time_update_monotonic (EV_A); 1300 time_update_monotonic (EV_A);
1268 else 1301 else
1269#endif 1302#endif
1270 { 1303 {
1271 ev_rt_now = ev_time (); 1304 ev_rt_now = ev_time ();
1272 mn_now = ev_rt_now; 1305 mn_now = ev_rt_now;
1273 } 1306 }
1274 1307
1275 if (flags & EVLOOP_NONBLOCK || idlecnt)
1276 block = 0.;
1277 else
1278 {
1279 block = MAX_BLOCKTIME; 1308 block = MAX_BLOCKTIME;
1280 1309
1281 if (timercnt) 1310 if (timercnt)
1282 { 1311 {
1283 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1312 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1284 if (block > to) block = to; 1313 if (block > to) block = to;
1285 } 1314 }
1286 1315
1287#if EV_PERIODICS 1316#if EV_PERIODIC_ENABLE
1288 if (periodiccnt) 1317 if (periodiccnt)
1289 { 1318 {
1290 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; 1319 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
1291 if (block > to) block = to; 1320 if (block > to) block = to;
1292 } 1321 }
1293#endif 1322#endif
1294 1323
1295 if (expect_false (block < 0.)) block = 0.; 1324 if (expect_false (block < 0.)) block = 0.;
1296 } 1325 }
1297 1326
1298 backend_poll (EV_A_ block); 1327 backend_poll (EV_A_ block);
1328 }
1299 1329
1300 /* update ev_rt_now, do magic */ 1330 /* update ev_rt_now, do magic */
1301 time_update (EV_A); 1331 time_update (EV_A);
1302 1332
1303 /* queue pending timers and reschedule them */ 1333 /* queue pending timers and reschedule them */
1304 timers_reify (EV_A); /* relative timers called last */ 1334 timers_reify (EV_A); /* relative timers called last */
1305#if EV_PERIODICS 1335#if EV_PERIODIC_ENABLE
1306 periodics_reify (EV_A); /* absolute timers called first */ 1336 periodics_reify (EV_A); /* absolute timers called first */
1307#endif 1337#endif
1308 1338
1309 /* queue idle watchers unless io or timers are pending */ 1339 /* queue idle watchers unless other events are pending */
1310 if (idlecnt && !any_pending (EV_A)) 1340 if (idlecnt && !any_pending (EV_A))
1311 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1341 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1312 1342
1313 /* queue check watchers, to be executed first */ 1343 /* queue check watchers, to be executed first */
1314 if (expect_false (checkcnt)) 1344 if (expect_false (checkcnt))
1318 1348
1319 if (expect_false (loop_done)) 1349 if (expect_false (loop_done))
1320 break; 1350 break;
1321 } 1351 }
1322 1352
1323 if (loop_done != 2) 1353 if (loop_done == EVUNLOOP_ONE)
1324 loop_done = 0; 1354 loop_done = EVUNLOOP_CANCEL;
1325} 1355}
1326 1356
1327void 1357void
1328ev_unloop (EV_P_ int how) 1358ev_unloop (EV_P_ int how)
1329{ 1359{
1330 loop_done = how; 1360 loop_done = how;
1331} 1361}
1332 1362
1333/*****************************************************************************/ 1363/*****************************************************************************/
1334 1364
1335inline void 1365void inline_size
1336wlist_add (WL *head, WL elem) 1366wlist_add (WL *head, WL elem)
1337{ 1367{
1338 elem->next = *head; 1368 elem->next = *head;
1339 *head = elem; 1369 *head = elem;
1340} 1370}
1341 1371
1342inline void 1372void inline_size
1343wlist_del (WL *head, WL elem) 1373wlist_del (WL *head, WL elem)
1344{ 1374{
1345 while (*head) 1375 while (*head)
1346 { 1376 {
1347 if (*head == elem) 1377 if (*head == elem)
1352 1382
1353 head = &(*head)->next; 1383 head = &(*head)->next;
1354 } 1384 }
1355} 1385}
1356 1386
1357inline void 1387void inline_speed
1358ev_clear_pending (EV_P_ W w) 1388ev_clear_pending (EV_P_ W w)
1359{ 1389{
1360 if (w->pending) 1390 if (w->pending)
1361 { 1391 {
1362 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1392 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1363 w->pending = 0; 1393 w->pending = 0;
1364 } 1394 }
1365} 1395}
1366 1396
1367inline void 1397void inline_speed
1368ev_start (EV_P_ W w, int active) 1398ev_start (EV_P_ W w, int active)
1369{ 1399{
1370 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1400 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
1371 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; 1401 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1372 1402
1373 w->active = active; 1403 w->active = active;
1374 ev_ref (EV_A); 1404 ev_ref (EV_A);
1375} 1405}
1376 1406
1377inline void 1407void inline_size
1378ev_stop (EV_P_ W w) 1408ev_stop (EV_P_ W w)
1379{ 1409{
1380 ev_unref (EV_A); 1410 ev_unref (EV_A);
1381 w->active = 0; 1411 w->active = 0;
1382} 1412}
1383 1413
1384/*****************************************************************************/ 1414/*****************************************************************************/
1385 1415
1386void 1416void
1387ev_io_start (EV_P_ struct ev_io *w) 1417ev_io_start (EV_P_ ev_io *w)
1388{ 1418{
1389 int fd = w->fd; 1419 int fd = w->fd;
1390 1420
1391 if (expect_false (ev_is_active (w))) 1421 if (expect_false (ev_is_active (w)))
1392 return; 1422 return;
1399 1429
1400 fd_change (EV_A_ fd); 1430 fd_change (EV_A_ fd);
1401} 1431}
1402 1432
1403void 1433void
1404ev_io_stop (EV_P_ struct ev_io *w) 1434ev_io_stop (EV_P_ ev_io *w)
1405{ 1435{
1406 ev_clear_pending (EV_A_ (W)w); 1436 ev_clear_pending (EV_A_ (W)w);
1407 if (expect_false (!ev_is_active (w))) 1437 if (expect_false (!ev_is_active (w)))
1408 return; 1438 return;
1409 1439
1414 1444
1415 fd_change (EV_A_ w->fd); 1445 fd_change (EV_A_ w->fd);
1416} 1446}
1417 1447
1418void 1448void
1419ev_timer_start (EV_P_ struct ev_timer *w) 1449ev_timer_start (EV_P_ ev_timer *w)
1420{ 1450{
1421 if (expect_false (ev_is_active (w))) 1451 if (expect_false (ev_is_active (w)))
1422 return; 1452 return;
1423 1453
1424 ((WT)w)->at += mn_now; 1454 ((WT)w)->at += mn_now;
1425 1455
1426 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1456 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1427 1457
1428 ev_start (EV_A_ (W)w, ++timercnt); 1458 ev_start (EV_A_ (W)w, ++timercnt);
1429 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); 1459 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1430 timers [timercnt - 1] = w; 1460 timers [timercnt - 1] = w;
1431 upheap ((WT *)timers, timercnt - 1); 1461 upheap ((WT *)timers, timercnt - 1);
1432 1462
1433 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1463 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1434} 1464}
1435 1465
1436void 1466void
1437ev_timer_stop (EV_P_ struct ev_timer *w) 1467ev_timer_stop (EV_P_ ev_timer *w)
1438{ 1468{
1439 ev_clear_pending (EV_A_ (W)w); 1469 ev_clear_pending (EV_A_ (W)w);
1440 if (expect_false (!ev_is_active (w))) 1470 if (expect_false (!ev_is_active (w)))
1441 return; 1471 return;
1442 1472
1452 1482
1453 ev_stop (EV_A_ (W)w); 1483 ev_stop (EV_A_ (W)w);
1454} 1484}
1455 1485
1456void 1486void
1457ev_timer_again (EV_P_ struct ev_timer *w) 1487ev_timer_again (EV_P_ ev_timer *w)
1458{ 1488{
1459 if (ev_is_active (w)) 1489 if (ev_is_active (w))
1460 { 1490 {
1461 if (w->repeat) 1491 if (w->repeat)
1462 { 1492 {
1471 w->at = w->repeat; 1501 w->at = w->repeat;
1472 ev_timer_start (EV_A_ w); 1502 ev_timer_start (EV_A_ w);
1473 } 1503 }
1474} 1504}
1475 1505
1476#if EV_PERIODICS 1506#if EV_PERIODIC_ENABLE
1477void 1507void
1478ev_periodic_start (EV_P_ struct ev_periodic *w) 1508ev_periodic_start (EV_P_ ev_periodic *w)
1479{ 1509{
1480 if (expect_false (ev_is_active (w))) 1510 if (expect_false (ev_is_active (w)))
1481 return; 1511 return;
1482 1512
1483 if (w->reschedule_cb) 1513 if (w->reschedule_cb)
1488 /* this formula differs from the one in periodic_reify because we do not always round up */ 1518 /* this formula differs from the one in periodic_reify because we do not always round up */
1489 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1519 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1490 } 1520 }
1491 1521
1492 ev_start (EV_A_ (W)w, ++periodiccnt); 1522 ev_start (EV_A_ (W)w, ++periodiccnt);
1493 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1523 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1494 periodics [periodiccnt - 1] = w; 1524 periodics [periodiccnt - 1] = w;
1495 upheap ((WT *)periodics, periodiccnt - 1); 1525 upheap ((WT *)periodics, periodiccnt - 1);
1496 1526
1497 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1527 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1498} 1528}
1499 1529
1500void 1530void
1501ev_periodic_stop (EV_P_ struct ev_periodic *w) 1531ev_periodic_stop (EV_P_ ev_periodic *w)
1502{ 1532{
1503 ev_clear_pending (EV_A_ (W)w); 1533 ev_clear_pending (EV_A_ (W)w);
1504 if (expect_false (!ev_is_active (w))) 1534 if (expect_false (!ev_is_active (w)))
1505 return; 1535 return;
1506 1536
1514 1544
1515 ev_stop (EV_A_ (W)w); 1545 ev_stop (EV_A_ (W)w);
1516} 1546}
1517 1547
1518void 1548void
1519ev_periodic_again (EV_P_ struct ev_periodic *w) 1549ev_periodic_again (EV_P_ ev_periodic *w)
1520{ 1550{
1521 /* TODO: use adjustheap and recalculation */ 1551 /* TODO: use adjustheap and recalculation */
1522 ev_periodic_stop (EV_A_ w); 1552 ev_periodic_stop (EV_A_ w);
1523 ev_periodic_start (EV_A_ w); 1553 ev_periodic_start (EV_A_ w);
1524} 1554}
1525#endif 1555#endif
1526 1556
1527void 1557void
1528ev_idle_start (EV_P_ struct ev_idle *w) 1558ev_idle_start (EV_P_ ev_idle *w)
1529{ 1559{
1530 if (expect_false (ev_is_active (w))) 1560 if (expect_false (ev_is_active (w)))
1531 return; 1561 return;
1532 1562
1533 ev_start (EV_A_ (W)w, ++idlecnt); 1563 ev_start (EV_A_ (W)w, ++idlecnt);
1534 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); 1564 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1535 idles [idlecnt - 1] = w; 1565 idles [idlecnt - 1] = w;
1536} 1566}
1537 1567
1538void 1568void
1539ev_idle_stop (EV_P_ struct ev_idle *w) 1569ev_idle_stop (EV_P_ ev_idle *w)
1540{ 1570{
1541 ev_clear_pending (EV_A_ (W)w); 1571 ev_clear_pending (EV_A_ (W)w);
1542 if (expect_false (!ev_is_active (w))) 1572 if (expect_false (!ev_is_active (w)))
1543 return; 1573 return;
1544 1574
1575 {
1576 int active = ((W)w)->active;
1545 idles [((W)w)->active - 1] = idles [--idlecnt]; 1577 idles [active - 1] = idles [--idlecnt];
1578 ((W)idles [active - 1])->active = active;
1579 }
1580
1546 ev_stop (EV_A_ (W)w); 1581 ev_stop (EV_A_ (W)w);
1547} 1582}
1548 1583
1549void 1584void
1550ev_prepare_start (EV_P_ struct ev_prepare *w) 1585ev_prepare_start (EV_P_ ev_prepare *w)
1551{ 1586{
1552 if (expect_false (ev_is_active (w))) 1587 if (expect_false (ev_is_active (w)))
1553 return; 1588 return;
1554 1589
1555 ev_start (EV_A_ (W)w, ++preparecnt); 1590 ev_start (EV_A_ (W)w, ++preparecnt);
1556 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); 1591 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1557 prepares [preparecnt - 1] = w; 1592 prepares [preparecnt - 1] = w;
1558} 1593}
1559 1594
1560void 1595void
1561ev_prepare_stop (EV_P_ struct ev_prepare *w) 1596ev_prepare_stop (EV_P_ ev_prepare *w)
1562{ 1597{
1563 ev_clear_pending (EV_A_ (W)w); 1598 ev_clear_pending (EV_A_ (W)w);
1564 if (expect_false (!ev_is_active (w))) 1599 if (expect_false (!ev_is_active (w)))
1565 return; 1600 return;
1566 1601
1602 {
1603 int active = ((W)w)->active;
1567 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1604 prepares [active - 1] = prepares [--preparecnt];
1605 ((W)prepares [active - 1])->active = active;
1606 }
1607
1568 ev_stop (EV_A_ (W)w); 1608 ev_stop (EV_A_ (W)w);
1569} 1609}
1570 1610
1571void 1611void
1572ev_check_start (EV_P_ struct ev_check *w) 1612ev_check_start (EV_P_ ev_check *w)
1573{ 1613{
1574 if (expect_false (ev_is_active (w))) 1614 if (expect_false (ev_is_active (w)))
1575 return; 1615 return;
1576 1616
1577 ev_start (EV_A_ (W)w, ++checkcnt); 1617 ev_start (EV_A_ (W)w, ++checkcnt);
1578 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); 1618 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1579 checks [checkcnt - 1] = w; 1619 checks [checkcnt - 1] = w;
1580} 1620}
1581 1621
1582void 1622void
1583ev_check_stop (EV_P_ struct ev_check *w) 1623ev_check_stop (EV_P_ ev_check *w)
1584{ 1624{
1585 ev_clear_pending (EV_A_ (W)w); 1625 ev_clear_pending (EV_A_ (W)w);
1586 if (expect_false (!ev_is_active (w))) 1626 if (expect_false (!ev_is_active (w)))
1587 return; 1627 return;
1588 1628
1629 {
1630 int active = ((W)w)->active;
1589 checks [((W)w)->active - 1] = checks [--checkcnt]; 1631 checks [active - 1] = checks [--checkcnt];
1632 ((W)checks [active - 1])->active = active;
1633 }
1634
1590 ev_stop (EV_A_ (W)w); 1635 ev_stop (EV_A_ (W)w);
1591} 1636}
1592 1637
1593#ifndef SA_RESTART 1638#ifndef SA_RESTART
1594# define SA_RESTART 0 1639# define SA_RESTART 0
1595#endif 1640#endif
1596 1641
1597void 1642void
1598ev_signal_start (EV_P_ struct ev_signal *w) 1643ev_signal_start (EV_P_ ev_signal *w)
1599{ 1644{
1600#if EV_MULTIPLICITY 1645#if EV_MULTIPLICITY
1601 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1646 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1602#endif 1647#endif
1603 if (expect_false (ev_is_active (w))) 1648 if (expect_false (ev_is_active (w)))
1622#endif 1667#endif
1623 } 1668 }
1624} 1669}
1625 1670
1626void 1671void
1627ev_signal_stop (EV_P_ struct ev_signal *w) 1672ev_signal_stop (EV_P_ ev_signal *w)
1628{ 1673{
1629 ev_clear_pending (EV_A_ (W)w); 1674 ev_clear_pending (EV_A_ (W)w);
1630 if (expect_false (!ev_is_active (w))) 1675 if (expect_false (!ev_is_active (w)))
1631 return; 1676 return;
1632 1677
1636 if (!signals [w->signum - 1].head) 1681 if (!signals [w->signum - 1].head)
1637 signal (w->signum, SIG_DFL); 1682 signal (w->signum, SIG_DFL);
1638} 1683}
1639 1684
1640void 1685void
1641ev_child_start (EV_P_ struct ev_child *w) 1686ev_child_start (EV_P_ ev_child *w)
1642{ 1687{
1643#if EV_MULTIPLICITY 1688#if EV_MULTIPLICITY
1644 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1689 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1645#endif 1690#endif
1646 if (expect_false (ev_is_active (w))) 1691 if (expect_false (ev_is_active (w)))
1649 ev_start (EV_A_ (W)w, 1); 1694 ev_start (EV_A_ (W)w, 1);
1650 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1695 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1651} 1696}
1652 1697
1653void 1698void
1654ev_child_stop (EV_P_ struct ev_child *w) 1699ev_child_stop (EV_P_ ev_child *w)
1655{ 1700{
1656 ev_clear_pending (EV_A_ (W)w); 1701 ev_clear_pending (EV_A_ (W)w);
1657 if (expect_false (!ev_is_active (w))) 1702 if (expect_false (!ev_is_active (w)))
1658 return; 1703 return;
1659 1704
1660 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1705 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1661 ev_stop (EV_A_ (W)w); 1706 ev_stop (EV_A_ (W)w);
1662} 1707}
1663 1708
1709#if EV_EMBED_ENABLE
1710void noinline
1711ev_embed_sweep (EV_P_ ev_embed *w)
1712{
1713 ev_loop (w->loop, EVLOOP_NONBLOCK);
1714}
1715
1716static void
1717embed_cb (EV_P_ ev_io *io, int revents)
1718{
1719 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
1720
1721 if (ev_cb (w))
1722 ev_feed_event (EV_A_ (W)w, EV_EMBED);
1723 else
1724 ev_embed_sweep (loop, w);
1725}
1726
1727void
1728ev_embed_start (EV_P_ ev_embed *w)
1729{
1730 if (expect_false (ev_is_active (w)))
1731 return;
1732
1733 {
1734 struct ev_loop *loop = w->loop;
1735 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
1736 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ);
1737 }
1738
1739 ev_set_priority (&w->io, ev_priority (w));
1740 ev_io_start (EV_A_ &w->io);
1741
1742 ev_start (EV_A_ (W)w, 1);
1743}
1744
1745void
1746ev_embed_stop (EV_P_ ev_embed *w)
1747{
1748 ev_clear_pending (EV_A_ (W)w);
1749 if (expect_false (!ev_is_active (w)))
1750 return;
1751
1752 ev_io_stop (EV_A_ &w->io);
1753
1754 ev_stop (EV_A_ (W)w);
1755}
1756#endif
1757
1758#if EV_STAT_ENABLE
1759
1760# ifdef _WIN32
1761# define lstat(a,b) stat(a,b)
1762# endif
1763
1764void
1765ev_stat_stat (EV_P_ ev_stat *w)
1766{
1767 if (lstat (w->path, &w->attr) < 0)
1768 w->attr.st_nlink = 0;
1769 else if (!w->attr.st_nlink)
1770 w->attr.st_nlink = 1;
1771}
1772
1773static void
1774stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1775{
1776 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1777
1778 /* we copy this here each the time so that */
1779 /* prev has the old value when the callback gets invoked */
1780 w->prev = w->attr;
1781 ev_stat_stat (EV_A_ w);
1782
1783 if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata)))
1784 ev_feed_event (EV_A_ w, EV_STAT);
1785}
1786
1787void
1788ev_stat_start (EV_P_ ev_stat *w)
1789{
1790 if (expect_false (ev_is_active (w)))
1791 return;
1792
1793 /* since we use memcmp, we need to clear any padding data etc. */
1794 memset (&w->prev, 0, sizeof (ev_statdata));
1795 memset (&w->attr, 0, sizeof (ev_statdata));
1796
1797 ev_stat_stat (EV_A_ w);
1798
1799 ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval);
1800 ev_set_priority (&w->timer, ev_priority (w));
1801 ev_timer_start (EV_A_ &w->timer);
1802
1803 ev_start (EV_A_ (W)w, 1);
1804}
1805
1806void
1807ev_stat_stop (EV_P_ ev_stat *w)
1808{
1809 ev_clear_pending (EV_A_ (W)w);
1810 if (expect_false (!ev_is_active (w)))
1811 return;
1812
1813 ev_timer_stop (EV_A_ &w->timer);
1814
1815 ev_stop (EV_A_ (W)w);
1816}
1817#endif
1818
1664/*****************************************************************************/ 1819/*****************************************************************************/
1665 1820
1666struct ev_once 1821struct ev_once
1667{ 1822{
1668 struct ev_io io; 1823 ev_io io;
1669 struct ev_timer to; 1824 ev_timer to;
1670 void (*cb)(int revents, void *arg); 1825 void (*cb)(int revents, void *arg);
1671 void *arg; 1826 void *arg;
1672}; 1827};
1673 1828
1674static void 1829static void
1683 1838
1684 cb (revents, arg); 1839 cb (revents, arg);
1685} 1840}
1686 1841
1687static void 1842static void
1688once_cb_io (EV_P_ struct ev_io *w, int revents) 1843once_cb_io (EV_P_ ev_io *w, int revents)
1689{ 1844{
1690 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); 1845 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1691} 1846}
1692 1847
1693static void 1848static void
1694once_cb_to (EV_P_ struct ev_timer *w, int revents) 1849once_cb_to (EV_P_ ev_timer *w, int revents)
1695{ 1850{
1696 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); 1851 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1697} 1852}
1698 1853
1699void 1854void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines