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

Comparing libev/ev.c (file contents):
Revision 1.162 by root, Mon Dec 3 13:41:24 2007 UTC vs.
Revision 1.188 by root, Thu Dec 20 07:12:57 2007 UTC

202#ifndef CLOCK_REALTIME 202#ifndef CLOCK_REALTIME
203# undef EV_USE_REALTIME 203# undef EV_USE_REALTIME
204# define EV_USE_REALTIME 0 204# define EV_USE_REALTIME 0
205#endif 205#endif
206 206
207#if !EV_STAT_ENABLE
208# undef EV_USE_INOTIFY
209# define EV_USE_INOTIFY 0
210#endif
211
212#if EV_USE_INOTIFY
213# include <sys/inotify.h>
214#endif
215
207#if EV_SELECT_IS_WINSOCKET 216#if EV_SELECT_IS_WINSOCKET
208# include <winsock.h> 217# include <winsock.h>
209#endif 218#endif
210 219
211#if !EV_STAT_ENABLE
212# define EV_USE_INOTIFY 0
213#endif
214
215#if EV_USE_INOTIFY
216# include <sys/inotify.h>
217#endif
218
219/**/ 220/**/
221
222/*
223 * This is used to avoid floating point rounding problems.
224 * It is added to ev_rt_now when scheduling periodics
225 * to ensure progress, time-wise, even when rounding
226 * errors are against us.
227 * This value is good at least till the year 4000.
228 * Better solutions welcome.
229 */
230#define TIME_EPSILON 0.0001220703125 /* 1/8192 */
220 231
221#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 232#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
222#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 233#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
223/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ 234/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */
224 235
225#if __GNUC__ >= 3 236#if __GNUC__ >= 4
226# define expect(expr,value) __builtin_expect ((expr),(value)) 237# define expect(expr,value) __builtin_expect ((expr),(value))
227# define inline_size static inline /* inline for codesize */
228# if EV_MINIMAL
229# define noinline __attribute__ ((noinline)) 238# define noinline __attribute__ ((noinline))
230# define inline_speed static noinline
231# else
232# define noinline
233# define inline_speed static inline
234# endif
235#else 239#else
236# define expect(expr,value) (expr) 240# define expect(expr,value) (expr)
237# define inline_speed static
238# define inline_size static
239# define noinline 241# define noinline
242# if __STDC_VERSION__ < 199901L
243# define inline
244# endif
240#endif 245#endif
241 246
242#define expect_false(expr) expect ((expr) != 0, 0) 247#define expect_false(expr) expect ((expr) != 0, 0)
243#define expect_true(expr) expect ((expr) != 0, 1) 248#define expect_true(expr) expect ((expr) != 0, 1)
249#define inline_size static inline
250
251#if EV_MINIMAL
252# define inline_speed static noinline
253#else
254# define inline_speed static inline
255#endif
244 256
245#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 257#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
246#define ABSPRI(w) ((w)->priority - EV_MINPRI) 258#define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
247 259
248#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 260#define EMPTY /* required for microsofts broken pseudo-c compiler */
249#define EMPTY2(a,b) /* used to suppress some warnings */ 261#define EMPTY2(a,b) /* used to suppress some warnings */
250 262
251typedef ev_watcher *W; 263typedef ev_watcher *W;
252typedef ev_watcher_list *WL; 264typedef ev_watcher_list *WL;
253typedef ev_watcher_time *WT; 265typedef ev_watcher_time *WT;
396{ 408{
397 return ev_rt_now; 409 return ev_rt_now;
398} 410}
399#endif 411#endif
400 412
401#define array_roundsize(type,n) (((n) | 4) & ~3) 413int inline_size
414array_nextsize (int elem, int cur, int cnt)
415{
416 int ncur = cur + 1;
417
418 do
419 ncur <<= 1;
420 while (cnt > ncur);
421
422 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */
423 if (elem * ncur > 4096)
424 {
425 ncur *= elem;
426 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095;
427 ncur = ncur - sizeof (void *) * 4;
428 ncur /= elem;
429 }
430
431 return ncur;
432}
433
434static noinline void *
435array_realloc (int elem, void *base, int *cur, int cnt)
436{
437 *cur = array_nextsize (elem, *cur, cnt);
438 return ev_realloc (base, elem * *cur);
439}
402 440
403#define array_needsize(type,base,cur,cnt,init) \ 441#define array_needsize(type,base,cur,cnt,init) \
404 if (expect_false ((cnt) > cur)) \ 442 if (expect_false ((cnt) > (cur))) \
405 { \ 443 { \
406 int newcnt = cur; \ 444 int ocur_ = (cur); \
407 do \ 445 (base) = (type *)array_realloc \
408 { \ 446 (sizeof (type), (base), &(cur), (cnt)); \
409 newcnt = array_roundsize (type, newcnt << 1); \ 447 init ((base) + (ocur_), (cur) - ocur_); \
410 } \
411 while ((cnt) > newcnt); \
412 \
413 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
414 init (base + cur, newcnt - cur); \
415 cur = newcnt; \
416 } 448 }
417 449
450#if 0
418#define array_slim(type,stem) \ 451#define array_slim(type,stem) \
419 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 452 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
420 { \ 453 { \
421 stem ## max = array_roundsize (stem ## cnt >> 1); \ 454 stem ## max = array_roundsize (stem ## cnt >> 1); \
422 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 455 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
423 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 456 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
424 } 457 }
458#endif
425 459
426#define array_free(stem, idx) \ 460#define array_free(stem, idx) \
427 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 461 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
428 462
429/*****************************************************************************/ 463/*****************************************************************************/
430 464
431void noinline 465void noinline
432ev_feed_event (EV_P_ void *w, int revents) 466ev_feed_event (EV_P_ void *w, int revents)
433{ 467{
434 W w_ = (W)w; 468 W w_ = (W)w;
469 int pri = ABSPRI (w_);
435 470
436 if (expect_false (w_->pending)) 471 if (expect_false (w_->pending))
472 pendings [pri][w_->pending - 1].events |= revents;
473 else
437 { 474 {
475 w_->pending = ++pendingcnt [pri];
476 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2);
477 pendings [pri][w_->pending - 1].w = w_;
438 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 478 pendings [pri][w_->pending - 1].events = revents;
439 return;
440 } 479 }
441
442 w_->pending = ++pendingcnt [ABSPRI (w_)];
443 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2);
444 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
445 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
446} 480}
447 481
448void inline_size 482void inline_speed
449queue_events (EV_P_ W *events, int eventcnt, int type) 483queue_events (EV_P_ W *events, int eventcnt, int type)
450{ 484{
451 int i; 485 int i;
452 486
453 for (i = 0; i < eventcnt; ++i) 487 for (i = 0; i < eventcnt; ++i)
485} 519}
486 520
487void 521void
488ev_feed_fd_event (EV_P_ int fd, int revents) 522ev_feed_fd_event (EV_P_ int fd, int revents)
489{ 523{
524 if (fd >= 0 && fd < anfdmax)
490 fd_event (EV_A_ fd, revents); 525 fd_event (EV_A_ fd, revents);
491} 526}
492 527
493void inline_size 528void inline_size
494fd_reify (EV_P) 529fd_reify (EV_P)
495{ 530{
499 { 534 {
500 int fd = fdchanges [i]; 535 int fd = fdchanges [i];
501 ANFD *anfd = anfds + fd; 536 ANFD *anfd = anfds + fd;
502 ev_io *w; 537 ev_io *w;
503 538
504 int events = 0; 539 unsigned char events = 0;
505 540
506 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) 541 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
507 events |= w->events; 542 events |= (unsigned char)w->events;
508 543
509#if EV_SELECT_IS_WINSOCKET 544#if EV_SELECT_IS_WINSOCKET
510 if (events) 545 if (events)
511 { 546 {
512 unsigned long argp; 547 unsigned long argp;
513 anfd->handle = _get_osfhandle (fd); 548 anfd->handle = _get_osfhandle (fd);
514 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0)); 549 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
515 } 550 }
516#endif 551#endif
517 552
553 {
554 unsigned char o_events = anfd->events;
555 unsigned char o_reify = anfd->reify;
556
518 anfd->reify = 0; 557 anfd->reify = 0;
519
520 backend_modify (EV_A_ fd, anfd->events, events);
521 anfd->events = events; 558 anfd->events = events;
559
560 if (o_events != events || o_reify & EV_IOFDSET)
561 backend_modify (EV_A_ fd, o_events, events);
562 }
522 } 563 }
523 564
524 fdchangecnt = 0; 565 fdchangecnt = 0;
525} 566}
526 567
527void inline_size 568void inline_size
528fd_change (EV_P_ int fd) 569fd_change (EV_P_ int fd, int flags)
529{ 570{
530 if (expect_false (anfds [fd].reify)) 571 unsigned char reify = anfds [fd].reify;
531 return;
532
533 anfds [fd].reify = 1; 572 anfds [fd].reify |= flags;
534 573
574 if (expect_true (!reify))
575 {
535 ++fdchangecnt; 576 ++fdchangecnt;
536 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 577 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
537 fdchanges [fdchangecnt - 1] = fd; 578 fdchanges [fdchangecnt - 1] = fd;
579 }
538} 580}
539 581
540void inline_speed 582void inline_speed
541fd_kill (EV_P_ int fd) 583fd_kill (EV_P_ int fd)
542{ 584{
593 635
594 for (fd = 0; fd < anfdmax; ++fd) 636 for (fd = 0; fd < anfdmax; ++fd)
595 if (anfds [fd].events) 637 if (anfds [fd].events)
596 { 638 {
597 anfds [fd].events = 0; 639 anfds [fd].events = 0;
598 fd_change (EV_A_ fd); 640 fd_change (EV_A_ fd, EV_IOFDSET | 1);
599 } 641 }
600} 642}
601 643
602/*****************************************************************************/ 644/*****************************************************************************/
603 645
604void inline_speed 646void inline_speed
605upheap (WT *heap, int k) 647upheap (WT *heap, int k)
606{ 648{
607 WT w = heap [k]; 649 WT w = heap [k];
608 650
609 while (k && heap [k >> 1]->at > w->at) 651 while (k)
610 { 652 {
653 int p = (k - 1) >> 1;
654
655 if (heap [p]->at <= w->at)
656 break;
657
611 heap [k] = heap [k >> 1]; 658 heap [k] = heap [p];
612 ((W)heap [k])->active = k + 1; 659 ((W)heap [k])->active = k + 1;
613 k >>= 1; 660 k = p;
614 } 661 }
615 662
616 heap [k] = w; 663 heap [k] = w;
617 ((W)heap [k])->active = k + 1; 664 ((W)heap [k])->active = k + 1;
618
619} 665}
620 666
621void inline_speed 667void inline_speed
622downheap (WT *heap, int N, int k) 668downheap (WT *heap, int N, int k)
623{ 669{
624 WT w = heap [k]; 670 WT w = heap [k];
625 671
626 while (k < (N >> 1)) 672 for (;;)
627 { 673 {
628 int j = k << 1; 674 int c = (k << 1) + 1;
629 675
630 if (j + 1 < N && heap [j]->at > heap [j + 1]->at) 676 if (c >= N)
631 ++j;
632
633 if (w->at <= heap [j]->at)
634 break; 677 break;
635 678
679 c += c + 1 < N && heap [c]->at > heap [c + 1]->at
680 ? 1 : 0;
681
682 if (w->at <= heap [c]->at)
683 break;
684
636 heap [k] = heap [j]; 685 heap [k] = heap [c];
637 ((W)heap [k])->active = k + 1; 686 ((W)heap [k])->active = k + 1;
687
638 k = j; 688 k = c;
639 } 689 }
640 690
641 heap [k] = w; 691 heap [k] = w;
642 ((W)heap [k])->active = k + 1; 692 ((W)heap [k])->active = k + 1;
643} 693}
725 for (signum = signalmax; signum--; ) 775 for (signum = signalmax; signum--; )
726 if (signals [signum].gotsig) 776 if (signals [signum].gotsig)
727 ev_feed_signal_event (EV_A_ signum + 1); 777 ev_feed_signal_event (EV_A_ signum + 1);
728} 778}
729 779
730void inline_size 780void inline_speed
731fd_intern (int fd) 781fd_intern (int fd)
732{ 782{
733#ifdef _WIN32 783#ifdef _WIN32
734 int arg = 1; 784 int arg = 1;
735 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 785 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
750 ev_unref (EV_A); /* child watcher should not keep loop alive */ 800 ev_unref (EV_A); /* child watcher should not keep loop alive */
751} 801}
752 802
753/*****************************************************************************/ 803/*****************************************************************************/
754 804
755static ev_child *childs [EV_PID_HASHSIZE]; 805static WL childs [EV_PID_HASHSIZE];
756 806
757#ifndef _WIN32 807#ifndef _WIN32
758 808
759static ev_signal childev; 809static ev_signal childev;
760 810
764 ev_child *w; 814 ev_child *w;
765 815
766 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 816 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
767 if (w->pid == pid || !w->pid) 817 if (w->pid == pid || !w->pid)
768 { 818 {
769 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 819 ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */
770 w->rpid = pid; 820 w->rpid = pid;
771 w->rstatus = status; 821 w->rstatus = status;
772 ev_feed_event (EV_A_ (W)w, EV_CHILD); 822 ev_feed_event (EV_A_ (W)w, EV_CHILD);
773 } 823 }
774} 824}
775 825
776#ifndef WCONTINUED 826#ifndef WCONTINUED
981#if EV_USE_SELECT 1031#if EV_USE_SELECT
982 if (backend == EVBACKEND_SELECT) select_destroy (EV_A); 1032 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
983#endif 1033#endif
984 1034
985 for (i = NUMPRI; i--; ) 1035 for (i = NUMPRI; i--; )
1036 {
986 array_free (pending, [i]); 1037 array_free (pending, [i]);
1038#if EV_IDLE_ENABLE
1039 array_free (idle, [i]);
1040#endif
1041 }
1042
1043 ev_free (anfds); anfdmax = 0;
987 1044
988 /* have to use the microsoft-never-gets-it-right macro */ 1045 /* have to use the microsoft-never-gets-it-right macro */
989 array_free (fdchange, EMPTY0); 1046 array_free (fdchange, EMPTY);
990 array_free (timer, EMPTY0); 1047 array_free (timer, EMPTY);
991#if EV_PERIODIC_ENABLE 1048#if EV_PERIODIC_ENABLE
992 array_free (periodic, EMPTY0); 1049 array_free (periodic, EMPTY);
993#endif 1050#endif
1051#if EV_FORK_ENABLE
994 array_free (idle, EMPTY0); 1052 array_free (fork, EMPTY);
1053#endif
995 array_free (prepare, EMPTY0); 1054 array_free (prepare, EMPTY);
996 array_free (check, EMPTY0); 1055 array_free (check, EMPTY);
997 1056
998 backend = 0; 1057 backend = 0;
999} 1058}
1000 1059
1001void inline_size infy_fork (EV_P); 1060void inline_size infy_fork (EV_P);
1137 postfork = 1; 1196 postfork = 1;
1138} 1197}
1139 1198
1140/*****************************************************************************/ 1199/*****************************************************************************/
1141 1200
1142int inline_size 1201void
1143any_pending (EV_P) 1202ev_invoke (EV_P_ void *w, int revents)
1144{ 1203{
1145 int pri; 1204 EV_CB_INVOKE ((W)w, revents);
1146
1147 for (pri = NUMPRI; pri--; )
1148 if (pendingcnt [pri])
1149 return 1;
1150
1151 return 0;
1152} 1205}
1153 1206
1154void inline_speed 1207void inline_speed
1155call_pending (EV_P) 1208call_pending (EV_P)
1156{ 1209{
1174void inline_size 1227void inline_size
1175timers_reify (EV_P) 1228timers_reify (EV_P)
1176{ 1229{
1177 while (timercnt && ((WT)timers [0])->at <= mn_now) 1230 while (timercnt && ((WT)timers [0])->at <= mn_now)
1178 { 1231 {
1179 ev_timer *w = timers [0]; 1232 ev_timer *w = (ev_timer *)timers [0];
1180 1233
1181 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1234 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1182 1235
1183 /* first reschedule or stop timer */ 1236 /* first reschedule or stop timer */
1184 if (w->repeat) 1237 if (w->repeat)
1187 1240
1188 ((WT)w)->at += w->repeat; 1241 ((WT)w)->at += w->repeat;
1189 if (((WT)w)->at < mn_now) 1242 if (((WT)w)->at < mn_now)
1190 ((WT)w)->at = mn_now; 1243 ((WT)w)->at = mn_now;
1191 1244
1192 downheap ((WT *)timers, timercnt, 0); 1245 downheap (timers, timercnt, 0);
1193 } 1246 }
1194 else 1247 else
1195 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1248 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1196 1249
1197 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1250 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1202void inline_size 1255void inline_size
1203periodics_reify (EV_P) 1256periodics_reify (EV_P)
1204{ 1257{
1205 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1258 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1206 { 1259 {
1207 ev_periodic *w = periodics [0]; 1260 ev_periodic *w = (ev_periodic *)periodics [0];
1208 1261
1209 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1262 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1210 1263
1211 /* first reschedule or stop timer */ 1264 /* first reschedule or stop timer */
1212 if (w->reschedule_cb) 1265 if (w->reschedule_cb)
1213 { 1266 {
1214 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); 1267 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1215 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); 1268 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1216 downheap ((WT *)periodics, periodiccnt, 0); 1269 downheap (periodics, periodiccnt, 0);
1217 } 1270 }
1218 else if (w->interval) 1271 else if (w->interval)
1219 { 1272 {
1220 ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1273 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1274 if (((WT)w)->at - ev_rt_now <= TIME_EPSILON) ((WT)w)->at += w->interval;
1221 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); 1275 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
1222 downheap ((WT *)periodics, periodiccnt, 0); 1276 downheap (periodics, periodiccnt, 0);
1223 } 1277 }
1224 else 1278 else
1225 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1279 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1226 1280
1227 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1281 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1234 int i; 1288 int i;
1235 1289
1236 /* adjust periodics after time jump */ 1290 /* adjust periodics after time jump */
1237 for (i = 0; i < periodiccnt; ++i) 1291 for (i = 0; i < periodiccnt; ++i)
1238 { 1292 {
1239 ev_periodic *w = periodics [i]; 1293 ev_periodic *w = (ev_periodic *)periodics [i];
1240 1294
1241 if (w->reschedule_cb) 1295 if (w->reschedule_cb)
1242 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1296 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1243 else if (w->interval) 1297 else if (w->interval)
1244 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1298 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1245 } 1299 }
1246 1300
1247 /* now rebuild the heap */ 1301 /* now rebuild the heap */
1248 for (i = periodiccnt >> 1; i--; ) 1302 for (i = periodiccnt >> 1; i--; )
1249 downheap ((WT *)periodics, periodiccnt, i); 1303 downheap (periodics, periodiccnt, i);
1250} 1304}
1251#endif 1305#endif
1252 1306
1307#if EV_IDLE_ENABLE
1253int inline_size 1308void inline_size
1254time_update_monotonic (EV_P) 1309idle_reify (EV_P)
1255{ 1310{
1311 if (expect_false (idleall))
1312 {
1313 int pri;
1314
1315 for (pri = NUMPRI; pri--; )
1316 {
1317 if (pendingcnt [pri])
1318 break;
1319
1320 if (idlecnt [pri])
1321 {
1322 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1323 break;
1324 }
1325 }
1326 }
1327}
1328#endif
1329
1330void inline_speed
1331time_update (EV_P_ ev_tstamp max_block)
1332{
1333 int i;
1334
1335#if EV_USE_MONOTONIC
1336 if (expect_true (have_monotonic))
1337 {
1338 ev_tstamp odiff = rtmn_diff;
1339
1256 mn_now = get_clock (); 1340 mn_now = get_clock ();
1257 1341
1342 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
1343 /* interpolate in the meantime */
1258 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1344 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1259 { 1345 {
1260 ev_rt_now = rtmn_diff + mn_now; 1346 ev_rt_now = rtmn_diff + mn_now;
1261 return 0; 1347 return;
1262 } 1348 }
1263 else 1349
1264 {
1265 now_floor = mn_now; 1350 now_floor = mn_now;
1266 ev_rt_now = ev_time (); 1351 ev_rt_now = ev_time ();
1267 return 1;
1268 }
1269}
1270 1352
1271void inline_size 1353 /* loop a few times, before making important decisions.
1272time_update (EV_P) 1354 * on the choice of "4": one iteration isn't enough,
1273{ 1355 * in case we get preempted during the calls to
1274 int i; 1356 * ev_time and get_clock. a second call is almost guaranteed
1275 1357 * to succeed in that case, though. and looping a few more times
1276#if EV_USE_MONOTONIC 1358 * doesn't hurt either as we only do this on time-jumps or
1277 if (expect_true (have_monotonic)) 1359 * in the unlikely event of having been preempted here.
1278 { 1360 */
1279 if (time_update_monotonic (EV_A)) 1361 for (i = 4; --i; )
1280 { 1362 {
1281 ev_tstamp odiff = rtmn_diff;
1282
1283 /* loop a few times, before making important decisions.
1284 * on the choice of "4": one iteration isn't enough,
1285 * in case we get preempted during the calls to
1286 * ev_time and get_clock. a second call is almost guaranteed
1287 * to succeed in that case, though. and looping a few more times
1288 * doesn't hurt either as we only do this on time-jumps or
1289 * in the unlikely event of having been preempted here.
1290 */
1291 for (i = 4; --i; )
1292 {
1293 rtmn_diff = ev_rt_now - mn_now; 1363 rtmn_diff = ev_rt_now - mn_now;
1294 1364
1295 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1365 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1296 return; /* all is well */ 1366 return; /* all is well */
1297 1367
1298 ev_rt_now = ev_time (); 1368 ev_rt_now = ev_time ();
1299 mn_now = get_clock (); 1369 mn_now = get_clock ();
1300 now_floor = mn_now; 1370 now_floor = mn_now;
1301 } 1371 }
1302 1372
1303# if EV_PERIODIC_ENABLE 1373# if EV_PERIODIC_ENABLE
1304 periodics_reschedule (EV_A); 1374 periodics_reschedule (EV_A);
1305# endif 1375# endif
1306 /* no timer adjustment, as the monotonic clock doesn't jump */ 1376 /* no timer adjustment, as the monotonic clock doesn't jump */
1307 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1377 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1308 }
1309 } 1378 }
1310 else 1379 else
1311#endif 1380#endif
1312 { 1381 {
1313 ev_rt_now = ev_time (); 1382 ev_rt_now = ev_time ();
1314 1383
1315 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1384 if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP))
1316 { 1385 {
1317#if EV_PERIODIC_ENABLE 1386#if EV_PERIODIC_ENABLE
1318 periodics_reschedule (EV_A); 1387 periodics_reschedule (EV_A);
1319#endif 1388#endif
1320
1321 /* adjust timers. this is easy, as the offset is the same for all of them */ 1389 /* adjust timers. this is easy, as the offset is the same for all of them */
1322 for (i = 0; i < timercnt; ++i) 1390 for (i = 0; i < timercnt; ++i)
1323 ((WT)timers [i])->at += ev_rt_now - mn_now; 1391 ((WT)timers [i])->at += ev_rt_now - mn_now;
1324 } 1392 }
1325 1393
1369 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 1437 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1370 call_pending (EV_A); 1438 call_pending (EV_A);
1371 } 1439 }
1372#endif 1440#endif
1373 1441
1374 /* queue check watchers (and execute them) */ 1442 /* queue prepare watchers (and execute them) */
1375 if (expect_false (preparecnt)) 1443 if (expect_false (preparecnt))
1376 { 1444 {
1377 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1445 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1378 call_pending (EV_A); 1446 call_pending (EV_A);
1379 } 1447 }
1390 1458
1391 /* calculate blocking time */ 1459 /* calculate blocking time */
1392 { 1460 {
1393 ev_tstamp block; 1461 ev_tstamp block;
1394 1462
1395 if (expect_false (flags & EVLOOP_NONBLOCK || idlecnt || !activecnt)) 1463 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1396 block = 0.; /* do not block at all */ 1464 block = 0.; /* do not block at all */
1397 else 1465 else
1398 { 1466 {
1399 /* update time to cancel out callback processing overhead */ 1467 /* update time to cancel out callback processing overhead */
1400#if EV_USE_MONOTONIC
1401 if (expect_true (have_monotonic))
1402 time_update_monotonic (EV_A); 1468 time_update (EV_A_ 1e100);
1403 else
1404#endif
1405 {
1406 ev_rt_now = ev_time ();
1407 mn_now = ev_rt_now;
1408 }
1409 1469
1410 block = MAX_BLOCKTIME; 1470 block = MAX_BLOCKTIME;
1411 1471
1412 if (timercnt) 1472 if (timercnt)
1413 { 1473 {
1426 if (expect_false (block < 0.)) block = 0.; 1486 if (expect_false (block < 0.)) block = 0.;
1427 } 1487 }
1428 1488
1429 ++loop_count; 1489 ++loop_count;
1430 backend_poll (EV_A_ block); 1490 backend_poll (EV_A_ block);
1491
1492 /* update ev_rt_now, do magic */
1493 time_update (EV_A_ block);
1431 } 1494 }
1432
1433 /* update ev_rt_now, do magic */
1434 time_update (EV_A);
1435 1495
1436 /* queue pending timers and reschedule them */ 1496 /* queue pending timers and reschedule them */
1437 timers_reify (EV_A); /* relative timers called last */ 1497 timers_reify (EV_A); /* relative timers called last */
1438#if EV_PERIODIC_ENABLE 1498#if EV_PERIODIC_ENABLE
1439 periodics_reify (EV_A); /* absolute timers called first */ 1499 periodics_reify (EV_A); /* absolute timers called first */
1440#endif 1500#endif
1441 1501
1502#if EV_IDLE_ENABLE
1442 /* queue idle watchers unless other events are pending */ 1503 /* queue idle watchers unless other events are pending */
1443 if (idlecnt && !any_pending (EV_A)) 1504 idle_reify (EV_A);
1444 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1505#endif
1445 1506
1446 /* queue check watchers, to be executed first */ 1507 /* queue check watchers, to be executed first */
1447 if (expect_false (checkcnt)) 1508 if (expect_false (checkcnt))
1448 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1509 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1449 1510
1485 head = &(*head)->next; 1546 head = &(*head)->next;
1486 } 1547 }
1487} 1548}
1488 1549
1489void inline_speed 1550void inline_speed
1490ev_clear_pending (EV_P_ W w) 1551clear_pending (EV_P_ W w)
1491{ 1552{
1492 if (w->pending) 1553 if (w->pending)
1493 { 1554 {
1494 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1555 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1495 w->pending = 0; 1556 w->pending = 0;
1496 } 1557 }
1497} 1558}
1498 1559
1560int
1561ev_clear_pending (EV_P_ void *w)
1562{
1563 W w_ = (W)w;
1564 int pending = w_->pending;
1565
1566 if (expect_true (pending))
1567 {
1568 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
1569 w_->pending = 0;
1570 p->w = 0;
1571 return p->events;
1572 }
1573 else
1574 return 0;
1575}
1576
1577void inline_size
1578pri_adjust (EV_P_ W w)
1579{
1580 int pri = w->priority;
1581 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
1582 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
1583 w->priority = pri;
1584}
1585
1499void inline_speed 1586void inline_speed
1500ev_start (EV_P_ W w, int active) 1587ev_start (EV_P_ W w, int active)
1501{ 1588{
1502 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1589 pri_adjust (EV_A_ w);
1503 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1504
1505 w->active = active; 1590 w->active = active;
1506 ev_ref (EV_A); 1591 ev_ref (EV_A);
1507} 1592}
1508 1593
1509void inline_size 1594void inline_size
1513 w->active = 0; 1598 w->active = 0;
1514} 1599}
1515 1600
1516/*****************************************************************************/ 1601/*****************************************************************************/
1517 1602
1518void 1603void noinline
1519ev_io_start (EV_P_ ev_io *w) 1604ev_io_start (EV_P_ ev_io *w)
1520{ 1605{
1521 int fd = w->fd; 1606 int fd = w->fd;
1522 1607
1523 if (expect_false (ev_is_active (w))) 1608 if (expect_false (ev_is_active (w)))
1525 1610
1526 assert (("ev_io_start called with negative fd", fd >= 0)); 1611 assert (("ev_io_start called with negative fd", fd >= 0));
1527 1612
1528 ev_start (EV_A_ (W)w, 1); 1613 ev_start (EV_A_ (W)w, 1);
1529 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); 1614 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1530 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1615 wlist_add (&anfds[fd].head, (WL)w);
1531 1616
1532 fd_change (EV_A_ fd); 1617 fd_change (EV_A_ fd, w->events & EV_IOFDSET | 1);
1618 w->events &= ~EV_IOFDSET;
1533} 1619}
1534 1620
1535void 1621void noinline
1536ev_io_stop (EV_P_ ev_io *w) 1622ev_io_stop (EV_P_ ev_io *w)
1537{ 1623{
1538 ev_clear_pending (EV_A_ (W)w); 1624 clear_pending (EV_A_ (W)w);
1539 if (expect_false (!ev_is_active (w))) 1625 if (expect_false (!ev_is_active (w)))
1540 return; 1626 return;
1541 1627
1542 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1628 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1543 1629
1544 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1630 wlist_del (&anfds[w->fd].head, (WL)w);
1545 ev_stop (EV_A_ (W)w); 1631 ev_stop (EV_A_ (W)w);
1546 1632
1547 fd_change (EV_A_ w->fd); 1633 fd_change (EV_A_ w->fd, 1);
1548} 1634}
1549 1635
1550void 1636void noinline
1551ev_timer_start (EV_P_ ev_timer *w) 1637ev_timer_start (EV_P_ ev_timer *w)
1552{ 1638{
1553 if (expect_false (ev_is_active (w))) 1639 if (expect_false (ev_is_active (w)))
1554 return; 1640 return;
1555 1641
1556 ((WT)w)->at += mn_now; 1642 ((WT)w)->at += mn_now;
1557 1643
1558 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1644 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1559 1645
1560 ev_start (EV_A_ (W)w, ++timercnt); 1646 ev_start (EV_A_ (W)w, ++timercnt);
1561 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); 1647 array_needsize (WT, timers, timermax, timercnt, EMPTY2);
1562 timers [timercnt - 1] = w; 1648 timers [timercnt - 1] = (WT)w;
1563 upheap ((WT *)timers, timercnt - 1); 1649 upheap (timers, timercnt - 1);
1564 1650
1565 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1651 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/
1566} 1652}
1567 1653
1568void 1654void noinline
1569ev_timer_stop (EV_P_ ev_timer *w) 1655ev_timer_stop (EV_P_ ev_timer *w)
1570{ 1656{
1571 ev_clear_pending (EV_A_ (W)w); 1657 clear_pending (EV_A_ (W)w);
1572 if (expect_false (!ev_is_active (w))) 1658 if (expect_false (!ev_is_active (w)))
1573 return; 1659 return;
1574 1660
1575 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1661 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w));
1576 1662
1577 { 1663 {
1578 int active = ((W)w)->active; 1664 int active = ((W)w)->active;
1579 1665
1580 if (expect_true (--active < --timercnt)) 1666 if (expect_true (--active < --timercnt))
1581 { 1667 {
1582 timers [active] = timers [timercnt]; 1668 timers [active] = timers [timercnt];
1583 adjustheap ((WT *)timers, timercnt, active); 1669 adjustheap (timers, timercnt, active);
1584 } 1670 }
1585 } 1671 }
1586 1672
1587 ((WT)w)->at -= mn_now; 1673 ((WT)w)->at -= mn_now;
1588 1674
1589 ev_stop (EV_A_ (W)w); 1675 ev_stop (EV_A_ (W)w);
1590} 1676}
1591 1677
1592void 1678void noinline
1593ev_timer_again (EV_P_ ev_timer *w) 1679ev_timer_again (EV_P_ ev_timer *w)
1594{ 1680{
1595 if (ev_is_active (w)) 1681 if (ev_is_active (w))
1596 { 1682 {
1597 if (w->repeat) 1683 if (w->repeat)
1598 { 1684 {
1599 ((WT)w)->at = mn_now + w->repeat; 1685 ((WT)w)->at = mn_now + w->repeat;
1600 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1686 adjustheap (timers, timercnt, ((W)w)->active - 1);
1601 } 1687 }
1602 else 1688 else
1603 ev_timer_stop (EV_A_ w); 1689 ev_timer_stop (EV_A_ w);
1604 } 1690 }
1605 else if (w->repeat) 1691 else if (w->repeat)
1608 ev_timer_start (EV_A_ w); 1694 ev_timer_start (EV_A_ w);
1609 } 1695 }
1610} 1696}
1611 1697
1612#if EV_PERIODIC_ENABLE 1698#if EV_PERIODIC_ENABLE
1613void 1699void noinline
1614ev_periodic_start (EV_P_ ev_periodic *w) 1700ev_periodic_start (EV_P_ ev_periodic *w)
1615{ 1701{
1616 if (expect_false (ev_is_active (w))) 1702 if (expect_false (ev_is_active (w)))
1617 return; 1703 return;
1618 1704
1620 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1706 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1621 else if (w->interval) 1707 else if (w->interval)
1622 { 1708 {
1623 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1709 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1624 /* this formula differs from the one in periodic_reify because we do not always round up */ 1710 /* this formula differs from the one in periodic_reify because we do not always round up */
1625 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1711 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1626 } 1712 }
1713 else
1714 ((WT)w)->at = w->offset;
1627 1715
1628 ev_start (EV_A_ (W)w, ++periodiccnt); 1716 ev_start (EV_A_ (W)w, ++periodiccnt);
1629 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1717 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2);
1630 periodics [periodiccnt - 1] = w; 1718 periodics [periodiccnt - 1] = (WT)w;
1631 upheap ((WT *)periodics, periodiccnt - 1); 1719 upheap (periodics, periodiccnt - 1);
1632 1720
1633 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1721 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/
1634} 1722}
1635 1723
1636void 1724void noinline
1637ev_periodic_stop (EV_P_ ev_periodic *w) 1725ev_periodic_stop (EV_P_ ev_periodic *w)
1638{ 1726{
1639 ev_clear_pending (EV_A_ (W)w); 1727 clear_pending (EV_A_ (W)w);
1640 if (expect_false (!ev_is_active (w))) 1728 if (expect_false (!ev_is_active (w)))
1641 return; 1729 return;
1642 1730
1643 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1731 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w));
1644 1732
1645 { 1733 {
1646 int active = ((W)w)->active; 1734 int active = ((W)w)->active;
1647 1735
1648 if (expect_true (--active < --periodiccnt)) 1736 if (expect_true (--active < --periodiccnt))
1649 { 1737 {
1650 periodics [active] = periodics [periodiccnt]; 1738 periodics [active] = periodics [periodiccnt];
1651 adjustheap ((WT *)periodics, periodiccnt, active); 1739 adjustheap (periodics, periodiccnt, active);
1652 } 1740 }
1653 } 1741 }
1654 1742
1655 ev_stop (EV_A_ (W)w); 1743 ev_stop (EV_A_ (W)w);
1656} 1744}
1657 1745
1658void 1746void noinline
1659ev_periodic_again (EV_P_ ev_periodic *w) 1747ev_periodic_again (EV_P_ ev_periodic *w)
1660{ 1748{
1661 /* TODO: use adjustheap and recalculation */ 1749 /* TODO: use adjustheap and recalculation */
1662 ev_periodic_stop (EV_A_ w); 1750 ev_periodic_stop (EV_A_ w);
1663 ev_periodic_start (EV_A_ w); 1751 ev_periodic_start (EV_A_ w);
1666 1754
1667#ifndef SA_RESTART 1755#ifndef SA_RESTART
1668# define SA_RESTART 0 1756# define SA_RESTART 0
1669#endif 1757#endif
1670 1758
1671void 1759void noinline
1672ev_signal_start (EV_P_ ev_signal *w) 1760ev_signal_start (EV_P_ ev_signal *w)
1673{ 1761{
1674#if EV_MULTIPLICITY 1762#if EV_MULTIPLICITY
1675 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1763 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1676#endif 1764#endif
1677 if (expect_false (ev_is_active (w))) 1765 if (expect_false (ev_is_active (w)))
1678 return; 1766 return;
1679 1767
1680 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1768 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1681 1769
1770 {
1771#ifndef _WIN32
1772 sigset_t full, prev;
1773 sigfillset (&full);
1774 sigprocmask (SIG_SETMASK, &full, &prev);
1775#endif
1776
1777 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1778
1779#ifndef _WIN32
1780 sigprocmask (SIG_SETMASK, &prev, 0);
1781#endif
1782 }
1783
1682 ev_start (EV_A_ (W)w, 1); 1784 ev_start (EV_A_ (W)w, 1);
1683 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1684 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1785 wlist_add (&signals [w->signum - 1].head, (WL)w);
1685 1786
1686 if (!((WL)w)->next) 1787 if (!((WL)w)->next)
1687 { 1788 {
1688#if _WIN32 1789#if _WIN32
1689 signal (w->signum, sighandler); 1790 signal (w->signum, sighandler);
1695 sigaction (w->signum, &sa, 0); 1796 sigaction (w->signum, &sa, 0);
1696#endif 1797#endif
1697 } 1798 }
1698} 1799}
1699 1800
1700void 1801void noinline
1701ev_signal_stop (EV_P_ ev_signal *w) 1802ev_signal_stop (EV_P_ ev_signal *w)
1702{ 1803{
1703 ev_clear_pending (EV_A_ (W)w); 1804 clear_pending (EV_A_ (W)w);
1704 if (expect_false (!ev_is_active (w))) 1805 if (expect_false (!ev_is_active (w)))
1705 return; 1806 return;
1706 1807
1707 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1808 wlist_del (&signals [w->signum - 1].head, (WL)w);
1708 ev_stop (EV_A_ (W)w); 1809 ev_stop (EV_A_ (W)w);
1709 1810
1710 if (!signals [w->signum - 1].head) 1811 if (!signals [w->signum - 1].head)
1711 signal (w->signum, SIG_DFL); 1812 signal (w->signum, SIG_DFL);
1712} 1813}
1719#endif 1820#endif
1720 if (expect_false (ev_is_active (w))) 1821 if (expect_false (ev_is_active (w)))
1721 return; 1822 return;
1722 1823
1723 ev_start (EV_A_ (W)w, 1); 1824 ev_start (EV_A_ (W)w, 1);
1724 wlist_add ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 1825 wlist_add (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1725} 1826}
1726 1827
1727void 1828void
1728ev_child_stop (EV_P_ ev_child *w) 1829ev_child_stop (EV_P_ ev_child *w)
1729{ 1830{
1730 ev_clear_pending (EV_A_ (W)w); 1831 clear_pending (EV_A_ (W)w);
1731 if (expect_false (!ev_is_active (w))) 1832 if (expect_false (!ev_is_active (w)))
1732 return; 1833 return;
1733 1834
1734 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 1835 wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1735 ev_stop (EV_A_ (W)w); 1836 ev_stop (EV_A_ (W)w);
1736} 1837}
1737 1838
1738#if EV_STAT_ENABLE 1839#if EV_STAT_ENABLE
1739 1840
1971} 2072}
1972 2073
1973void 2074void
1974ev_stat_stop (EV_P_ ev_stat *w) 2075ev_stat_stop (EV_P_ ev_stat *w)
1975{ 2076{
1976 ev_clear_pending (EV_A_ (W)w); 2077 clear_pending (EV_A_ (W)w);
1977 if (expect_false (!ev_is_active (w))) 2078 if (expect_false (!ev_is_active (w)))
1978 return; 2079 return;
1979 2080
1980#if EV_USE_INOTIFY 2081#if EV_USE_INOTIFY
1981 infy_del (EV_A_ w); 2082 infy_del (EV_A_ w);
1984 2085
1985 ev_stop (EV_A_ (W)w); 2086 ev_stop (EV_A_ (W)w);
1986} 2087}
1987#endif 2088#endif
1988 2089
2090#if EV_IDLE_ENABLE
1989void 2091void
1990ev_idle_start (EV_P_ ev_idle *w) 2092ev_idle_start (EV_P_ ev_idle *w)
1991{ 2093{
1992 if (expect_false (ev_is_active (w))) 2094 if (expect_false (ev_is_active (w)))
1993 return; 2095 return;
1994 2096
2097 pri_adjust (EV_A_ (W)w);
2098
2099 {
2100 int active = ++idlecnt [ABSPRI (w)];
2101
2102 ++idleall;
1995 ev_start (EV_A_ (W)w, ++idlecnt); 2103 ev_start (EV_A_ (W)w, active);
2104
1996 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); 2105 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
1997 idles [idlecnt - 1] = w; 2106 idles [ABSPRI (w)][active - 1] = w;
2107 }
1998} 2108}
1999 2109
2000void 2110void
2001ev_idle_stop (EV_P_ ev_idle *w) 2111ev_idle_stop (EV_P_ ev_idle *w)
2002{ 2112{
2003 ev_clear_pending (EV_A_ (W)w); 2113 clear_pending (EV_A_ (W)w);
2004 if (expect_false (!ev_is_active (w))) 2114 if (expect_false (!ev_is_active (w)))
2005 return; 2115 return;
2006 2116
2007 { 2117 {
2008 int active = ((W)w)->active; 2118 int active = ((W)w)->active;
2009 idles [active - 1] = idles [--idlecnt]; 2119
2120 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2010 ((W)idles [active - 1])->active = active; 2121 ((W)idles [ABSPRI (w)][active - 1])->active = active;
2122
2123 ev_stop (EV_A_ (W)w);
2124 --idleall;
2011 } 2125 }
2012
2013 ev_stop (EV_A_ (W)w);
2014} 2126}
2127#endif
2015 2128
2016void 2129void
2017ev_prepare_start (EV_P_ ev_prepare *w) 2130ev_prepare_start (EV_P_ ev_prepare *w)
2018{ 2131{
2019 if (expect_false (ev_is_active (w))) 2132 if (expect_false (ev_is_active (w)))
2025} 2138}
2026 2139
2027void 2140void
2028ev_prepare_stop (EV_P_ ev_prepare *w) 2141ev_prepare_stop (EV_P_ ev_prepare *w)
2029{ 2142{
2030 ev_clear_pending (EV_A_ (W)w); 2143 clear_pending (EV_A_ (W)w);
2031 if (expect_false (!ev_is_active (w))) 2144 if (expect_false (!ev_is_active (w)))
2032 return; 2145 return;
2033 2146
2034 { 2147 {
2035 int active = ((W)w)->active; 2148 int active = ((W)w)->active;
2052} 2165}
2053 2166
2054void 2167void
2055ev_check_stop (EV_P_ ev_check *w) 2168ev_check_stop (EV_P_ ev_check *w)
2056{ 2169{
2057 ev_clear_pending (EV_A_ (W)w); 2170 clear_pending (EV_A_ (W)w);
2058 if (expect_false (!ev_is_active (w))) 2171 if (expect_false (!ev_is_active (w)))
2059 return; 2172 return;
2060 2173
2061 { 2174 {
2062 int active = ((W)w)->active; 2175 int active = ((W)w)->active;
2069 2182
2070#if EV_EMBED_ENABLE 2183#if EV_EMBED_ENABLE
2071void noinline 2184void noinline
2072ev_embed_sweep (EV_P_ ev_embed *w) 2185ev_embed_sweep (EV_P_ ev_embed *w)
2073{ 2186{
2074 ev_loop (w->loop, EVLOOP_NONBLOCK); 2187 ev_loop (w->other, EVLOOP_NONBLOCK);
2075} 2188}
2076 2189
2077static void 2190static void
2078embed_cb (EV_P_ ev_io *io, int revents) 2191embed_cb (EV_P_ ev_io *io, int revents)
2079{ 2192{
2090{ 2203{
2091 if (expect_false (ev_is_active (w))) 2204 if (expect_false (ev_is_active (w)))
2092 return; 2205 return;
2093 2206
2094 { 2207 {
2095 struct ev_loop *loop = w->loop; 2208 struct ev_loop *loop = w->other;
2096 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); 2209 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
2097 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); 2210 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ);
2098 } 2211 }
2099 2212
2100 ev_set_priority (&w->io, ev_priority (w)); 2213 ev_set_priority (&w->io, ev_priority (w));
2104} 2217}
2105 2218
2106void 2219void
2107ev_embed_stop (EV_P_ ev_embed *w) 2220ev_embed_stop (EV_P_ ev_embed *w)
2108{ 2221{
2109 ev_clear_pending (EV_A_ (W)w); 2222 clear_pending (EV_A_ (W)w);
2110 if (expect_false (!ev_is_active (w))) 2223 if (expect_false (!ev_is_active (w)))
2111 return; 2224 return;
2112 2225
2113 ev_io_stop (EV_A_ &w->io); 2226 ev_io_stop (EV_A_ &w->io);
2114 2227
2129} 2242}
2130 2243
2131void 2244void
2132ev_fork_stop (EV_P_ ev_fork *w) 2245ev_fork_stop (EV_P_ ev_fork *w)
2133{ 2246{
2134 ev_clear_pending (EV_A_ (W)w); 2247 clear_pending (EV_A_ (W)w);
2135 if (expect_false (!ev_is_active (w))) 2248 if (expect_false (!ev_is_active (w)))
2136 return; 2249 return;
2137 2250
2138 { 2251 {
2139 int active = ((W)w)->active; 2252 int active = ((W)w)->active;
2207 ev_timer_set (&once->to, timeout, 0.); 2320 ev_timer_set (&once->to, timeout, 0.);
2208 ev_timer_start (EV_A_ &once->to); 2321 ev_timer_start (EV_A_ &once->to);
2209 } 2322 }
2210} 2323}
2211 2324
2325#if EV_MULTIPLICITY
2326 #include "ev_wrap.h"
2327#endif
2328
2212#ifdef __cplusplus 2329#ifdef __cplusplus
2213} 2330}
2214#endif 2331#endif
2215 2332

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines