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

Comparing libev/ev.c (file contents):
Revision 1.64 by root, Sun Nov 4 23:14:11 2007 UTC vs.
Revision 1.147 by root, Tue Nov 27 10:59:11 2007 UTC

26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
31#ifndef EV_STANDALONE 36#ifndef EV_STANDALONE
37# ifdef EV_CONFIG_H
38# include EV_CONFIG_H
39# else
32# include "config.h" 40# include "config.h"
41# endif
33 42
34# if HAVE_CLOCK_GETTIME 43# if HAVE_CLOCK_GETTIME
44# ifndef EV_USE_MONOTONIC
35# define EV_USE_MONOTONIC 1 45# define EV_USE_MONOTONIC 1
46# endif
47# ifndef EV_USE_REALTIME
36# define EV_USE_REALTIME 1 48# define EV_USE_REALTIME 1
49# endif
50# else
51# ifndef EV_USE_MONOTONIC
52# define EV_USE_MONOTONIC 0
53# endif
54# ifndef EV_USE_REALTIME
55# define EV_USE_REALTIME 0
56# endif
37# endif 57# endif
38 58
59# ifndef EV_USE_SELECT
39# if HAVE_SELECT && HAVE_SYS_SELECT_H 60# if HAVE_SELECT && HAVE_SYS_SELECT_H
40# define EV_USE_SELECT 1 61# define EV_USE_SELECT 1
62# else
63# define EV_USE_SELECT 0
64# endif
41# endif 65# endif
42 66
67# ifndef EV_USE_POLL
43# if HAVE_POLL && HAVE_POLL_H 68# if HAVE_POLL && HAVE_POLL_H
44# define EV_USE_POLL 1 69# define EV_USE_POLL 1
70# else
71# define EV_USE_POLL 0
72# endif
45# endif 73# endif
46 74
75# ifndef EV_USE_EPOLL
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H 76# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
48# define EV_USE_EPOLL 1 77# define EV_USE_EPOLL 1
78# else
79# define EV_USE_EPOLL 0
80# endif
49# endif 81# endif
50 82
83# ifndef EV_USE_KQUEUE
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H 84# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
52# define EV_USE_KQUEUE 1 85# define EV_USE_KQUEUE 1
86# else
87# define EV_USE_KQUEUE 0
88# endif
89# endif
90
91# ifndef EV_USE_PORT
92# if HAVE_PORT_H && HAVE_PORT_CREATE
93# define EV_USE_PORT 1
94# else
95# define EV_USE_PORT 0
96# endif
53# endif 97# endif
54 98
55#endif 99#endif
56 100
57#include <math.h> 101#include <math.h>
58#include <stdlib.h> 102#include <stdlib.h>
59#include <unistd.h>
60#include <fcntl.h> 103#include <fcntl.h>
61#include <signal.h>
62#include <stddef.h> 104#include <stddef.h>
63 105
64#include <stdio.h> 106#include <stdio.h>
65 107
66#include <assert.h> 108#include <assert.h>
67#include <errno.h> 109#include <errno.h>
68#include <sys/types.h> 110#include <sys/types.h>
111#include <time.h>
112
113#include <signal.h>
114
69#ifndef WIN32 115#ifndef _WIN32
116# include <sys/time.h>
70# include <sys/wait.h> 117# include <sys/wait.h>
118# include <unistd.h>
119#else
120# define WIN32_LEAN_AND_MEAN
121# include <windows.h>
122# ifndef EV_SELECT_IS_WINSOCKET
123# define EV_SELECT_IS_WINSOCKET 1
71#endif 124# endif
72#include <sys/time.h> 125#endif
73#include <time.h>
74 126
75/**/ 127/**/
76 128
77#ifndef EV_USE_MONOTONIC 129#ifndef EV_USE_MONOTONIC
78# define EV_USE_MONOTONIC 1 130# define EV_USE_MONOTONIC 0
131#endif
132
133#ifndef EV_USE_REALTIME
134# define EV_USE_REALTIME 0
79#endif 135#endif
80 136
81#ifndef EV_USE_SELECT 137#ifndef EV_USE_SELECT
82# define EV_USE_SELECT 1 138# define EV_USE_SELECT 1
83#endif 139#endif
84 140
85#ifndef EV_USE_POLL 141#ifndef EV_USE_POLL
86# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ 142# ifdef _WIN32
143# define EV_USE_POLL 0
144# else
145# define EV_USE_POLL 1
146# endif
87#endif 147#endif
88 148
89#ifndef EV_USE_EPOLL 149#ifndef EV_USE_EPOLL
90# define EV_USE_EPOLL 0 150# define EV_USE_EPOLL 0
91#endif 151#endif
92 152
93#ifndef EV_USE_KQUEUE 153#ifndef EV_USE_KQUEUE
94# define EV_USE_KQUEUE 0 154# define EV_USE_KQUEUE 0
95#endif 155#endif
96 156
97#ifndef EV_USE_WIN32
98# ifdef WIN32
99# define EV_USE_WIN32 1
100# else
101# define EV_USE_WIN32 0
102# endif
103#endif
104
105#ifndef EV_USE_REALTIME 157#ifndef EV_USE_PORT
106# define EV_USE_REALTIME 1 158# define EV_USE_PORT 0
107#endif 159#endif
108 160
109/**/ 161/**/
110 162
111#ifndef CLOCK_MONOTONIC 163#ifndef CLOCK_MONOTONIC
116#ifndef CLOCK_REALTIME 168#ifndef CLOCK_REALTIME
117# undef EV_USE_REALTIME 169# undef EV_USE_REALTIME
118# define EV_USE_REALTIME 0 170# define EV_USE_REALTIME 0
119#endif 171#endif
120 172
173#if EV_SELECT_IS_WINSOCKET
174# include <winsock.h>
175#endif
176
121/**/ 177/**/
122 178
123#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 179#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
124#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ 180#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
125#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 181#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
126/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ 182/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
127 183
184#ifdef EV_H
185# include EV_H
186#else
128#include "ev.h" 187# include "ev.h"
188#endif
129 189
130#if __GNUC__ >= 3 190#if __GNUC__ >= 3
131# define expect(expr,value) __builtin_expect ((expr),(value)) 191# define expect(expr,value) __builtin_expect ((expr),(value))
192# define inline_size static inline /* inline for codesize */
193# if EV_MINIMAL
132# define inline inline 194# define noinline __attribute__ ((noinline))
195# define inline_speed static noinline
196# else
197# define noinline
198# define inline_speed static inline
199# endif
133#else 200#else
134# define expect(expr,value) (expr) 201# define expect(expr,value) (expr)
202# define inline_speed static
135# define inline static 203# define inline_size static
204# define noinline
136#endif 205#endif
137 206
138#define expect_false(expr) expect ((expr) != 0, 0) 207#define expect_false(expr) expect ((expr) != 0, 0)
139#define expect_true(expr) expect ((expr) != 0, 1) 208#define expect_true(expr) expect ((expr) != 0, 1)
140 209
141#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 210#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
142#define ABSPRI(w) ((w)->priority - EV_MINPRI) 211#define ABSPRI(w) ((w)->priority - EV_MINPRI)
143 212
213#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
214#define EMPTY2(a,b) /* used to suppress some warnings */
215
144typedef struct ev_watcher *W; 216typedef ev_watcher *W;
145typedef struct ev_watcher_list *WL; 217typedef ev_watcher_list *WL;
146typedef struct ev_watcher_time *WT; 218typedef ev_watcher_time *WT;
147 219
148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 220static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
149 221
222#ifdef _WIN32
223# include "ev_win32.c"
224#endif
225
150/*****************************************************************************/ 226/*****************************************************************************/
151 227
228static void (*syserr_cb)(const char *msg);
229
230void
231ev_set_syserr_cb (void (*cb)(const char *msg))
232{
233 syserr_cb = cb;
234}
235
236static void noinline
237syserr (const char *msg)
238{
239 if (!msg)
240 msg = "(libev) system error";
241
242 if (syserr_cb)
243 syserr_cb (msg);
244 else
245 {
246 perror (msg);
247 abort ();
248 }
249}
250
251static void *(*alloc)(void *ptr, long size);
252
253void
254ev_set_allocator (void *(*cb)(void *ptr, long size))
255{
256 alloc = cb;
257}
258
259static void *
260ev_realloc (void *ptr, long size)
261{
262 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
263
264 if (!ptr && size)
265 {
266 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
267 abort ();
268 }
269
270 return ptr;
271}
272
273#define ev_malloc(size) ev_realloc (0, (size))
274#define ev_free(ptr) ev_realloc ((ptr), 0)
275
276/*****************************************************************************/
277
152typedef struct 278typedef struct
153{ 279{
154 struct ev_watcher_list *head; 280 WL head;
155 unsigned char events; 281 unsigned char events;
156 unsigned char reify; 282 unsigned char reify;
283#if EV_SELECT_IS_WINSOCKET
284 SOCKET handle;
285#endif
157} ANFD; 286} ANFD;
158 287
159typedef struct 288typedef struct
160{ 289{
161 W w; 290 W w;
162 int events; 291 int events;
163} ANPENDING; 292} ANPENDING;
164 293
165#if EV_MULTIPLICITY 294#if EV_MULTIPLICITY
166 295
167struct ev_loop 296 struct ev_loop
168{ 297 {
298 ev_tstamp ev_rt_now;
299 #define ev_rt_now ((loop)->ev_rt_now)
169# define VAR(name,decl) decl; 300 #define VAR(name,decl) decl;
170# include "ev_vars.h" 301 #include "ev_vars.h"
171};
172# undef VAR 302 #undef VAR
303 };
173# include "ev_wrap.h" 304 #include "ev_wrap.h"
305
306 static struct ev_loop default_loop_struct;
307 struct ev_loop *ev_default_loop_ptr;
174 308
175#else 309#else
176 310
311 ev_tstamp ev_rt_now;
177# define VAR(name,decl) static decl; 312 #define VAR(name,decl) static decl;
178# include "ev_vars.h" 313 #include "ev_vars.h"
179# undef VAR 314 #undef VAR
315
316 static int ev_default_loop_ptr;
180 317
181#endif 318#endif
182 319
183/*****************************************************************************/ 320/*****************************************************************************/
184 321
185inline ev_tstamp 322ev_tstamp
186ev_time (void) 323ev_time (void)
187{ 324{
188#if EV_USE_REALTIME 325#if EV_USE_REALTIME
189 struct timespec ts; 326 struct timespec ts;
190 clock_gettime (CLOCK_REALTIME, &ts); 327 clock_gettime (CLOCK_REALTIME, &ts);
194 gettimeofday (&tv, 0); 331 gettimeofday (&tv, 0);
195 return tv.tv_sec + tv.tv_usec * 1e-6; 332 return tv.tv_sec + tv.tv_usec * 1e-6;
196#endif 333#endif
197} 334}
198 335
199inline ev_tstamp 336ev_tstamp inline_size
200get_clock (void) 337get_clock (void)
201{ 338{
202#if EV_USE_MONOTONIC 339#if EV_USE_MONOTONIC
203 if (expect_true (have_monotonic)) 340 if (expect_true (have_monotonic))
204 { 341 {
209#endif 346#endif
210 347
211 return ev_time (); 348 return ev_time ();
212} 349}
213 350
351#if EV_MULTIPLICITY
214ev_tstamp 352ev_tstamp
215ev_now (EV_P) 353ev_now (EV_P)
216{ 354{
217 return rt_now; 355 return ev_rt_now;
218} 356}
357#endif
219 358
220#define array_roundsize(base,n) ((n) | 4 & ~3) 359#define array_roundsize(type,n) (((n) | 4) & ~3)
221 360
222#define array_needsize(base,cur,cnt,init) \ 361#define array_needsize(type,base,cur,cnt,init) \
223 if (expect_false ((cnt) > cur)) \ 362 if (expect_false ((cnt) > cur)) \
224 { \ 363 { \
225 int newcnt = cur; \ 364 int newcnt = cur; \
226 do \ 365 do \
227 { \ 366 { \
228 newcnt = array_roundsize (base, newcnt << 1); \ 367 newcnt = array_roundsize (type, newcnt << 1); \
229 } \ 368 } \
230 while ((cnt) > newcnt); \ 369 while ((cnt) > newcnt); \
231 \ 370 \
232 base = realloc (base, sizeof (*base) * (newcnt)); \ 371 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
233 init (base + cur, newcnt - cur); \ 372 init (base + cur, newcnt - cur); \
234 cur = newcnt; \ 373 cur = newcnt; \
235 } 374 }
375
376#define array_slim(type,stem) \
377 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
378 { \
379 stem ## max = array_roundsize (stem ## cnt >> 1); \
380 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
381 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
382 }
383
384#define array_free(stem, idx) \
385 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
236 386
237/*****************************************************************************/ 387/*****************************************************************************/
238 388
239static void 389void noinline
390ev_feed_event (EV_P_ void *w, int revents)
391{
392 W w_ = (W)w;
393
394 if (expect_false (w_->pending))
395 {
396 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
397 return;
398 }
399
400 w_->pending = ++pendingcnt [ABSPRI (w_)];
401 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2);
402 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
403 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
404}
405
406void inline_size
407queue_events (EV_P_ W *events, int eventcnt, int type)
408{
409 int i;
410
411 for (i = 0; i < eventcnt; ++i)
412 ev_feed_event (EV_A_ events [i], type);
413}
414
415/*****************************************************************************/
416
417void inline_size
240anfds_init (ANFD *base, int count) 418anfds_init (ANFD *base, int count)
241{ 419{
242 while (count--) 420 while (count--)
243 { 421 {
244 base->head = 0; 422 base->head = 0;
247 425
248 ++base; 426 ++base;
249 } 427 }
250} 428}
251 429
252static void 430void inline_speed
253event (EV_P_ W w, int events)
254{
255 if (w->pending)
256 {
257 pendings [ABSPRI (w)][w->pending - 1].events |= events;
258 return;
259 }
260
261 w->pending = ++pendingcnt [ABSPRI (w)];
262 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
263 pendings [ABSPRI (w)][w->pending - 1].w = w;
264 pendings [ABSPRI (w)][w->pending - 1].events = events;
265}
266
267static void
268queue_events (EV_P_ W *events, int eventcnt, int type)
269{
270 int i;
271
272 for (i = 0; i < eventcnt; ++i)
273 event (EV_A_ events [i], type);
274}
275
276static void
277fd_event (EV_P_ int fd, int events) 431fd_event (EV_P_ int fd, int revents)
278{ 432{
279 ANFD *anfd = anfds + fd; 433 ANFD *anfd = anfds + fd;
280 struct ev_io *w; 434 ev_io *w;
281 435
282 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 436 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
283 { 437 {
284 int ev = w->events & events; 438 int ev = w->events & revents;
285 439
286 if (ev) 440 if (ev)
287 event (EV_A_ (W)w, ev); 441 ev_feed_event (EV_A_ (W)w, ev);
288 } 442 }
289} 443}
290 444
291/*****************************************************************************/ 445void
446ev_feed_fd_event (EV_P_ int fd, int revents)
447{
448 fd_event (EV_A_ fd, revents);
449}
292 450
293static void 451void inline_size
294fd_reify (EV_P) 452fd_reify (EV_P)
295{ 453{
296 int i; 454 int i;
297 455
298 for (i = 0; i < fdchangecnt; ++i) 456 for (i = 0; i < fdchangecnt; ++i)
299 { 457 {
300 int fd = fdchanges [i]; 458 int fd = fdchanges [i];
301 ANFD *anfd = anfds + fd; 459 ANFD *anfd = anfds + fd;
302 struct ev_io *w; 460 ev_io *w;
303 461
304 int events = 0; 462 int events = 0;
305 463
306 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 464 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
307 events |= w->events; 465 events |= w->events;
308 466
467#if EV_SELECT_IS_WINSOCKET
468 if (events)
469 {
470 unsigned long argp;
471 anfd->handle = _get_osfhandle (fd);
472 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
473 }
474#endif
475
309 anfd->reify = 0; 476 anfd->reify = 0;
310 477
311 method_modify (EV_A_ fd, anfd->events, events); 478 backend_modify (EV_A_ fd, anfd->events, events);
312 anfd->events = events; 479 anfd->events = events;
313 } 480 }
314 481
315 fdchangecnt = 0; 482 fdchangecnt = 0;
316} 483}
317 484
318static void 485void inline_size
319fd_change (EV_P_ int fd) 486fd_change (EV_P_ int fd)
320{ 487{
321 if (anfds [fd].reify || fdchangecnt < 0) 488 if (expect_false (anfds [fd].reify))
322 return; 489 return;
323 490
324 anfds [fd].reify = 1; 491 anfds [fd].reify = 1;
325 492
326 ++fdchangecnt; 493 ++fdchangecnt;
327 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 494 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
328 fdchanges [fdchangecnt - 1] = fd; 495 fdchanges [fdchangecnt - 1] = fd;
329} 496}
330 497
331static void 498void inline_speed
332fd_kill (EV_P_ int fd) 499fd_kill (EV_P_ int fd)
333{ 500{
334 struct ev_io *w; 501 ev_io *w;
335 502
336 while ((w = (struct ev_io *)anfds [fd].head)) 503 while ((w = (ev_io *)anfds [fd].head))
337 { 504 {
338 ev_io_stop (EV_A_ w); 505 ev_io_stop (EV_A_ w);
339 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 506 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
340 } 507 }
508}
509
510int inline_size
511fd_valid (int fd)
512{
513#ifdef _WIN32
514 return _get_osfhandle (fd) != -1;
515#else
516 return fcntl (fd, F_GETFD) != -1;
517#endif
341} 518}
342 519
343/* called on EBADF to verify fds */ 520/* called on EBADF to verify fds */
344static void 521static void noinline
345fd_ebadf (EV_P) 522fd_ebadf (EV_P)
346{ 523{
347 int fd; 524 int fd;
348 525
349 for (fd = 0; fd < anfdmax; ++fd) 526 for (fd = 0; fd < anfdmax; ++fd)
350 if (anfds [fd].events) 527 if (anfds [fd].events)
351 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 528 if (!fd_valid (fd) == -1 && errno == EBADF)
352 fd_kill (EV_A_ fd); 529 fd_kill (EV_A_ fd);
353} 530}
354 531
355/* called on ENOMEM in select/poll to kill some fds and retry */ 532/* called on ENOMEM in select/poll to kill some fds and retry */
356static void 533static void noinline
357fd_enomem (EV_P) 534fd_enomem (EV_P)
358{ 535{
359 int fd; 536 int fd;
360 537
361 for (fd = anfdmax; fd--; ) 538 for (fd = anfdmax; fd--; )
362 if (anfds [fd].events) 539 if (anfds [fd].events)
363 { 540 {
364 close (fd);
365 fd_kill (EV_A_ fd); 541 fd_kill (EV_A_ fd);
366 return; 542 return;
367 } 543 }
368} 544}
369 545
370/* susually called after fork if method needs to re-arm all fds from scratch */ 546/* usually called after fork if backend needs to re-arm all fds from scratch */
371static void 547static void noinline
372fd_rearm_all (EV_P) 548fd_rearm_all (EV_P)
373{ 549{
374 int fd; 550 int fd;
375 551
376 /* this should be highly optimised to not do anything but set a flag */ 552 /* this should be highly optimised to not do anything but set a flag */
382 } 558 }
383} 559}
384 560
385/*****************************************************************************/ 561/*****************************************************************************/
386 562
387static void 563void inline_speed
388upheap (WT *heap, int k) 564upheap (WT *heap, int k)
389{ 565{
390 WT w = heap [k]; 566 WT w = heap [k];
391 567
392 while (k && heap [k >> 1]->at > w->at) 568 while (k && heap [k >> 1]->at > w->at)
399 heap [k] = w; 575 heap [k] = w;
400 ((W)heap [k])->active = k + 1; 576 ((W)heap [k])->active = k + 1;
401 577
402} 578}
403 579
404static void 580void inline_speed
405downheap (WT *heap, int N, int k) 581downheap (WT *heap, int N, int k)
406{ 582{
407 WT w = heap [k]; 583 WT w = heap [k];
408 584
409 while (k < (N >> 1)) 585 while (k < (N >> 1))
423 599
424 heap [k] = w; 600 heap [k] = w;
425 ((W)heap [k])->active = k + 1; 601 ((W)heap [k])->active = k + 1;
426} 602}
427 603
604void inline_size
605adjustheap (WT *heap, int N, int k)
606{
607 upheap (heap, k);
608 downheap (heap, N, k);
609}
610
428/*****************************************************************************/ 611/*****************************************************************************/
429 612
430typedef struct 613typedef struct
431{ 614{
432 struct ev_watcher_list *head; 615 WL head;
433 sig_atomic_t volatile gotsig; 616 sig_atomic_t volatile gotsig;
434} ANSIG; 617} ANSIG;
435 618
436static ANSIG *signals; 619static ANSIG *signals;
437static int signalmax; 620static int signalmax;
438 621
439static int sigpipe [2]; 622static int sigpipe [2];
440static sig_atomic_t volatile gotsig; 623static sig_atomic_t volatile gotsig;
441static struct ev_io sigev; 624static ev_io sigev;
442 625
443static void 626void inline_size
444signals_init (ANSIG *base, int count) 627signals_init (ANSIG *base, int count)
445{ 628{
446 while (count--) 629 while (count--)
447 { 630 {
448 base->head = 0; 631 base->head = 0;
453} 636}
454 637
455static void 638static void
456sighandler (int signum) 639sighandler (int signum)
457{ 640{
641#if _WIN32
642 signal (signum, sighandler);
643#endif
644
458 signals [signum - 1].gotsig = 1; 645 signals [signum - 1].gotsig = 1;
459 646
460 if (!gotsig) 647 if (!gotsig)
461 { 648 {
462 int old_errno = errno; 649 int old_errno = errno;
464 write (sigpipe [1], &signum, 1); 651 write (sigpipe [1], &signum, 1);
465 errno = old_errno; 652 errno = old_errno;
466 } 653 }
467} 654}
468 655
656void noinline
657ev_feed_signal_event (EV_P_ int signum)
658{
659 WL w;
660
661#if EV_MULTIPLICITY
662 assert (("feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr));
663#endif
664
665 --signum;
666
667 if (signum < 0 || signum >= signalmax)
668 return;
669
670 signals [signum].gotsig = 0;
671
672 for (w = signals [signum].head; w; w = w->next)
673 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
674}
675
469static void 676static void
470sigcb (EV_P_ struct ev_io *iow, int revents) 677sigcb (EV_P_ ev_io *iow, int revents)
471{ 678{
472 struct ev_watcher_list *w;
473 int signum; 679 int signum;
474 680
475 read (sigpipe [0], &revents, 1); 681 read (sigpipe [0], &revents, 1);
476 gotsig = 0; 682 gotsig = 0;
477 683
478 for (signum = signalmax; signum--; ) 684 for (signum = signalmax; signum--; )
479 if (signals [signum].gotsig) 685 if (signals [signum].gotsig)
480 { 686 ev_feed_signal_event (EV_A_ signum + 1);
481 signals [signum].gotsig = 0;
482
483 for (w = signals [signum].head; w; w = w->next)
484 event (EV_A_ (W)w, EV_SIGNAL);
485 }
486} 687}
487 688
488static void 689void inline_size
690fd_intern (int fd)
691{
692#ifdef _WIN32
693 int arg = 1;
694 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
695#else
696 fcntl (fd, F_SETFD, FD_CLOEXEC);
697 fcntl (fd, F_SETFL, O_NONBLOCK);
698#endif
699}
700
701static void noinline
489siginit (EV_P) 702siginit (EV_P)
490{ 703{
491#ifndef WIN32 704 fd_intern (sigpipe [0]);
492 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 705 fd_intern (sigpipe [1]);
493 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
494
495 /* rather than sort out wether we really need nb, set it */
496 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
497 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
498#endif
499 706
500 ev_io_set (&sigev, sigpipe [0], EV_READ); 707 ev_io_set (&sigev, sigpipe [0], EV_READ);
501 ev_io_start (EV_A_ &sigev); 708 ev_io_start (EV_A_ &sigev);
502 ev_unref (EV_A); /* child watcher should not keep loop alive */ 709 ev_unref (EV_A); /* child watcher should not keep loop alive */
503} 710}
504 711
505/*****************************************************************************/ 712/*****************************************************************************/
506 713
714static ev_child *childs [PID_HASHSIZE];
715
507#ifndef WIN32 716#ifndef _WIN32
508 717
509static struct ev_child *childs [PID_HASHSIZE];
510static struct ev_signal childev; 718static ev_signal childev;
511 719
512#ifndef WCONTINUED 720void inline_speed
513# define WCONTINUED 0
514#endif
515
516static void
517child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) 721child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status)
518{ 722{
519 struct ev_child *w; 723 ev_child *w;
520 724
521 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 725 for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
522 if (w->pid == pid || !w->pid) 726 if (w->pid == pid || !w->pid)
523 { 727 {
524 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 728 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
525 w->rpid = pid; 729 w->rpid = pid;
526 w->rstatus = status; 730 w->rstatus = status;
527 event (EV_A_ (W)w, EV_CHILD); 731 ev_feed_event (EV_A_ (W)w, EV_CHILD);
528 } 732 }
529} 733}
530 734
735#ifndef WCONTINUED
736# define WCONTINUED 0
737#endif
738
531static void 739static void
532childcb (EV_P_ struct ev_signal *sw, int revents) 740childcb (EV_P_ ev_signal *sw, int revents)
533{ 741{
534 int pid, status; 742 int pid, status;
535 743
744 /* some systems define WCONTINUED but then fail to support it (linux 2.4) */
536 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 745 if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
537 { 746 if (!WCONTINUED
747 || errno != EINVAL
748 || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED)))
749 return;
750
538 /* make sure we are called again until all childs have been reaped */ 751 /* 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 */
539 event (EV_A_ (W)sw, EV_SIGNAL); 753 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
540 754
541 child_reap (EV_A_ sw, pid, pid, status); 755 child_reap (EV_A_ sw, pid, pid, status);
542 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ 756 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
543 }
544} 757}
545 758
546#endif 759#endif
547 760
548/*****************************************************************************/ 761/*****************************************************************************/
549 762
763#if EV_USE_PORT
764# include "ev_port.c"
765#endif
550#if EV_USE_KQUEUE 766#if EV_USE_KQUEUE
551# include "ev_kqueue.c" 767# include "ev_kqueue.c"
552#endif 768#endif
553#if EV_USE_EPOLL 769#if EV_USE_EPOLL
554# include "ev_epoll.c" 770# include "ev_epoll.c"
571{ 787{
572 return EV_VERSION_MINOR; 788 return EV_VERSION_MINOR;
573} 789}
574 790
575/* return true if we are running with elevated privileges and should ignore env variables */ 791/* return true if we are running with elevated privileges and should ignore env variables */
576static int 792int inline_size
577enable_secure (void) 793enable_secure (void)
578{ 794{
579#ifdef WIN32 795#ifdef _WIN32
580 return 0; 796 return 0;
581#else 797#else
582 return getuid () != geteuid () 798 return getuid () != geteuid ()
583 || getgid () != getegid (); 799 || getgid () != getegid ();
584#endif 800#endif
585} 801}
586 802
587int 803unsigned int
588ev_method (EV_P) 804ev_supported_backends (void)
589{ 805{
590 return method; 806 unsigned int flags = 0;
807
808 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
809 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
810 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
811 if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
812 if (EV_USE_SELECT) flags |= EVBACKEND_SELECT;
813
814 return flags;
815}
816
817unsigned int
818ev_recommended_backends (void)
819{
820 unsigned int flags = ev_supported_backends ();
821
822#ifndef __NetBSD__
823 /* kqueue is borked on everything but netbsd apparently */
824 /* it usually doesn't work correctly on anything but sockets and pipes */
825 flags &= ~EVBACKEND_KQUEUE;
826#endif
827#ifdef __APPLE__
828 // flags &= ~EVBACKEND_KQUEUE; for documentation
829 flags &= ~EVBACKEND_POLL;
830#endif
831
832 return flags;
833}
834
835unsigned int
836ev_embeddable_backends (void)
837{
838 return EVBACKEND_EPOLL
839 | EVBACKEND_KQUEUE
840 | EVBACKEND_PORT;
841}
842
843unsigned int
844ev_backend (EV_P)
845{
846 return backend;
591} 847}
592 848
593static void 849static void
594loop_init (EV_P_ int methods) 850loop_init (EV_P_ unsigned int flags)
595{ 851{
596 if (!method) 852 if (!backend)
597 { 853 {
598#if EV_USE_MONOTONIC 854#if EV_USE_MONOTONIC
599 { 855 {
600 struct timespec ts; 856 struct timespec ts;
601 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 857 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
602 have_monotonic = 1; 858 have_monotonic = 1;
603 } 859 }
604#endif 860#endif
605 861
606 rt_now = ev_time (); 862 ev_rt_now = ev_time ();
607 mn_now = get_clock (); 863 mn_now = get_clock ();
608 now_floor = mn_now; 864 now_floor = mn_now;
609 rtmn_diff = rt_now - mn_now; 865 rtmn_diff = ev_rt_now - mn_now;
610 866
611 if (methods == EVMETHOD_AUTO) 867 if (!(flags & EVFLAG_NOENV)
612 if (!enable_secure () && getenv ("LIBEV_METHODS")) 868 && !enable_secure ()
869 && getenv ("LIBEV_FLAGS"))
613 methods = atoi (getenv ("LIBEV_METHODS")); 870 flags = atoi (getenv ("LIBEV_FLAGS"));
614 else
615 methods = EVMETHOD_ANY;
616 871
617 method = 0; 872 if (!(flags & 0x0000ffffUL))
618#if EV_USE_WIN32 873 flags |= ev_recommended_backends ();
619 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); 874
875 backend = 0;
876#if EV_USE_PORT
877 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
620#endif 878#endif
621#if EV_USE_KQUEUE 879#if EV_USE_KQUEUE
622 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 880 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
623#endif 881#endif
624#if EV_USE_EPOLL 882#if EV_USE_EPOLL
625 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 883 if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
626#endif 884#endif
627#if EV_USE_POLL 885#if EV_USE_POLL
628 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 886 if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags);
629#endif 887#endif
630#if EV_USE_SELECT 888#if EV_USE_SELECT
631 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 889 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
632#endif 890#endif
633 }
634}
635 891
636void 892 ev_init (&sigev, sigcb);
893 ev_set_priority (&sigev, EV_MAXPRI);
894 }
895}
896
897static void
637loop_destroy (EV_P) 898loop_destroy (EV_P)
638{ 899{
639#if EV_USE_WIN32 900 int i;
640 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); 901
902#if EV_USE_PORT
903 if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
641#endif 904#endif
642#if EV_USE_KQUEUE 905#if EV_USE_KQUEUE
643 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 906 if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A);
644#endif 907#endif
645#if EV_USE_EPOLL 908#if EV_USE_EPOLL
646 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 909 if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
647#endif 910#endif
648#if EV_USE_POLL 911#if EV_USE_POLL
649 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 912 if (backend == EVBACKEND_POLL ) poll_destroy (EV_A);
650#endif 913#endif
651#if EV_USE_SELECT 914#if EV_USE_SELECT
652 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 915 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
653#endif 916#endif
654 917
655 method = 0; 918 for (i = NUMPRI; i--; )
656 /*TODO*/ 919 array_free (pending, [i]);
657}
658 920
659void 921 /* have to use the microsoft-never-gets-it-right macro */
922 array_free (fdchange, EMPTY0);
923 array_free (timer, EMPTY0);
924#if EV_PERIODIC_ENABLE
925 array_free (periodic, EMPTY0);
926#endif
927 array_free (idle, EMPTY0);
928 array_free (prepare, EMPTY0);
929 array_free (check, EMPTY0);
930
931 backend = 0;
932}
933
934static void
660loop_fork (EV_P) 935loop_fork (EV_P)
661{ 936{
662 /*TODO*/ 937#if EV_USE_PORT
938 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
939#endif
940#if EV_USE_KQUEUE
941 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
942#endif
663#if EV_USE_EPOLL 943#if EV_USE_EPOLL
664 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 944 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
665#endif 945#endif
666#if EV_USE_KQUEUE 946
667 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 947 if (ev_is_active (&sigev))
668#endif 948 {
949 /* default loop */
950
951 ev_ref (EV_A);
952 ev_io_stop (EV_A_ &sigev);
953 close (sigpipe [0]);
954 close (sigpipe [1]);
955
956 while (pipe (sigpipe))
957 syserr ("(libev) error creating pipe");
958
959 siginit (EV_A);
960 }
961
962 postfork = 0;
669} 963}
670 964
671#if EV_MULTIPLICITY 965#if EV_MULTIPLICITY
672struct ev_loop * 966struct ev_loop *
673ev_loop_new (int methods) 967ev_loop_new (unsigned int flags)
674{ 968{
675 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); 969 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
676 970
971 memset (loop, 0, sizeof (struct ev_loop));
972
677 loop_init (EV_A_ methods); 973 loop_init (EV_A_ flags);
678 974
679 if (ev_method (EV_A)) 975 if (ev_backend (EV_A))
680 return loop; 976 return loop;
681 977
682 return 0; 978 return 0;
683} 979}
684 980
685void 981void
686ev_loop_destroy (EV_P) 982ev_loop_destroy (EV_P)
687{ 983{
688 loop_destroy (EV_A); 984 loop_destroy (EV_A);
689 free (loop); 985 ev_free (loop);
690} 986}
691 987
692void 988void
693ev_loop_fork (EV_P) 989ev_loop_fork (EV_P)
694{ 990{
695 loop_fork (EV_A); 991 postfork = 1;
696} 992}
697 993
698#endif 994#endif
699 995
700#if EV_MULTIPLICITY 996#if EV_MULTIPLICITY
701struct ev_loop default_loop_struct;
702static struct ev_loop *default_loop;
703
704struct ev_loop * 997struct ev_loop *
998ev_default_loop_init (unsigned int flags)
705#else 999#else
706static int default_loop;
707
708int 1000int
1001ev_default_loop (unsigned int flags)
709#endif 1002#endif
710ev_default_loop (int methods)
711{ 1003{
712 if (sigpipe [0] == sigpipe [1]) 1004 if (sigpipe [0] == sigpipe [1])
713 if (pipe (sigpipe)) 1005 if (pipe (sigpipe))
714 return 0; 1006 return 0;
715 1007
716 if (!default_loop) 1008 if (!ev_default_loop_ptr)
717 { 1009 {
718#if EV_MULTIPLICITY 1010#if EV_MULTIPLICITY
719 struct ev_loop *loop = default_loop = &default_loop_struct; 1011 struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct;
720#else 1012#else
721 default_loop = 1; 1013 ev_default_loop_ptr = 1;
722#endif 1014#endif
723 1015
724 loop_init (EV_A_ methods); 1016 loop_init (EV_A_ flags);
725 1017
726 if (ev_method (EV_A)) 1018 if (ev_backend (EV_A))
727 { 1019 {
728 ev_watcher_init (&sigev, sigcb);
729 ev_set_priority (&sigev, EV_MAXPRI);
730 siginit (EV_A); 1020 siginit (EV_A);
731 1021
732#ifndef WIN32 1022#ifndef _WIN32
733 ev_signal_init (&childev, childcb, SIGCHLD); 1023 ev_signal_init (&childev, childcb, SIGCHLD);
734 ev_set_priority (&childev, EV_MAXPRI); 1024 ev_set_priority (&childev, EV_MAXPRI);
735 ev_signal_start (EV_A_ &childev); 1025 ev_signal_start (EV_A_ &childev);
736 ev_unref (EV_A); /* child watcher should not keep loop alive */ 1026 ev_unref (EV_A); /* child watcher should not keep loop alive */
737#endif 1027#endif
738 } 1028 }
739 else 1029 else
740 default_loop = 0; 1030 ev_default_loop_ptr = 0;
741 } 1031 }
742 1032
743 return default_loop; 1033 return ev_default_loop_ptr;
744} 1034}
745 1035
746void 1036void
747ev_default_destroy (void) 1037ev_default_destroy (void)
748{ 1038{
749#if EV_MULTIPLICITY 1039#if EV_MULTIPLICITY
750 struct ev_loop *loop = default_loop; 1040 struct ev_loop *loop = ev_default_loop_ptr;
751#endif 1041#endif
752 1042
1043#ifndef _WIN32
753 ev_ref (EV_A); /* child watcher */ 1044 ev_ref (EV_A); /* child watcher */
754 ev_signal_stop (EV_A_ &childev); 1045 ev_signal_stop (EV_A_ &childev);
1046#endif
755 1047
756 ev_ref (EV_A); /* signal watcher */ 1048 ev_ref (EV_A); /* signal watcher */
757 ev_io_stop (EV_A_ &sigev); 1049 ev_io_stop (EV_A_ &sigev);
758 1050
759 close (sigpipe [0]); sigpipe [0] = 0; 1051 close (sigpipe [0]); sigpipe [0] = 0;
764 1056
765void 1057void
766ev_default_fork (void) 1058ev_default_fork (void)
767{ 1059{
768#if EV_MULTIPLICITY 1060#if EV_MULTIPLICITY
769 struct ev_loop *loop = default_loop; 1061 struct ev_loop *loop = ev_default_loop_ptr;
770#endif 1062#endif
771 1063
772 loop_fork (EV_A); 1064 if (backend)
773 1065 postfork = 1;
774 ev_io_stop (EV_A_ &sigev);
775 close (sigpipe [0]);
776 close (sigpipe [1]);
777 pipe (sigpipe);
778
779 ev_ref (EV_A); /* signal watcher */
780 siginit (EV_A);
781} 1066}
782 1067
783/*****************************************************************************/ 1068/*****************************************************************************/
784 1069
785static void 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
786call_pending (EV_P) 1083call_pending (EV_P)
787{ 1084{
788 int pri; 1085 int pri;
789 1086
790 for (pri = NUMPRI; pri--; ) 1087 for (pri = NUMPRI; pri--; )
791 while (pendingcnt [pri]) 1088 while (pendingcnt [pri])
792 { 1089 {
793 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1090 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
794 1091
795 if (p->w) 1092 if (expect_true (p->w))
796 { 1093 {
1094 assert (("non-pending watcher on pending list", p->w->pending));
1095
797 p->w->pending = 0; 1096 p->w->pending = 0;
798 1097 EV_CB_INVOKE (p->w, p->events);
799 (*(void (**)(EV_P_ W, int))&p->w->cb) (EV_A_ p->w, p->events);
800 } 1098 }
801 } 1099 }
802} 1100}
803 1101
804static void 1102void inline_size
805timers_reify (EV_P) 1103timers_reify (EV_P)
806{ 1104{
807 while (timercnt && ((WT)timers [0])->at <= mn_now) 1105 while (timercnt && ((WT)timers [0])->at <= mn_now)
808 { 1106 {
809 struct ev_timer *w = timers [0]; 1107 ev_timer *w = timers [0];
810 1108
811 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1109 assert (("inactive timer on timer heap detected", ev_is_active (w)));
812 1110
813 /* first reschedule or stop timer */ 1111 /* first reschedule or stop timer */
814 if (w->repeat) 1112 if (w->repeat)
815 { 1113 {
816 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 1114 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1115
817 ((WT)w)->at = mn_now + w->repeat; 1116 ((WT)w)->at += w->repeat;
1117 if (((WT)w)->at < mn_now)
1118 ((WT)w)->at = mn_now;
1119
818 downheap ((WT *)timers, timercnt, 0); 1120 downheap ((WT *)timers, timercnt, 0);
819 } 1121 }
820 else 1122 else
821 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1123 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
822 1124
823 event (EV_A_ (W)w, EV_TIMEOUT); 1125 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
824 } 1126 }
825} 1127}
826 1128
827static void 1129#if EV_PERIODIC_ENABLE
1130void inline_size
828periodics_reify (EV_P) 1131periodics_reify (EV_P)
829{ 1132{
830 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 1133 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
831 { 1134 {
832 struct ev_periodic *w = periodics [0]; 1135 ev_periodic *w = periodics [0];
833 1136
834 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1137 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
835 1138
836 /* first reschedule or stop timer */ 1139 /* first reschedule or stop timer */
837 if (w->interval) 1140 if (w->reschedule_cb)
838 { 1141 {
1142 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
1143 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1144 downheap ((WT *)periodics, periodiccnt, 0);
1145 }
1146 else if (w->interval)
1147 {
839 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1148 ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
840 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); 1149 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
841 downheap ((WT *)periodics, periodiccnt, 0); 1150 downheap ((WT *)periodics, periodiccnt, 0);
842 } 1151 }
843 else 1152 else
844 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1153 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
845 1154
846 event (EV_A_ (W)w, EV_PERIODIC); 1155 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
847 } 1156 }
848} 1157}
849 1158
850static void 1159static void noinline
851periodics_reschedule (EV_P) 1160periodics_reschedule (EV_P)
852{ 1161{
853 int i; 1162 int i;
854 1163
855 /* adjust periodics after time jump */ 1164 /* adjust periodics after time jump */
856 for (i = 0; i < periodiccnt; ++i) 1165 for (i = 0; i < periodiccnt; ++i)
857 { 1166 {
858 struct ev_periodic *w = periodics [i]; 1167 ev_periodic *w = periodics [i];
859 1168
1169 if (w->reschedule_cb)
1170 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
860 if (w->interval) 1171 else if (w->interval)
861 {
862 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1172 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
863
864 if (fabs (diff) >= 1e-4)
865 {
866 ev_periodic_stop (EV_A_ w);
867 ev_periodic_start (EV_A_ w);
868
869 i = 0; /* restart loop, inefficient, but time jumps should be rare */
870 }
871 }
872 } 1173 }
873}
874 1174
875inline int 1175 /* now rebuild the heap */
1176 for (i = periodiccnt >> 1; i--; )
1177 downheap ((WT *)periodics, periodiccnt, i);
1178}
1179#endif
1180
1181int inline_size
876time_update_monotonic (EV_P) 1182time_update_monotonic (EV_P)
877{ 1183{
878 mn_now = get_clock (); 1184 mn_now = get_clock ();
879 1185
880 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1186 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
881 { 1187 {
882 rt_now = rtmn_diff + mn_now; 1188 ev_rt_now = rtmn_diff + mn_now;
883 return 0; 1189 return 0;
884 } 1190 }
885 else 1191 else
886 { 1192 {
887 now_floor = mn_now; 1193 now_floor = mn_now;
888 rt_now = ev_time (); 1194 ev_rt_now = ev_time ();
889 return 1; 1195 return 1;
890 } 1196 }
891} 1197}
892 1198
893static void 1199void inline_size
894time_update (EV_P) 1200time_update (EV_P)
895{ 1201{
896 int i; 1202 int i;
897 1203
898#if EV_USE_MONOTONIC 1204#if EV_USE_MONOTONIC
900 { 1206 {
901 if (time_update_monotonic (EV_A)) 1207 if (time_update_monotonic (EV_A))
902 { 1208 {
903 ev_tstamp odiff = rtmn_diff; 1209 ev_tstamp odiff = rtmn_diff;
904 1210
905 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1211 /* loop a few times, before making important decisions.
1212 * on the choice of "4": one iteration isn't enough,
1213 * in case we get preempted during the calls to
1214 * ev_time and get_clock. a second call is almost guarenteed
1215 * 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
1217 * in the unlikely event of getting preempted here.
1218 */
1219 for (i = 4; --i; )
906 { 1220 {
907 rtmn_diff = rt_now - mn_now; 1221 rtmn_diff = ev_rt_now - mn_now;
908 1222
909 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1223 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
910 return; /* all is well */ 1224 return; /* all is well */
911 1225
912 rt_now = ev_time (); 1226 ev_rt_now = ev_time ();
913 mn_now = get_clock (); 1227 mn_now = get_clock ();
914 now_floor = mn_now; 1228 now_floor = mn_now;
915 } 1229 }
916 1230
1231# if EV_PERIODIC_ENABLE
917 periodics_reschedule (EV_A); 1232 periodics_reschedule (EV_A);
1233# endif
918 /* no timer adjustment, as the monotonic clock doesn't jump */ 1234 /* no timer adjustment, as the monotonic clock doesn't jump */
919 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1235 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
920 } 1236 }
921 } 1237 }
922 else 1238 else
923#endif 1239#endif
924 { 1240 {
925 rt_now = ev_time (); 1241 ev_rt_now = ev_time ();
926 1242
927 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1243 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
928 { 1244 {
1245#if EV_PERIODIC_ENABLE
929 periodics_reschedule (EV_A); 1246 periodics_reschedule (EV_A);
1247#endif
930 1248
931 /* adjust timers. this is easy, as the offset is the same for all */ 1249 /* adjust timers. this is easy, as the offset is the same for all */
932 for (i = 0; i < timercnt; ++i) 1250 for (i = 0; i < timercnt; ++i)
933 ((WT)timers [i])->at += rt_now - mn_now; 1251 ((WT)timers [i])->at += ev_rt_now - mn_now;
934 } 1252 }
935 1253
936 mn_now = rt_now; 1254 mn_now = ev_rt_now;
937 } 1255 }
938} 1256}
939 1257
940void 1258void
941ev_ref (EV_P) 1259ev_ref (EV_P)
952static int loop_done; 1270static int loop_done;
953 1271
954void 1272void
955ev_loop (EV_P_ int flags) 1273ev_loop (EV_P_ int flags)
956{ 1274{
957 double block;
958 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1275 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1276 ? EVUNLOOP_ONE
1277 : EVUNLOOP_CANCEL;
959 1278
960 do 1279 while (activecnt)
961 { 1280 {
1281 /* we might have forked, so reify kernel state if necessary */
1282 if (expect_false (postfork))
1283 if (forkcnt)
1284 {
1285 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1286 call_pending (EV_A);
1287 }
1288
962 /* queue check watchers (and execute them) */ 1289 /* queue check watchers (and execute them) */
963 if (expect_false (preparecnt)) 1290 if (expect_false (preparecnt))
964 { 1291 {
965 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1292 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
966 call_pending (EV_A); 1293 call_pending (EV_A);
967 } 1294 }
968 1295
1296 /* we might have forked, so reify kernel state if necessary */
1297 if (expect_false (postfork))
1298 loop_fork (EV_A);
1299
969 /* update fd-related kernel structures */ 1300 /* update fd-related kernel structures */
970 fd_reify (EV_A); 1301 fd_reify (EV_A);
971 1302
972 /* calculate blocking time */ 1303 /* calculate blocking time */
1304 {
1305 double block;
973 1306
974 /* we only need this for !monotonic clockor timers, but as we basically 1307 if (flags & EVLOOP_NONBLOCK || idlecnt)
975 always have timers, we just calculate it always */ 1308 block = 0.; /* do not block at all */
1309 else
1310 {
1311 /* update time to cancel out callback processing overhead */
976#if EV_USE_MONOTONIC 1312#if EV_USE_MONOTONIC
977 if (expect_true (have_monotonic)) 1313 if (expect_true (have_monotonic))
978 time_update_monotonic (EV_A); 1314 time_update_monotonic (EV_A);
979 else 1315 else
980#endif 1316#endif
981 { 1317 {
982 rt_now = ev_time (); 1318 ev_rt_now = ev_time ();
983 mn_now = rt_now; 1319 mn_now = ev_rt_now;
984 } 1320 }
985 1321
986 if (flags & EVLOOP_NONBLOCK || idlecnt)
987 block = 0.;
988 else
989 {
990 block = MAX_BLOCKTIME; 1322 block = MAX_BLOCKTIME;
991 1323
992 if (timercnt) 1324 if (timercnt)
993 { 1325 {
994 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1326 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
995 if (block > to) block = to; 1327 if (block > to) block = to;
996 } 1328 }
997 1329
1330#if EV_PERIODIC_ENABLE
998 if (periodiccnt) 1331 if (periodiccnt)
999 { 1332 {
1000 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1333 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
1001 if (block > to) block = to; 1334 if (block > to) block = to;
1002 } 1335 }
1336#endif
1003 1337
1004 if (block < 0.) block = 0.; 1338 if (expect_false (block < 0.)) block = 0.;
1005 } 1339 }
1006 1340
1007 method_poll (EV_A_ block); 1341 backend_poll (EV_A_ block);
1342 }
1008 1343
1009 /* update rt_now, do magic */ 1344 /* update ev_rt_now, do magic */
1010 time_update (EV_A); 1345 time_update (EV_A);
1011 1346
1012 /* queue pending timers and reschedule them */ 1347 /* queue pending timers and reschedule them */
1013 timers_reify (EV_A); /* relative timers called last */ 1348 timers_reify (EV_A); /* relative timers called last */
1349#if EV_PERIODIC_ENABLE
1014 periodics_reify (EV_A); /* absolute timers called first */ 1350 periodics_reify (EV_A); /* absolute timers called first */
1351#endif
1015 1352
1016 /* queue idle watchers unless io or timers are pending */ 1353 /* queue idle watchers unless other events are pending */
1017 if (!pendingcnt) 1354 if (idlecnt && !any_pending (EV_A))
1018 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1355 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1019 1356
1020 /* queue check watchers, to be executed first */ 1357 /* queue check watchers, to be executed first */
1021 if (checkcnt) 1358 if (expect_false (checkcnt))
1022 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1359 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1023 1360
1024 call_pending (EV_A); 1361 call_pending (EV_A);
1025 }
1026 while (activecnt && !loop_done);
1027 1362
1028 if (loop_done != 2) 1363 if (expect_false (loop_done))
1029 loop_done = 0; 1364 break;
1365 }
1366
1367 if (loop_done == EVUNLOOP_ONE)
1368 loop_done = EVUNLOOP_CANCEL;
1030} 1369}
1031 1370
1032void 1371void
1033ev_unloop (EV_P_ int how) 1372ev_unloop (EV_P_ int how)
1034{ 1373{
1035 loop_done = how; 1374 loop_done = how;
1036} 1375}
1037 1376
1038/*****************************************************************************/ 1377/*****************************************************************************/
1039 1378
1040inline void 1379void inline_size
1041wlist_add (WL *head, WL elem) 1380wlist_add (WL *head, WL elem)
1042{ 1381{
1043 elem->next = *head; 1382 elem->next = *head;
1044 *head = elem; 1383 *head = elem;
1045} 1384}
1046 1385
1047inline void 1386void inline_size
1048wlist_del (WL *head, WL elem) 1387wlist_del (WL *head, WL elem)
1049{ 1388{
1050 while (*head) 1389 while (*head)
1051 { 1390 {
1052 if (*head == elem) 1391 if (*head == elem)
1057 1396
1058 head = &(*head)->next; 1397 head = &(*head)->next;
1059 } 1398 }
1060} 1399}
1061 1400
1062inline void 1401void inline_speed
1063ev_clear_pending (EV_P_ W w) 1402ev_clear_pending (EV_P_ W w)
1064{ 1403{
1065 if (w->pending) 1404 if (w->pending)
1066 { 1405 {
1067 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1406 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1068 w->pending = 0; 1407 w->pending = 0;
1069 } 1408 }
1070} 1409}
1071 1410
1072inline void 1411void inline_speed
1073ev_start (EV_P_ W w, int active) 1412ev_start (EV_P_ W w, int active)
1074{ 1413{
1075 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1414 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
1076 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; 1415 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1077 1416
1078 w->active = active; 1417 w->active = active;
1079 ev_ref (EV_A); 1418 ev_ref (EV_A);
1080} 1419}
1081 1420
1082inline void 1421void inline_size
1083ev_stop (EV_P_ W w) 1422ev_stop (EV_P_ W w)
1084{ 1423{
1085 ev_unref (EV_A); 1424 ev_unref (EV_A);
1086 w->active = 0; 1425 w->active = 0;
1087} 1426}
1088 1427
1089/*****************************************************************************/ 1428/*****************************************************************************/
1090 1429
1091void 1430void
1092ev_io_start (EV_P_ struct ev_io *w) 1431ev_io_start (EV_P_ ev_io *w)
1093{ 1432{
1094 int fd = w->fd; 1433 int fd = w->fd;
1095 1434
1096 if (ev_is_active (w)) 1435 if (expect_false (ev_is_active (w)))
1097 return; 1436 return;
1098 1437
1099 assert (("ev_io_start called with negative fd", fd >= 0)); 1438 assert (("ev_io_start called with negative fd", fd >= 0));
1100 1439
1101 ev_start (EV_A_ (W)w, 1); 1440 ev_start (EV_A_ (W)w, 1);
1102 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 1441 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1103 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1442 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1104 1443
1105 fd_change (EV_A_ fd); 1444 fd_change (EV_A_ fd);
1106} 1445}
1107 1446
1108void 1447void
1109ev_io_stop (EV_P_ struct ev_io *w) 1448ev_io_stop (EV_P_ ev_io *w)
1110{ 1449{
1111 ev_clear_pending (EV_A_ (W)w); 1450 ev_clear_pending (EV_A_ (W)w);
1112 if (!ev_is_active (w)) 1451 if (expect_false (!ev_is_active (w)))
1113 return; 1452 return;
1453
1454 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1114 1455
1115 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1456 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1116 ev_stop (EV_A_ (W)w); 1457 ev_stop (EV_A_ (W)w);
1117 1458
1118 fd_change (EV_A_ w->fd); 1459 fd_change (EV_A_ w->fd);
1119} 1460}
1120 1461
1121void 1462void
1122ev_timer_start (EV_P_ struct ev_timer *w) 1463ev_timer_start (EV_P_ ev_timer *w)
1123{ 1464{
1124 if (ev_is_active (w)) 1465 if (expect_false (ev_is_active (w)))
1125 return; 1466 return;
1126 1467
1127 ((WT)w)->at += mn_now; 1468 ((WT)w)->at += mn_now;
1128 1469
1129 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1470 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1130 1471
1131 ev_start (EV_A_ (W)w, ++timercnt); 1472 ev_start (EV_A_ (W)w, ++timercnt);
1132 array_needsize (timers, timermax, timercnt, ); 1473 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1133 timers [timercnt - 1] = w; 1474 timers [timercnt - 1] = w;
1134 upheap ((WT *)timers, timercnt - 1); 1475 upheap ((WT *)timers, timercnt - 1);
1135 1476
1136 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1477 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1137} 1478}
1138 1479
1139void 1480void
1140ev_timer_stop (EV_P_ struct ev_timer *w) 1481ev_timer_stop (EV_P_ ev_timer *w)
1141{ 1482{
1142 ev_clear_pending (EV_A_ (W)w); 1483 ev_clear_pending (EV_A_ (W)w);
1143 if (!ev_is_active (w)) 1484 if (expect_false (!ev_is_active (w)))
1144 return; 1485 return;
1145 1486
1146 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1487 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1147 1488
1148 if (((W)w)->active < timercnt--) 1489 if (expect_true (((W)w)->active < timercnt--))
1149 { 1490 {
1150 timers [((W)w)->active - 1] = timers [timercnt]; 1491 timers [((W)w)->active - 1] = timers [timercnt];
1151 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1492 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1152 } 1493 }
1153 1494
1154 ((WT)w)->at = w->repeat; 1495 ((WT)w)->at -= mn_now;
1155 1496
1156 ev_stop (EV_A_ (W)w); 1497 ev_stop (EV_A_ (W)w);
1157} 1498}
1158 1499
1159void 1500void
1160ev_timer_again (EV_P_ struct ev_timer *w) 1501ev_timer_again (EV_P_ ev_timer *w)
1161{ 1502{
1162 if (ev_is_active (w)) 1503 if (ev_is_active (w))
1163 { 1504 {
1164 if (w->repeat) 1505 if (w->repeat)
1165 { 1506 {
1166 ((WT)w)->at = mn_now + w->repeat; 1507 ((WT)w)->at = mn_now + w->repeat;
1167 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1508 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1168 } 1509 }
1169 else 1510 else
1170 ev_timer_stop (EV_A_ w); 1511 ev_timer_stop (EV_A_ w);
1171 } 1512 }
1172 else if (w->repeat) 1513 else if (w->repeat)
1514 {
1515 w->at = w->repeat;
1173 ev_timer_start (EV_A_ w); 1516 ev_timer_start (EV_A_ w);
1517 }
1174} 1518}
1175 1519
1520#if EV_PERIODIC_ENABLE
1176void 1521void
1177ev_periodic_start (EV_P_ struct ev_periodic *w) 1522ev_periodic_start (EV_P_ ev_periodic *w)
1178{ 1523{
1179 if (ev_is_active (w)) 1524 if (expect_false (ev_is_active (w)))
1180 return; 1525 return;
1181 1526
1527 if (w->reschedule_cb)
1528 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1529 else if (w->interval)
1530 {
1182 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1531 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1183
1184 /* this formula differs from the one in periodic_reify because we do not always round up */ 1532 /* this formula differs from the one in periodic_reify because we do not always round up */
1185 if (w->interval)
1186 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1533 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1534 }
1187 1535
1188 ev_start (EV_A_ (W)w, ++periodiccnt); 1536 ev_start (EV_A_ (W)w, ++periodiccnt);
1189 array_needsize (periodics, periodicmax, periodiccnt, ); 1537 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1190 periodics [periodiccnt - 1] = w; 1538 periodics [periodiccnt - 1] = w;
1191 upheap ((WT *)periodics, periodiccnt - 1); 1539 upheap ((WT *)periodics, periodiccnt - 1);
1192 1540
1193 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1541 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1194} 1542}
1195 1543
1196void 1544void
1197ev_periodic_stop (EV_P_ struct ev_periodic *w) 1545ev_periodic_stop (EV_P_ ev_periodic *w)
1198{ 1546{
1199 ev_clear_pending (EV_A_ (W)w); 1547 ev_clear_pending (EV_A_ (W)w);
1200 if (!ev_is_active (w)) 1548 if (expect_false (!ev_is_active (w)))
1201 return; 1549 return;
1202 1550
1203 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1551 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1204 1552
1205 if (((W)w)->active < periodiccnt--) 1553 if (expect_true (((W)w)->active < periodiccnt--))
1206 { 1554 {
1207 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1555 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1208 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1556 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1209 } 1557 }
1210 1558
1211 ev_stop (EV_A_ (W)w); 1559 ev_stop (EV_A_ (W)w);
1212} 1560}
1213 1561
1214void 1562void
1215ev_idle_start (EV_P_ struct ev_idle *w) 1563ev_periodic_again (EV_P_ ev_periodic *w)
1216{ 1564{
1217 if (ev_is_active (w)) 1565 /* TODO: use adjustheap and recalculation */
1218 return;
1219
1220 ev_start (EV_A_ (W)w, ++idlecnt);
1221 array_needsize (idles, idlemax, idlecnt, );
1222 idles [idlecnt - 1] = w;
1223}
1224
1225void
1226ev_idle_stop (EV_P_ struct ev_idle *w)
1227{
1228 ev_clear_pending (EV_A_ (W)w);
1229 if (ev_is_active (w))
1230 return;
1231
1232 idles [((W)w)->active - 1] = idles [--idlecnt];
1233 ev_stop (EV_A_ (W)w); 1566 ev_periodic_stop (EV_A_ w);
1567 ev_periodic_start (EV_A_ w);
1234} 1568}
1235 1569#endif
1236void
1237ev_prepare_start (EV_P_ struct ev_prepare *w)
1238{
1239 if (ev_is_active (w))
1240 return;
1241
1242 ev_start (EV_A_ (W)w, ++preparecnt);
1243 array_needsize (prepares, preparemax, preparecnt, );
1244 prepares [preparecnt - 1] = w;
1245}
1246
1247void
1248ev_prepare_stop (EV_P_ struct ev_prepare *w)
1249{
1250 ev_clear_pending (EV_A_ (W)w);
1251 if (ev_is_active (w))
1252 return;
1253
1254 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1255 ev_stop (EV_A_ (W)w);
1256}
1257
1258void
1259ev_check_start (EV_P_ struct ev_check *w)
1260{
1261 if (ev_is_active (w))
1262 return;
1263
1264 ev_start (EV_A_ (W)w, ++checkcnt);
1265 array_needsize (checks, checkmax, checkcnt, );
1266 checks [checkcnt - 1] = w;
1267}
1268
1269void
1270ev_check_stop (EV_P_ struct ev_check *w)
1271{
1272 ev_clear_pending (EV_A_ (W)w);
1273 if (ev_is_active (w))
1274 return;
1275
1276 checks [((W)w)->active - 1] = checks [--checkcnt];
1277 ev_stop (EV_A_ (W)w);
1278}
1279 1570
1280#ifndef SA_RESTART 1571#ifndef SA_RESTART
1281# define SA_RESTART 0 1572# define SA_RESTART 0
1282#endif 1573#endif
1283 1574
1284void 1575void
1285ev_signal_start (EV_P_ struct ev_signal *w) 1576ev_signal_start (EV_P_ ev_signal *w)
1286{ 1577{
1287#if EV_MULTIPLICITY 1578#if EV_MULTIPLICITY
1288 assert (("signal watchers are only supported in the default loop", loop == default_loop)); 1579 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1289#endif 1580#endif
1290 if (ev_is_active (w)) 1581 if (expect_false (ev_is_active (w)))
1291 return; 1582 return;
1292 1583
1293 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1584 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1294 1585
1295 ev_start (EV_A_ (W)w, 1); 1586 ev_start (EV_A_ (W)w, 1);
1296 array_needsize (signals, signalmax, w->signum, signals_init); 1587 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1297 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1588 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1298 1589
1299 if (!((WL)w)->next) 1590 if (!((WL)w)->next)
1300 { 1591 {
1592#if _WIN32
1593 signal (w->signum, sighandler);
1594#else
1301 struct sigaction sa; 1595 struct sigaction sa;
1302 sa.sa_handler = sighandler; 1596 sa.sa_handler = sighandler;
1303 sigfillset (&sa.sa_mask); 1597 sigfillset (&sa.sa_mask);
1304 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1598 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1305 sigaction (w->signum, &sa, 0); 1599 sigaction (w->signum, &sa, 0);
1600#endif
1306 } 1601 }
1307} 1602}
1308 1603
1309void 1604void
1310ev_signal_stop (EV_P_ struct ev_signal *w) 1605ev_signal_stop (EV_P_ ev_signal *w)
1311{ 1606{
1312 ev_clear_pending (EV_A_ (W)w); 1607 ev_clear_pending (EV_A_ (W)w);
1313 if (!ev_is_active (w)) 1608 if (expect_false (!ev_is_active (w)))
1314 return; 1609 return;
1315 1610
1316 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1611 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1317 ev_stop (EV_A_ (W)w); 1612 ev_stop (EV_A_ (W)w);
1318 1613
1319 if (!signals [w->signum - 1].head) 1614 if (!signals [w->signum - 1].head)
1320 signal (w->signum, SIG_DFL); 1615 signal (w->signum, SIG_DFL);
1321} 1616}
1322 1617
1323void 1618void
1324ev_child_start (EV_P_ struct ev_child *w) 1619ev_child_start (EV_P_ ev_child *w)
1325{ 1620{
1326#if EV_MULTIPLICITY 1621#if EV_MULTIPLICITY
1327 assert (("child watchers are only supported in the default loop", loop == default_loop)); 1622 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1328#endif 1623#endif
1329 if (ev_is_active (w)) 1624 if (expect_false (ev_is_active (w)))
1330 return; 1625 return;
1331 1626
1332 ev_start (EV_A_ (W)w, 1); 1627 ev_start (EV_A_ (W)w, 1);
1333 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1628 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1334} 1629}
1335 1630
1336void 1631void
1337ev_child_stop (EV_P_ struct ev_child *w) 1632ev_child_stop (EV_P_ ev_child *w)
1338{ 1633{
1339 ev_clear_pending (EV_A_ (W)w); 1634 ev_clear_pending (EV_A_ (W)w);
1340 if (ev_is_active (w)) 1635 if (expect_false (!ev_is_active (w)))
1341 return; 1636 return;
1342 1637
1343 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1638 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1344 ev_stop (EV_A_ (W)w); 1639 ev_stop (EV_A_ (W)w);
1345} 1640}
1346 1641
1642#if EV_STAT_ENABLE
1643
1644# ifdef _WIN32
1645# undef lstat
1646# define lstat(a,b) _stati64 (a,b)
1647# endif
1648
1649#define DEF_STAT_INTERVAL 5.0074891
1650#define MIN_STAT_INTERVAL 0.1074891
1651
1652void
1653ev_stat_stat (EV_P_ ev_stat *w)
1654{
1655 if (lstat (w->path, &w->attr) < 0)
1656 w->attr.st_nlink = 0;
1657 else if (!w->attr.st_nlink)
1658 w->attr.st_nlink = 1;
1659}
1660
1661static void
1662stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1663{
1664 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1665
1666 /* we copy this here each the time so that */
1667 /* prev has the old value when the callback gets invoked */
1668 w->prev = w->attr;
1669 ev_stat_stat (EV_A_ w);
1670
1671 if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata)))
1672 ev_feed_event (EV_A_ w, EV_STAT);
1673}
1674
1675void
1676ev_stat_start (EV_P_ ev_stat *w)
1677{
1678 if (expect_false (ev_is_active (w)))
1679 return;
1680
1681 /* since we use memcmp, we need to clear any padding data etc. */
1682 memset (&w->prev, 0, sizeof (ev_statdata));
1683 memset (&w->attr, 0, sizeof (ev_statdata));
1684
1685 ev_stat_stat (EV_A_ w);
1686
1687 if (w->interval < MIN_STAT_INTERVAL)
1688 w->interval = w->interval ? MIN_STAT_INTERVAL : DEF_STAT_INTERVAL;
1689
1690 ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval);
1691 ev_set_priority (&w->timer, ev_priority (w));
1692 ev_timer_start (EV_A_ &w->timer);
1693
1694 ev_start (EV_A_ (W)w, 1);
1695}
1696
1697void
1698ev_stat_stop (EV_P_ ev_stat *w)
1699{
1700 ev_clear_pending (EV_A_ (W)w);
1701 if (expect_false (!ev_is_active (w)))
1702 return;
1703
1704 ev_timer_stop (EV_A_ &w->timer);
1705
1706 ev_stop (EV_A_ (W)w);
1707}
1708#endif
1709
1710void
1711ev_idle_start (EV_P_ ev_idle *w)
1712{
1713 if (expect_false (ev_is_active (w)))
1714 return;
1715
1716 ev_start (EV_A_ (W)w, ++idlecnt);
1717 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1718 idles [idlecnt - 1] = w;
1719}
1720
1721void
1722ev_idle_stop (EV_P_ ev_idle *w)
1723{
1724 ev_clear_pending (EV_A_ (W)w);
1725 if (expect_false (!ev_is_active (w)))
1726 return;
1727
1728 {
1729 int active = ((W)w)->active;
1730 idles [active - 1] = idles [--idlecnt];
1731 ((W)idles [active - 1])->active = active;
1732 }
1733
1734 ev_stop (EV_A_ (W)w);
1735}
1736
1737void
1738ev_prepare_start (EV_P_ ev_prepare *w)
1739{
1740 if (expect_false (ev_is_active (w)))
1741 return;
1742
1743 ev_start (EV_A_ (W)w, ++preparecnt);
1744 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1745 prepares [preparecnt - 1] = w;
1746}
1747
1748void
1749ev_prepare_stop (EV_P_ ev_prepare *w)
1750{
1751 ev_clear_pending (EV_A_ (W)w);
1752 if (expect_false (!ev_is_active (w)))
1753 return;
1754
1755 {
1756 int active = ((W)w)->active;
1757 prepares [active - 1] = prepares [--preparecnt];
1758 ((W)prepares [active - 1])->active = active;
1759 }
1760
1761 ev_stop (EV_A_ (W)w);
1762}
1763
1764void
1765ev_check_start (EV_P_ ev_check *w)
1766{
1767 if (expect_false (ev_is_active (w)))
1768 return;
1769
1770 ev_start (EV_A_ (W)w, ++checkcnt);
1771 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1772 checks [checkcnt - 1] = w;
1773}
1774
1775void
1776ev_check_stop (EV_P_ ev_check *w)
1777{
1778 ev_clear_pending (EV_A_ (W)w);
1779 if (expect_false (!ev_is_active (w)))
1780 return;
1781
1782 {
1783 int active = ((W)w)->active;
1784 checks [active - 1] = checks [--checkcnt];
1785 ((W)checks [active - 1])->active = active;
1786 }
1787
1788 ev_stop (EV_A_ (W)w);
1789}
1790
1791#if EV_EMBED_ENABLE
1792void noinline
1793ev_embed_sweep (EV_P_ ev_embed *w)
1794{
1795 ev_loop (w->loop, EVLOOP_NONBLOCK);
1796}
1797
1798static void
1799embed_cb (EV_P_ ev_io *io, int revents)
1800{
1801 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
1802
1803 if (ev_cb (w))
1804 ev_feed_event (EV_A_ (W)w, EV_EMBED);
1805 else
1806 ev_embed_sweep (loop, w);
1807}
1808
1809void
1810ev_embed_start (EV_P_ ev_embed *w)
1811{
1812 if (expect_false (ev_is_active (w)))
1813 return;
1814
1815 {
1816 struct ev_loop *loop = w->loop;
1817 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
1818 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ);
1819 }
1820
1821 ev_set_priority (&w->io, ev_priority (w));
1822 ev_io_start (EV_A_ &w->io);
1823
1824 ev_start (EV_A_ (W)w, 1);
1825}
1826
1827void
1828ev_embed_stop (EV_P_ ev_embed *w)
1829{
1830 ev_clear_pending (EV_A_ (W)w);
1831 if (expect_false (!ev_is_active (w)))
1832 return;
1833
1834 ev_io_stop (EV_A_ &w->io);
1835
1836 ev_stop (EV_A_ (W)w);
1837}
1838#endif
1839
1840#if EV_FORK_ENABLE
1841void
1842ev_fork_start (EV_P_ ev_fork *w)
1843{
1844 if (expect_false (ev_is_active (w)))
1845 return;
1846
1847 ev_start (EV_A_ (W)w, ++forkcnt);
1848 array_needsize (ev_fork *, forks, forkmax, forkcnt, EMPTY2);
1849 forks [forkcnt - 1] = w;
1850}
1851
1852void
1853ev_fork_stop (EV_P_ ev_fork *w)
1854{
1855 ev_clear_pending (EV_A_ (W)w);
1856 if (expect_false (!ev_is_active (w)))
1857 return;
1858
1859 {
1860 int active = ((W)w)->active;
1861 forks [active - 1] = forks [--forkcnt];
1862 ((W)forks [active - 1])->active = active;
1863 }
1864
1865 ev_stop (EV_A_ (W)w);
1866}
1867#endif
1868
1347/*****************************************************************************/ 1869/*****************************************************************************/
1348 1870
1349struct ev_once 1871struct ev_once
1350{ 1872{
1351 struct ev_io io; 1873 ev_io io;
1352 struct ev_timer to; 1874 ev_timer to;
1353 void (*cb)(int revents, void *arg); 1875 void (*cb)(int revents, void *arg);
1354 void *arg; 1876 void *arg;
1355}; 1877};
1356 1878
1357static void 1879static void
1360 void (*cb)(int revents, void *arg) = once->cb; 1882 void (*cb)(int revents, void *arg) = once->cb;
1361 void *arg = once->arg; 1883 void *arg = once->arg;
1362 1884
1363 ev_io_stop (EV_A_ &once->io); 1885 ev_io_stop (EV_A_ &once->io);
1364 ev_timer_stop (EV_A_ &once->to); 1886 ev_timer_stop (EV_A_ &once->to);
1365 free (once); 1887 ev_free (once);
1366 1888
1367 cb (revents, arg); 1889 cb (revents, arg);
1368} 1890}
1369 1891
1370static void 1892static void
1371once_cb_io (EV_P_ struct ev_io *w, int revents) 1893once_cb_io (EV_P_ ev_io *w, int revents)
1372{ 1894{
1373 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); 1895 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1374} 1896}
1375 1897
1376static void 1898static void
1377once_cb_to (EV_P_ struct ev_timer *w, int revents) 1899once_cb_to (EV_P_ ev_timer *w, int revents)
1378{ 1900{
1379 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); 1901 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1380} 1902}
1381 1903
1382void 1904void
1383ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1905ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1384{ 1906{
1385 struct ev_once *once = malloc (sizeof (struct ev_once)); 1907 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1386 1908
1387 if (!once) 1909 if (expect_false (!once))
1910 {
1388 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1911 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1389 else 1912 return;
1390 { 1913 }
1914
1391 once->cb = cb; 1915 once->cb = cb;
1392 once->arg = arg; 1916 once->arg = arg;
1393 1917
1394 ev_watcher_init (&once->io, once_cb_io); 1918 ev_init (&once->io, once_cb_io);
1395 if (fd >= 0) 1919 if (fd >= 0)
1396 { 1920 {
1397 ev_io_set (&once->io, fd, events); 1921 ev_io_set (&once->io, fd, events);
1398 ev_io_start (EV_A_ &once->io); 1922 ev_io_start (EV_A_ &once->io);
1399 } 1923 }
1400 1924
1401 ev_watcher_init (&once->to, once_cb_to); 1925 ev_init (&once->to, once_cb_to);
1402 if (timeout >= 0.) 1926 if (timeout >= 0.)
1403 { 1927 {
1404 ev_timer_set (&once->to, timeout, 0.); 1928 ev_timer_set (&once->to, timeout, 0.);
1405 ev_timer_start (EV_A_ &once->to); 1929 ev_timer_start (EV_A_ &once->to);
1406 }
1407 } 1930 }
1408} 1931}
1409 1932
1933#ifdef __cplusplus
1934}
1935#endif
1936

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines