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

Comparing libev/ev.c (file contents):
Revision 1.51 by root, Sat Nov 3 21:58:51 2007 UTC vs.
Revision 1.70 by root, Tue Nov 6 00:52:32 2007 UTC

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#ifndef EV_STANDALONE 31#ifndef EV_STANDALONE
32# include "config.h" 32# include "config.h"
33
34# if HAVE_CLOCK_GETTIME
35# define EV_USE_MONOTONIC 1
36# define EV_USE_REALTIME 1
37# endif
38
39# if HAVE_SELECT && HAVE_SYS_SELECT_H
40# define EV_USE_SELECT 1
41# endif
42
43# if HAVE_POLL && HAVE_POLL_H
44# define EV_USE_POLL 1
45# endif
46
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
48# define EV_USE_EPOLL 1
49# endif
50
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
52# define EV_USE_KQUEUE 1
53# endif
54
33#endif 55#endif
34 56
35#include <math.h> 57#include <math.h>
36#include <stdlib.h> 58#include <stdlib.h>
37#include <unistd.h> 59#include <unistd.h>
58 80
59#ifndef EV_USE_SELECT 81#ifndef EV_USE_SELECT
60# define EV_USE_SELECT 1 82# define EV_USE_SELECT 1
61#endif 83#endif
62 84
63#ifndef EV_USEV_POLL 85#ifndef EV_USE_POLL
64# define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ 86# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
65#endif 87#endif
66 88
67#ifndef EV_USE_EPOLL 89#ifndef EV_USE_EPOLL
68# define EV_USE_EPOLL 0 90# define EV_USE_EPOLL 0
69#endif 91#endif
70 92
71#ifndef EV_USE_KQUEUE 93#ifndef EV_USE_KQUEUE
72# define EV_USE_KQUEUE 0 94# define EV_USE_KQUEUE 0
95#endif
96
97#ifndef EV_USE_WIN32
98# ifdef WIN32
99# define EV_USE_WIN32 1
100# else
101# define EV_USE_WIN32 0
102# endif
73#endif 103#endif
74 104
75#ifndef EV_USE_REALTIME 105#ifndef EV_USE_REALTIME
76# define EV_USE_REALTIME 1 106# define EV_USE_REALTIME 1
77#endif 107#endif
113 143
114typedef struct ev_watcher *W; 144typedef struct ev_watcher *W;
115typedef struct ev_watcher_list *WL; 145typedef struct ev_watcher_list *WL;
116typedef struct ev_watcher_time *WT; 146typedef struct ev_watcher_time *WT;
117 147
118static ev_tstamp now_floor, mn_now, diff; /* monotonic clock */ 148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
119static ev_tstamp rt_now;
120static int method;
121 149
122static int have_monotonic; /* runtime */ 150#if WIN32
123 151/* note: the comment below could not be substantiated, but what would I care */
124static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 152/* MSDN says this is required to handle SIGFPE */
125static void (*method_modify)(EV_P_ int fd, int oev, int nev); 153volatile double SIGFPE_REQ = 0.0f;
126static void (*method_poll)(EV_P_ ev_tstamp timeout);
127
128static int activecnt; /* number of active events */
129
130#if EV_USE_SELECT
131static unsigned char *vec_ri, *vec_ro, *vec_wi, *vec_wo;
132static int vec_max;
133#endif 154#endif
134 155
135#if EV_USEV_POLL 156/*****************************************************************************/
136static struct pollfd *polls;
137static int pollmax, pollcnt;
138static int *pollidxs; /* maps fds into structure indices */
139static int pollidxmax;
140#endif
141 157
142#if EV_USE_EPOLL 158static void (*syserr_cb)(const char *msg);
143static int epoll_fd = -1;
144 159
145static struct epoll_event *events; 160void ev_set_syserr_cb (void (*cb)(const char *msg))
146static int eventmax; 161{
147#endif 162 syserr_cb = cb;
163}
148 164
149#if EV_USE_KQUEUE 165static void
150static int kqueue_fd; 166syserr (const char *msg)
151static struct kevent *kqueue_changes; 167{
152static int kqueue_changemax, kqueue_changecnt; 168 if (!msg)
153static struct kevent *kqueue_events; 169 msg = "(libev) system error";
154static int kqueue_eventmax; 170
171 if (syserr_cb)
172 syserr_cb (msg);
173 else
174 {
175 perror (msg);
176 abort ();
177 }
178}
179
180static void *(*alloc)(void *ptr, long size);
181
182void ev_set_allocator (void *(*cb)(void *ptr, long size))
183{
184 alloc = cb;
185}
186
187static void *
188ev_realloc (void *ptr, long size)
189{
190 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
191
192 if (!ptr && size)
193 {
194 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
195 abort ();
196 }
197
198 return ptr;
199}
200
201#define ev_malloc(size) ev_realloc (0, (size))
202#define ev_free(ptr) ev_realloc ((ptr), 0)
203
204/*****************************************************************************/
205
206typedef struct
207{
208 WL head;
209 unsigned char events;
210 unsigned char reify;
211} ANFD;
212
213typedef struct
214{
215 W w;
216 int events;
217} ANPENDING;
218
219#if EV_MULTIPLICITY
220
221struct ev_loop
222{
223# define VAR(name,decl) decl;
224# include "ev_vars.h"
225};
226# undef VAR
227# include "ev_wrap.h"
228
229#else
230
231# define VAR(name,decl) static decl;
232# include "ev_vars.h"
233# undef VAR
234
155#endif 235#endif
156 236
157/*****************************************************************************/ 237/*****************************************************************************/
158 238
159inline ev_tstamp 239inline ev_tstamp
191 return rt_now; 271 return rt_now;
192} 272}
193 273
194#define array_roundsize(base,n) ((n) | 4 & ~3) 274#define array_roundsize(base,n) ((n) | 4 & ~3)
195 275
196#define array_needsize(base,cur,cnt,init) \ 276#define array_needsize(base,cur,cnt,init) \
197 if (expect_false ((cnt) > cur)) \ 277 if (expect_false ((cnt) > cur)) \
198 { \ 278 { \
199 int newcnt = cur; \ 279 int newcnt = cur; \
200 do \ 280 do \
201 { \ 281 { \
202 newcnt = array_roundsize (base, newcnt << 1); \ 282 newcnt = array_roundsize (base, newcnt << 1); \
203 } \ 283 } \
204 while ((cnt) > newcnt); \ 284 while ((cnt) > newcnt); \
205 \ 285 \
206 base = realloc (base, sizeof (*base) * (newcnt)); \ 286 base = ev_realloc (base, sizeof (*base) * (newcnt)); \
207 init (base + cur, newcnt - cur); \ 287 init (base + cur, newcnt - cur); \
208 cur = newcnt; \ 288 cur = newcnt; \
209 } 289 }
290
291#define array_slim(stem) \
292 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
293 { \
294 stem ## max = array_roundsize (stem ## cnt >> 1); \
295 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \
296 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
297 }
298
299#define array_free(stem, idx) \
300 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
210 301
211/*****************************************************************************/ 302/*****************************************************************************/
212
213typedef struct
214{
215 struct ev_watcher_list *head;
216 unsigned char events;
217 unsigned char reify;
218} ANFD;
219
220static ANFD *anfds;
221static int anfdmax;
222 303
223static void 304static void
224anfds_init (ANFD *base, int count) 305anfds_init (ANFD *base, int count)
225{ 306{
226 while (count--) 307 while (count--)
230 base->reify = 0; 311 base->reify = 0;
231 312
232 ++base; 313 ++base;
233 } 314 }
234} 315}
235
236typedef struct
237{
238 W w;
239 int events;
240} ANPENDING;
241
242static ANPENDING *pendings [NUMPRI];
243static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
244 316
245static void 317static void
246event (EV_P_ W w, int events) 318event (EV_P_ W w, int events)
247{ 319{
248 if (w->pending) 320 if (w->pending)
281 } 353 }
282} 354}
283 355
284/*****************************************************************************/ 356/*****************************************************************************/
285 357
286static int *fdchanges;
287static int fdchangemax, fdchangecnt;
288
289static void 358static void
290fd_reify (EV_P) 359fd_reify (EV_P)
291{ 360{
292 int i; 361 int i;
293 362
302 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 371 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
303 events |= w->events; 372 events |= w->events;
304 373
305 anfd->reify = 0; 374 anfd->reify = 0;
306 375
307 if (anfd->events != events)
308 {
309 method_modify (EV_A_ fd, anfd->events, events); 376 method_modify (EV_A_ fd, anfd->events, events);
310 anfd->events = events; 377 anfd->events = events;
311 }
312 } 378 }
313 379
314 fdchangecnt = 0; 380 fdchangecnt = 0;
315} 381}
316 382
317static void 383static void
318fd_change (EV_P_ int fd) 384fd_change (EV_P_ int fd)
319{ 385{
320 if (anfds [fd].reify || fdchangecnt < 0) 386 if (anfds [fd].reify)
321 return; 387 return;
322 388
323 anfds [fd].reify = 1; 389 anfds [fd].reify = 1;
324 390
325 ++fdchangecnt; 391 ++fdchangecnt;
353 419
354/* called on ENOMEM in select/poll to kill some fds and retry */ 420/* called on ENOMEM in select/poll to kill some fds and retry */
355static void 421static void
356fd_enomem (EV_P) 422fd_enomem (EV_P)
357{ 423{
358 int fd = anfdmax; 424 int fd;
359 425
360 while (fd--) 426 for (fd = anfdmax; fd--; )
361 if (anfds [fd].events) 427 if (anfds [fd].events)
362 { 428 {
363 close (fd);
364 fd_kill (EV_A_ fd); 429 fd_kill (EV_A_ fd);
365 return; 430 return;
366 } 431 }
367} 432}
368 433
434/* usually called after fork if method needs to re-arm all fds from scratch */
435static void
436fd_rearm_all (EV_P)
437{
438 int fd;
439
440 /* this should be highly optimised to not do anything but set a flag */
441 for (fd = 0; fd < anfdmax; ++fd)
442 if (anfds [fd].events)
443 {
444 anfds [fd].events = 0;
445 fd_change (EV_A_ fd);
446 }
447}
448
369/*****************************************************************************/ 449/*****************************************************************************/
370 450
371static struct ev_timer **timers;
372static int timermax, timercnt;
373
374static struct ev_periodic **periodics;
375static int periodicmax, periodiccnt;
376
377static void 451static void
378upheap (WT *timers, int k) 452upheap (WT *heap, int k)
379{ 453{
380 WT w = timers [k]; 454 WT w = heap [k];
381 455
382 while (k && timers [k >> 1]->at > w->at) 456 while (k && heap [k >> 1]->at > w->at)
383 { 457 {
384 timers [k] = timers [k >> 1]; 458 heap [k] = heap [k >> 1];
385 timers [k]->active = k + 1; 459 ((W)heap [k])->active = k + 1;
386 k >>= 1; 460 k >>= 1;
387 } 461 }
388 462
389 timers [k] = w; 463 heap [k] = w;
390 timers [k]->active = k + 1; 464 ((W)heap [k])->active = k + 1;
391 465
392} 466}
393 467
394static void 468static void
395downheap (WT *timers, int N, int k) 469downheap (WT *heap, int N, int k)
396{ 470{
397 WT w = timers [k]; 471 WT w = heap [k];
398 472
399 while (k < (N >> 1)) 473 while (k < (N >> 1))
400 { 474 {
401 int j = k << 1; 475 int j = k << 1;
402 476
403 if (j + 1 < N && timers [j]->at > timers [j + 1]->at) 477 if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
404 ++j; 478 ++j;
405 479
406 if (w->at <= timers [j]->at) 480 if (w->at <= heap [j]->at)
407 break; 481 break;
408 482
409 timers [k] = timers [j]; 483 heap [k] = heap [j];
410 timers [k]->active = k + 1; 484 ((W)heap [k])->active = k + 1;
411 k = j; 485 k = j;
412 } 486 }
413 487
414 timers [k] = w; 488 heap [k] = w;
415 timers [k]->active = k + 1; 489 ((W)heap [k])->active = k + 1;
416} 490}
417 491
418/*****************************************************************************/ 492/*****************************************************************************/
419 493
420typedef struct 494typedef struct
421{ 495{
422 struct ev_watcher_list *head; 496 WL head;
423 sig_atomic_t volatile gotsig; 497 sig_atomic_t volatile gotsig;
424} ANSIG; 498} ANSIG;
425 499
426static ANSIG *signals; 500static ANSIG *signals;
427static int signalmax; 501static int signalmax;
443} 517}
444 518
445static void 519static void
446sighandler (int signum) 520sighandler (int signum)
447{ 521{
522#if WIN32
523 signal (signum, sighandler);
524#endif
525
448 signals [signum - 1].gotsig = 1; 526 signals [signum - 1].gotsig = 1;
449 527
450 if (!gotsig) 528 if (!gotsig)
451 { 529 {
452 int old_errno = errno; 530 int old_errno = errno;
457} 535}
458 536
459static void 537static void
460sigcb (EV_P_ struct ev_io *iow, int revents) 538sigcb (EV_P_ struct ev_io *iow, int revents)
461{ 539{
462 struct ev_watcher_list *w; 540 WL w;
463 int signum; 541 int signum;
464 542
465 read (sigpipe [0], &revents, 1); 543 read (sigpipe [0], &revents, 1);
466 gotsig = 0; 544 gotsig = 0;
467 545
486 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 564 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
487 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 565 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
488#endif 566#endif
489 567
490 ev_io_set (&sigev, sigpipe [0], EV_READ); 568 ev_io_set (&sigev, sigpipe [0], EV_READ);
491 ev_io_start (&sigev); 569 ev_io_start (EV_A_ &sigev);
570 ev_unref (EV_A); /* child watcher should not keep loop alive */
492} 571}
493 572
494/*****************************************************************************/ 573/*****************************************************************************/
495 574
496static struct ev_idle **idles; 575#ifndef WIN32
497static int idlemax, idlecnt;
498
499static struct ev_prepare **prepares;
500static int preparemax, preparecnt;
501
502static struct ev_check **checks;
503static int checkmax, checkcnt;
504
505/*****************************************************************************/
506 576
507static struct ev_child *childs [PID_HASHSIZE]; 577static struct ev_child *childs [PID_HASHSIZE];
508static struct ev_signal childev; 578static struct ev_signal childev;
509
510#ifndef WIN32
511 579
512#ifndef WCONTINUED 580#ifndef WCONTINUED
513# define WCONTINUED 0 581# define WCONTINUED 0
514#endif 582#endif
515 583
519 struct ev_child *w; 587 struct ev_child *w;
520 588
521 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 589 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
522 if (w->pid == pid || !w->pid) 590 if (w->pid == pid || !w->pid)
523 { 591 {
524 w->priority = sw->priority; /* need to do it *now* */ 592 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
525 w->rpid = pid; 593 w->rpid = pid;
526 w->rstatus = status; 594 w->rstatus = status;
527 event (EV_A_ (W)w, EV_CHILD); 595 event (EV_A_ (W)w, EV_CHILD);
528 } 596 }
529} 597}
530 598
531static void 599static void
551# include "ev_kqueue.c" 619# include "ev_kqueue.c"
552#endif 620#endif
553#if EV_USE_EPOLL 621#if EV_USE_EPOLL
554# include "ev_epoll.c" 622# include "ev_epoll.c"
555#endif 623#endif
556#if EV_USEV_POLL 624#if EV_USE_POLL
557# include "ev_poll.c" 625# include "ev_poll.c"
558#endif 626#endif
559#if EV_USE_SELECT 627#if EV_USE_SELECT
560# include "ev_select.c" 628# include "ev_select.c"
561#endif 629#endif
588ev_method (EV_P) 656ev_method (EV_P)
589{ 657{
590 return method; 658 return method;
591} 659}
592 660
593int 661static void
594ev_init (EV_P_ int methods) 662loop_init (EV_P_ int methods)
595{ 663{
596 if (!method) 664 if (!method)
597 { 665 {
598#if EV_USE_MONOTONIC 666#if EV_USE_MONOTONIC
599 { 667 {
604#endif 672#endif
605 673
606 rt_now = ev_time (); 674 rt_now = ev_time ();
607 mn_now = get_clock (); 675 mn_now = get_clock ();
608 now_floor = mn_now; 676 now_floor = mn_now;
609 diff = rt_now - mn_now; 677 rtmn_diff = rt_now - mn_now;
610
611 if (pipe (sigpipe))
612 return 0;
613 678
614 if (methods == EVMETHOD_AUTO) 679 if (methods == EVMETHOD_AUTO)
615 if (!enable_secure () && getenv ("LIBmethodS")) 680 if (!enable_secure () && getenv ("LIBEV_METHODS"))
616 methods = atoi (getenv ("LIBmethodS")); 681 methods = atoi (getenv ("LIBEV_METHODS"));
617 else 682 else
618 methods = EVMETHOD_ANY; 683 methods = EVMETHOD_ANY;
619 684
620 method = 0; 685 method = 0;
686#if EV_USE_WIN32
687 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
688#endif
621#if EV_USE_KQUEUE 689#if EV_USE_KQUEUE
622 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 690 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
623#endif 691#endif
624#if EV_USE_EPOLL 692#if EV_USE_EPOLL
625 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 693 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
626#endif 694#endif
627#if EV_USEV_POLL 695#if EV_USE_POLL
628 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 696 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
629#endif 697#endif
630#if EV_USE_SELECT 698#if EV_USE_SELECT
631 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 699 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
632#endif 700#endif
633 701
702 ev_watcher_init (&sigev, sigcb);
703 ev_set_priority (&sigev, EV_MAXPRI);
704 }
705}
706
707void
708loop_destroy (EV_P)
709{
710 int i;
711
712#if EV_USE_WIN32
713 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
714#endif
715#if EV_USE_KQUEUE
716 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
717#endif
718#if EV_USE_EPOLL
719 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
720#endif
721#if EV_USE_POLL
722 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
723#endif
724#if EV_USE_SELECT
725 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
726#endif
727
728 for (i = NUMPRI; i--; )
729 array_free (pending, [i]);
730
731 array_free (fdchange, );
732 array_free (timer, );
733 array_free (periodic, );
734 array_free (idle, );
735 array_free (prepare, );
736 array_free (check, );
737
738 method = 0;
739}
740
741static void
742loop_fork (EV_P)
743{
744#if EV_USE_EPOLL
745 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
746#endif
747#if EV_USE_KQUEUE
748 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
749#endif
750
751 if (ev_is_active (&sigev))
752 {
753 /* default loop */
754
755 ev_ref (EV_A);
756 ev_io_stop (EV_A_ &sigev);
757 close (sigpipe [0]);
758 close (sigpipe [1]);
759
760 while (pipe (sigpipe))
761 syserr ("(libev) error creating pipe");
762
763 siginit (EV_A);
764 }
765
766 postfork = 0;
767}
768
769#if EV_MULTIPLICITY
770struct ev_loop *
771ev_loop_new (int methods)
772{
773 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
774
775 memset (loop, 0, sizeof (struct ev_loop));
776
777 loop_init (EV_A_ methods);
778
779 if (ev_method (EV_A))
780 return loop;
781
782 return 0;
783}
784
785void
786ev_loop_destroy (EV_P)
787{
788 loop_destroy (EV_A);
789 ev_free (loop);
790}
791
792void
793ev_loop_fork (EV_P)
794{
795 postfork = 1;
796}
797
798#endif
799
800#if EV_MULTIPLICITY
801struct ev_loop default_loop_struct;
802static struct ev_loop *default_loop;
803
804struct ev_loop *
805#else
806static int default_loop;
807
808int
809#endif
810ev_default_loop (int methods)
811{
812 if (sigpipe [0] == sigpipe [1])
813 if (pipe (sigpipe))
814 return 0;
815
816 if (!default_loop)
817 {
818#if EV_MULTIPLICITY
819 struct ev_loop *loop = default_loop = &default_loop_struct;
820#else
821 default_loop = 1;
822#endif
823
824 loop_init (EV_A_ methods);
825
634 if (method) 826 if (ev_method (EV_A))
635 { 827 {
636 ev_watcher_init (&sigev, sigcb);
637 ev_set_priority (&sigev, EV_MAXPRI);
638 siginit (EV_A); 828 siginit (EV_A);
639 829
640#ifndef WIN32 830#ifndef WIN32
641 ev_signal_init (&childev, childcb, SIGCHLD); 831 ev_signal_init (&childev, childcb, SIGCHLD);
642 ev_set_priority (&childev, EV_MAXPRI); 832 ev_set_priority (&childev, EV_MAXPRI);
643 ev_signal_start (EV_A_ &childev); 833 ev_signal_start (EV_A_ &childev);
834 ev_unref (EV_A); /* child watcher should not keep loop alive */
644#endif 835#endif
645 } 836 }
837 else
838 default_loop = 0;
646 } 839 }
647 840
648 return method; 841 return default_loop;
649} 842}
650 843
651/*****************************************************************************/
652
653void 844void
654ev_fork_prepare (void) 845ev_default_destroy (void)
655{ 846{
656 /* nop */ 847#if EV_MULTIPLICITY
657} 848 struct ev_loop *loop = default_loop;
658
659void
660ev_fork_parent (void)
661{
662 /* nop */
663}
664
665void
666ev_fork_child (void)
667{
668#if EV_USE_EPOLL
669 if (method == EVMETHOD_EPOLL)
670 epoll_postfork_child ();
671#endif 849#endif
672 850
851 ev_ref (EV_A); /* child watcher */
852 ev_signal_stop (EV_A_ &childev);
853
854 ev_ref (EV_A); /* signal watcher */
673 ev_io_stop (&sigev); 855 ev_io_stop (EV_A_ &sigev);
674 close (sigpipe [0]); 856
675 close (sigpipe [1]); 857 close (sigpipe [0]); sigpipe [0] = 0;
676 pipe (sigpipe); 858 close (sigpipe [1]); sigpipe [1] = 0;
677 siginit (); 859
860 loop_destroy (EV_A);
861}
862
863void
864ev_default_fork (void)
865{
866#if EV_MULTIPLICITY
867 struct ev_loop *loop = default_loop;
868#endif
869
870 if (method)
871 postfork = 1;
678} 872}
679 873
680/*****************************************************************************/ 874/*****************************************************************************/
681 875
682static void 876static void
698} 892}
699 893
700static void 894static void
701timers_reify (EV_P) 895timers_reify (EV_P)
702{ 896{
703 while (timercnt && timers [0]->at <= mn_now) 897 while (timercnt && ((WT)timers [0])->at <= mn_now)
704 { 898 {
705 struct ev_timer *w = timers [0]; 899 struct ev_timer *w = timers [0];
900
901 assert (("inactive timer on timer heap detected", ev_is_active (w)));
706 902
707 /* first reschedule or stop timer */ 903 /* first reschedule or stop timer */
708 if (w->repeat) 904 if (w->repeat)
709 { 905 {
710 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 906 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
711 w->at = mn_now + w->repeat; 907 ((WT)w)->at = mn_now + w->repeat;
712 downheap ((WT *)timers, timercnt, 0); 908 downheap ((WT *)timers, timercnt, 0);
713 } 909 }
714 else 910 else
715 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 911 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
716 912
717 event ((W)w, EV_TIMEOUT); 913 event (EV_A_ (W)w, EV_TIMEOUT);
718 } 914 }
719} 915}
720 916
721static void 917static void
722periodics_reify (EV_P) 918periodics_reify (EV_P)
723{ 919{
724 while (periodiccnt && periodics [0]->at <= rt_now) 920 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
725 { 921 {
726 struct ev_periodic *w = periodics [0]; 922 struct ev_periodic *w = periodics [0];
923
924 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
727 925
728 /* first reschedule or stop timer */ 926 /* first reschedule or stop timer */
729 if (w->interval) 927 if (w->interval)
730 { 928 {
731 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 929 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
732 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); 930 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
733 downheap ((WT *)periodics, periodiccnt, 0); 931 downheap ((WT *)periodics, periodiccnt, 0);
734 } 932 }
735 else 933 else
736 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 934 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
737 935
738 event (EV_A_ (W)w, EV_PERIODIC); 936 event (EV_A_ (W)w, EV_PERIODIC);
739 } 937 }
740} 938}
741 939
742static void 940static void
743periodics_reschedule (EV_P_ ev_tstamp diff) 941periodics_reschedule (EV_P)
744{ 942{
745 int i; 943 int i;
746 944
747 /* adjust periodics after time jump */ 945 /* adjust periodics after time jump */
748 for (i = 0; i < periodiccnt; ++i) 946 for (i = 0; i < periodiccnt; ++i)
749 { 947 {
750 struct ev_periodic *w = periodics [i]; 948 struct ev_periodic *w = periodics [i];
751 949
752 if (w->interval) 950 if (w->interval)
753 { 951 {
754 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; 952 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
755 953
756 if (fabs (diff) >= 1e-4) 954 if (fabs (diff) >= 1e-4)
757 { 955 {
758 ev_periodic_stop (EV_A_ w); 956 ev_periodic_stop (EV_A_ w);
759 ev_periodic_start (EV_A_ w); 957 ev_periodic_start (EV_A_ w);
769{ 967{
770 mn_now = get_clock (); 968 mn_now = get_clock ();
771 969
772 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 970 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
773 { 971 {
774 rt_now = mn_now + diff; 972 rt_now = rtmn_diff + mn_now;
775 return 0; 973 return 0;
776 } 974 }
777 else 975 else
778 { 976 {
779 now_floor = mn_now; 977 now_floor = mn_now;
790#if EV_USE_MONOTONIC 988#if EV_USE_MONOTONIC
791 if (expect_true (have_monotonic)) 989 if (expect_true (have_monotonic))
792 { 990 {
793 if (time_update_monotonic (EV_A)) 991 if (time_update_monotonic (EV_A))
794 { 992 {
795 ev_tstamp odiff = diff; 993 ev_tstamp odiff = rtmn_diff;
796 994
797 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 995 for (i = 4; --i; ) /* loop a few times, before making important decisions */
798 { 996 {
799 diff = rt_now - mn_now; 997 rtmn_diff = rt_now - mn_now;
800 998
801 if (fabs (odiff - diff) < MIN_TIMEJUMP) 999 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
802 return; /* all is well */ 1000 return; /* all is well */
803 1001
804 rt_now = ev_time (); 1002 rt_now = ev_time ();
805 mn_now = get_clock (); 1003 mn_now = get_clock ();
806 now_floor = mn_now; 1004 now_floor = mn_now;
807 } 1005 }
808 1006
809 periodics_reschedule (EV_A_ diff - odiff); 1007 periodics_reschedule (EV_A);
810 /* no timer adjustment, as the monotonic clock doesn't jump */ 1008 /* no timer adjustment, as the monotonic clock doesn't jump */
1009 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
811 } 1010 }
812 } 1011 }
813 else 1012 else
814#endif 1013#endif
815 { 1014 {
816 rt_now = ev_time (); 1015 rt_now = ev_time ();
817 1016
818 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1017 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
819 { 1018 {
820 periodics_reschedule (EV_A_ rt_now - mn_now); 1019 periodics_reschedule (EV_A);
821 1020
822 /* adjust timers. this is easy, as the offset is the same for all */ 1021 /* adjust timers. this is easy, as the offset is the same for all */
823 for (i = 0; i < timercnt; ++i) 1022 for (i = 0; i < timercnt; ++i)
824 timers [i]->at += diff; 1023 ((WT)timers [i])->at += rt_now - mn_now;
825 } 1024 }
826 1025
827 mn_now = rt_now; 1026 mn_now = rt_now;
828 } 1027 }
829} 1028}
855 { 1054 {
856 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1055 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
857 call_pending (EV_A); 1056 call_pending (EV_A);
858 } 1057 }
859 1058
1059 /* we might have forked, so reify kernel state if necessary */
1060 if (expect_false (postfork))
1061 loop_fork (EV_A);
1062
860 /* update fd-related kernel structures */ 1063 /* update fd-related kernel structures */
861 fd_reify (EV_A); 1064 fd_reify (EV_A);
862 1065
863 /* calculate blocking time */ 1066 /* calculate blocking time */
864 1067
880 { 1083 {
881 block = MAX_BLOCKTIME; 1084 block = MAX_BLOCKTIME;
882 1085
883 if (timercnt) 1086 if (timercnt)
884 { 1087 {
885 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1088 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
886 if (block > to) block = to; 1089 if (block > to) block = to;
887 } 1090 }
888 1091
889 if (periodiccnt) 1092 if (periodiccnt)
890 { 1093 {
891 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1094 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
892 if (block > to) block = to; 1095 if (block > to) block = to;
893 } 1096 }
894 1097
895 if (block < 0.) block = 0.; 1098 if (block < 0.) block = 0.;
896 } 1099 }
911 /* queue check watchers, to be executed first */ 1114 /* queue check watchers, to be executed first */
912 if (checkcnt) 1115 if (checkcnt)
913 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1116 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
914 1117
915 call_pending (EV_A); 1118 call_pending (EV_A);
916 printf ("activecnt %d\n", activecnt);//D
917 } 1119 }
918 while (activecnt && !loop_done); 1120 while (activecnt && !loop_done);
919 1121
920 if (loop_done != 2) 1122 if (loop_done != 2)
921 loop_done = 0; 1123 loop_done = 0;
1014ev_timer_start (EV_P_ struct ev_timer *w) 1216ev_timer_start (EV_P_ struct ev_timer *w)
1015{ 1217{
1016 if (ev_is_active (w)) 1218 if (ev_is_active (w))
1017 return; 1219 return;
1018 1220
1019 w->at += mn_now; 1221 ((WT)w)->at += mn_now;
1020 1222
1021 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1223 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1022 1224
1023 ev_start (EV_A_ (W)w, ++timercnt); 1225 ev_start (EV_A_ (W)w, ++timercnt);
1024 array_needsize (timers, timermax, timercnt, ); 1226 array_needsize (timers, timermax, timercnt, );
1025 timers [timercnt - 1] = w; 1227 timers [timercnt - 1] = w;
1026 upheap ((WT *)timers, timercnt - 1); 1228 upheap ((WT *)timers, timercnt - 1);
1229
1230 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1027} 1231}
1028 1232
1029void 1233void
1030ev_timer_stop (EV_P_ struct ev_timer *w) 1234ev_timer_stop (EV_P_ struct ev_timer *w)
1031{ 1235{
1032 ev_clear_pending (EV_A_ (W)w); 1236 ev_clear_pending (EV_A_ (W)w);
1033 if (!ev_is_active (w)) 1237 if (!ev_is_active (w))
1034 return; 1238 return;
1035 1239
1240 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1241
1036 if (w->active < timercnt--) 1242 if (((W)w)->active < timercnt--)
1037 { 1243 {
1038 timers [w->active - 1] = timers [timercnt]; 1244 timers [((W)w)->active - 1] = timers [timercnt];
1039 downheap ((WT *)timers, timercnt, w->active - 1); 1245 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1040 } 1246 }
1041 1247
1042 w->at = w->repeat; 1248 ((WT)w)->at = w->repeat;
1043 1249
1044 ev_stop (EV_A_ (W)w); 1250 ev_stop (EV_A_ (W)w);
1045} 1251}
1046 1252
1047void 1253void
1049{ 1255{
1050 if (ev_is_active (w)) 1256 if (ev_is_active (w))
1051 { 1257 {
1052 if (w->repeat) 1258 if (w->repeat)
1053 { 1259 {
1054 w->at = mn_now + w->repeat; 1260 ((WT)w)->at = mn_now + w->repeat;
1055 downheap ((WT *)timers, timercnt, w->active - 1); 1261 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1056 } 1262 }
1057 else 1263 else
1058 ev_timer_stop (EV_A_ w); 1264 ev_timer_stop (EV_A_ w);
1059 } 1265 }
1060 else if (w->repeat) 1266 else if (w->repeat)
1069 1275
1070 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1276 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1071 1277
1072 /* this formula differs from the one in periodic_reify because we do not always round up */ 1278 /* this formula differs from the one in periodic_reify because we do not always round up */
1073 if (w->interval) 1279 if (w->interval)
1074 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; 1280 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1075 1281
1076 ev_start (EV_A_ (W)w, ++periodiccnt); 1282 ev_start (EV_A_ (W)w, ++periodiccnt);
1077 array_needsize (periodics, periodicmax, periodiccnt, ); 1283 array_needsize (periodics, periodicmax, periodiccnt, );
1078 periodics [periodiccnt - 1] = w; 1284 periodics [periodiccnt - 1] = w;
1079 upheap ((WT *)periodics, periodiccnt - 1); 1285 upheap ((WT *)periodics, periodiccnt - 1);
1286
1287 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1080} 1288}
1081 1289
1082void 1290void
1083ev_periodic_stop (EV_P_ struct ev_periodic *w) 1291ev_periodic_stop (EV_P_ struct ev_periodic *w)
1084{ 1292{
1085 ev_clear_pending (EV_A_ (W)w); 1293 ev_clear_pending (EV_A_ (W)w);
1086 if (!ev_is_active (w)) 1294 if (!ev_is_active (w))
1087 return; 1295 return;
1088 1296
1297 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1298
1089 if (w->active < periodiccnt--) 1299 if (((W)w)->active < periodiccnt--)
1090 { 1300 {
1091 periodics [w->active - 1] = periodics [periodiccnt]; 1301 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1092 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1302 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1093 } 1303 }
1094 1304
1305 ev_stop (EV_A_ (W)w);
1306}
1307
1308void
1309ev_idle_start (EV_P_ struct ev_idle *w)
1310{
1311 if (ev_is_active (w))
1312 return;
1313
1314 ev_start (EV_A_ (W)w, ++idlecnt);
1315 array_needsize (idles, idlemax, idlecnt, );
1316 idles [idlecnt - 1] = w;
1317}
1318
1319void
1320ev_idle_stop (EV_P_ struct ev_idle *w)
1321{
1322 ev_clear_pending (EV_A_ (W)w);
1323 if (ev_is_active (w))
1324 return;
1325
1326 idles [((W)w)->active - 1] = idles [--idlecnt];
1327 ev_stop (EV_A_ (W)w);
1328}
1329
1330void
1331ev_prepare_start (EV_P_ struct ev_prepare *w)
1332{
1333 if (ev_is_active (w))
1334 return;
1335
1336 ev_start (EV_A_ (W)w, ++preparecnt);
1337 array_needsize (prepares, preparemax, preparecnt, );
1338 prepares [preparecnt - 1] = w;
1339}
1340
1341void
1342ev_prepare_stop (EV_P_ struct ev_prepare *w)
1343{
1344 ev_clear_pending (EV_A_ (W)w);
1345 if (ev_is_active (w))
1346 return;
1347
1348 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1349 ev_stop (EV_A_ (W)w);
1350}
1351
1352void
1353ev_check_start (EV_P_ struct ev_check *w)
1354{
1355 if (ev_is_active (w))
1356 return;
1357
1358 ev_start (EV_A_ (W)w, ++checkcnt);
1359 array_needsize (checks, checkmax, checkcnt, );
1360 checks [checkcnt - 1] = w;
1361}
1362
1363void
1364ev_check_stop (EV_P_ struct ev_check *w)
1365{
1366 ev_clear_pending (EV_A_ (W)w);
1367 if (ev_is_active (w))
1368 return;
1369
1370 checks [((W)w)->active - 1] = checks [--checkcnt];
1095 ev_stop (EV_A_ (W)w); 1371 ev_stop (EV_A_ (W)w);
1096} 1372}
1097 1373
1098#ifndef SA_RESTART 1374#ifndef SA_RESTART
1099# define SA_RESTART 0 1375# define SA_RESTART 0
1100#endif 1376#endif
1101 1377
1102void 1378void
1103ev_signal_start (EV_P_ struct ev_signal *w) 1379ev_signal_start (EV_P_ struct ev_signal *w)
1104{ 1380{
1381#if EV_MULTIPLICITY
1382 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1383#endif
1105 if (ev_is_active (w)) 1384 if (ev_is_active (w))
1106 return; 1385 return;
1107 1386
1108 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1387 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1109 1388
1110 ev_start (EV_A_ (W)w, 1); 1389 ev_start (EV_A_ (W)w, 1);
1111 array_needsize (signals, signalmax, w->signum, signals_init); 1390 array_needsize (signals, signalmax, w->signum, signals_init);
1112 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1391 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1113 1392
1114 if (!w->next) 1393 if (!((WL)w)->next)
1115 { 1394 {
1395#if WIN32
1396 signal (w->signum, sighandler);
1397#else
1116 struct sigaction sa; 1398 struct sigaction sa;
1117 sa.sa_handler = sighandler; 1399 sa.sa_handler = sighandler;
1118 sigfillset (&sa.sa_mask); 1400 sigfillset (&sa.sa_mask);
1119 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1401 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1120 sigaction (w->signum, &sa, 0); 1402 sigaction (w->signum, &sa, 0);
1403#endif
1121 } 1404 }
1122} 1405}
1123 1406
1124void 1407void
1125ev_signal_stop (EV_P_ struct ev_signal *w) 1408ev_signal_stop (EV_P_ struct ev_signal *w)
1134 if (!signals [w->signum - 1].head) 1417 if (!signals [w->signum - 1].head)
1135 signal (w->signum, SIG_DFL); 1418 signal (w->signum, SIG_DFL);
1136} 1419}
1137 1420
1138void 1421void
1139ev_idle_start (EV_P_ struct ev_idle *w)
1140{
1141 if (ev_is_active (w))
1142 return;
1143
1144 ev_start (EV_A_ (W)w, ++idlecnt);
1145 array_needsize (idles, idlemax, idlecnt, );
1146 idles [idlecnt - 1] = w;
1147}
1148
1149void
1150ev_idle_stop (EV_P_ struct ev_idle *w)
1151{
1152 ev_clear_pending (EV_A_ (W)w);
1153 if (ev_is_active (w))
1154 return;
1155
1156 idles [w->active - 1] = idles [--idlecnt];
1157 ev_stop (EV_A_ (W)w);
1158}
1159
1160void
1161ev_prepare_start (EV_P_ struct ev_prepare *w)
1162{
1163 if (ev_is_active (w))
1164 return;
1165
1166 ev_start (EV_A_ (W)w, ++preparecnt);
1167 array_needsize (prepares, preparemax, preparecnt, );
1168 prepares [preparecnt - 1] = w;
1169}
1170
1171void
1172ev_prepare_stop (EV_P_ struct ev_prepare *w)
1173{
1174 ev_clear_pending (EV_A_ (W)w);
1175 if (ev_is_active (w))
1176 return;
1177
1178 prepares [w->active - 1] = prepares [--preparecnt];
1179 ev_stop (EV_A_ (W)w);
1180}
1181
1182void
1183ev_check_start (EV_P_ struct ev_check *w)
1184{
1185 if (ev_is_active (w))
1186 return;
1187
1188 ev_start (EV_A_ (W)w, ++checkcnt);
1189 array_needsize (checks, checkmax, checkcnt, );
1190 checks [checkcnt - 1] = w;
1191}
1192
1193void
1194ev_check_stop (EV_P_ struct ev_check *w)
1195{
1196 ev_clear_pending (EV_A_ (W)w);
1197 if (ev_is_active (w))
1198 return;
1199
1200 checks [w->active - 1] = checks [--checkcnt];
1201 ev_stop (EV_A_ (W)w);
1202}
1203
1204void
1205ev_child_start (EV_P_ struct ev_child *w) 1422ev_child_start (EV_P_ struct ev_child *w)
1206{ 1423{
1424#if EV_MULTIPLICITY
1425 assert (("child watchers are only supported in the default loop", loop == default_loop));
1426#endif
1207 if (ev_is_active (w)) 1427 if (ev_is_active (w))
1208 return; 1428 return;
1209 1429
1210 ev_start (EV_A_ (W)w, 1); 1430 ev_start (EV_A_ (W)w, 1);
1211 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1431 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1238 void (*cb)(int revents, void *arg) = once->cb; 1458 void (*cb)(int revents, void *arg) = once->cb;
1239 void *arg = once->arg; 1459 void *arg = once->arg;
1240 1460
1241 ev_io_stop (EV_A_ &once->io); 1461 ev_io_stop (EV_A_ &once->io);
1242 ev_timer_stop (EV_A_ &once->to); 1462 ev_timer_stop (EV_A_ &once->to);
1243 free (once); 1463 ev_free (once);
1244 1464
1245 cb (revents, arg); 1465 cb (revents, arg);
1246} 1466}
1247 1467
1248static void 1468static void
1258} 1478}
1259 1479
1260void 1480void
1261ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1481ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1262{ 1482{
1263 struct ev_once *once = malloc (sizeof (struct ev_once)); 1483 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1264 1484
1265 if (!once) 1485 if (!once)
1266 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1486 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1267 else 1487 else
1268 { 1488 {
1283 ev_timer_start (EV_A_ &once->to); 1503 ev_timer_start (EV_A_ &once->to);
1284 } 1504 }
1285 } 1505 }
1286} 1506}
1287 1507
1288/*****************************************************************************/
1289
1290#if 0
1291
1292struct ev_io wio;
1293
1294static void
1295sin_cb (struct ev_io *w, int revents)
1296{
1297 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1298}
1299
1300static void
1301ocb (struct ev_timer *w, int revents)
1302{
1303 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1304 ev_timer_stop (w);
1305 ev_timer_start (w);
1306}
1307
1308static void
1309scb (struct ev_signal *w, int revents)
1310{
1311 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1312 ev_io_stop (&wio);
1313 ev_io_start (&wio);
1314}
1315
1316static void
1317gcb (struct ev_signal *w, int revents)
1318{
1319 fprintf (stderr, "generic %x\n", revents);
1320
1321}
1322
1323int main (void)
1324{
1325 ev_init (0);
1326
1327 ev_io_init (&wio, sin_cb, 0, EV_READ);
1328 ev_io_start (&wio);
1329
1330 struct ev_timer t[10000];
1331
1332#if 0
1333 int i;
1334 for (i = 0; i < 10000; ++i)
1335 {
1336 struct ev_timer *w = t + i;
1337 ev_watcher_init (w, ocb, i);
1338 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1339 ev_timer_start (w);
1340 if (drand48 () < 0.5)
1341 ev_timer_stop (w);
1342 }
1343#endif
1344
1345 struct ev_timer t1;
1346 ev_timer_init (&t1, ocb, 5, 10);
1347 ev_timer_start (&t1);
1348
1349 struct ev_signal sig;
1350 ev_signal_init (&sig, scb, SIGQUIT);
1351 ev_signal_start (&sig);
1352
1353 struct ev_check cw;
1354 ev_check_init (&cw, gcb);
1355 ev_check_start (&cw);
1356
1357 struct ev_idle iw;
1358 ev_idle_init (&iw, gcb);
1359 ev_idle_start (&iw);
1360
1361 ev_loop (0);
1362
1363 return 0;
1364}
1365
1366#endif
1367
1368
1369
1370

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines