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

Comparing libev/ev.c (file contents):
Revision 1.148 by root, Tue Nov 27 11:11:13 2007 UTC vs.
Revision 1.167 by root, Sat Dec 8 04:02:31 2007 UTC

94# else 94# else
95# define EV_USE_PORT 0 95# define EV_USE_PORT 0
96# endif 96# endif
97# endif 97# endif
98 98
99# ifndef EV_USE_INOTIFY
100# if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H
101# define EV_USE_INOTIFY 1
102# else
103# define EV_USE_INOTIFY 0
104# endif
105# endif
106
99#endif 107#endif
100 108
101#include <math.h> 109#include <math.h>
102#include <stdlib.h> 110#include <stdlib.h>
103#include <fcntl.h> 111#include <fcntl.h>
109#include <errno.h> 117#include <errno.h>
110#include <sys/types.h> 118#include <sys/types.h>
111#include <time.h> 119#include <time.h>
112 120
113#include <signal.h> 121#include <signal.h>
122
123#ifdef EV_H
124# include EV_H
125#else
126# include "ev.h"
127#endif
114 128
115#ifndef _WIN32 129#ifndef _WIN32
116# include <sys/time.h> 130# include <sys/time.h>
117# include <sys/wait.h> 131# include <sys/wait.h>
118# include <unistd.h> 132# include <unistd.h>
156 170
157#ifndef EV_USE_PORT 171#ifndef EV_USE_PORT
158# define EV_USE_PORT 0 172# define EV_USE_PORT 0
159#endif 173#endif
160 174
175#ifndef EV_USE_INOTIFY
176# define EV_USE_INOTIFY 0
177#endif
178
179#ifndef EV_PID_HASHSIZE
180# if EV_MINIMAL
181# define EV_PID_HASHSIZE 1
182# else
183# define EV_PID_HASHSIZE 16
184# endif
185#endif
186
187#ifndef EV_INOTIFY_HASHSIZE
188# if EV_MINIMAL
189# define EV_INOTIFY_HASHSIZE 1
190# else
191# define EV_INOTIFY_HASHSIZE 16
192# endif
193#endif
194
161/**/ 195/**/
162 196
163#ifndef CLOCK_MONOTONIC 197#ifndef CLOCK_MONOTONIC
164# undef EV_USE_MONOTONIC 198# undef EV_USE_MONOTONIC
165# define EV_USE_MONOTONIC 0 199# define EV_USE_MONOTONIC 0
172 206
173#if EV_SELECT_IS_WINSOCKET 207#if EV_SELECT_IS_WINSOCKET
174# include <winsock.h> 208# include <winsock.h>
175#endif 209#endif
176 210
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
177/**/ 219/**/
178 220
179#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 221#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) */ 222#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 */ 223/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
183
184#ifdef EV_H
185# include EV_H
186#else
187# include "ev.h"
188#endif
189 224
190#if __GNUC__ >= 3 225#if __GNUC__ >= 3
191# define expect(expr,value) __builtin_expect ((expr),(value)) 226# define expect(expr,value) __builtin_expect ((expr),(value))
192# define inline_size static inline /* inline for codesize */ 227# define inline_size static inline /* inline for codesize */
193# if EV_MINIMAL 228# if EV_MINIMAL
206 241
207#define expect_false(expr) expect ((expr) != 0, 0) 242#define expect_false(expr) expect ((expr) != 0, 0)
208#define expect_true(expr) expect ((expr) != 0, 1) 243#define expect_true(expr) expect ((expr) != 0, 1)
209 244
210#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 245#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
211#define ABSPRI(w) ((w)->priority - EV_MINPRI) 246#define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
212 247
213#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 248#define EMPTY /* required for microsofts broken pseudo-c compiler */
214#define EMPTY2(a,b) /* used to suppress some warnings */ 249#define EMPTY2(a,b) /* used to suppress some warnings */
215 250
216typedef ev_watcher *W; 251typedef ev_watcher *W;
217typedef ev_watcher_list *WL; 252typedef ev_watcher_list *WL;
218typedef ev_watcher_time *WT; 253typedef ev_watcher_time *WT;
254ev_set_allocator (void *(*cb)(void *ptr, long size)) 289ev_set_allocator (void *(*cb)(void *ptr, long size))
255{ 290{
256 alloc = cb; 291 alloc = cb;
257} 292}
258 293
259static void * 294inline_speed void *
260ev_realloc (void *ptr, long size) 295ev_realloc (void *ptr, long size)
261{ 296{
262 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); 297 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
263 298
264 if (!ptr && size) 299 if (!ptr && size)
288typedef struct 323typedef struct
289{ 324{
290 W w; 325 W w;
291 int events; 326 int events;
292} ANPENDING; 327} ANPENDING;
328
329#if EV_USE_INOTIFY
330typedef struct
331{
332 WL head;
333} ANFS;
334#endif
293 335
294#if EV_MULTIPLICITY 336#if EV_MULTIPLICITY
295 337
296 struct ev_loop 338 struct ev_loop
297 { 339 {
354{ 396{
355 return ev_rt_now; 397 return ev_rt_now;
356} 398}
357#endif 399#endif
358 400
359#define array_roundsize(type,n) (((n) | 4) & ~3) 401int inline_size
402array_nextsize (int elem, int cur, int cnt)
403{
404 int ncur = cur + 1;
405
406 do
407 ncur <<= 1;
408 while (cnt > ncur);
409
410 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */
411 if (elem * ncur > 4096)
412 {
413 ncur *= elem;
414 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095;
415 ncur = ncur - sizeof (void *) * 4;
416 ncur /= elem;
417 }
418
419 return ncur;
420}
421
422inline_speed void *
423array_realloc (int elem, void *base, int *cur, int cnt)
424{
425 *cur = array_nextsize (elem, *cur, cnt);
426 return ev_realloc (base, elem * *cur);
427}
360 428
361#define array_needsize(type,base,cur,cnt,init) \ 429#define array_needsize(type,base,cur,cnt,init) \
362 if (expect_false ((cnt) > cur)) \ 430 if (expect_false ((cnt) > (cur))) \
363 { \ 431 { \
364 int newcnt = cur; \ 432 int ocur_ = (cur); \
365 do \ 433 (base) = (type *)array_realloc \
366 { \ 434 (sizeof (type), (base), &(cur), (cnt)); \
367 newcnt = array_roundsize (type, newcnt << 1); \ 435 init ((base) + (ocur_), (cur) - ocur_); \
368 } \
369 while ((cnt) > newcnt); \
370 \
371 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
372 init (base + cur, newcnt - cur); \
373 cur = newcnt; \
374 } 436 }
375 437
438#if 0
376#define array_slim(type,stem) \ 439#define array_slim(type,stem) \
377 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 440 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
378 { \ 441 { \
379 stem ## max = array_roundsize (stem ## cnt >> 1); \ 442 stem ## max = array_roundsize (stem ## cnt >> 1); \
380 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 443 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
381 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 444 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
382 } 445 }
446#endif
383 447
384#define array_free(stem, idx) \ 448#define array_free(stem, idx) \
385 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 449 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
386 450
387/*****************************************************************************/ 451/*****************************************************************************/
547static void noinline 611static void noinline
548fd_rearm_all (EV_P) 612fd_rearm_all (EV_P)
549{ 613{
550 int fd; 614 int fd;
551 615
552 /* this should be highly optimised to not do anything but set a flag */
553 for (fd = 0; fd < anfdmax; ++fd) 616 for (fd = 0; fd < anfdmax; ++fd)
554 if (anfds [fd].events) 617 if (anfds [fd].events)
555 { 618 {
556 anfds [fd].events = 0; 619 anfds [fd].events = 0;
557 fd_change (EV_A_ fd); 620 fd_change (EV_A_ fd);
709 ev_unref (EV_A); /* child watcher should not keep loop alive */ 772 ev_unref (EV_A); /* child watcher should not keep loop alive */
710} 773}
711 774
712/*****************************************************************************/ 775/*****************************************************************************/
713 776
714static ev_child *childs [PID_HASHSIZE]; 777static ev_child *childs [EV_PID_HASHSIZE];
715 778
716#ifndef _WIN32 779#ifndef _WIN32
717 780
718static ev_signal childev; 781static ev_signal childev;
719 782
720void inline_speed 783void inline_speed
721child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) 784child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status)
722{ 785{
723 ev_child *w; 786 ev_child *w;
724 787
725 for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 788 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
726 if (w->pid == pid || !w->pid) 789 if (w->pid == pid || !w->pid)
727 { 790 {
728 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 791 ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */
729 w->rpid = pid; 792 w->rpid = pid;
730 w->rstatus = status; 793 w->rstatus = status;
731 ev_feed_event (EV_A_ (W)w, EV_CHILD); 794 ev_feed_event (EV_A_ (W)w, EV_CHILD);
732 } 795 }
733} 796}
734 797
735#ifndef WCONTINUED 798#ifndef WCONTINUED
751 /* make sure we are called again until all childs have been reaped */ 814 /* make sure we are called again until all childs have been reaped */
752 /* we need to do it this way so that the callback gets called before we continue */ 815 /* we need to do it this way so that the callback gets called before we continue */
753 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); 816 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
754 817
755 child_reap (EV_A_ sw, pid, pid, status); 818 child_reap (EV_A_ sw, pid, pid, status);
819 if (EV_PID_HASHSIZE > 1)
756 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ 820 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
757} 821}
758 822
759#endif 823#endif
760 824
761/*****************************************************************************/ 825/*****************************************************************************/
844ev_backend (EV_P) 908ev_backend (EV_P)
845{ 909{
846 return backend; 910 return backend;
847} 911}
848 912
849static void 913unsigned int
914ev_loop_count (EV_P)
915{
916 return loop_count;
917}
918
919static void noinline
850loop_init (EV_P_ unsigned int flags) 920loop_init (EV_P_ unsigned int flags)
851{ 921{
852 if (!backend) 922 if (!backend)
853 { 923 {
854#if EV_USE_MONOTONIC 924#if EV_USE_MONOTONIC
862 ev_rt_now = ev_time (); 932 ev_rt_now = ev_time ();
863 mn_now = get_clock (); 933 mn_now = get_clock ();
864 now_floor = mn_now; 934 now_floor = mn_now;
865 rtmn_diff = ev_rt_now - mn_now; 935 rtmn_diff = ev_rt_now - mn_now;
866 936
937 /* pid check not overridable via env */
938#ifndef _WIN32
939 if (flags & EVFLAG_FORKCHECK)
940 curpid = getpid ();
941#endif
942
867 if (!(flags & EVFLAG_NOENV) 943 if (!(flags & EVFLAG_NOENV)
868 && !enable_secure () 944 && !enable_secure ()
869 && getenv ("LIBEV_FLAGS")) 945 && getenv ("LIBEV_FLAGS"))
870 flags = atoi (getenv ("LIBEV_FLAGS")); 946 flags = atoi (getenv ("LIBEV_FLAGS"));
871 947
872 if (!(flags & 0x0000ffffUL)) 948 if (!(flags & 0x0000ffffUL))
873 flags |= ev_recommended_backends (); 949 flags |= ev_recommended_backends ();
874 950
875 backend = 0; 951 backend = 0;
952 backend_fd = -1;
953#if EV_USE_INOTIFY
954 fs_fd = -2;
955#endif
956
876#if EV_USE_PORT 957#if EV_USE_PORT
877 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 958 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
878#endif 959#endif
879#if EV_USE_KQUEUE 960#if EV_USE_KQUEUE
880 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags); 961 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
892 ev_init (&sigev, sigcb); 973 ev_init (&sigev, sigcb);
893 ev_set_priority (&sigev, EV_MAXPRI); 974 ev_set_priority (&sigev, EV_MAXPRI);
894 } 975 }
895} 976}
896 977
897static void 978static void noinline
898loop_destroy (EV_P) 979loop_destroy (EV_P)
899{ 980{
900 int i; 981 int i;
982
983#if EV_USE_INOTIFY
984 if (fs_fd >= 0)
985 close (fs_fd);
986#endif
987
988 if (backend_fd >= 0)
989 close (backend_fd);
901 990
902#if EV_USE_PORT 991#if EV_USE_PORT
903 if (backend == EVBACKEND_PORT ) port_destroy (EV_A); 992 if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
904#endif 993#endif
905#if EV_USE_KQUEUE 994#if EV_USE_KQUEUE
914#if EV_USE_SELECT 1003#if EV_USE_SELECT
915 if (backend == EVBACKEND_SELECT) select_destroy (EV_A); 1004 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
916#endif 1005#endif
917 1006
918 for (i = NUMPRI; i--; ) 1007 for (i = NUMPRI; i--; )
1008 {
919 array_free (pending, [i]); 1009 array_free (pending, [i]);
1010#if EV_IDLE_ENABLE
1011 array_free (idle, [i]);
1012#endif
1013 }
920 1014
921 /* have to use the microsoft-never-gets-it-right macro */ 1015 /* have to use the microsoft-never-gets-it-right macro */
922 array_free (fdchange, EMPTY0); 1016 array_free (fdchange, EMPTY);
923 array_free (timer, EMPTY0); 1017 array_free (timer, EMPTY);
924#if EV_PERIODIC_ENABLE 1018#if EV_PERIODIC_ENABLE
925 array_free (periodic, EMPTY0); 1019 array_free (periodic, EMPTY);
926#endif 1020#endif
927 array_free (idle, EMPTY0);
928 array_free (prepare, EMPTY0); 1021 array_free (prepare, EMPTY);
929 array_free (check, EMPTY0); 1022 array_free (check, EMPTY);
930 1023
931 backend = 0; 1024 backend = 0;
932} 1025}
933 1026
934static void 1027void inline_size infy_fork (EV_P);
1028
1029void inline_size
935loop_fork (EV_P) 1030loop_fork (EV_P)
936{ 1031{
937#if EV_USE_PORT 1032#if EV_USE_PORT
938 if (backend == EVBACKEND_PORT ) port_fork (EV_A); 1033 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
939#endif 1034#endif
940#if EV_USE_KQUEUE 1035#if EV_USE_KQUEUE
941 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A); 1036 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
942#endif 1037#endif
943#if EV_USE_EPOLL 1038#if EV_USE_EPOLL
944 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); 1039 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
1040#endif
1041#if EV_USE_INOTIFY
1042 infy_fork (EV_A);
945#endif 1043#endif
946 1044
947 if (ev_is_active (&sigev)) 1045 if (ev_is_active (&sigev))
948 { 1046 {
949 /* default loop */ 1047 /* default loop */
1065 postfork = 1; 1163 postfork = 1;
1066} 1164}
1067 1165
1068/*****************************************************************************/ 1166/*****************************************************************************/
1069 1167
1070int inline_size
1071any_pending (EV_P)
1072{
1073 int pri;
1074
1075 for (pri = NUMPRI; pri--; )
1076 if (pendingcnt [pri])
1077 return 1;
1078
1079 return 0;
1080}
1081
1082void inline_speed 1168void inline_speed
1083call_pending (EV_P) 1169call_pending (EV_P)
1084{ 1170{
1085 int pri; 1171 int pri;
1086 1172
1089 { 1175 {
1090 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1176 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1091 1177
1092 if (expect_true (p->w)) 1178 if (expect_true (p->w))
1093 { 1179 {
1094 assert (("non-pending watcher on pending list", p->w->pending)); 1180 /*assert (("non-pending watcher on pending list", p->w->pending));*/
1095 1181
1096 p->w->pending = 0; 1182 p->w->pending = 0;
1097 EV_CB_INVOKE (p->w, p->events); 1183 EV_CB_INVOKE (p->w, p->events);
1098 } 1184 }
1099 } 1185 }
1104{ 1190{
1105 while (timercnt && ((WT)timers [0])->at <= mn_now) 1191 while (timercnt && ((WT)timers [0])->at <= mn_now)
1106 { 1192 {
1107 ev_timer *w = timers [0]; 1193 ev_timer *w = timers [0];
1108 1194
1109 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1195 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1110 1196
1111 /* first reschedule or stop timer */ 1197 /* first reschedule or stop timer */
1112 if (w->repeat) 1198 if (w->repeat)
1113 { 1199 {
1114 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 1200 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1132{ 1218{
1133 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1219 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1134 { 1220 {
1135 ev_periodic *w = periodics [0]; 1221 ev_periodic *w = periodics [0];
1136 1222
1137 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1223 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1138 1224
1139 /* first reschedule or stop timer */ 1225 /* first reschedule or stop timer */
1140 if (w->reschedule_cb) 1226 if (w->reschedule_cb)
1141 { 1227 {
1142 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); 1228 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
1176 for (i = periodiccnt >> 1; i--; ) 1262 for (i = periodiccnt >> 1; i--; )
1177 downheap ((WT *)periodics, periodiccnt, i); 1263 downheap ((WT *)periodics, periodiccnt, i);
1178} 1264}
1179#endif 1265#endif
1180 1266
1267#if EV_IDLE_ENABLE
1268void inline_size
1269idle_reify (EV_P)
1270{
1271 if (expect_false (idleall))
1272 {
1273 int pri;
1274
1275 for (pri = NUMPRI; pri--; )
1276 {
1277 if (pendingcnt [pri])
1278 break;
1279
1280 if (idlecnt [pri])
1281 {
1282 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1283 break;
1284 }
1285 }
1286 }
1287}
1288#endif
1289
1181int inline_size 1290int inline_size
1182time_update_monotonic (EV_P) 1291time_update_monotonic (EV_P)
1183{ 1292{
1184 mn_now = get_clock (); 1293 mn_now = get_clock ();
1185 1294
1209 ev_tstamp odiff = rtmn_diff; 1318 ev_tstamp odiff = rtmn_diff;
1210 1319
1211 /* loop a few times, before making important decisions. 1320 /* loop a few times, before making important decisions.
1212 * on the choice of "4": one iteration isn't enough, 1321 * on the choice of "4": one iteration isn't enough,
1213 * in case we get preempted during the calls to 1322 * in case we get preempted during the calls to
1214 * ev_time and get_clock. a second call is almost guarenteed 1323 * ev_time and get_clock. a second call is almost guaranteed
1215 * to succeed in that case, though. and looping a few more times 1324 * to succeed in that case, though. and looping a few more times
1216 * doesn't hurt either as we only do this on time-jumps or 1325 * doesn't hurt either as we only do this on time-jumps or
1217 * in the unlikely event of getting preempted here. 1326 * in the unlikely event of having been preempted here.
1218 */ 1327 */
1219 for (i = 4; --i; ) 1328 for (i = 4; --i; )
1220 { 1329 {
1221 rtmn_diff = ev_rt_now - mn_now; 1330 rtmn_diff = ev_rt_now - mn_now;
1222 1331
1244 { 1353 {
1245#if EV_PERIODIC_ENABLE 1354#if EV_PERIODIC_ENABLE
1246 periodics_reschedule (EV_A); 1355 periodics_reschedule (EV_A);
1247#endif 1356#endif
1248 1357
1249 /* adjust timers. this is easy, as the offset is the same for all */ 1358 /* adjust timers. this is easy, as the offset is the same for all of them */
1250 for (i = 0; i < timercnt; ++i) 1359 for (i = 0; i < timercnt; ++i)
1251 ((WT)timers [i])->at += ev_rt_now - mn_now; 1360 ((WT)timers [i])->at += ev_rt_now - mn_now;
1252 } 1361 }
1253 1362
1254 mn_now = ev_rt_now; 1363 mn_now = ev_rt_now;
1274{ 1383{
1275 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) 1384 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1276 ? EVUNLOOP_ONE 1385 ? EVUNLOOP_ONE
1277 : EVUNLOOP_CANCEL; 1386 : EVUNLOOP_CANCEL;
1278 1387
1279 while (activecnt) 1388 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1389
1390 do
1280 { 1391 {
1281 /* we might have forked, so reify kernel state if necessary */ 1392#ifndef _WIN32
1393 if (expect_false (curpid)) /* penalise the forking check even more */
1394 if (expect_false (getpid () != curpid))
1395 {
1396 curpid = getpid ();
1397 postfork = 1;
1398 }
1399#endif
1400
1282 #if EV_FORK_ENABLE 1401#if EV_FORK_ENABLE
1402 /* we might have forked, so queue fork handlers */
1283 if (expect_false (postfork)) 1403 if (expect_false (postfork))
1284 if (forkcnt) 1404 if (forkcnt)
1285 { 1405 {
1286 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 1406 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1287 call_pending (EV_A); 1407 call_pending (EV_A);
1288 } 1408 }
1289 #endif 1409#endif
1290 1410
1291 /* queue check watchers (and execute them) */ 1411 /* queue check watchers (and execute them) */
1292 if (expect_false (preparecnt)) 1412 if (expect_false (preparecnt))
1293 { 1413 {
1294 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1414 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1295 call_pending (EV_A); 1415 call_pending (EV_A);
1296 } 1416 }
1297 1417
1418 if (expect_false (!activecnt))
1419 break;
1420
1298 /* we might have forked, so reify kernel state if necessary */ 1421 /* we might have forked, so reify kernel state if necessary */
1299 if (expect_false (postfork)) 1422 if (expect_false (postfork))
1300 loop_fork (EV_A); 1423 loop_fork (EV_A);
1301 1424
1302 /* update fd-related kernel structures */ 1425 /* update fd-related kernel structures */
1303 fd_reify (EV_A); 1426 fd_reify (EV_A);
1304 1427
1305 /* calculate blocking time */ 1428 /* calculate blocking time */
1306 { 1429 {
1307 double block; 1430 ev_tstamp block;
1308 1431
1309 if (flags & EVLOOP_NONBLOCK || idlecnt) 1432 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1310 block = 0.; /* do not block at all */ 1433 block = 0.; /* do not block at all */
1311 else 1434 else
1312 { 1435 {
1313 /* update time to cancel out callback processing overhead */ 1436 /* update time to cancel out callback processing overhead */
1314#if EV_USE_MONOTONIC 1437#if EV_USE_MONOTONIC
1338#endif 1461#endif
1339 1462
1340 if (expect_false (block < 0.)) block = 0.; 1463 if (expect_false (block < 0.)) block = 0.;
1341 } 1464 }
1342 1465
1466 ++loop_count;
1343 backend_poll (EV_A_ block); 1467 backend_poll (EV_A_ block);
1344 } 1468 }
1345 1469
1346 /* update ev_rt_now, do magic */ 1470 /* update ev_rt_now, do magic */
1347 time_update (EV_A); 1471 time_update (EV_A);
1350 timers_reify (EV_A); /* relative timers called last */ 1474 timers_reify (EV_A); /* relative timers called last */
1351#if EV_PERIODIC_ENABLE 1475#if EV_PERIODIC_ENABLE
1352 periodics_reify (EV_A); /* absolute timers called first */ 1476 periodics_reify (EV_A); /* absolute timers called first */
1353#endif 1477#endif
1354 1478
1479#if EV_IDLE_ENABLE
1355 /* queue idle watchers unless other events are pending */ 1480 /* queue idle watchers unless other events are pending */
1356 if (idlecnt && !any_pending (EV_A)) 1481 idle_reify (EV_A);
1357 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1482#endif
1358 1483
1359 /* queue check watchers, to be executed first */ 1484 /* queue check watchers, to be executed first */
1360 if (expect_false (checkcnt)) 1485 if (expect_false (checkcnt))
1361 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1486 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1362 1487
1363 call_pending (EV_A); 1488 call_pending (EV_A);
1364 1489
1365 if (expect_false (loop_done))
1366 break;
1367 } 1490 }
1491 while (expect_true (activecnt && !loop_done));
1368 1492
1369 if (loop_done == EVUNLOOP_ONE) 1493 if (loop_done == EVUNLOOP_ONE)
1370 loop_done = EVUNLOOP_CANCEL; 1494 loop_done = EVUNLOOP_CANCEL;
1371} 1495}
1372 1496
1399 head = &(*head)->next; 1523 head = &(*head)->next;
1400 } 1524 }
1401} 1525}
1402 1526
1403void inline_speed 1527void inline_speed
1404ev_clear_pending (EV_P_ W w) 1528clear_pending (EV_P_ W w)
1405{ 1529{
1406 if (w->pending) 1530 if (w->pending)
1407 { 1531 {
1408 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1532 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1409 w->pending = 0; 1533 w->pending = 0;
1410 } 1534 }
1411} 1535}
1412 1536
1537int
1538ev_clear_pending (EV_P_ void *w)
1539{
1540 W w_ = (W)w;
1541 int pending = w_->pending;
1542
1543 if (!pending)
1544 return 0;
1545
1546 w_->pending = 0;
1547 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
1548 p->w = 0;
1549
1550 return p->events;
1551}
1552
1553void inline_size
1554pri_adjust (EV_P_ W w)
1555{
1556 int pri = w->priority;
1557 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
1558 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
1559 w->priority = pri;
1560}
1561
1413void inline_speed 1562void inline_speed
1414ev_start (EV_P_ W w, int active) 1563ev_start (EV_P_ W w, int active)
1415{ 1564{
1416 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1565 pri_adjust (EV_A_ w);
1417 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1418
1419 w->active = active; 1566 w->active = active;
1420 ev_ref (EV_A); 1567 ev_ref (EV_A);
1421} 1568}
1422 1569
1423void inline_size 1570void inline_size
1447} 1594}
1448 1595
1449void 1596void
1450ev_io_stop (EV_P_ ev_io *w) 1597ev_io_stop (EV_P_ ev_io *w)
1451{ 1598{
1452 ev_clear_pending (EV_A_ (W)w); 1599 clear_pending (EV_A_ (W)w);
1453 if (expect_false (!ev_is_active (w))) 1600 if (expect_false (!ev_is_active (w)))
1454 return; 1601 return;
1455 1602
1456 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1603 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1457 1604
1474 ev_start (EV_A_ (W)w, ++timercnt); 1621 ev_start (EV_A_ (W)w, ++timercnt);
1475 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); 1622 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1476 timers [timercnt - 1] = w; 1623 timers [timercnt - 1] = w;
1477 upheap ((WT *)timers, timercnt - 1); 1624 upheap ((WT *)timers, timercnt - 1);
1478 1625
1626 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/
1627}
1628
1629void
1630ev_timer_stop (EV_P_ ev_timer *w)
1631{
1632 clear_pending (EV_A_ (W)w);
1633 if (expect_false (!ev_is_active (w)))
1634 return;
1635
1479 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1636 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1480}
1481 1637
1482void 1638 {
1483ev_timer_stop (EV_P_ ev_timer *w) 1639 int active = ((W)w)->active;
1484{
1485 ev_clear_pending (EV_A_ (W)w);
1486 if (expect_false (!ev_is_active (w)))
1487 return;
1488 1640
1489 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1490
1491 if (expect_true (((W)w)->active < timercnt--)) 1641 if (expect_true (--active < --timercnt))
1492 { 1642 {
1493 timers [((W)w)->active - 1] = timers [timercnt]; 1643 timers [active] = timers [timercnt];
1494 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1644 adjustheap ((WT *)timers, timercnt, active);
1495 } 1645 }
1646 }
1496 1647
1497 ((WT)w)->at -= mn_now; 1648 ((WT)w)->at -= mn_now;
1498 1649
1499 ev_stop (EV_A_ (W)w); 1650 ev_stop (EV_A_ (W)w);
1500} 1651}
1538 ev_start (EV_A_ (W)w, ++periodiccnt); 1689 ev_start (EV_A_ (W)w, ++periodiccnt);
1539 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1690 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1540 periodics [periodiccnt - 1] = w; 1691 periodics [periodiccnt - 1] = w;
1541 upheap ((WT *)periodics, periodiccnt - 1); 1692 upheap ((WT *)periodics, periodiccnt - 1);
1542 1693
1694 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/
1695}
1696
1697void
1698ev_periodic_stop (EV_P_ ev_periodic *w)
1699{
1700 clear_pending (EV_A_ (W)w);
1701 if (expect_false (!ev_is_active (w)))
1702 return;
1703
1543 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1704 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1544}
1545 1705
1546void 1706 {
1547ev_periodic_stop (EV_P_ ev_periodic *w) 1707 int active = ((W)w)->active;
1548{
1549 ev_clear_pending (EV_A_ (W)w);
1550 if (expect_false (!ev_is_active (w)))
1551 return;
1552 1708
1553 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1554
1555 if (expect_true (((W)w)->active < periodiccnt--)) 1709 if (expect_true (--active < --periodiccnt))
1556 { 1710 {
1557 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1711 periodics [active] = periodics [periodiccnt];
1558 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1712 adjustheap ((WT *)periodics, periodiccnt, active);
1559 } 1713 }
1714 }
1560 1715
1561 ev_stop (EV_A_ (W)w); 1716 ev_stop (EV_A_ (W)w);
1562} 1717}
1563 1718
1564void 1719void
1604} 1759}
1605 1760
1606void 1761void
1607ev_signal_stop (EV_P_ ev_signal *w) 1762ev_signal_stop (EV_P_ ev_signal *w)
1608{ 1763{
1609 ev_clear_pending (EV_A_ (W)w); 1764 clear_pending (EV_A_ (W)w);
1610 if (expect_false (!ev_is_active (w))) 1765 if (expect_false (!ev_is_active (w)))
1611 return; 1766 return;
1612 1767
1613 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1768 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1614 ev_stop (EV_A_ (W)w); 1769 ev_stop (EV_A_ (W)w);
1625#endif 1780#endif
1626 if (expect_false (ev_is_active (w))) 1781 if (expect_false (ev_is_active (w)))
1627 return; 1782 return;
1628 1783
1629 ev_start (EV_A_ (W)w, 1); 1784 ev_start (EV_A_ (W)w, 1);
1630 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1785 wlist_add ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1631} 1786}
1632 1787
1633void 1788void
1634ev_child_stop (EV_P_ ev_child *w) 1789ev_child_stop (EV_P_ ev_child *w)
1635{ 1790{
1636 ev_clear_pending (EV_A_ (W)w); 1791 clear_pending (EV_A_ (W)w);
1637 if (expect_false (!ev_is_active (w))) 1792 if (expect_false (!ev_is_active (w)))
1638 return; 1793 return;
1639 1794
1640 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1795 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1641 ev_stop (EV_A_ (W)w); 1796 ev_stop (EV_A_ (W)w);
1642} 1797}
1643 1798
1644#if EV_STAT_ENABLE 1799#if EV_STAT_ENABLE
1645 1800
1649# endif 1804# endif
1650 1805
1651#define DEF_STAT_INTERVAL 5.0074891 1806#define DEF_STAT_INTERVAL 5.0074891
1652#define MIN_STAT_INTERVAL 0.1074891 1807#define MIN_STAT_INTERVAL 0.1074891
1653 1808
1809static void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents);
1810
1811#if EV_USE_INOTIFY
1812# define EV_INOTIFY_BUFSIZE 8192
1813
1814static void noinline
1815infy_add (EV_P_ ev_stat *w)
1816{
1817 w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD);
1818
1819 if (w->wd < 0)
1820 {
1821 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
1822
1823 /* monitor some parent directory for speedup hints */
1824 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
1825 {
1826 char path [4096];
1827 strcpy (path, w->path);
1828
1829 do
1830 {
1831 int mask = IN_MASK_ADD | IN_DELETE_SELF | IN_MOVE_SELF
1832 | (errno == EACCES ? IN_ATTRIB : IN_CREATE | IN_MOVED_TO);
1833
1834 char *pend = strrchr (path, '/');
1835
1836 if (!pend)
1837 break; /* whoops, no '/', complain to your admin */
1838
1839 *pend = 0;
1840 w->wd = inotify_add_watch (fs_fd, path, mask);
1841 }
1842 while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
1843 }
1844 }
1845 else
1846 ev_timer_stop (EV_A_ &w->timer); /* we can watch this in a race-free way */
1847
1848 if (w->wd >= 0)
1849 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w);
1850}
1851
1852static void noinline
1853infy_del (EV_P_ ev_stat *w)
1854{
1855 int slot;
1856 int wd = w->wd;
1857
1858 if (wd < 0)
1859 return;
1860
1861 w->wd = -2;
1862 slot = wd & (EV_INOTIFY_HASHSIZE - 1);
1863 wlist_del (&fs_hash [slot].head, (WL)w);
1864
1865 /* remove this watcher, if others are watching it, they will rearm */
1866 inotify_rm_watch (fs_fd, wd);
1867}
1868
1869static void noinline
1870infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev)
1871{
1872 if (slot < 0)
1873 /* overflow, need to check for all hahs slots */
1874 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
1875 infy_wd (EV_A_ slot, wd, ev);
1876 else
1877 {
1878 WL w_;
1879
1880 for (w_ = fs_hash [slot & (EV_INOTIFY_HASHSIZE - 1)].head; w_; )
1881 {
1882 ev_stat *w = (ev_stat *)w_;
1883 w_ = w_->next; /* lets us remove this watcher and all before it */
1884
1885 if (w->wd == wd || wd == -1)
1886 {
1887 if (ev->mask & (IN_IGNORED | IN_UNMOUNT | IN_DELETE_SELF))
1888 {
1889 w->wd = -1;
1890 infy_add (EV_A_ w); /* re-add, no matter what */
1891 }
1892
1893 stat_timer_cb (EV_A_ &w->timer, 0);
1894 }
1895 }
1896 }
1897}
1898
1899static void
1900infy_cb (EV_P_ ev_io *w, int revents)
1901{
1902 char buf [EV_INOTIFY_BUFSIZE];
1903 struct inotify_event *ev = (struct inotify_event *)buf;
1904 int ofs;
1905 int len = read (fs_fd, buf, sizeof (buf));
1906
1907 for (ofs = 0; ofs < len; ofs += sizeof (struct inotify_event) + ev->len)
1908 infy_wd (EV_A_ ev->wd, ev->wd, ev);
1909}
1910
1911void inline_size
1912infy_init (EV_P)
1913{
1914 if (fs_fd != -2)
1915 return;
1916
1917 fs_fd = inotify_init ();
1918
1919 if (fs_fd >= 0)
1920 {
1921 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ);
1922 ev_set_priority (&fs_w, EV_MAXPRI);
1923 ev_io_start (EV_A_ &fs_w);
1924 }
1925}
1926
1927void inline_size
1928infy_fork (EV_P)
1929{
1930 int slot;
1931
1932 if (fs_fd < 0)
1933 return;
1934
1935 close (fs_fd);
1936 fs_fd = inotify_init ();
1937
1938 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
1939 {
1940 WL w_ = fs_hash [slot].head;
1941 fs_hash [slot].head = 0;
1942
1943 while (w_)
1944 {
1945 ev_stat *w = (ev_stat *)w_;
1946 w_ = w_->next; /* lets us add this watcher */
1947
1948 w->wd = -1;
1949
1950 if (fs_fd >= 0)
1951 infy_add (EV_A_ w); /* re-add, no matter what */
1952 else
1953 ev_timer_start (EV_A_ &w->timer);
1954 }
1955
1956 }
1957}
1958
1959#endif
1960
1654void 1961void
1655ev_stat_stat (EV_P_ ev_stat *w) 1962ev_stat_stat (EV_P_ ev_stat *w)
1656{ 1963{
1657 if (lstat (w->path, &w->attr) < 0) 1964 if (lstat (w->path, &w->attr) < 0)
1658 w->attr.st_nlink = 0; 1965 w->attr.st_nlink = 0;
1659 else if (!w->attr.st_nlink) 1966 else if (!w->attr.st_nlink)
1660 w->attr.st_nlink = 1; 1967 w->attr.st_nlink = 1;
1661} 1968}
1662 1969
1663static void 1970static void noinline
1664stat_timer_cb (EV_P_ ev_timer *w_, int revents) 1971stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1665{ 1972{
1666 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); 1973 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1667 1974
1668 /* we copy this here each the time so that */ 1975 /* we copy this here each the time so that */
1669 /* prev has the old value when the callback gets invoked */ 1976 /* prev has the old value when the callback gets invoked */
1670 w->prev = w->attr; 1977 w->prev = w->attr;
1671 ev_stat_stat (EV_A_ w); 1978 ev_stat_stat (EV_A_ w);
1672 1979
1673 if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata))) 1980 /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */
1981 if (
1982 w->prev.st_dev != w->attr.st_dev
1983 || w->prev.st_ino != w->attr.st_ino
1984 || w->prev.st_mode != w->attr.st_mode
1985 || w->prev.st_nlink != w->attr.st_nlink
1986 || w->prev.st_uid != w->attr.st_uid
1987 || w->prev.st_gid != w->attr.st_gid
1988 || w->prev.st_rdev != w->attr.st_rdev
1989 || w->prev.st_size != w->attr.st_size
1990 || w->prev.st_atime != w->attr.st_atime
1991 || w->prev.st_mtime != w->attr.st_mtime
1992 || w->prev.st_ctime != w->attr.st_ctime
1993 ) {
1994 #if EV_USE_INOTIFY
1995 infy_del (EV_A_ w);
1996 infy_add (EV_A_ w);
1997 ev_stat_stat (EV_A_ w); /* avoid race... */
1998 #endif
1999
1674 ev_feed_event (EV_A_ w, EV_STAT); 2000 ev_feed_event (EV_A_ w, EV_STAT);
2001 }
1675} 2002}
1676 2003
1677void 2004void
1678ev_stat_start (EV_P_ ev_stat *w) 2005ev_stat_start (EV_P_ ev_stat *w)
1679{ 2006{
1689 if (w->interval < MIN_STAT_INTERVAL) 2016 if (w->interval < MIN_STAT_INTERVAL)
1690 w->interval = w->interval ? MIN_STAT_INTERVAL : DEF_STAT_INTERVAL; 2017 w->interval = w->interval ? MIN_STAT_INTERVAL : DEF_STAT_INTERVAL;
1691 2018
1692 ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval); 2019 ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval);
1693 ev_set_priority (&w->timer, ev_priority (w)); 2020 ev_set_priority (&w->timer, ev_priority (w));
2021
2022#if EV_USE_INOTIFY
2023 infy_init (EV_A);
2024
2025 if (fs_fd >= 0)
2026 infy_add (EV_A_ w);
2027 else
2028#endif
1694 ev_timer_start (EV_A_ &w->timer); 2029 ev_timer_start (EV_A_ &w->timer);
1695 2030
1696 ev_start (EV_A_ (W)w, 1); 2031 ev_start (EV_A_ (W)w, 1);
1697} 2032}
1698 2033
1699void 2034void
1700ev_stat_stop (EV_P_ ev_stat *w) 2035ev_stat_stop (EV_P_ ev_stat *w)
1701{ 2036{
1702 ev_clear_pending (EV_A_ (W)w); 2037 clear_pending (EV_A_ (W)w);
1703 if (expect_false (!ev_is_active (w))) 2038 if (expect_false (!ev_is_active (w)))
1704 return; 2039 return;
1705 2040
2041#if EV_USE_INOTIFY
2042 infy_del (EV_A_ w);
2043#endif
1706 ev_timer_stop (EV_A_ &w->timer); 2044 ev_timer_stop (EV_A_ &w->timer);
1707 2045
1708 ev_stop (EV_A_ (W)w); 2046 ev_stop (EV_A_ (W)w);
1709} 2047}
1710#endif 2048#endif
1711 2049
2050#if EV_IDLE_ENABLE
1712void 2051void
1713ev_idle_start (EV_P_ ev_idle *w) 2052ev_idle_start (EV_P_ ev_idle *w)
1714{ 2053{
1715 if (expect_false (ev_is_active (w))) 2054 if (expect_false (ev_is_active (w)))
1716 return; 2055 return;
1717 2056
2057 pri_adjust (EV_A_ (W)w);
2058
2059 {
2060 int active = ++idlecnt [ABSPRI (w)];
2061
2062 ++idleall;
1718 ev_start (EV_A_ (W)w, ++idlecnt); 2063 ev_start (EV_A_ (W)w, active);
2064
1719 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); 2065 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
1720 idles [idlecnt - 1] = w; 2066 idles [ABSPRI (w)][active - 1] = w;
2067 }
1721} 2068}
1722 2069
1723void 2070void
1724ev_idle_stop (EV_P_ ev_idle *w) 2071ev_idle_stop (EV_P_ ev_idle *w)
1725{ 2072{
1726 ev_clear_pending (EV_A_ (W)w); 2073 clear_pending (EV_A_ (W)w);
1727 if (expect_false (!ev_is_active (w))) 2074 if (expect_false (!ev_is_active (w)))
1728 return; 2075 return;
1729 2076
1730 { 2077 {
1731 int active = ((W)w)->active; 2078 int active = ((W)w)->active;
1732 idles [active - 1] = idles [--idlecnt]; 2079
2080 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
1733 ((W)idles [active - 1])->active = active; 2081 ((W)idles [ABSPRI (w)][active - 1])->active = active;
2082
2083 ev_stop (EV_A_ (W)w);
2084 --idleall;
1734 } 2085 }
1735
1736 ev_stop (EV_A_ (W)w);
1737} 2086}
2087#endif
1738 2088
1739void 2089void
1740ev_prepare_start (EV_P_ ev_prepare *w) 2090ev_prepare_start (EV_P_ ev_prepare *w)
1741{ 2091{
1742 if (expect_false (ev_is_active (w))) 2092 if (expect_false (ev_is_active (w)))
1748} 2098}
1749 2099
1750void 2100void
1751ev_prepare_stop (EV_P_ ev_prepare *w) 2101ev_prepare_stop (EV_P_ ev_prepare *w)
1752{ 2102{
1753 ev_clear_pending (EV_A_ (W)w); 2103 clear_pending (EV_A_ (W)w);
1754 if (expect_false (!ev_is_active (w))) 2104 if (expect_false (!ev_is_active (w)))
1755 return; 2105 return;
1756 2106
1757 { 2107 {
1758 int active = ((W)w)->active; 2108 int active = ((W)w)->active;
1775} 2125}
1776 2126
1777void 2127void
1778ev_check_stop (EV_P_ ev_check *w) 2128ev_check_stop (EV_P_ ev_check *w)
1779{ 2129{
1780 ev_clear_pending (EV_A_ (W)w); 2130 clear_pending (EV_A_ (W)w);
1781 if (expect_false (!ev_is_active (w))) 2131 if (expect_false (!ev_is_active (w)))
1782 return; 2132 return;
1783 2133
1784 { 2134 {
1785 int active = ((W)w)->active; 2135 int active = ((W)w)->active;
1827} 2177}
1828 2178
1829void 2179void
1830ev_embed_stop (EV_P_ ev_embed *w) 2180ev_embed_stop (EV_P_ ev_embed *w)
1831{ 2181{
1832 ev_clear_pending (EV_A_ (W)w); 2182 clear_pending (EV_A_ (W)w);
1833 if (expect_false (!ev_is_active (w))) 2183 if (expect_false (!ev_is_active (w)))
1834 return; 2184 return;
1835 2185
1836 ev_io_stop (EV_A_ &w->io); 2186 ev_io_stop (EV_A_ &w->io);
1837 2187
1852} 2202}
1853 2203
1854void 2204void
1855ev_fork_stop (EV_P_ ev_fork *w) 2205ev_fork_stop (EV_P_ ev_fork *w)
1856{ 2206{
1857 ev_clear_pending (EV_A_ (W)w); 2207 clear_pending (EV_A_ (W)w);
1858 if (expect_false (!ev_is_active (w))) 2208 if (expect_false (!ev_is_active (w)))
1859 return; 2209 return;
1860 2210
1861 { 2211 {
1862 int active = ((W)w)->active; 2212 int active = ((W)w)->active;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines