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

Comparing libev/ev.c (file contents):
Revision 1.134 by root, Fri Nov 23 19:13:33 2007 UTC vs.
Revision 1.163 by root, Wed Dec 5 13:54:36 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>
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>
114 122
123#ifdef EV_H
124# include EV_H
125#else
126# include "ev.h"
127#endif
128
115#ifndef _WIN32 129#ifndef _WIN32
116# include <unistd.h>
117# include <sys/time.h> 130# include <sys/time.h>
118# include <sys/wait.h> 131# include <sys/wait.h>
132# include <unistd.h>
119#else 133#else
120# define WIN32_LEAN_AND_MEAN 134# define WIN32_LEAN_AND_MEAN
121# include <windows.h> 135# include <windows.h>
122# ifndef EV_SELECT_IS_WINSOCKET 136# ifndef EV_SELECT_IS_WINSOCKET
123# define EV_SELECT_IS_WINSOCKET 1 137# define EV_SELECT_IS_WINSOCKET 1
156 170
157#ifndef EV_USE_PORT 171#ifndef EV_USE_PORT
158# define EV_USE_PORT 0 172# define EV_USE_PORT 0
159#endif 173#endif
160 174
175#ifndef EV_USE_INOTIFY
176# define EV_USE_INOTIFY 0
177#endif
178
179#ifndef EV_PID_HASHSIZE
180# if EV_MINIMAL
181# define EV_PID_HASHSIZE 1
182# else
183# define EV_PID_HASHSIZE 16
184# endif
185#endif
186
187#ifndef EV_INOTIFY_HASHSIZE
188# if EV_MINIMAL
189# define EV_INOTIFY_HASHSIZE 1
190# else
191# define EV_INOTIFY_HASHSIZE 16
192# endif
193#endif
194
161/**/ 195/**/
162 196
163#ifndef CLOCK_MONOTONIC 197#ifndef CLOCK_MONOTONIC
164# undef EV_USE_MONOTONIC 198# undef EV_USE_MONOTONIC
165# define EV_USE_MONOTONIC 0 199# define EV_USE_MONOTONIC 0
172 206
173#if EV_SELECT_IS_WINSOCKET 207#if EV_SELECT_IS_WINSOCKET
174# include <winsock.h> 208# include <winsock.h>
175#endif 209#endif
176 210
211#if !EV_STAT_ENABLE
212# define EV_USE_INOTIFY 0
213#endif
214
215#if EV_USE_INOTIFY
216# include <sys/inotify.h>
217#endif
218
177/**/ 219/**/
178 220
179#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 221#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
180#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 222#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
181#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
182/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ 223/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
183
184#ifdef EV_H
185# include EV_H
186#else
187# include "ev.h"
188#endif
189 224
190#if __GNUC__ >= 3 225#if __GNUC__ >= 3
191# define expect(expr,value) __builtin_expect ((expr),(value)) 226# define expect(expr,value) __builtin_expect ((expr),(value))
227# define inline_size static inline /* inline for codesize */
228# if EV_MINIMAL
229# define noinline __attribute__ ((noinline))
230# define inline_speed static noinline
231# else
232# define noinline
192# define inline static inline 233# define inline_speed static inline
234# endif
193#else 235#else
194# define expect(expr,value) (expr) 236# define expect(expr,value) (expr)
237# define inline_speed static
195# define inline static 238# define inline_size static
239# define noinline
196#endif 240#endif
197 241
198#define expect_false(expr) expect ((expr) != 0, 0) 242#define expect_false(expr) expect ((expr) != 0, 0)
199#define expect_true(expr) expect ((expr) != 0, 1) 243#define expect_true(expr) expect ((expr) != 0, 1)
200 244
202#define ABSPRI(w) ((w)->priority - EV_MINPRI) 246#define ABSPRI(w) ((w)->priority - EV_MINPRI)
203 247
204#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 248#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
205#define EMPTY2(a,b) /* used to suppress some warnings */ 249#define EMPTY2(a,b) /* used to suppress some warnings */
206 250
207typedef struct ev_watcher *W; 251typedef ev_watcher *W;
208typedef struct ev_watcher_list *WL; 252typedef ev_watcher_list *WL;
209typedef struct ev_watcher_time *WT; 253typedef ev_watcher_time *WT;
210 254
211static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 255static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
212 256
213#ifdef _WIN32 257#ifdef _WIN32
214# include "ev_win32.c" 258# include "ev_win32.c"
216 260
217/*****************************************************************************/ 261/*****************************************************************************/
218 262
219static void (*syserr_cb)(const char *msg); 263static void (*syserr_cb)(const char *msg);
220 264
265void
221void ev_set_syserr_cb (void (*cb)(const char *msg)) 266ev_set_syserr_cb (void (*cb)(const char *msg))
222{ 267{
223 syserr_cb = cb; 268 syserr_cb = cb;
224} 269}
225 270
226static void 271static void noinline
227syserr (const char *msg) 272syserr (const char *msg)
228{ 273{
229 if (!msg) 274 if (!msg)
230 msg = "(libev) system error"; 275 msg = "(libev) system error";
231 276
238 } 283 }
239} 284}
240 285
241static void *(*alloc)(void *ptr, long size); 286static void *(*alloc)(void *ptr, long size);
242 287
288void
243void ev_set_allocator (void *(*cb)(void *ptr, long size)) 289ev_set_allocator (void *(*cb)(void *ptr, long size))
244{ 290{
245 alloc = cb; 291 alloc = cb;
246} 292}
247 293
248static void * 294inline_speed void *
249ev_realloc (void *ptr, long size) 295ev_realloc (void *ptr, long size)
250{ 296{
251 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); 297 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
252 298
253 if (!ptr && size) 299 if (!ptr && size)
277typedef struct 323typedef struct
278{ 324{
279 W w; 325 W w;
280 int events; 326 int events;
281} ANPENDING; 327} ANPENDING;
328
329#if EV_USE_INOTIFY
330typedef struct
331{
332 WL head;
333} ANFS;
334#endif
282 335
283#if EV_MULTIPLICITY 336#if EV_MULTIPLICITY
284 337
285 struct ev_loop 338 struct ev_loop
286 { 339 {
320 gettimeofday (&tv, 0); 373 gettimeofday (&tv, 0);
321 return tv.tv_sec + tv.tv_usec * 1e-6; 374 return tv.tv_sec + tv.tv_usec * 1e-6;
322#endif 375#endif
323} 376}
324 377
325inline ev_tstamp 378ev_tstamp inline_size
326get_clock (void) 379get_clock (void)
327{ 380{
328#if EV_USE_MONOTONIC 381#if EV_USE_MONOTONIC
329 if (expect_true (have_monotonic)) 382 if (expect_true (have_monotonic))
330 { 383 {
343{ 396{
344 return ev_rt_now; 397 return ev_rt_now;
345} 398}
346#endif 399#endif
347 400
348#define array_roundsize(type,n) (((n) | 4) & ~3) 401int inline_size
402array_nextsize (int elem, int cur, int cnt)
403{
404 int ncur = cur + 1;
405
406 do
407 ncur <<= 1;
408 while (cnt > ncur);
409
410 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */
411 if (elem * ncur > 4096)
412 {
413 ncur *= elem;
414 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095;
415 ncur = ncur - sizeof (void *) * 4;
416 ncur /= elem;
417 }
418
419 return ncur;
420}
421
422inline_speed void *
423array_realloc (int elem, void *base, int *cur, int cnt)
424{
425 *cur = array_nextsize (elem, *cur, cnt);
426 return ev_realloc (base, elem * *cur);
427}
349 428
350#define array_needsize(type,base,cur,cnt,init) \ 429#define array_needsize(type,base,cur,cnt,init) \
351 if (expect_false ((cnt) > cur)) \ 430 if (expect_false ((cnt) > (cur))) \
352 { \ 431 { \
353 int newcnt = cur; \ 432 int ocur_ = (cur); \
354 do \ 433 (base) = (type *)array_realloc \
355 { \ 434 (sizeof (type), (base), &(cur), (cnt)); \
356 newcnt = array_roundsize (type, newcnt << 1); \ 435 init ((base) + (ocur_), (cur) - ocur_); \
357 } \
358 while ((cnt) > newcnt); \
359 \
360 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
361 init (base + cur, newcnt - cur); \
362 cur = newcnt; \
363 } 436 }
364 437
438#if 0
365#define array_slim(type,stem) \ 439#define array_slim(type,stem) \
366 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 440 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
367 { \ 441 { \
368 stem ## max = array_roundsize (stem ## cnt >> 1); \ 442 stem ## max = array_roundsize (stem ## cnt >> 1); \
369 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 443 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
370 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 444 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
371 } 445 }
446#endif
372 447
373#define array_free(stem, idx) \ 448#define array_free(stem, idx) \
374 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 449 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
375 450
376/*****************************************************************************/ 451/*****************************************************************************/
377 452
378static void 453void noinline
379anfds_init (ANFD *base, int count)
380{
381 while (count--)
382 {
383 base->head = 0;
384 base->events = EV_NONE;
385 base->reify = 0;
386
387 ++base;
388 }
389}
390
391void
392ev_feed_event (EV_P_ void *w, int revents) 454ev_feed_event (EV_P_ void *w, int revents)
393{ 455{
394 W w_ = (W)w; 456 W w_ = (W)w;
395 457
396 if (expect_false (w_->pending)) 458 if (expect_false (w_->pending))
397 { 459 {
398 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 460 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
399 return; 461 return;
400 } 462 }
401
402 if (expect_false (!w_->cb))
403 return;
404 463
405 w_->pending = ++pendingcnt [ABSPRI (w_)]; 464 w_->pending = ++pendingcnt [ABSPRI (w_)];
406 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); 465 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2);
407 pendings [ABSPRI (w_)][w_->pending - 1].w = w_; 466 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
408 pendings [ABSPRI (w_)][w_->pending - 1].events = revents; 467 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
409} 468}
410 469
411static void 470void inline_size
412queue_events (EV_P_ W *events, int eventcnt, int type) 471queue_events (EV_P_ W *events, int eventcnt, int type)
413{ 472{
414 int i; 473 int i;
415 474
416 for (i = 0; i < eventcnt; ++i) 475 for (i = 0; i < eventcnt; ++i)
417 ev_feed_event (EV_A_ events [i], type); 476 ev_feed_event (EV_A_ events [i], type);
418} 477}
419 478
420inline void 479/*****************************************************************************/
480
481void inline_size
482anfds_init (ANFD *base, int count)
483{
484 while (count--)
485 {
486 base->head = 0;
487 base->events = EV_NONE;
488 base->reify = 0;
489
490 ++base;
491 }
492}
493
494void inline_speed
421fd_event (EV_P_ int fd, int revents) 495fd_event (EV_P_ int fd, int revents)
422{ 496{
423 ANFD *anfd = anfds + fd; 497 ANFD *anfd = anfds + fd;
424 struct ev_io *w; 498 ev_io *w;
425 499
426 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 500 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
427 { 501 {
428 int ev = w->events & revents; 502 int ev = w->events & revents;
429 503
430 if (ev) 504 if (ev)
431 ev_feed_event (EV_A_ (W)w, ev); 505 ev_feed_event (EV_A_ (W)w, ev);
436ev_feed_fd_event (EV_P_ int fd, int revents) 510ev_feed_fd_event (EV_P_ int fd, int revents)
437{ 511{
438 fd_event (EV_A_ fd, revents); 512 fd_event (EV_A_ fd, revents);
439} 513}
440 514
441/*****************************************************************************/ 515void inline_size
442
443inline void
444fd_reify (EV_P) 516fd_reify (EV_P)
445{ 517{
446 int i; 518 int i;
447 519
448 for (i = 0; i < fdchangecnt; ++i) 520 for (i = 0; i < fdchangecnt; ++i)
449 { 521 {
450 int fd = fdchanges [i]; 522 int fd = fdchanges [i];
451 ANFD *anfd = anfds + fd; 523 ANFD *anfd = anfds + fd;
452 struct ev_io *w; 524 ev_io *w;
453 525
454 int events = 0; 526 int events = 0;
455 527
456 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 528 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
457 events |= w->events; 529 events |= w->events;
458 530
459#if EV_SELECT_IS_WINSOCKET 531#if EV_SELECT_IS_WINSOCKET
460 if (events) 532 if (events)
461 { 533 {
472 } 544 }
473 545
474 fdchangecnt = 0; 546 fdchangecnt = 0;
475} 547}
476 548
477static void 549void inline_size
478fd_change (EV_P_ int fd) 550fd_change (EV_P_ int fd)
479{ 551{
480 if (expect_false (anfds [fd].reify)) 552 if (expect_false (anfds [fd].reify))
481 return; 553 return;
482 554
485 ++fdchangecnt; 557 ++fdchangecnt;
486 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 558 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
487 fdchanges [fdchangecnt - 1] = fd; 559 fdchanges [fdchangecnt - 1] = fd;
488} 560}
489 561
490static void 562void inline_speed
491fd_kill (EV_P_ int fd) 563fd_kill (EV_P_ int fd)
492{ 564{
493 struct ev_io *w; 565 ev_io *w;
494 566
495 while ((w = (struct ev_io *)anfds [fd].head)) 567 while ((w = (ev_io *)anfds [fd].head))
496 { 568 {
497 ev_io_stop (EV_A_ w); 569 ev_io_stop (EV_A_ w);
498 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 570 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
499 } 571 }
500} 572}
501 573
502inline int 574int inline_size
503fd_valid (int fd) 575fd_valid (int fd)
504{ 576{
505#ifdef _WIN32 577#ifdef _WIN32
506 return _get_osfhandle (fd) != -1; 578 return _get_osfhandle (fd) != -1;
507#else 579#else
508 return fcntl (fd, F_GETFD) != -1; 580 return fcntl (fd, F_GETFD) != -1;
509#endif 581#endif
510} 582}
511 583
512/* called on EBADF to verify fds */ 584/* called on EBADF to verify fds */
513static void 585static void noinline
514fd_ebadf (EV_P) 586fd_ebadf (EV_P)
515{ 587{
516 int fd; 588 int fd;
517 589
518 for (fd = 0; fd < anfdmax; ++fd) 590 for (fd = 0; fd < anfdmax; ++fd)
520 if (!fd_valid (fd) == -1 && errno == EBADF) 592 if (!fd_valid (fd) == -1 && errno == EBADF)
521 fd_kill (EV_A_ fd); 593 fd_kill (EV_A_ fd);
522} 594}
523 595
524/* called on ENOMEM in select/poll to kill some fds and retry */ 596/* called on ENOMEM in select/poll to kill some fds and retry */
525static void 597static void noinline
526fd_enomem (EV_P) 598fd_enomem (EV_P)
527{ 599{
528 int fd; 600 int fd;
529 601
530 for (fd = anfdmax; fd--; ) 602 for (fd = anfdmax; fd--; )
534 return; 606 return;
535 } 607 }
536} 608}
537 609
538/* usually called after fork if backend needs to re-arm all fds from scratch */ 610/* usually called after fork if backend needs to re-arm all fds from scratch */
539static void 611static void noinline
540fd_rearm_all (EV_P) 612fd_rearm_all (EV_P)
541{ 613{
542 int fd; 614 int fd;
543 615
544 /* this should be highly optimised to not do anything but set a flag */
545 for (fd = 0; fd < anfdmax; ++fd) 616 for (fd = 0; fd < anfdmax; ++fd)
546 if (anfds [fd].events) 617 if (anfds [fd].events)
547 { 618 {
548 anfds [fd].events = 0; 619 anfds [fd].events = 0;
549 fd_change (EV_A_ fd); 620 fd_change (EV_A_ fd);
550 } 621 }
551} 622}
552 623
553/*****************************************************************************/ 624/*****************************************************************************/
554 625
555static void 626void inline_speed
556upheap (WT *heap, int k) 627upheap (WT *heap, int k)
557{ 628{
558 WT w = heap [k]; 629 WT w = heap [k];
559 630
560 while (k && heap [k >> 1]->at > w->at) 631 while (k && heap [k >> 1]->at > w->at)
567 heap [k] = w; 638 heap [k] = w;
568 ((W)heap [k])->active = k + 1; 639 ((W)heap [k])->active = k + 1;
569 640
570} 641}
571 642
572static void 643void inline_speed
573downheap (WT *heap, int N, int k) 644downheap (WT *heap, int N, int k)
574{ 645{
575 WT w = heap [k]; 646 WT w = heap [k];
576 647
577 while (k < (N >> 1)) 648 while (k < (N >> 1))
591 662
592 heap [k] = w; 663 heap [k] = w;
593 ((W)heap [k])->active = k + 1; 664 ((W)heap [k])->active = k + 1;
594} 665}
595 666
596inline void 667void inline_size
597adjustheap (WT *heap, int N, int k) 668adjustheap (WT *heap, int N, int k)
598{ 669{
599 upheap (heap, k); 670 upheap (heap, k);
600 downheap (heap, N, k); 671 downheap (heap, N, k);
601} 672}
611static ANSIG *signals; 682static ANSIG *signals;
612static int signalmax; 683static int signalmax;
613 684
614static int sigpipe [2]; 685static int sigpipe [2];
615static sig_atomic_t volatile gotsig; 686static sig_atomic_t volatile gotsig;
616static struct ev_io sigev; 687static ev_io sigev;
617 688
618static void 689void inline_size
619signals_init (ANSIG *base, int count) 690signals_init (ANSIG *base, int count)
620{ 691{
621 while (count--) 692 while (count--)
622 { 693 {
623 base->head = 0; 694 base->head = 0;
643 write (sigpipe [1], &signum, 1); 714 write (sigpipe [1], &signum, 1);
644 errno = old_errno; 715 errno = old_errno;
645 } 716 }
646} 717}
647 718
648void 719void noinline
649ev_feed_signal_event (EV_P_ int signum) 720ev_feed_signal_event (EV_P_ int signum)
650{ 721{
651 WL w; 722 WL w;
652 723
653#if EV_MULTIPLICITY 724#if EV_MULTIPLICITY
664 for (w = signals [signum].head; w; w = w->next) 735 for (w = signals [signum].head; w; w = w->next)
665 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 736 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
666} 737}
667 738
668static void 739static void
669sigcb (EV_P_ struct ev_io *iow, int revents) 740sigcb (EV_P_ ev_io *iow, int revents)
670{ 741{
671 int signum; 742 int signum;
672 743
673 read (sigpipe [0], &revents, 1); 744 read (sigpipe [0], &revents, 1);
674 gotsig = 0; 745 gotsig = 0;
676 for (signum = signalmax; signum--; ) 747 for (signum = signalmax; signum--; )
677 if (signals [signum].gotsig) 748 if (signals [signum].gotsig)
678 ev_feed_signal_event (EV_A_ signum + 1); 749 ev_feed_signal_event (EV_A_ signum + 1);
679} 750}
680 751
681static void 752void inline_size
682fd_intern (int fd) 753fd_intern (int fd)
683{ 754{
684#ifdef _WIN32 755#ifdef _WIN32
685 int arg = 1; 756 int arg = 1;
686 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 757 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
688 fcntl (fd, F_SETFD, FD_CLOEXEC); 759 fcntl (fd, F_SETFD, FD_CLOEXEC);
689 fcntl (fd, F_SETFL, O_NONBLOCK); 760 fcntl (fd, F_SETFL, O_NONBLOCK);
690#endif 761#endif
691} 762}
692 763
693static void 764static void noinline
694siginit (EV_P) 765siginit (EV_P)
695{ 766{
696 fd_intern (sigpipe [0]); 767 fd_intern (sigpipe [0]);
697 fd_intern (sigpipe [1]); 768 fd_intern (sigpipe [1]);
698 769
701 ev_unref (EV_A); /* child watcher should not keep loop alive */ 772 ev_unref (EV_A); /* child watcher should not keep loop alive */
702} 773}
703 774
704/*****************************************************************************/ 775/*****************************************************************************/
705 776
706static struct ev_child *childs [PID_HASHSIZE]; 777static ev_child *childs [EV_PID_HASHSIZE];
707 778
708#ifndef _WIN32 779#ifndef _WIN32
709 780
710static struct ev_signal childev; 781static ev_signal childev;
711 782
712#ifndef WCONTINUED 783void inline_speed
713# define WCONTINUED 0
714#endif
715
716static void
717child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) 784child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status)
718{ 785{
719 struct ev_child *w; 786 ev_child *w;
720 787
721 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 788 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
722 if (w->pid == pid || !w->pid) 789 if (w->pid == pid || !w->pid)
723 { 790 {
724 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 791 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
725 w->rpid = pid; 792 w->rpid = pid;
726 w->rstatus = status; 793 w->rstatus = status;
727 ev_feed_event (EV_A_ (W)w, EV_CHILD); 794 ev_feed_event (EV_A_ (W)w, EV_CHILD);
728 } 795 }
729} 796}
730 797
798#ifndef WCONTINUED
799# define WCONTINUED 0
800#endif
801
731static void 802static void
732childcb (EV_P_ struct ev_signal *sw, int revents) 803childcb (EV_P_ ev_signal *sw, int revents)
733{ 804{
734 int pid, status; 805 int pid, status;
735 806
807 /* some systems define WCONTINUED but then fail to support it (linux 2.4) */
736 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 808 if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
737 { 809 if (!WCONTINUED
810 || errno != EINVAL
811 || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED)))
812 return;
813
738 /* make sure we are called again until all childs have been reaped */ 814 /* make sure we are called again until all childs have been reaped */
739 /* we need to do it this way so that the callback gets called before we continue */ 815 /* we need to do it this way so that the callback gets called before we continue */
740 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); 816 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
741 817
742 child_reap (EV_A_ sw, pid, pid, status); 818 child_reap (EV_A_ sw, pid, pid, status);
819 if (EV_PID_HASHSIZE > 1)
743 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ 820 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
744 }
745} 821}
746 822
747#endif 823#endif
748 824
749/*****************************************************************************/ 825/*****************************************************************************/
775{ 851{
776 return EV_VERSION_MINOR; 852 return EV_VERSION_MINOR;
777} 853}
778 854
779/* return true if we are running with elevated privileges and should ignore env variables */ 855/* return true if we are running with elevated privileges and should ignore env variables */
780static int 856int inline_size
781enable_secure (void) 857enable_secure (void)
782{ 858{
783#ifdef _WIN32 859#ifdef _WIN32
784 return 0; 860 return 0;
785#else 861#else
832ev_backend (EV_P) 908ev_backend (EV_P)
833{ 909{
834 return backend; 910 return backend;
835} 911}
836 912
837static void 913unsigned int
914ev_loop_count (EV_P)
915{
916 return loop_count;
917}
918
919static void noinline
838loop_init (EV_P_ unsigned int flags) 920loop_init (EV_P_ unsigned int flags)
839{ 921{
840 if (!backend) 922 if (!backend)
841 { 923 {
842#if EV_USE_MONOTONIC 924#if EV_USE_MONOTONIC
850 ev_rt_now = ev_time (); 932 ev_rt_now = ev_time ();
851 mn_now = get_clock (); 933 mn_now = get_clock ();
852 now_floor = mn_now; 934 now_floor = mn_now;
853 rtmn_diff = ev_rt_now - mn_now; 935 rtmn_diff = ev_rt_now - mn_now;
854 936
937 /* pid check not overridable via env */
938#ifndef _WIN32
939 if (flags & EVFLAG_FORKCHECK)
940 curpid = getpid ();
941#endif
942
855 if (!(flags & EVFLAG_NOENV) 943 if (!(flags & EVFLAG_NOENV)
856 && !enable_secure () 944 && !enable_secure ()
857 && getenv ("LIBEV_FLAGS")) 945 && getenv ("LIBEV_FLAGS"))
858 flags = atoi (getenv ("LIBEV_FLAGS")); 946 flags = atoi (getenv ("LIBEV_FLAGS"));
859 947
860 if (!(flags & 0x0000ffffUL)) 948 if (!(flags & 0x0000ffffUL))
861 flags |= ev_recommended_backends (); 949 flags |= ev_recommended_backends ();
862 950
863 backend = 0; 951 backend = 0;
952 backend_fd = -1;
953#if EV_USE_INOTIFY
954 fs_fd = -2;
955#endif
956
864#if EV_USE_PORT 957#if EV_USE_PORT
865 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 958 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
866#endif 959#endif
867#if EV_USE_KQUEUE 960#if EV_USE_KQUEUE
868 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags); 961 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
880 ev_init (&sigev, sigcb); 973 ev_init (&sigev, sigcb);
881 ev_set_priority (&sigev, EV_MAXPRI); 974 ev_set_priority (&sigev, EV_MAXPRI);
882 } 975 }
883} 976}
884 977
885static void 978static void noinline
886loop_destroy (EV_P) 979loop_destroy (EV_P)
887{ 980{
888 int i; 981 int i;
982
983#if EV_USE_INOTIFY
984 if (fs_fd >= 0)
985 close (fs_fd);
986#endif
987
988 if (backend_fd >= 0)
989 close (backend_fd);
889 990
890#if EV_USE_PORT 991#if EV_USE_PORT
891 if (backend == EVBACKEND_PORT ) port_destroy (EV_A); 992 if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
892#endif 993#endif
893#if EV_USE_KQUEUE 994#if EV_USE_KQUEUE
907 array_free (pending, [i]); 1008 array_free (pending, [i]);
908 1009
909 /* have to use the microsoft-never-gets-it-right macro */ 1010 /* have to use the microsoft-never-gets-it-right macro */
910 array_free (fdchange, EMPTY0); 1011 array_free (fdchange, EMPTY0);
911 array_free (timer, EMPTY0); 1012 array_free (timer, EMPTY0);
912#if EV_PERIODICS 1013#if EV_PERIODIC_ENABLE
913 array_free (periodic, EMPTY0); 1014 array_free (periodic, EMPTY0);
914#endif 1015#endif
915 array_free (idle, EMPTY0); 1016 array_free (idle, EMPTY0);
916 array_free (prepare, EMPTY0); 1017 array_free (prepare, EMPTY0);
917 array_free (check, EMPTY0); 1018 array_free (check, EMPTY0);
918 1019
919 backend = 0; 1020 backend = 0;
920} 1021}
921 1022
922static void 1023void inline_size infy_fork (EV_P);
1024
1025void inline_size
923loop_fork (EV_P) 1026loop_fork (EV_P)
924{ 1027{
925#if EV_USE_PORT 1028#if EV_USE_PORT
926 if (backend == EVBACKEND_PORT ) port_fork (EV_A); 1029 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
927#endif 1030#endif
928#if EV_USE_KQUEUE 1031#if EV_USE_KQUEUE
929 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A); 1032 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
930#endif 1033#endif
931#if EV_USE_EPOLL 1034#if EV_USE_EPOLL
932 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); 1035 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
1036#endif
1037#if EV_USE_INOTIFY
1038 infy_fork (EV_A);
933#endif 1039#endif
934 1040
935 if (ev_is_active (&sigev)) 1041 if (ev_is_active (&sigev))
936 { 1042 {
937 /* default loop */ 1043 /* default loop */
1053 postfork = 1; 1159 postfork = 1;
1054} 1160}
1055 1161
1056/*****************************************************************************/ 1162/*****************************************************************************/
1057 1163
1058static int 1164int inline_size
1059any_pending (EV_P) 1165any_pending (EV_P)
1060{ 1166{
1061 int pri; 1167 int pri;
1062 1168
1063 for (pri = NUMPRI; pri--; ) 1169 for (pri = NUMPRI; pri--; )
1065 return 1; 1171 return 1;
1066 1172
1067 return 0; 1173 return 0;
1068} 1174}
1069 1175
1070inline void 1176void inline_speed
1071call_pending (EV_P) 1177call_pending (EV_P)
1072{ 1178{
1073 int pri; 1179 int pri;
1074 1180
1075 for (pri = NUMPRI; pri--; ) 1181 for (pri = NUMPRI; pri--; )
1077 { 1183 {
1078 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1184 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1079 1185
1080 if (expect_true (p->w)) 1186 if (expect_true (p->w))
1081 { 1187 {
1188 /*assert (("non-pending watcher on pending list", p->w->pending));*/
1189
1082 p->w->pending = 0; 1190 p->w->pending = 0;
1083 EV_CB_INVOKE (p->w, p->events); 1191 EV_CB_INVOKE (p->w, p->events);
1084 } 1192 }
1085 } 1193 }
1086} 1194}
1087 1195
1088inline void 1196void inline_size
1089timers_reify (EV_P) 1197timers_reify (EV_P)
1090{ 1198{
1091 while (timercnt && ((WT)timers [0])->at <= mn_now) 1199 while (timercnt && ((WT)timers [0])->at <= mn_now)
1092 { 1200 {
1093 struct ev_timer *w = timers [0]; 1201 ev_timer *w = timers [0];
1094 1202
1095 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1203 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1096 1204
1097 /* first reschedule or stop timer */ 1205 /* first reschedule or stop timer */
1098 if (w->repeat) 1206 if (w->repeat)
1099 { 1207 {
1100 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.));
1110 1218
1111 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1219 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1112 } 1220 }
1113} 1221}
1114 1222
1115#if EV_PERIODICS 1223#if EV_PERIODIC_ENABLE
1116inline void 1224void inline_size
1117periodics_reify (EV_P) 1225periodics_reify (EV_P)
1118{ 1226{
1119 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1227 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1120 { 1228 {
1121 struct ev_periodic *w = periodics [0]; 1229 ev_periodic *w = periodics [0];
1122 1230
1123 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1231 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1124 1232
1125 /* first reschedule or stop timer */ 1233 /* first reschedule or stop timer */
1126 if (w->reschedule_cb) 1234 if (w->reschedule_cb)
1127 { 1235 {
1128 ((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);
1140 1248
1141 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1249 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1142 } 1250 }
1143} 1251}
1144 1252
1145static void 1253static void noinline
1146periodics_reschedule (EV_P) 1254periodics_reschedule (EV_P)
1147{ 1255{
1148 int i; 1256 int i;
1149 1257
1150 /* adjust periodics after time jump */ 1258 /* adjust periodics after time jump */
1151 for (i = 0; i < periodiccnt; ++i) 1259 for (i = 0; i < periodiccnt; ++i)
1152 { 1260 {
1153 struct ev_periodic *w = periodics [i]; 1261 ev_periodic *w = periodics [i];
1154 1262
1155 if (w->reschedule_cb) 1263 if (w->reschedule_cb)
1156 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1264 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1157 else if (w->interval) 1265 else if (w->interval)
1158 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1266 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1162 for (i = periodiccnt >> 1; i--; ) 1270 for (i = periodiccnt >> 1; i--; )
1163 downheap ((WT *)periodics, periodiccnt, i); 1271 downheap ((WT *)periodics, periodiccnt, i);
1164} 1272}
1165#endif 1273#endif
1166 1274
1167inline int 1275int inline_size
1168time_update_monotonic (EV_P) 1276time_update_monotonic (EV_P)
1169{ 1277{
1170 mn_now = get_clock (); 1278 mn_now = get_clock ();
1171 1279
1172 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1280 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1180 ev_rt_now = ev_time (); 1288 ev_rt_now = ev_time ();
1181 return 1; 1289 return 1;
1182 } 1290 }
1183} 1291}
1184 1292
1185inline void 1293void inline_size
1186time_update (EV_P) 1294time_update (EV_P)
1187{ 1295{
1188 int i; 1296 int i;
1189 1297
1190#if EV_USE_MONOTONIC 1298#if EV_USE_MONOTONIC
1192 { 1300 {
1193 if (time_update_monotonic (EV_A)) 1301 if (time_update_monotonic (EV_A))
1194 { 1302 {
1195 ev_tstamp odiff = rtmn_diff; 1303 ev_tstamp odiff = rtmn_diff;
1196 1304
1197 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1305 /* loop a few times, before making important decisions.
1306 * on the choice of "4": one iteration isn't enough,
1307 * in case we get preempted during the calls to
1308 * ev_time and get_clock. a second call is almost guaranteed
1309 * to succeed in that case, though. and looping a few more times
1310 * doesn't hurt either as we only do this on time-jumps or
1311 * in the unlikely event of having been preempted here.
1312 */
1313 for (i = 4; --i; )
1198 { 1314 {
1199 rtmn_diff = ev_rt_now - mn_now; 1315 rtmn_diff = ev_rt_now - mn_now;
1200 1316
1201 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1317 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1202 return; /* all is well */ 1318 return; /* all is well */
1204 ev_rt_now = ev_time (); 1320 ev_rt_now = ev_time ();
1205 mn_now = get_clock (); 1321 mn_now = get_clock ();
1206 now_floor = mn_now; 1322 now_floor = mn_now;
1207 } 1323 }
1208 1324
1209# if EV_PERIODICS 1325# if EV_PERIODIC_ENABLE
1210 periodics_reschedule (EV_A); 1326 periodics_reschedule (EV_A);
1211# endif 1327# endif
1212 /* no timer adjustment, as the monotonic clock doesn't jump */ 1328 /* no timer adjustment, as the monotonic clock doesn't jump */
1213 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1329 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1214 } 1330 }
1218 { 1334 {
1219 ev_rt_now = ev_time (); 1335 ev_rt_now = ev_time ();
1220 1336
1221 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1337 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1222 { 1338 {
1223#if EV_PERIODICS 1339#if EV_PERIODIC_ENABLE
1224 periodics_reschedule (EV_A); 1340 periodics_reschedule (EV_A);
1225#endif 1341#endif
1226 1342
1227 /* adjust timers. this is easy, as the offset is the same for all */ 1343 /* adjust timers. this is easy, as the offset is the same for all of them */
1228 for (i = 0; i < timercnt; ++i) 1344 for (i = 0; i < timercnt; ++i)
1229 ((WT)timers [i])->at += ev_rt_now - mn_now; 1345 ((WT)timers [i])->at += ev_rt_now - mn_now;
1230 } 1346 }
1231 1347
1232 mn_now = ev_rt_now; 1348 mn_now = ev_rt_now;
1248static int loop_done; 1364static int loop_done;
1249 1365
1250void 1366void
1251ev_loop (EV_P_ int flags) 1367ev_loop (EV_P_ int flags)
1252{ 1368{
1253 double block;
1254 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1369 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1370 ? EVUNLOOP_ONE
1371 : EVUNLOOP_CANCEL;
1255 1372
1256 while (activecnt) 1373 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1374
1375 do
1257 { 1376 {
1377#ifndef _WIN32
1378 if (expect_false (curpid)) /* penalise the forking check even more */
1379 if (expect_false (getpid () != curpid))
1380 {
1381 curpid = getpid ();
1382 postfork = 1;
1383 }
1384#endif
1385
1386#if EV_FORK_ENABLE
1387 /* we might have forked, so queue fork handlers */
1388 if (expect_false (postfork))
1389 if (forkcnt)
1390 {
1391 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1392 call_pending (EV_A);
1393 }
1394#endif
1395
1258 /* queue check watchers (and execute them) */ 1396 /* queue check watchers (and execute them) */
1259 if (expect_false (preparecnt)) 1397 if (expect_false (preparecnt))
1260 { 1398 {
1261 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1399 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1262 call_pending (EV_A); 1400 call_pending (EV_A);
1263 } 1401 }
1264 1402
1403 if (expect_false (!activecnt))
1404 break;
1405
1265 /* we might have forked, so reify kernel state if necessary */ 1406 /* we might have forked, so reify kernel state if necessary */
1266 if (expect_false (postfork)) 1407 if (expect_false (postfork))
1267 loop_fork (EV_A); 1408 loop_fork (EV_A);
1268 1409
1269 /* update fd-related kernel structures */ 1410 /* update fd-related kernel structures */
1270 fd_reify (EV_A); 1411 fd_reify (EV_A);
1271 1412
1272 /* calculate blocking time */ 1413 /* calculate blocking time */
1414 {
1415 ev_tstamp block;
1273 1416
1274 /* we only need this for !monotonic clock or timers, but as we basically 1417 if (expect_false (flags & EVLOOP_NONBLOCK || idlecnt || !activecnt))
1275 always have timers, we just calculate it always */ 1418 block = 0.; /* do not block at all */
1419 else
1420 {
1421 /* update time to cancel out callback processing overhead */
1276#if EV_USE_MONOTONIC 1422#if EV_USE_MONOTONIC
1277 if (expect_true (have_monotonic)) 1423 if (expect_true (have_monotonic))
1278 time_update_monotonic (EV_A); 1424 time_update_monotonic (EV_A);
1279 else 1425 else
1280#endif 1426#endif
1281 { 1427 {
1282 ev_rt_now = ev_time (); 1428 ev_rt_now = ev_time ();
1283 mn_now = ev_rt_now; 1429 mn_now = ev_rt_now;
1284 } 1430 }
1285 1431
1286 if (flags & EVLOOP_NONBLOCK || idlecnt)
1287 block = 0.;
1288 else
1289 {
1290 block = MAX_BLOCKTIME; 1432 block = MAX_BLOCKTIME;
1291 1433
1292 if (timercnt) 1434 if (timercnt)
1293 { 1435 {
1294 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1436 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1295 if (block > to) block = to; 1437 if (block > to) block = to;
1296 } 1438 }
1297 1439
1298#if EV_PERIODICS 1440#if EV_PERIODIC_ENABLE
1299 if (periodiccnt) 1441 if (periodiccnt)
1300 { 1442 {
1301 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; 1443 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
1302 if (block > to) block = to; 1444 if (block > to) block = to;
1303 } 1445 }
1304#endif 1446#endif
1305 1447
1306 if (expect_false (block < 0.)) block = 0.; 1448 if (expect_false (block < 0.)) block = 0.;
1307 } 1449 }
1308 1450
1451 ++loop_count;
1309 backend_poll (EV_A_ block); 1452 backend_poll (EV_A_ block);
1453 }
1310 1454
1311 /* update ev_rt_now, do magic */ 1455 /* update ev_rt_now, do magic */
1312 time_update (EV_A); 1456 time_update (EV_A);
1313 1457
1314 /* queue pending timers and reschedule them */ 1458 /* queue pending timers and reschedule them */
1315 timers_reify (EV_A); /* relative timers called last */ 1459 timers_reify (EV_A); /* relative timers called last */
1316#if EV_PERIODICS 1460#if EV_PERIODIC_ENABLE
1317 periodics_reify (EV_A); /* absolute timers called first */ 1461 periodics_reify (EV_A); /* absolute timers called first */
1318#endif 1462#endif
1319 1463
1320 /* queue idle watchers unless io or timers are pending */ 1464 /* queue idle watchers unless other events are pending */
1321 if (idlecnt && !any_pending (EV_A)) 1465 if (idlecnt && !any_pending (EV_A))
1322 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1466 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1323 1467
1324 /* queue check watchers, to be executed first */ 1468 /* queue check watchers, to be executed first */
1325 if (expect_false (checkcnt)) 1469 if (expect_false (checkcnt))
1326 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1470 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1327 1471
1328 call_pending (EV_A); 1472 call_pending (EV_A);
1329 1473
1330 if (expect_false (loop_done))
1331 break;
1332 } 1474 }
1475 while (expect_true (activecnt && !loop_done));
1333 1476
1334 if (loop_done != 2) 1477 if (loop_done == EVUNLOOP_ONE)
1335 loop_done = 0; 1478 loop_done = EVUNLOOP_CANCEL;
1336} 1479}
1337 1480
1338void 1481void
1339ev_unloop (EV_P_ int how) 1482ev_unloop (EV_P_ int how)
1340{ 1483{
1341 loop_done = how; 1484 loop_done = how;
1342} 1485}
1343 1486
1344/*****************************************************************************/ 1487/*****************************************************************************/
1345 1488
1346inline void 1489void inline_size
1347wlist_add (WL *head, WL elem) 1490wlist_add (WL *head, WL elem)
1348{ 1491{
1349 elem->next = *head; 1492 elem->next = *head;
1350 *head = elem; 1493 *head = elem;
1351} 1494}
1352 1495
1353inline void 1496void inline_size
1354wlist_del (WL *head, WL elem) 1497wlist_del (WL *head, WL elem)
1355{ 1498{
1356 while (*head) 1499 while (*head)
1357 { 1500 {
1358 if (*head == elem) 1501 if (*head == elem)
1363 1506
1364 head = &(*head)->next; 1507 head = &(*head)->next;
1365 } 1508 }
1366} 1509}
1367 1510
1368inline void 1511void inline_speed
1369ev_clear_pending (EV_P_ W w) 1512ev_clear_pending (EV_P_ W w)
1370{ 1513{
1371 if (w->pending) 1514 if (w->pending)
1372 { 1515 {
1373 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1516 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1374 w->pending = 0; 1517 w->pending = 0;
1375 } 1518 }
1376} 1519}
1377 1520
1378inline void 1521void inline_speed
1379ev_start (EV_P_ W w, int active) 1522ev_start (EV_P_ W w, int active)
1380{ 1523{
1381 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1524 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
1382 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; 1525 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1383 1526
1384 w->active = active; 1527 w->active = active;
1385 ev_ref (EV_A); 1528 ev_ref (EV_A);
1386} 1529}
1387 1530
1388inline void 1531void inline_size
1389ev_stop (EV_P_ W w) 1532ev_stop (EV_P_ W w)
1390{ 1533{
1391 ev_unref (EV_A); 1534 ev_unref (EV_A);
1392 w->active = 0; 1535 w->active = 0;
1393} 1536}
1394 1537
1395/*****************************************************************************/ 1538/*****************************************************************************/
1396 1539
1397void 1540void
1398ev_io_start (EV_P_ struct ev_io *w) 1541ev_io_start (EV_P_ ev_io *w)
1399{ 1542{
1400 int fd = w->fd; 1543 int fd = w->fd;
1401 1544
1402 if (expect_false (ev_is_active (w))) 1545 if (expect_false (ev_is_active (w)))
1403 return; 1546 return;
1410 1553
1411 fd_change (EV_A_ fd); 1554 fd_change (EV_A_ fd);
1412} 1555}
1413 1556
1414void 1557void
1415ev_io_stop (EV_P_ struct ev_io *w) 1558ev_io_stop (EV_P_ ev_io *w)
1416{ 1559{
1417 ev_clear_pending (EV_A_ (W)w); 1560 ev_clear_pending (EV_A_ (W)w);
1418 if (expect_false (!ev_is_active (w))) 1561 if (expect_false (!ev_is_active (w)))
1419 return; 1562 return;
1420 1563
1425 1568
1426 fd_change (EV_A_ w->fd); 1569 fd_change (EV_A_ w->fd);
1427} 1570}
1428 1571
1429void 1572void
1430ev_timer_start (EV_P_ struct ev_timer *w) 1573ev_timer_start (EV_P_ ev_timer *w)
1431{ 1574{
1432 if (expect_false (ev_is_active (w))) 1575 if (expect_false (ev_is_active (w)))
1433 return; 1576 return;
1434 1577
1435 ((WT)w)->at += mn_now; 1578 ((WT)w)->at += mn_now;
1436 1579
1437 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1580 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1438 1581
1439 ev_start (EV_A_ (W)w, ++timercnt); 1582 ev_start (EV_A_ (W)w, ++timercnt);
1440 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); 1583 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1441 timers [timercnt - 1] = w; 1584 timers [timercnt - 1] = w;
1442 upheap ((WT *)timers, timercnt - 1); 1585 upheap ((WT *)timers, timercnt - 1);
1443 1586
1444 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1587 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/
1445} 1588}
1446 1589
1447void 1590void
1448ev_timer_stop (EV_P_ struct ev_timer *w) 1591ev_timer_stop (EV_P_ ev_timer *w)
1449{ 1592{
1450 ev_clear_pending (EV_A_ (W)w); 1593 ev_clear_pending (EV_A_ (W)w);
1451 if (expect_false (!ev_is_active (w))) 1594 if (expect_false (!ev_is_active (w)))
1452 return; 1595 return;
1453 1596
1454 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1597 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1455 1598
1599 {
1600 int active = ((W)w)->active;
1601
1456 if (expect_true (((W)w)->active < timercnt--)) 1602 if (expect_true (--active < --timercnt))
1457 { 1603 {
1458 timers [((W)w)->active - 1] = timers [timercnt]; 1604 timers [active] = timers [timercnt];
1459 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1605 adjustheap ((WT *)timers, timercnt, active);
1460 } 1606 }
1607 }
1461 1608
1462 ((WT)w)->at -= mn_now; 1609 ((WT)w)->at -= mn_now;
1463 1610
1464 ev_stop (EV_A_ (W)w); 1611 ev_stop (EV_A_ (W)w);
1465} 1612}
1466 1613
1467void 1614void
1468ev_timer_again (EV_P_ struct ev_timer *w) 1615ev_timer_again (EV_P_ ev_timer *w)
1469{ 1616{
1470 if (ev_is_active (w)) 1617 if (ev_is_active (w))
1471 { 1618 {
1472 if (w->repeat) 1619 if (w->repeat)
1473 { 1620 {
1482 w->at = w->repeat; 1629 w->at = w->repeat;
1483 ev_timer_start (EV_A_ w); 1630 ev_timer_start (EV_A_ w);
1484 } 1631 }
1485} 1632}
1486 1633
1487#if EV_PERIODICS 1634#if EV_PERIODIC_ENABLE
1488void 1635void
1489ev_periodic_start (EV_P_ struct ev_periodic *w) 1636ev_periodic_start (EV_P_ ev_periodic *w)
1490{ 1637{
1491 if (expect_false (ev_is_active (w))) 1638 if (expect_false (ev_is_active (w)))
1492 return; 1639 return;
1493 1640
1494 if (w->reschedule_cb) 1641 if (w->reschedule_cb)
1499 /* this formula differs from the one in periodic_reify because we do not always round up */ 1646 /* this formula differs from the one in periodic_reify because we do not always round up */
1500 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1647 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1501 } 1648 }
1502 1649
1503 ev_start (EV_A_ (W)w, ++periodiccnt); 1650 ev_start (EV_A_ (W)w, ++periodiccnt);
1504 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1651 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1505 periodics [periodiccnt - 1] = w; 1652 periodics [periodiccnt - 1] = w;
1506 upheap ((WT *)periodics, periodiccnt - 1); 1653 upheap ((WT *)periodics, periodiccnt - 1);
1507 1654
1508 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1655 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/
1509} 1656}
1510 1657
1511void 1658void
1512ev_periodic_stop (EV_P_ struct ev_periodic *w) 1659ev_periodic_stop (EV_P_ ev_periodic *w)
1513{ 1660{
1514 ev_clear_pending (EV_A_ (W)w); 1661 ev_clear_pending (EV_A_ (W)w);
1515 if (expect_false (!ev_is_active (w))) 1662 if (expect_false (!ev_is_active (w)))
1516 return; 1663 return;
1517 1664
1518 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1665 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1519 1666
1667 {
1668 int active = ((W)w)->active;
1669
1520 if (expect_true (((W)w)->active < periodiccnt--)) 1670 if (expect_true (--active < --periodiccnt))
1521 { 1671 {
1522 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1672 periodics [active] = periodics [periodiccnt];
1523 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1673 adjustheap ((WT *)periodics, periodiccnt, active);
1524 } 1674 }
1675 }
1525 1676
1526 ev_stop (EV_A_ (W)w); 1677 ev_stop (EV_A_ (W)w);
1527} 1678}
1528 1679
1529void 1680void
1530ev_periodic_again (EV_P_ struct ev_periodic *w) 1681ev_periodic_again (EV_P_ ev_periodic *w)
1531{ 1682{
1532 /* TODO: use adjustheap and recalculation */ 1683 /* TODO: use adjustheap and recalculation */
1533 ev_periodic_stop (EV_A_ w); 1684 ev_periodic_stop (EV_A_ w);
1534 ev_periodic_start (EV_A_ w); 1685 ev_periodic_start (EV_A_ w);
1535} 1686}
1536#endif 1687#endif
1537 1688
1538void
1539ev_idle_start (EV_P_ struct ev_idle *w)
1540{
1541 if (expect_false (ev_is_active (w)))
1542 return;
1543
1544 ev_start (EV_A_ (W)w, ++idlecnt);
1545 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1546 idles [idlecnt - 1] = w;
1547}
1548
1549void
1550ev_idle_stop (EV_P_ struct ev_idle *w)
1551{
1552 ev_clear_pending (EV_A_ (W)w);
1553 if (expect_false (!ev_is_active (w)))
1554 return;
1555
1556 idles [((W)w)->active - 1] = idles [--idlecnt];
1557 ev_stop (EV_A_ (W)w);
1558}
1559
1560void
1561ev_prepare_start (EV_P_ struct ev_prepare *w)
1562{
1563 if (expect_false (ev_is_active (w)))
1564 return;
1565
1566 ev_start (EV_A_ (W)w, ++preparecnt);
1567 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1568 prepares [preparecnt - 1] = w;
1569}
1570
1571void
1572ev_prepare_stop (EV_P_ struct ev_prepare *w)
1573{
1574 ev_clear_pending (EV_A_ (W)w);
1575 if (expect_false (!ev_is_active (w)))
1576 return;
1577
1578 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1579 ev_stop (EV_A_ (W)w);
1580}
1581
1582void
1583ev_check_start (EV_P_ struct ev_check *w)
1584{
1585 if (expect_false (ev_is_active (w)))
1586 return;
1587
1588 ev_start (EV_A_ (W)w, ++checkcnt);
1589 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1590 checks [checkcnt - 1] = w;
1591}
1592
1593void
1594ev_check_stop (EV_P_ struct ev_check *w)
1595{
1596 ev_clear_pending (EV_A_ (W)w);
1597 if (expect_false (!ev_is_active (w)))
1598 return;
1599
1600 checks [((W)w)->active - 1] = checks [--checkcnt];
1601 ev_stop (EV_A_ (W)w);
1602}
1603
1604#ifndef SA_RESTART 1689#ifndef SA_RESTART
1605# define SA_RESTART 0 1690# define SA_RESTART 0
1606#endif 1691#endif
1607 1692
1608void 1693void
1609ev_signal_start (EV_P_ struct ev_signal *w) 1694ev_signal_start (EV_P_ ev_signal *w)
1610{ 1695{
1611#if EV_MULTIPLICITY 1696#if EV_MULTIPLICITY
1612 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1697 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1613#endif 1698#endif
1614 if (expect_false (ev_is_active (w))) 1699 if (expect_false (ev_is_active (w)))
1633#endif 1718#endif
1634 } 1719 }
1635} 1720}
1636 1721
1637void 1722void
1638ev_signal_stop (EV_P_ struct ev_signal *w) 1723ev_signal_stop (EV_P_ ev_signal *w)
1639{ 1724{
1640 ev_clear_pending (EV_A_ (W)w); 1725 ev_clear_pending (EV_A_ (W)w);
1641 if (expect_false (!ev_is_active (w))) 1726 if (expect_false (!ev_is_active (w)))
1642 return; 1727 return;
1643 1728
1647 if (!signals [w->signum - 1].head) 1732 if (!signals [w->signum - 1].head)
1648 signal (w->signum, SIG_DFL); 1733 signal (w->signum, SIG_DFL);
1649} 1734}
1650 1735
1651void 1736void
1652ev_child_start (EV_P_ struct ev_child *w) 1737ev_child_start (EV_P_ ev_child *w)
1653{ 1738{
1654#if EV_MULTIPLICITY 1739#if EV_MULTIPLICITY
1655 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1740 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1656#endif 1741#endif
1657 if (expect_false (ev_is_active (w))) 1742 if (expect_false (ev_is_active (w)))
1658 return; 1743 return;
1659 1744
1660 ev_start (EV_A_ (W)w, 1); 1745 ev_start (EV_A_ (W)w, 1);
1661 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1746 wlist_add ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1662} 1747}
1663 1748
1664void 1749void
1665ev_child_stop (EV_P_ struct ev_child *w) 1750ev_child_stop (EV_P_ ev_child *w)
1666{ 1751{
1667 ev_clear_pending (EV_A_ (W)w); 1752 ev_clear_pending (EV_A_ (W)w);
1668 if (expect_false (!ev_is_active (w))) 1753 if (expect_false (!ev_is_active (w)))
1669 return; 1754 return;
1670 1755
1671 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1756 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1672 ev_stop (EV_A_ (W)w); 1757 ev_stop (EV_A_ (W)w);
1673} 1758}
1674 1759
1675#if EV_MULTIPLICITY 1760#if EV_STAT_ENABLE
1761
1762# ifdef _WIN32
1763# undef lstat
1764# define lstat(a,b) _stati64 (a,b)
1765# endif
1766
1767#define DEF_STAT_INTERVAL 5.0074891
1768#define MIN_STAT_INTERVAL 0.1074891
1769
1770static void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents);
1771
1772#if EV_USE_INOTIFY
1773# define EV_INOTIFY_BUFSIZE 8192
1774
1775static void noinline
1776infy_add (EV_P_ ev_stat *w)
1777{
1778 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);
1779
1780 if (w->wd < 0)
1781 {
1782 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
1783
1784 /* monitor some parent directory for speedup hints */
1785 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
1786 {
1787 char path [4096];
1788 strcpy (path, w->path);
1789
1790 do
1791 {
1792 int mask = IN_MASK_ADD | IN_DELETE_SELF | IN_MOVE_SELF
1793 | (errno == EACCES ? IN_ATTRIB : IN_CREATE | IN_MOVED_TO);
1794
1795 char *pend = strrchr (path, '/');
1796
1797 if (!pend)
1798 break; /* whoops, no '/', complain to your admin */
1799
1800 *pend = 0;
1801 w->wd = inotify_add_watch (fs_fd, path, mask);
1802 }
1803 while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
1804 }
1805 }
1806 else
1807 ev_timer_stop (EV_A_ &w->timer); /* we can watch this in a race-free way */
1808
1809 if (w->wd >= 0)
1810 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w);
1811}
1812
1813static void noinline
1814infy_del (EV_P_ ev_stat *w)
1815{
1816 int slot;
1817 int wd = w->wd;
1818
1819 if (wd < 0)
1820 return;
1821
1822 w->wd = -2;
1823 slot = wd & (EV_INOTIFY_HASHSIZE - 1);
1824 wlist_del (&fs_hash [slot].head, (WL)w);
1825
1826 /* remove this watcher, if others are watching it, they will rearm */
1827 inotify_rm_watch (fs_fd, wd);
1828}
1829
1830static void noinline
1831infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev)
1832{
1833 if (slot < 0)
1834 /* overflow, need to check for all hahs slots */
1835 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
1836 infy_wd (EV_A_ slot, wd, ev);
1837 else
1838 {
1839 WL w_;
1840
1841 for (w_ = fs_hash [slot & (EV_INOTIFY_HASHSIZE - 1)].head; w_; )
1842 {
1843 ev_stat *w = (ev_stat *)w_;
1844 w_ = w_->next; /* lets us remove this watcher and all before it */
1845
1846 if (w->wd == wd || wd == -1)
1847 {
1848 if (ev->mask & (IN_IGNORED | IN_UNMOUNT | IN_DELETE_SELF))
1849 {
1850 w->wd = -1;
1851 infy_add (EV_A_ w); /* re-add, no matter what */
1852 }
1853
1854 stat_timer_cb (EV_A_ &w->timer, 0);
1855 }
1856 }
1857 }
1858}
1859
1676static void 1860static void
1677embed_cb (EV_P_ struct ev_io *io, int revents) 1861infy_cb (EV_P_ ev_io *w, int revents)
1678{ 1862{
1679 struct ev_embed *w = (struct ev_embed *)(((char *)io) - offsetof (struct ev_embed, io)); 1863 char buf [EV_INOTIFY_BUFSIZE];
1864 struct inotify_event *ev = (struct inotify_event *)buf;
1865 int ofs;
1866 int len = read (fs_fd, buf, sizeof (buf));
1680 1867
1868 for (ofs = 0; ofs < len; ofs += sizeof (struct inotify_event) + ev->len)
1869 infy_wd (EV_A_ ev->wd, ev->wd, ev);
1870}
1871
1872void inline_size
1873infy_init (EV_P)
1874{
1875 if (fs_fd != -2)
1876 return;
1877
1878 fs_fd = inotify_init ();
1879
1880 if (fs_fd >= 0)
1881 {
1882 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ);
1883 ev_set_priority (&fs_w, EV_MAXPRI);
1884 ev_io_start (EV_A_ &fs_w);
1885 }
1886}
1887
1888void inline_size
1889infy_fork (EV_P)
1890{
1891 int slot;
1892
1893 if (fs_fd < 0)
1894 return;
1895
1896 close (fs_fd);
1897 fs_fd = inotify_init ();
1898
1899 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
1900 {
1901 WL w_ = fs_hash [slot].head;
1902 fs_hash [slot].head = 0;
1903
1904 while (w_)
1905 {
1906 ev_stat *w = (ev_stat *)w_;
1907 w_ = w_->next; /* lets us add this watcher */
1908
1909 w->wd = -1;
1910
1911 if (fs_fd >= 0)
1912 infy_add (EV_A_ w); /* re-add, no matter what */
1913 else
1914 ev_timer_start (EV_A_ &w->timer);
1915 }
1916
1917 }
1918}
1919
1920#endif
1921
1922void
1923ev_stat_stat (EV_P_ ev_stat *w)
1924{
1925 if (lstat (w->path, &w->attr) < 0)
1926 w->attr.st_nlink = 0;
1927 else if (!w->attr.st_nlink)
1928 w->attr.st_nlink = 1;
1929}
1930
1931static void noinline
1932stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1933{
1934 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1935
1936 /* we copy this here each the time so that */
1937 /* prev has the old value when the callback gets invoked */
1938 w->prev = w->attr;
1939 ev_stat_stat (EV_A_ w);
1940
1941 /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */
1942 if (
1943 w->prev.st_dev != w->attr.st_dev
1944 || w->prev.st_ino != w->attr.st_ino
1945 || w->prev.st_mode != w->attr.st_mode
1946 || w->prev.st_nlink != w->attr.st_nlink
1947 || w->prev.st_uid != w->attr.st_uid
1948 || w->prev.st_gid != w->attr.st_gid
1949 || w->prev.st_rdev != w->attr.st_rdev
1950 || w->prev.st_size != w->attr.st_size
1951 || w->prev.st_atime != w->attr.st_atime
1952 || w->prev.st_mtime != w->attr.st_mtime
1953 || w->prev.st_ctime != w->attr.st_ctime
1954 ) {
1955 #if EV_USE_INOTIFY
1956 infy_del (EV_A_ w);
1957 infy_add (EV_A_ w);
1958 ev_stat_stat (EV_A_ w); /* avoid race... */
1959 #endif
1960
1681 ev_feed_event (EV_A_ (W)w, EV_EMBED); 1961 ev_feed_event (EV_A_ w, EV_STAT);
1962 }
1963}
1964
1965void
1966ev_stat_start (EV_P_ ev_stat *w)
1967{
1968 if (expect_false (ev_is_active (w)))
1969 return;
1970
1971 /* since we use memcmp, we need to clear any padding data etc. */
1972 memset (&w->prev, 0, sizeof (ev_statdata));
1973 memset (&w->attr, 0, sizeof (ev_statdata));
1974
1975 ev_stat_stat (EV_A_ w);
1976
1977 if (w->interval < MIN_STAT_INTERVAL)
1978 w->interval = w->interval ? MIN_STAT_INTERVAL : DEF_STAT_INTERVAL;
1979
1980 ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval);
1981 ev_set_priority (&w->timer, ev_priority (w));
1982
1983#if EV_USE_INOTIFY
1984 infy_init (EV_A);
1985
1986 if (fs_fd >= 0)
1987 infy_add (EV_A_ w);
1988 else
1989#endif
1990 ev_timer_start (EV_A_ &w->timer);
1991
1992 ev_start (EV_A_ (W)w, 1);
1993}
1994
1995void
1996ev_stat_stop (EV_P_ ev_stat *w)
1997{
1998 ev_clear_pending (EV_A_ (W)w);
1999 if (expect_false (!ev_is_active (w)))
2000 return;
2001
2002#if EV_USE_INOTIFY
2003 infy_del (EV_A_ w);
2004#endif
2005 ev_timer_stop (EV_A_ &w->timer);
2006
2007 ev_stop (EV_A_ (W)w);
2008}
2009#endif
2010
2011void
2012ev_idle_start (EV_P_ ev_idle *w)
2013{
2014 if (expect_false (ev_is_active (w)))
2015 return;
2016
2017 ev_start (EV_A_ (W)w, ++idlecnt);
2018 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
2019 idles [idlecnt - 1] = w;
2020}
2021
2022void
2023ev_idle_stop (EV_P_ ev_idle *w)
2024{
2025 ev_clear_pending (EV_A_ (W)w);
2026 if (expect_false (!ev_is_active (w)))
2027 return;
2028
2029 {
2030 int active = ((W)w)->active;
2031 idles [active - 1] = idles [--idlecnt];
2032 ((W)idles [active - 1])->active = active;
2033 }
2034
2035 ev_stop (EV_A_ (W)w);
2036}
2037
2038void
2039ev_prepare_start (EV_P_ ev_prepare *w)
2040{
2041 if (expect_false (ev_is_active (w)))
2042 return;
2043
2044 ev_start (EV_A_ (W)w, ++preparecnt);
2045 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
2046 prepares [preparecnt - 1] = w;
2047}
2048
2049void
2050ev_prepare_stop (EV_P_ ev_prepare *w)
2051{
2052 ev_clear_pending (EV_A_ (W)w);
2053 if (expect_false (!ev_is_active (w)))
2054 return;
2055
2056 {
2057 int active = ((W)w)->active;
2058 prepares [active - 1] = prepares [--preparecnt];
2059 ((W)prepares [active - 1])->active = active;
2060 }
2061
2062 ev_stop (EV_A_ (W)w);
2063}
2064
2065void
2066ev_check_start (EV_P_ ev_check *w)
2067{
2068 if (expect_false (ev_is_active (w)))
2069 return;
2070
2071 ev_start (EV_A_ (W)w, ++checkcnt);
2072 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
2073 checks [checkcnt - 1] = w;
2074}
2075
2076void
2077ev_check_stop (EV_P_ ev_check *w)
2078{
2079 ev_clear_pending (EV_A_ (W)w);
2080 if (expect_false (!ev_is_active (w)))
2081 return;
2082
2083 {
2084 int active = ((W)w)->active;
2085 checks [active - 1] = checks [--checkcnt];
2086 ((W)checks [active - 1])->active = active;
2087 }
2088
2089 ev_stop (EV_A_ (W)w);
2090}
2091
2092#if EV_EMBED_ENABLE
2093void noinline
2094ev_embed_sweep (EV_P_ ev_embed *w)
2095{
1682 ev_loop (w->loop, EVLOOP_NONBLOCK); 2096 ev_loop (w->loop, EVLOOP_NONBLOCK);
1683} 2097}
1684 2098
2099static void
2100embed_cb (EV_P_ ev_io *io, int revents)
2101{
2102 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
2103
2104 if (ev_cb (w))
2105 ev_feed_event (EV_A_ (W)w, EV_EMBED);
2106 else
2107 ev_embed_sweep (loop, w);
2108}
2109
1685void 2110void
1686ev_embed_start (EV_P_ struct ev_embed *w) 2111ev_embed_start (EV_P_ ev_embed *w)
1687{ 2112{
1688 if (expect_false (ev_is_active (w))) 2113 if (expect_false (ev_is_active (w)))
1689 return; 2114 return;
1690 2115
1691 { 2116 {
1692 struct ev_loop *loop = w->loop; 2117 struct ev_loop *loop = w->loop;
1693 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); 2118 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
1694 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); 2119 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ);
1695 } 2120 }
1696 2121
2122 ev_set_priority (&w->io, ev_priority (w));
1697 ev_io_start (EV_A_ &w->io); 2123 ev_io_start (EV_A_ &w->io);
2124
1698 ev_start (EV_A_ (W)w, 1); 2125 ev_start (EV_A_ (W)w, 1);
1699} 2126}
1700 2127
1701void 2128void
1702ev_embed_stop (EV_P_ struct ev_embed *w) 2129ev_embed_stop (EV_P_ ev_embed *w)
1703{ 2130{
1704 ev_clear_pending (EV_A_ (W)w); 2131 ev_clear_pending (EV_A_ (W)w);
1705 if (expect_false (!ev_is_active (w))) 2132 if (expect_false (!ev_is_active (w)))
1706 return; 2133 return;
1707 2134
1708 ev_io_stop (EV_A_ &w->io); 2135 ev_io_stop (EV_A_ &w->io);
2136
1709 ev_stop (EV_A_ (W)w); 2137 ev_stop (EV_A_ (W)w);
1710} 2138}
1711#endif 2139#endif
1712 2140
2141#if EV_FORK_ENABLE
2142void
2143ev_fork_start (EV_P_ ev_fork *w)
2144{
2145 if (expect_false (ev_is_active (w)))
2146 return;
2147
2148 ev_start (EV_A_ (W)w, ++forkcnt);
2149 array_needsize (ev_fork *, forks, forkmax, forkcnt, EMPTY2);
2150 forks [forkcnt - 1] = w;
2151}
2152
2153void
2154ev_fork_stop (EV_P_ ev_fork *w)
2155{
2156 ev_clear_pending (EV_A_ (W)w);
2157 if (expect_false (!ev_is_active (w)))
2158 return;
2159
2160 {
2161 int active = ((W)w)->active;
2162 forks [active - 1] = forks [--forkcnt];
2163 ((W)forks [active - 1])->active = active;
2164 }
2165
2166 ev_stop (EV_A_ (W)w);
2167}
2168#endif
2169
1713/*****************************************************************************/ 2170/*****************************************************************************/
1714 2171
1715struct ev_once 2172struct ev_once
1716{ 2173{
1717 struct ev_io io; 2174 ev_io io;
1718 struct ev_timer to; 2175 ev_timer to;
1719 void (*cb)(int revents, void *arg); 2176 void (*cb)(int revents, void *arg);
1720 void *arg; 2177 void *arg;
1721}; 2178};
1722 2179
1723static void 2180static void
1732 2189
1733 cb (revents, arg); 2190 cb (revents, arg);
1734} 2191}
1735 2192
1736static void 2193static void
1737once_cb_io (EV_P_ struct ev_io *w, int revents) 2194once_cb_io (EV_P_ ev_io *w, int revents)
1738{ 2195{
1739 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); 2196 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1740} 2197}
1741 2198
1742static void 2199static void
1743once_cb_to (EV_P_ struct ev_timer *w, int revents) 2200once_cb_to (EV_P_ ev_timer *w, int revents)
1744{ 2201{
1745 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); 2202 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1746} 2203}
1747 2204
1748void 2205void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines