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

Comparing libev/ev.c (file contents):
Revision 1.150 by root, Tue Nov 27 19:41:52 2007 UTC vs.
Revision 1.170 by root, Sat Dec 8 22:11:14 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
161#ifndef EV_PID_HASHSIZE 179#ifndef EV_PID_HASHSIZE
162# if EV_MINIMAL 180# if EV_MINIMAL
163# define EV_PID_HASHSIZE 1 181# define EV_PID_HASHSIZE 1
164# else 182# else
165# define EV_PID_HASHSIZE 16 183# define EV_PID_HASHSIZE 16
166# endif 184# endif
167#endif 185#endif
168 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
169/**/ 195/**/
170 196
171#ifndef CLOCK_MONOTONIC 197#ifndef CLOCK_MONOTONIC
172# undef EV_USE_MONOTONIC 198# undef EV_USE_MONOTONIC
173# define EV_USE_MONOTONIC 0 199# define EV_USE_MONOTONIC 0
180 206
181#if EV_SELECT_IS_WINSOCKET 207#if EV_SELECT_IS_WINSOCKET
182# include <winsock.h> 208# include <winsock.h>
183#endif 209#endif
184 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
185/**/ 219/**/
186 220
187#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) */
188#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) */
189/*#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 */
190 224
191#ifdef EV_H
192# include EV_H
193#else
194# include "ev.h"
195#endif
196
197#if __GNUC__ >= 3 225#if __GNUC__ >= 3
198# define expect(expr,value) __builtin_expect ((expr),(value)) 226# define expect(expr,value) __builtin_expect ((expr),(value))
199# define inline_size static inline /* inline for codesize */
200# if EV_MINIMAL
201# define noinline __attribute__ ((noinline)) 227# define noinline __attribute__ ((noinline))
202# define inline_speed static noinline
203# else
204# define noinline
205# define inline_speed static inline
206# endif
207#else 228#else
208# define expect(expr,value) (expr) 229# define expect(expr,value) (expr)
209# define inline_speed static
210# define inline_size static
211# define noinline 230# define noinline
231# if __STDC_VERSION__ < 199901L
232# define inline
233# endif
212#endif 234#endif
213 235
214#define expect_false(expr) expect ((expr) != 0, 0) 236#define expect_false(expr) expect ((expr) != 0, 0)
215#define expect_true(expr) expect ((expr) != 0, 1) 237#define expect_true(expr) expect ((expr) != 0, 1)
238#define inline_size static inline
239
240#if EV_MINIMAL
241# define inline_speed static noinline
242#else
243# define inline_speed static inline
244#endif
216 245
217#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 246#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
218#define ABSPRI(w) ((w)->priority - EV_MINPRI) 247#define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
219 248
220#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 249#define EMPTY /* required for microsofts broken pseudo-c compiler */
221#define EMPTY2(a,b) /* used to suppress some warnings */ 250#define EMPTY2(a,b) /* used to suppress some warnings */
222 251
223typedef ev_watcher *W; 252typedef ev_watcher *W;
224typedef ev_watcher_list *WL; 253typedef ev_watcher_list *WL;
225typedef ev_watcher_time *WT; 254typedef ev_watcher_time *WT;
253 perror (msg); 282 perror (msg);
254 abort (); 283 abort ();
255 } 284 }
256} 285}
257 286
258static void *(*alloc)(void *ptr, size_t size) = realloc; 287static void *(*alloc)(void *ptr, long size);
259 288
260void 289void
261ev_set_allocator (void *(*cb)(void *ptr, size_t size)) 290ev_set_allocator (void *(*cb)(void *ptr, long size))
262{ 291{
263 alloc = cb; 292 alloc = cb;
264} 293}
265 294
266inline_speed void * 295inline_speed void *
267ev_realloc (void *ptr, size_t size) 296ev_realloc (void *ptr, long size)
268{ 297{
269 ptr = alloc (ptr, size); 298 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
270 299
271 if (!ptr && size) 300 if (!ptr && size)
272 { 301 {
273 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", (long)size); 302 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
274 abort (); 303 abort ();
275 } 304 }
276 305
277 return ptr; 306 return ptr;
278} 307}
295typedef struct 324typedef struct
296{ 325{
297 W w; 326 W w;
298 int events; 327 int events;
299} ANPENDING; 328} ANPENDING;
329
330#if EV_USE_INOTIFY
331typedef struct
332{
333 WL head;
334} ANFS;
335#endif
300 336
301#if EV_MULTIPLICITY 337#if EV_MULTIPLICITY
302 338
303 struct ev_loop 339 struct ev_loop
304 { 340 {
361{ 397{
362 return ev_rt_now; 398 return ev_rt_now;
363} 399}
364#endif 400#endif
365 401
366#define array_roundsize(type,n) (((n) | 4) & ~3) 402int inline_size
403array_nextsize (int elem, int cur, int cnt)
404{
405 int ncur = cur + 1;
406
407 do
408 ncur <<= 1;
409 while (cnt > ncur);
410
411 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */
412 if (elem * ncur > 4096)
413 {
414 ncur *= elem;
415 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095;
416 ncur = ncur - sizeof (void *) * 4;
417 ncur /= elem;
418 }
419
420 return ncur;
421}
422
423inline_speed void *
424array_realloc (int elem, void *base, int *cur, int cnt)
425{
426 *cur = array_nextsize (elem, *cur, cnt);
427 return ev_realloc (base, elem * *cur);
428}
367 429
368#define array_needsize(type,base,cur,cnt,init) \ 430#define array_needsize(type,base,cur,cnt,init) \
369 if (expect_false ((cnt) > cur)) \ 431 if (expect_false ((cnt) > (cur))) \
370 { \ 432 { \
371 int newcnt = cur; \ 433 int ocur_ = (cur); \
372 do \ 434 (base) = (type *)array_realloc \
373 { \ 435 (sizeof (type), (base), &(cur), (cnt)); \
374 newcnt = array_roundsize (type, newcnt << 1); \ 436 init ((base) + (ocur_), (cur) - ocur_); \
375 } \
376 while ((cnt) > newcnt); \
377 \
378 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
379 init (base + cur, newcnt - cur); \
380 cur = newcnt; \
381 } 437 }
382 438
439#if 0
383#define array_slim(type,stem) \ 440#define array_slim(type,stem) \
384 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 441 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
385 { \ 442 { \
386 stem ## max = array_roundsize (stem ## cnt >> 1); \ 443 stem ## max = array_roundsize (stem ## cnt >> 1); \
387 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 444 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
388 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 445 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
389 } 446 }
447#endif
390 448
391#define array_free(stem, idx) \ 449#define array_free(stem, idx) \
392 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 450 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
393 451
394/*****************************************************************************/ 452/*****************************************************************************/
450} 508}
451 509
452void 510void
453ev_feed_fd_event (EV_P_ int fd, int revents) 511ev_feed_fd_event (EV_P_ int fd, int revents)
454{ 512{
513 if (fd >= 0 && fd < anfdmax)
455 fd_event (EV_A_ fd, revents); 514 fd_event (EV_A_ fd, revents);
456} 515}
457 516
458void inline_size 517void inline_size
459fd_reify (EV_P) 518fd_reify (EV_P)
460{ 519{
554static void noinline 613static void noinline
555fd_rearm_all (EV_P) 614fd_rearm_all (EV_P)
556{ 615{
557 int fd; 616 int fd;
558 617
559 /* this should be highly optimised to not do anything but set a flag */
560 for (fd = 0; fd < anfdmax; ++fd) 618 for (fd = 0; fd < anfdmax; ++fd)
561 if (anfds [fd].events) 619 if (anfds [fd].events)
562 { 620 {
563 anfds [fd].events = 0; 621 anfds [fd].events = 0;
564 fd_change (EV_A_ fd); 622 fd_change (EV_A_ fd);
730 ev_child *w; 788 ev_child *w;
731 789
732 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 790 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
733 if (w->pid == pid || !w->pid) 791 if (w->pid == pid || !w->pid)
734 { 792 {
735 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 793 ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */
736 w->rpid = pid; 794 w->rpid = pid;
737 w->rstatus = status; 795 w->rstatus = status;
738 ev_feed_event (EV_A_ (W)w, EV_CHILD); 796 ev_feed_event (EV_A_ (W)w, EV_CHILD);
739 } 797 }
740} 798}
741 799
742#ifndef WCONTINUED 800#ifndef WCONTINUED
852ev_backend (EV_P) 910ev_backend (EV_P)
853{ 911{
854 return backend; 912 return backend;
855} 913}
856 914
857static void 915unsigned int
916ev_loop_count (EV_P)
917{
918 return loop_count;
919}
920
921static void noinline
858loop_init (EV_P_ unsigned int flags) 922loop_init (EV_P_ unsigned int flags)
859{ 923{
860 if (!backend) 924 if (!backend)
861 { 925 {
862#if EV_USE_MONOTONIC 926#if EV_USE_MONOTONIC
870 ev_rt_now = ev_time (); 934 ev_rt_now = ev_time ();
871 mn_now = get_clock (); 935 mn_now = get_clock ();
872 now_floor = mn_now; 936 now_floor = mn_now;
873 rtmn_diff = ev_rt_now - mn_now; 937 rtmn_diff = ev_rt_now - mn_now;
874 938
939 /* pid check not overridable via env */
940#ifndef _WIN32
941 if (flags & EVFLAG_FORKCHECK)
942 curpid = getpid ();
943#endif
944
875 if (!(flags & EVFLAG_NOENV) 945 if (!(flags & EVFLAG_NOENV)
876 && !enable_secure () 946 && !enable_secure ()
877 && getenv ("LIBEV_FLAGS")) 947 && getenv ("LIBEV_FLAGS"))
878 flags = atoi (getenv ("LIBEV_FLAGS")); 948 flags = atoi (getenv ("LIBEV_FLAGS"));
879 949
880 if (!(flags & 0x0000ffffUL)) 950 if (!(flags & 0x0000ffffUL))
881 flags |= ev_recommended_backends (); 951 flags |= ev_recommended_backends ();
882 952
883 backend = 0; 953 backend = 0;
954 backend_fd = -1;
955#if EV_USE_INOTIFY
956 fs_fd = -2;
957#endif
958
884#if EV_USE_PORT 959#if EV_USE_PORT
885 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 960 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
886#endif 961#endif
887#if EV_USE_KQUEUE 962#if EV_USE_KQUEUE
888 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags); 963 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
900 ev_init (&sigev, sigcb); 975 ev_init (&sigev, sigcb);
901 ev_set_priority (&sigev, EV_MAXPRI); 976 ev_set_priority (&sigev, EV_MAXPRI);
902 } 977 }
903} 978}
904 979
905static void 980static void noinline
906loop_destroy (EV_P) 981loop_destroy (EV_P)
907{ 982{
908 int i; 983 int i;
984
985#if EV_USE_INOTIFY
986 if (fs_fd >= 0)
987 close (fs_fd);
988#endif
989
990 if (backend_fd >= 0)
991 close (backend_fd);
909 992
910#if EV_USE_PORT 993#if EV_USE_PORT
911 if (backend == EVBACKEND_PORT ) port_destroy (EV_A); 994 if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
912#endif 995#endif
913#if EV_USE_KQUEUE 996#if EV_USE_KQUEUE
922#if EV_USE_SELECT 1005#if EV_USE_SELECT
923 if (backend == EVBACKEND_SELECT) select_destroy (EV_A); 1006 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
924#endif 1007#endif
925 1008
926 for (i = NUMPRI; i--; ) 1009 for (i = NUMPRI; i--; )
1010 {
927 array_free (pending, [i]); 1011 array_free (pending, [i]);
1012#if EV_IDLE_ENABLE
1013 array_free (idle, [i]);
1014#endif
1015 }
928 1016
929 /* have to use the microsoft-never-gets-it-right macro */ 1017 /* have to use the microsoft-never-gets-it-right macro */
930 array_free (fdchange, EMPTY0); 1018 array_free (fdchange, EMPTY);
931 array_free (timer, EMPTY0); 1019 array_free (timer, EMPTY);
932#if EV_PERIODIC_ENABLE 1020#if EV_PERIODIC_ENABLE
933 array_free (periodic, EMPTY0); 1021 array_free (periodic, EMPTY);
934#endif 1022#endif
935 array_free (idle, EMPTY0);
936 array_free (prepare, EMPTY0); 1023 array_free (prepare, EMPTY);
937 array_free (check, EMPTY0); 1024 array_free (check, EMPTY);
938 1025
939 backend = 0; 1026 backend = 0;
940} 1027}
941 1028
942static void 1029void inline_size infy_fork (EV_P);
1030
1031void inline_size
943loop_fork (EV_P) 1032loop_fork (EV_P)
944{ 1033{
945#if EV_USE_PORT 1034#if EV_USE_PORT
946 if (backend == EVBACKEND_PORT ) port_fork (EV_A); 1035 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
947#endif 1036#endif
948#if EV_USE_KQUEUE 1037#if EV_USE_KQUEUE
949 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A); 1038 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
950#endif 1039#endif
951#if EV_USE_EPOLL 1040#if EV_USE_EPOLL
952 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); 1041 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
1042#endif
1043#if EV_USE_INOTIFY
1044 infy_fork (EV_A);
953#endif 1045#endif
954 1046
955 if (ev_is_active (&sigev)) 1047 if (ev_is_active (&sigev))
956 { 1048 {
957 /* default loop */ 1049 /* default loop */
1073 postfork = 1; 1165 postfork = 1;
1074} 1166}
1075 1167
1076/*****************************************************************************/ 1168/*****************************************************************************/
1077 1169
1078int inline_size 1170void
1079any_pending (EV_P) 1171ev_invoke (EV_P_ void *w, int revents)
1080{ 1172{
1081 int pri; 1173 EV_CB_INVOKE ((W)w, revents);
1082
1083 for (pri = NUMPRI; pri--; )
1084 if (pendingcnt [pri])
1085 return 1;
1086
1087 return 0;
1088} 1174}
1089 1175
1090void inline_speed 1176void inline_speed
1091call_pending (EV_P) 1177call_pending (EV_P)
1092{ 1178{
1097 { 1183 {
1098 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1184 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1099 1185
1100 if (expect_true (p->w)) 1186 if (expect_true (p->w))
1101 { 1187 {
1102 assert (("non-pending watcher on pending list", p->w->pending)); 1188 /*assert (("non-pending watcher on pending list", p->w->pending));*/
1103 1189
1104 p->w->pending = 0; 1190 p->w->pending = 0;
1105 EV_CB_INVOKE (p->w, p->events); 1191 EV_CB_INVOKE (p->w, p->events);
1106 } 1192 }
1107 } 1193 }
1112{ 1198{
1113 while (timercnt && ((WT)timers [0])->at <= mn_now) 1199 while (timercnt && ((WT)timers [0])->at <= mn_now)
1114 { 1200 {
1115 ev_timer *w = timers [0]; 1201 ev_timer *w = timers [0];
1116 1202
1117 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1203 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1118 1204
1119 /* first reschedule or stop timer */ 1205 /* first reschedule or stop timer */
1120 if (w->repeat) 1206 if (w->repeat)
1121 { 1207 {
1122 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 1208 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1140{ 1226{
1141 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1227 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1142 { 1228 {
1143 ev_periodic *w = periodics [0]; 1229 ev_periodic *w = periodics [0];
1144 1230
1145 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1231 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1146 1232
1147 /* first reschedule or stop timer */ 1233 /* first reschedule or stop timer */
1148 if (w->reschedule_cb) 1234 if (w->reschedule_cb)
1149 { 1235 {
1150 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); 1236 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
1184 for (i = periodiccnt >> 1; i--; ) 1270 for (i = periodiccnt >> 1; i--; )
1185 downheap ((WT *)periodics, periodiccnt, i); 1271 downheap ((WT *)periodics, periodiccnt, i);
1186} 1272}
1187#endif 1273#endif
1188 1274
1275#if EV_IDLE_ENABLE
1276void inline_size
1277idle_reify (EV_P)
1278{
1279 if (expect_false (idleall))
1280 {
1281 int pri;
1282
1283 for (pri = NUMPRI; pri--; )
1284 {
1285 if (pendingcnt [pri])
1286 break;
1287
1288 if (idlecnt [pri])
1289 {
1290 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1291 break;
1292 }
1293 }
1294 }
1295}
1296#endif
1297
1189int inline_size 1298int inline_size
1190time_update_monotonic (EV_P) 1299time_update_monotonic (EV_P)
1191{ 1300{
1192 mn_now = get_clock (); 1301 mn_now = get_clock ();
1193 1302
1217 ev_tstamp odiff = rtmn_diff; 1326 ev_tstamp odiff = rtmn_diff;
1218 1327
1219 /* loop a few times, before making important decisions. 1328 /* loop a few times, before making important decisions.
1220 * on the choice of "4": one iteration isn't enough, 1329 * on the choice of "4": one iteration isn't enough,
1221 * in case we get preempted during the calls to 1330 * in case we get preempted during the calls to
1222 * ev_time and get_clock. a second call is almost guarenteed 1331 * ev_time and get_clock. a second call is almost guaranteed
1223 * to succeed in that case, though. and looping a few more times 1332 * to succeed in that case, though. and looping a few more times
1224 * doesn't hurt either as we only do this on time-jumps or 1333 * doesn't hurt either as we only do this on time-jumps or
1225 * in the unlikely event of getting preempted here. 1334 * in the unlikely event of having been preempted here.
1226 */ 1335 */
1227 for (i = 4; --i; ) 1336 for (i = 4; --i; )
1228 { 1337 {
1229 rtmn_diff = ev_rt_now - mn_now; 1338 rtmn_diff = ev_rt_now - mn_now;
1230 1339
1252 { 1361 {
1253#if EV_PERIODIC_ENABLE 1362#if EV_PERIODIC_ENABLE
1254 periodics_reschedule (EV_A); 1363 periodics_reschedule (EV_A);
1255#endif 1364#endif
1256 1365
1257 /* adjust timers. this is easy, as the offset is the same for all */ 1366 /* adjust timers. this is easy, as the offset is the same for all of them */
1258 for (i = 0; i < timercnt; ++i) 1367 for (i = 0; i < timercnt; ++i)
1259 ((WT)timers [i])->at += ev_rt_now - mn_now; 1368 ((WT)timers [i])->at += ev_rt_now - mn_now;
1260 } 1369 }
1261 1370
1262 mn_now = ev_rt_now; 1371 mn_now = ev_rt_now;
1282{ 1391{
1283 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) 1392 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1284 ? EVUNLOOP_ONE 1393 ? EVUNLOOP_ONE
1285 : EVUNLOOP_CANCEL; 1394 : EVUNLOOP_CANCEL;
1286 1395
1287 while (activecnt) 1396 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1397
1398 do
1288 { 1399 {
1289 /* we might have forked, so reify kernel state if necessary */ 1400#ifndef _WIN32
1401 if (expect_false (curpid)) /* penalise the forking check even more */
1402 if (expect_false (getpid () != curpid))
1403 {
1404 curpid = getpid ();
1405 postfork = 1;
1406 }
1407#endif
1408
1290 #if EV_FORK_ENABLE 1409#if EV_FORK_ENABLE
1410 /* we might have forked, so queue fork handlers */
1291 if (expect_false (postfork)) 1411 if (expect_false (postfork))
1292 if (forkcnt) 1412 if (forkcnt)
1293 { 1413 {
1294 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 1414 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1295 call_pending (EV_A); 1415 call_pending (EV_A);
1296 } 1416 }
1297 #endif 1417#endif
1298 1418
1299 /* queue check watchers (and execute them) */ 1419 /* queue prepare watchers (and execute them) */
1300 if (expect_false (preparecnt)) 1420 if (expect_false (preparecnt))
1301 { 1421 {
1302 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1422 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1303 call_pending (EV_A); 1423 call_pending (EV_A);
1304 } 1424 }
1305 1425
1426 if (expect_false (!activecnt))
1427 break;
1428
1306 /* we might have forked, so reify kernel state if necessary */ 1429 /* we might have forked, so reify kernel state if necessary */
1307 if (expect_false (postfork)) 1430 if (expect_false (postfork))
1308 loop_fork (EV_A); 1431 loop_fork (EV_A);
1309 1432
1310 /* update fd-related kernel structures */ 1433 /* update fd-related kernel structures */
1311 fd_reify (EV_A); 1434 fd_reify (EV_A);
1312 1435
1313 /* calculate blocking time */ 1436 /* calculate blocking time */
1314 { 1437 {
1315 double block; 1438 ev_tstamp block;
1316 1439
1317 if (flags & EVLOOP_NONBLOCK || idlecnt) 1440 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1318 block = 0.; /* do not block at all */ 1441 block = 0.; /* do not block at all */
1319 else 1442 else
1320 { 1443 {
1321 /* update time to cancel out callback processing overhead */ 1444 /* update time to cancel out callback processing overhead */
1322#if EV_USE_MONOTONIC 1445#if EV_USE_MONOTONIC
1346#endif 1469#endif
1347 1470
1348 if (expect_false (block < 0.)) block = 0.; 1471 if (expect_false (block < 0.)) block = 0.;
1349 } 1472 }
1350 1473
1474 ++loop_count;
1351 backend_poll (EV_A_ block); 1475 backend_poll (EV_A_ block);
1352 } 1476 }
1353 1477
1354 /* update ev_rt_now, do magic */ 1478 /* update ev_rt_now, do magic */
1355 time_update (EV_A); 1479 time_update (EV_A);
1358 timers_reify (EV_A); /* relative timers called last */ 1482 timers_reify (EV_A); /* relative timers called last */
1359#if EV_PERIODIC_ENABLE 1483#if EV_PERIODIC_ENABLE
1360 periodics_reify (EV_A); /* absolute timers called first */ 1484 periodics_reify (EV_A); /* absolute timers called first */
1361#endif 1485#endif
1362 1486
1487#if EV_IDLE_ENABLE
1363 /* queue idle watchers unless other events are pending */ 1488 /* queue idle watchers unless other events are pending */
1364 if (idlecnt && !any_pending (EV_A)) 1489 idle_reify (EV_A);
1365 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1490#endif
1366 1491
1367 /* queue check watchers, to be executed first */ 1492 /* queue check watchers, to be executed first */
1368 if (expect_false (checkcnt)) 1493 if (expect_false (checkcnt))
1369 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1494 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1370 1495
1371 call_pending (EV_A); 1496 call_pending (EV_A);
1372 1497
1373 if (expect_false (loop_done))
1374 break;
1375 } 1498 }
1499 while (expect_true (activecnt && !loop_done));
1376 1500
1377 if (loop_done == EVUNLOOP_ONE) 1501 if (loop_done == EVUNLOOP_ONE)
1378 loop_done = EVUNLOOP_CANCEL; 1502 loop_done = EVUNLOOP_CANCEL;
1379} 1503}
1380 1504
1407 head = &(*head)->next; 1531 head = &(*head)->next;
1408 } 1532 }
1409} 1533}
1410 1534
1411void inline_speed 1535void inline_speed
1412ev_clear_pending (EV_P_ W w) 1536clear_pending (EV_P_ W w)
1413{ 1537{
1414 if (w->pending) 1538 if (w->pending)
1415 { 1539 {
1416 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1540 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1417 w->pending = 0; 1541 w->pending = 0;
1418 } 1542 }
1419} 1543}
1420 1544
1545int
1546ev_clear_pending (EV_P_ void *w)
1547{
1548 W w_ = (W)w;
1549 int pending = w_->pending;
1550
1551 if (!pending)
1552 return 0;
1553
1554 w_->pending = 0;
1555 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
1556 p->w = 0;
1557
1558 return p->events;
1559}
1560
1561void inline_size
1562pri_adjust (EV_P_ W w)
1563{
1564 int pri = w->priority;
1565 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
1566 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
1567 w->priority = pri;
1568}
1569
1421void inline_speed 1570void inline_speed
1422ev_start (EV_P_ W w, int active) 1571ev_start (EV_P_ W w, int active)
1423{ 1572{
1424 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1573 pri_adjust (EV_A_ w);
1425 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1426
1427 w->active = active; 1574 w->active = active;
1428 ev_ref (EV_A); 1575 ev_ref (EV_A);
1429} 1576}
1430 1577
1431void inline_size 1578void inline_size
1455} 1602}
1456 1603
1457void 1604void
1458ev_io_stop (EV_P_ ev_io *w) 1605ev_io_stop (EV_P_ ev_io *w)
1459{ 1606{
1460 ev_clear_pending (EV_A_ (W)w); 1607 clear_pending (EV_A_ (W)w);
1461 if (expect_false (!ev_is_active (w))) 1608 if (expect_false (!ev_is_active (w)))
1462 return; 1609 return;
1463 1610
1464 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1611 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1465 1612
1482 ev_start (EV_A_ (W)w, ++timercnt); 1629 ev_start (EV_A_ (W)w, ++timercnt);
1483 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); 1630 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1484 timers [timercnt - 1] = w; 1631 timers [timercnt - 1] = w;
1485 upheap ((WT *)timers, timercnt - 1); 1632 upheap ((WT *)timers, timercnt - 1);
1486 1633
1634 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/
1635}
1636
1637void
1638ev_timer_stop (EV_P_ ev_timer *w)
1639{
1640 clear_pending (EV_A_ (W)w);
1641 if (expect_false (!ev_is_active (w)))
1642 return;
1643
1487 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1644 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1488}
1489 1645
1490void 1646 {
1491ev_timer_stop (EV_P_ ev_timer *w) 1647 int active = ((W)w)->active;
1492{
1493 ev_clear_pending (EV_A_ (W)w);
1494 if (expect_false (!ev_is_active (w)))
1495 return;
1496 1648
1497 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1498
1499 if (expect_true (((W)w)->active < timercnt--)) 1649 if (expect_true (--active < --timercnt))
1500 { 1650 {
1501 timers [((W)w)->active - 1] = timers [timercnt]; 1651 timers [active] = timers [timercnt];
1502 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1652 adjustheap ((WT *)timers, timercnt, active);
1503 } 1653 }
1654 }
1504 1655
1505 ((WT)w)->at -= mn_now; 1656 ((WT)w)->at -= mn_now;
1506 1657
1507 ev_stop (EV_A_ (W)w); 1658 ev_stop (EV_A_ (W)w);
1508} 1659}
1546 ev_start (EV_A_ (W)w, ++periodiccnt); 1697 ev_start (EV_A_ (W)w, ++periodiccnt);
1547 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1698 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1548 periodics [periodiccnt - 1] = w; 1699 periodics [periodiccnt - 1] = w;
1549 upheap ((WT *)periodics, periodiccnt - 1); 1700 upheap ((WT *)periodics, periodiccnt - 1);
1550 1701
1702 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/
1703}
1704
1705void
1706ev_periodic_stop (EV_P_ ev_periodic *w)
1707{
1708 clear_pending (EV_A_ (W)w);
1709 if (expect_false (!ev_is_active (w)))
1710 return;
1711
1551 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1712 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1552}
1553 1713
1554void 1714 {
1555ev_periodic_stop (EV_P_ ev_periodic *w) 1715 int active = ((W)w)->active;
1556{
1557 ev_clear_pending (EV_A_ (W)w);
1558 if (expect_false (!ev_is_active (w)))
1559 return;
1560 1716
1561 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1562
1563 if (expect_true (((W)w)->active < periodiccnt--)) 1717 if (expect_true (--active < --periodiccnt))
1564 { 1718 {
1565 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1719 periodics [active] = periodics [periodiccnt];
1566 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1720 adjustheap ((WT *)periodics, periodiccnt, active);
1567 } 1721 }
1722 }
1568 1723
1569 ev_stop (EV_A_ (W)w); 1724 ev_stop (EV_A_ (W)w);
1570} 1725}
1571 1726
1572void 1727void
1612} 1767}
1613 1768
1614void 1769void
1615ev_signal_stop (EV_P_ ev_signal *w) 1770ev_signal_stop (EV_P_ ev_signal *w)
1616{ 1771{
1617 ev_clear_pending (EV_A_ (W)w); 1772 clear_pending (EV_A_ (W)w);
1618 if (expect_false (!ev_is_active (w))) 1773 if (expect_false (!ev_is_active (w)))
1619 return; 1774 return;
1620 1775
1621 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1776 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1622 ev_stop (EV_A_ (W)w); 1777 ev_stop (EV_A_ (W)w);
1639} 1794}
1640 1795
1641void 1796void
1642ev_child_stop (EV_P_ ev_child *w) 1797ev_child_stop (EV_P_ ev_child *w)
1643{ 1798{
1644 ev_clear_pending (EV_A_ (W)w); 1799 clear_pending (EV_A_ (W)w);
1645 if (expect_false (!ev_is_active (w))) 1800 if (expect_false (!ev_is_active (w)))
1646 return; 1801 return;
1647 1802
1648 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 1803 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1649 ev_stop (EV_A_ (W)w); 1804 ev_stop (EV_A_ (W)w);
1657# endif 1812# endif
1658 1813
1659#define DEF_STAT_INTERVAL 5.0074891 1814#define DEF_STAT_INTERVAL 5.0074891
1660#define MIN_STAT_INTERVAL 0.1074891 1815#define MIN_STAT_INTERVAL 0.1074891
1661 1816
1817static void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents);
1818
1819#if EV_USE_INOTIFY
1820# define EV_INOTIFY_BUFSIZE 8192
1821
1822static void noinline
1823infy_add (EV_P_ ev_stat *w)
1824{
1825 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);
1826
1827 if (w->wd < 0)
1828 {
1829 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
1830
1831 /* monitor some parent directory for speedup hints */
1832 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
1833 {
1834 char path [4096];
1835 strcpy (path, w->path);
1836
1837 do
1838 {
1839 int mask = IN_MASK_ADD | IN_DELETE_SELF | IN_MOVE_SELF
1840 | (errno == EACCES ? IN_ATTRIB : IN_CREATE | IN_MOVED_TO);
1841
1842 char *pend = strrchr (path, '/');
1843
1844 if (!pend)
1845 break; /* whoops, no '/', complain to your admin */
1846
1847 *pend = 0;
1848 w->wd = inotify_add_watch (fs_fd, path, mask);
1849 }
1850 while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
1851 }
1852 }
1853 else
1854 ev_timer_stop (EV_A_ &w->timer); /* we can watch this in a race-free way */
1855
1856 if (w->wd >= 0)
1857 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w);
1858}
1859
1860static void noinline
1861infy_del (EV_P_ ev_stat *w)
1862{
1863 int slot;
1864 int wd = w->wd;
1865
1866 if (wd < 0)
1867 return;
1868
1869 w->wd = -2;
1870 slot = wd & (EV_INOTIFY_HASHSIZE - 1);
1871 wlist_del (&fs_hash [slot].head, (WL)w);
1872
1873 /* remove this watcher, if others are watching it, they will rearm */
1874 inotify_rm_watch (fs_fd, wd);
1875}
1876
1877static void noinline
1878infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev)
1879{
1880 if (slot < 0)
1881 /* overflow, need to check for all hahs slots */
1882 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
1883 infy_wd (EV_A_ slot, wd, ev);
1884 else
1885 {
1886 WL w_;
1887
1888 for (w_ = fs_hash [slot & (EV_INOTIFY_HASHSIZE - 1)].head; w_; )
1889 {
1890 ev_stat *w = (ev_stat *)w_;
1891 w_ = w_->next; /* lets us remove this watcher and all before it */
1892
1893 if (w->wd == wd || wd == -1)
1894 {
1895 if (ev->mask & (IN_IGNORED | IN_UNMOUNT | IN_DELETE_SELF))
1896 {
1897 w->wd = -1;
1898 infy_add (EV_A_ w); /* re-add, no matter what */
1899 }
1900
1901 stat_timer_cb (EV_A_ &w->timer, 0);
1902 }
1903 }
1904 }
1905}
1906
1907static void
1908infy_cb (EV_P_ ev_io *w, int revents)
1909{
1910 char buf [EV_INOTIFY_BUFSIZE];
1911 struct inotify_event *ev = (struct inotify_event *)buf;
1912 int ofs;
1913 int len = read (fs_fd, buf, sizeof (buf));
1914
1915 for (ofs = 0; ofs < len; ofs += sizeof (struct inotify_event) + ev->len)
1916 infy_wd (EV_A_ ev->wd, ev->wd, ev);
1917}
1918
1919void inline_size
1920infy_init (EV_P)
1921{
1922 if (fs_fd != -2)
1923 return;
1924
1925 fs_fd = inotify_init ();
1926
1927 if (fs_fd >= 0)
1928 {
1929 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ);
1930 ev_set_priority (&fs_w, EV_MAXPRI);
1931 ev_io_start (EV_A_ &fs_w);
1932 }
1933}
1934
1935void inline_size
1936infy_fork (EV_P)
1937{
1938 int slot;
1939
1940 if (fs_fd < 0)
1941 return;
1942
1943 close (fs_fd);
1944 fs_fd = inotify_init ();
1945
1946 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
1947 {
1948 WL w_ = fs_hash [slot].head;
1949 fs_hash [slot].head = 0;
1950
1951 while (w_)
1952 {
1953 ev_stat *w = (ev_stat *)w_;
1954 w_ = w_->next; /* lets us add this watcher */
1955
1956 w->wd = -1;
1957
1958 if (fs_fd >= 0)
1959 infy_add (EV_A_ w); /* re-add, no matter what */
1960 else
1961 ev_timer_start (EV_A_ &w->timer);
1962 }
1963
1964 }
1965}
1966
1967#endif
1968
1662void 1969void
1663ev_stat_stat (EV_P_ ev_stat *w) 1970ev_stat_stat (EV_P_ ev_stat *w)
1664{ 1971{
1665 if (lstat (w->path, &w->attr) < 0) 1972 if (lstat (w->path, &w->attr) < 0)
1666 w->attr.st_nlink = 0; 1973 w->attr.st_nlink = 0;
1667 else if (!w->attr.st_nlink) 1974 else if (!w->attr.st_nlink)
1668 w->attr.st_nlink = 1; 1975 w->attr.st_nlink = 1;
1669} 1976}
1670 1977
1671static void 1978static void noinline
1672stat_timer_cb (EV_P_ ev_timer *w_, int revents) 1979stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1673{ 1980{
1674 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); 1981 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1675 1982
1676 /* we copy this here each the time so that */ 1983 /* we copy this here each the time so that */
1677 /* prev has the old value when the callback gets invoked */ 1984 /* prev has the old value when the callback gets invoked */
1678 w->prev = w->attr; 1985 w->prev = w->attr;
1679 ev_stat_stat (EV_A_ w); 1986 ev_stat_stat (EV_A_ w);
1680 1987
1681 if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata))) 1988 /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */
1989 if (
1990 w->prev.st_dev != w->attr.st_dev
1991 || w->prev.st_ino != w->attr.st_ino
1992 || w->prev.st_mode != w->attr.st_mode
1993 || w->prev.st_nlink != w->attr.st_nlink
1994 || w->prev.st_uid != w->attr.st_uid
1995 || w->prev.st_gid != w->attr.st_gid
1996 || w->prev.st_rdev != w->attr.st_rdev
1997 || w->prev.st_size != w->attr.st_size
1998 || w->prev.st_atime != w->attr.st_atime
1999 || w->prev.st_mtime != w->attr.st_mtime
2000 || w->prev.st_ctime != w->attr.st_ctime
2001 ) {
2002 #if EV_USE_INOTIFY
2003 infy_del (EV_A_ w);
2004 infy_add (EV_A_ w);
2005 ev_stat_stat (EV_A_ w); /* avoid race... */
2006 #endif
2007
1682 ev_feed_event (EV_A_ w, EV_STAT); 2008 ev_feed_event (EV_A_ w, EV_STAT);
2009 }
1683} 2010}
1684 2011
1685void 2012void
1686ev_stat_start (EV_P_ ev_stat *w) 2013ev_stat_start (EV_P_ ev_stat *w)
1687{ 2014{
1697 if (w->interval < MIN_STAT_INTERVAL) 2024 if (w->interval < MIN_STAT_INTERVAL)
1698 w->interval = w->interval ? MIN_STAT_INTERVAL : DEF_STAT_INTERVAL; 2025 w->interval = w->interval ? MIN_STAT_INTERVAL : DEF_STAT_INTERVAL;
1699 2026
1700 ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval); 2027 ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval);
1701 ev_set_priority (&w->timer, ev_priority (w)); 2028 ev_set_priority (&w->timer, ev_priority (w));
2029
2030#if EV_USE_INOTIFY
2031 infy_init (EV_A);
2032
2033 if (fs_fd >= 0)
2034 infy_add (EV_A_ w);
2035 else
2036#endif
1702 ev_timer_start (EV_A_ &w->timer); 2037 ev_timer_start (EV_A_ &w->timer);
1703 2038
1704 ev_start (EV_A_ (W)w, 1); 2039 ev_start (EV_A_ (W)w, 1);
1705} 2040}
1706 2041
1707void 2042void
1708ev_stat_stop (EV_P_ ev_stat *w) 2043ev_stat_stop (EV_P_ ev_stat *w)
1709{ 2044{
1710 ev_clear_pending (EV_A_ (W)w); 2045 clear_pending (EV_A_ (W)w);
1711 if (expect_false (!ev_is_active (w))) 2046 if (expect_false (!ev_is_active (w)))
1712 return; 2047 return;
1713 2048
2049#if EV_USE_INOTIFY
2050 infy_del (EV_A_ w);
2051#endif
1714 ev_timer_stop (EV_A_ &w->timer); 2052 ev_timer_stop (EV_A_ &w->timer);
1715 2053
1716 ev_stop (EV_A_ (W)w); 2054 ev_stop (EV_A_ (W)w);
1717} 2055}
1718#endif 2056#endif
1719 2057
2058#if EV_IDLE_ENABLE
1720void 2059void
1721ev_idle_start (EV_P_ ev_idle *w) 2060ev_idle_start (EV_P_ ev_idle *w)
1722{ 2061{
1723 if (expect_false (ev_is_active (w))) 2062 if (expect_false (ev_is_active (w)))
1724 return; 2063 return;
1725 2064
2065 pri_adjust (EV_A_ (W)w);
2066
2067 {
2068 int active = ++idlecnt [ABSPRI (w)];
2069
2070 ++idleall;
1726 ev_start (EV_A_ (W)w, ++idlecnt); 2071 ev_start (EV_A_ (W)w, active);
2072
1727 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); 2073 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
1728 idles [idlecnt - 1] = w; 2074 idles [ABSPRI (w)][active - 1] = w;
2075 }
1729} 2076}
1730 2077
1731void 2078void
1732ev_idle_stop (EV_P_ ev_idle *w) 2079ev_idle_stop (EV_P_ ev_idle *w)
1733{ 2080{
1734 ev_clear_pending (EV_A_ (W)w); 2081 clear_pending (EV_A_ (W)w);
1735 if (expect_false (!ev_is_active (w))) 2082 if (expect_false (!ev_is_active (w)))
1736 return; 2083 return;
1737 2084
1738 { 2085 {
1739 int active = ((W)w)->active; 2086 int active = ((W)w)->active;
1740 idles [active - 1] = idles [--idlecnt]; 2087
2088 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
1741 ((W)idles [active - 1])->active = active; 2089 ((W)idles [ABSPRI (w)][active - 1])->active = active;
2090
2091 ev_stop (EV_A_ (W)w);
2092 --idleall;
1742 } 2093 }
1743
1744 ev_stop (EV_A_ (W)w);
1745} 2094}
2095#endif
1746 2096
1747void 2097void
1748ev_prepare_start (EV_P_ ev_prepare *w) 2098ev_prepare_start (EV_P_ ev_prepare *w)
1749{ 2099{
1750 if (expect_false (ev_is_active (w))) 2100 if (expect_false (ev_is_active (w)))
1756} 2106}
1757 2107
1758void 2108void
1759ev_prepare_stop (EV_P_ ev_prepare *w) 2109ev_prepare_stop (EV_P_ ev_prepare *w)
1760{ 2110{
1761 ev_clear_pending (EV_A_ (W)w); 2111 clear_pending (EV_A_ (W)w);
1762 if (expect_false (!ev_is_active (w))) 2112 if (expect_false (!ev_is_active (w)))
1763 return; 2113 return;
1764 2114
1765 { 2115 {
1766 int active = ((W)w)->active; 2116 int active = ((W)w)->active;
1783} 2133}
1784 2134
1785void 2135void
1786ev_check_stop (EV_P_ ev_check *w) 2136ev_check_stop (EV_P_ ev_check *w)
1787{ 2137{
1788 ev_clear_pending (EV_A_ (W)w); 2138 clear_pending (EV_A_ (W)w);
1789 if (expect_false (!ev_is_active (w))) 2139 if (expect_false (!ev_is_active (w)))
1790 return; 2140 return;
1791 2141
1792 { 2142 {
1793 int active = ((W)w)->active; 2143 int active = ((W)w)->active;
1835} 2185}
1836 2186
1837void 2187void
1838ev_embed_stop (EV_P_ ev_embed *w) 2188ev_embed_stop (EV_P_ ev_embed *w)
1839{ 2189{
1840 ev_clear_pending (EV_A_ (W)w); 2190 clear_pending (EV_A_ (W)w);
1841 if (expect_false (!ev_is_active (w))) 2191 if (expect_false (!ev_is_active (w)))
1842 return; 2192 return;
1843 2193
1844 ev_io_stop (EV_A_ &w->io); 2194 ev_io_stop (EV_A_ &w->io);
1845 2195
1860} 2210}
1861 2211
1862void 2212void
1863ev_fork_stop (EV_P_ ev_fork *w) 2213ev_fork_stop (EV_P_ ev_fork *w)
1864{ 2214{
1865 ev_clear_pending (EV_A_ (W)w); 2215 clear_pending (EV_A_ (W)w);
1866 if (expect_false (!ev_is_active (w))) 2216 if (expect_false (!ev_is_active (w)))
1867 return; 2217 return;
1868 2218
1869 { 2219 {
1870 int active = ((W)w)->active; 2220 int active = ((W)w)->active;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines