ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.27
Committed: Wed Oct 31 22:16:36 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.26: +55 -47 lines
Log Message:
only add fds to fdchangeset one

File Contents

# Content
1 /*
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <math.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <signal.h>
35 #include <stddef.h>
36
37 #include <stdio.h>
38
39 #include <assert.h>
40 #include <errno.h>
41 #include <sys/types.h>
42 #include <sys/wait.h>
43 #include <sys/time.h>
44 #include <time.h>
45
46 #ifndef HAVE_MONOTONIC
47 # ifdef CLOCK_MONOTONIC
48 # define HAVE_MONOTONIC 1
49 # endif
50 #endif
51
52 #ifndef HAVE_SELECT
53 # define HAVE_SELECT 1
54 #endif
55
56 #ifndef HAVE_EPOLL
57 # define HAVE_EPOLL 0
58 #endif
59
60 #ifndef HAVE_REALTIME
61 # define HAVE_REALTIME 1 /* posix requirement, but might be slower */
62 #endif
63
64 #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
65 #define MAX_BLOCKTIME 60.
66 #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
67
68 #include "ev.h"
69
70 typedef struct ev_watcher *W;
71 typedef struct ev_watcher_list *WL;
72 typedef struct ev_watcher_time *WT;
73
74 static ev_tstamp now, diff; /* monotonic clock */
75 ev_tstamp ev_now;
76 int ev_method;
77
78 static int have_monotonic; /* runtime */
79
80 static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
81 static void (*method_modify)(int fd, int oev, int nev);
82 static void (*method_poll)(ev_tstamp timeout);
83
84 /*****************************************************************************/
85
86 ev_tstamp
87 ev_time (void)
88 {
89 #if HAVE_REALTIME
90 struct timespec ts;
91 clock_gettime (CLOCK_REALTIME, &ts);
92 return ts.tv_sec + ts.tv_nsec * 1e-9;
93 #else
94 struct timeval tv;
95 gettimeofday (&tv, 0);
96 return tv.tv_sec + tv.tv_usec * 1e-6;
97 #endif
98 }
99
100 static ev_tstamp
101 get_clock (void)
102 {
103 #if HAVE_MONOTONIC
104 if (have_monotonic)
105 {
106 struct timespec ts;
107 clock_gettime (CLOCK_MONOTONIC, &ts);
108 return ts.tv_sec + ts.tv_nsec * 1e-9;
109 }
110 #endif
111
112 return ev_time ();
113 }
114
115 #define array_needsize(base,cur,cnt,init) \
116 if ((cnt) > cur) \
117 { \
118 int newcnt = cur; \
119 do \
120 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \
122 } \
123 while ((cnt) > newcnt); \
124 \
125 base = realloc (base, sizeof (*base) * (newcnt)); \
126 init (base + cur, newcnt - cur); \
127 cur = newcnt; \
128 }
129
130 /*****************************************************************************/
131
132 typedef struct
133 {
134 struct ev_io *head;
135 int events;
136 } ANFD;
137
138 static ANFD *anfds;
139 static int anfdmax;
140
141 static void
142 anfds_init (ANFD *base, int count)
143 {
144 while (count--)
145 {
146 base->head = 0;
147 base->events = EV_NONE;
148 ++base;
149 }
150 }
151
152 typedef struct
153 {
154 W w;
155 int events;
156 } ANPENDING;
157
158 static ANPENDING *pendings;
159 static int pendingmax, pendingcnt;
160
161 static void
162 event (W w, int events)
163 {
164 if (w->active)
165 {
166 w->pending = ++pendingcnt;
167 array_needsize (pendings, pendingmax, pendingcnt, );
168 pendings [pendingcnt - 1].w = w;
169 pendings [pendingcnt - 1].events = events;
170 }
171 }
172
173 static void
174 queue_events (W *events, int eventcnt, int type)
175 {
176 int i;
177
178 for (i = 0; i < eventcnt; ++i)
179 event (events [i], type);
180 }
181
182 static void
183 fd_event (int fd, int events)
184 {
185 ANFD *anfd = anfds + fd;
186 struct ev_io *w;
187
188 for (w = anfd->head; w; w = w->next)
189 {
190 int ev = w->events & events;
191
192 if (ev)
193 event ((W)w, ev);
194 }
195 }
196
197 /*****************************************************************************/
198
199 static int *fdchanges;
200 static int fdchangemax, fdchangecnt;
201
202 static void
203 fd_reify (void)
204 {
205 int i;
206
207 for (i = 0; i < fdchangecnt; ++i)
208 {
209 int fd = fdchanges [i];
210 ANFD *anfd = anfds + fd;
211 struct ev_io *w;
212
213 int events = 0;
214
215 for (w = anfd->head; w; w = w->next)
216 events |= w->events;
217
218 anfd->events &= ~EV_REIFY;
219
220 if (anfd->events != events)
221 {
222 method_modify (fd, anfd->events, events);
223 anfd->events = events;
224 }
225 }
226
227 fdchangecnt = 0;
228 }
229
230 static void
231 fd_change (int fd)
232 {
233 if (anfds [fd].events & EV_REIFY)
234 return;
235
236 anfds [fd].events |= EV_REIFY;
237
238 ++fdchangecnt;
239 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
240 fdchanges [fdchangecnt - 1] = fd;
241 }
242
243 /* called on EBADF to verify fds */
244 static void
245 fd_recheck (void)
246 {
247 int fd;
248
249 for (fd = 0; fd < anfdmax; ++fd)
250 if (anfds [fd].events)
251 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
252 while (anfds [fd].head)
253 {
254 event ((W)anfds [fd].head, EV_ERROR);
255 evio_stop (anfds [fd].head);
256 }
257 }
258
259 /*****************************************************************************/
260
261 static struct ev_timer **timers;
262 static int timermax, timercnt;
263
264 static struct ev_periodic **periodics;
265 static int periodicmax, periodiccnt;
266
267 static void
268 upheap (WT *timers, int k)
269 {
270 WT w = timers [k];
271
272 while (k && timers [k >> 1]->at > w->at)
273 {
274 timers [k] = timers [k >> 1];
275 timers [k]->active = k + 1;
276 k >>= 1;
277 }
278
279 timers [k] = w;
280 timers [k]->active = k + 1;
281
282 }
283
284 static void
285 downheap (WT *timers, int N, int k)
286 {
287 WT w = timers [k];
288
289 while (k < (N >> 1))
290 {
291 int j = k << 1;
292
293 if (j + 1 < N && timers [j]->at > timers [j + 1]->at)
294 ++j;
295
296 if (w->at <= timers [j]->at)
297 break;
298
299 timers [k] = timers [j];
300 timers [k]->active = k + 1;
301 k = j;
302 }
303
304 timers [k] = w;
305 timers [k]->active = k + 1;
306 }
307
308 /*****************************************************************************/
309
310 typedef struct
311 {
312 struct ev_signal *head;
313 sig_atomic_t gotsig;
314 } ANSIG;
315
316 static ANSIG *signals;
317 static int signalmax;
318
319 static int sigpipe [2];
320 static sig_atomic_t gotsig;
321 static struct ev_io sigev;
322
323 static void
324 signals_init (ANSIG *base, int count)
325 {
326 while (count--)
327 {
328 base->head = 0;
329 base->gotsig = 0;
330 ++base;
331 }
332 }
333
334 static void
335 sighandler (int signum)
336 {
337 signals [signum - 1].gotsig = 1;
338
339 if (!gotsig)
340 {
341 gotsig = 1;
342 write (sigpipe [1], &gotsig, 1);
343 }
344 }
345
346 static void
347 sigcb (struct ev_io *iow, int revents)
348 {
349 struct ev_signal *w;
350 int sig;
351
352 gotsig = 0;
353 read (sigpipe [0], &revents, 1);
354
355 for (sig = signalmax; sig--; )
356 if (signals [sig].gotsig)
357 {
358 signals [sig].gotsig = 0;
359
360 for (w = signals [sig].head; w; w = w->next)
361 event ((W)w, EV_SIGNAL);
362 }
363 }
364
365 static void
366 siginit (void)
367 {
368 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
369 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
370
371 /* rather than sort out wether we really need nb, set it */
372 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
373 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
374
375 evio_set (&sigev, sigpipe [0], EV_READ);
376 evio_start (&sigev);
377 }
378
379 /*****************************************************************************/
380
381 static struct ev_idle **idles;
382 static int idlemax, idlecnt;
383
384 static struct ev_prepare **prepares;
385 static int preparemax, preparecnt;
386
387 static struct ev_check **checks;
388 static int checkmax, checkcnt;
389
390 /*****************************************************************************/
391
392 static struct ev_child *childs [PID_HASHSIZE];
393 static struct ev_signal childev;
394
395 #ifndef WCONTINUED
396 # define WCONTINUED 0
397 #endif
398
399 static void
400 childcb (struct ev_signal *sw, int revents)
401 {
402 struct ev_child *w;
403 int pid, status;
404
405 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
406 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
407 if (w->pid == pid || w->pid == -1)
408 {
409 w->status = status;
410 event ((W)w, EV_CHILD);
411 }
412 }
413
414 /*****************************************************************************/
415
416 #if HAVE_EPOLL
417 # include "ev_epoll.c"
418 #endif
419 #if HAVE_SELECT
420 # include "ev_select.c"
421 #endif
422
423 int
424 ev_version_major (void)
425 {
426 return EV_VERSION_MAJOR;
427 }
428
429 int
430 ev_version_minor (void)
431 {
432 return EV_VERSION_MINOR;
433 }
434
435 int ev_init (int flags)
436 {
437 if (!ev_method)
438 {
439 #if HAVE_MONOTONIC
440 {
441 struct timespec ts;
442 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
443 have_monotonic = 1;
444 }
445 #endif
446
447 ev_now = ev_time ();
448 now = get_clock ();
449 diff = ev_now - now;
450
451 if (pipe (sigpipe))
452 return 0;
453
454 ev_method = EVMETHOD_NONE;
455 #if HAVE_EPOLL
456 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
457 #endif
458 #if HAVE_SELECT
459 if (ev_method == EVMETHOD_NONE) select_init (flags);
460 #endif
461
462 if (ev_method)
463 {
464 evw_init (&sigev, sigcb);
465 siginit ();
466
467 evsignal_init (&childev, childcb, SIGCHLD);
468 evsignal_start (&childev);
469 }
470 }
471
472 return ev_method;
473 }
474
475 /*****************************************************************************/
476
477 void
478 ev_prefork (void)
479 {
480 /* nop */
481 }
482
483 void
484 ev_postfork_parent (void)
485 {
486 /* nop */
487 }
488
489 void
490 ev_postfork_child (void)
491 {
492 #if HAVE_EPOLL
493 if (ev_method == EVMETHOD_EPOLL)
494 epoll_postfork_child ();
495 #endif
496
497 evio_stop (&sigev);
498 close (sigpipe [0]);
499 close (sigpipe [1]);
500 pipe (sigpipe);
501 siginit ();
502 }
503
504 /*****************************************************************************/
505
506 static void
507 call_pending (void)
508 {
509 while (pendingcnt)
510 {
511 ANPENDING *p = pendings + --pendingcnt;
512
513 if (p->w)
514 {
515 p->w->pending = 0;
516 p->w->cb (p->w, p->events);
517 }
518 }
519 }
520
521 static void
522 timers_reify (void)
523 {
524 while (timercnt && timers [0]->at <= now)
525 {
526 struct ev_timer *w = timers [0];
527
528 event ((W)w, EV_TIMEOUT);
529
530 /* first reschedule or stop timer */
531 if (w->repeat)
532 {
533 w->at = now + w->repeat;
534 assert (("timer timeout in the past, negative repeat?", w->at > now));
535 downheap ((WT *)timers, timercnt, 0);
536 }
537 else
538 evtimer_stop (w); /* nonrepeating: stop timer */
539 }
540 }
541
542 static void
543 periodics_reify (void)
544 {
545 while (periodiccnt && periodics [0]->at <= ev_now)
546 {
547 struct ev_periodic *w = periodics [0];
548
549 /* first reschedule or stop timer */
550 if (w->interval)
551 {
552 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
553 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
554 downheap ((WT *)periodics, periodiccnt, 0);
555 }
556 else
557 evperiodic_stop (w); /* nonrepeating: stop timer */
558
559 event ((W)w, EV_TIMEOUT);
560 }
561 }
562
563 static void
564 periodics_reschedule (ev_tstamp diff)
565 {
566 int i;
567
568 /* adjust periodics after time jump */
569 for (i = 0; i < periodiccnt; ++i)
570 {
571 struct ev_periodic *w = periodics [i];
572
573 if (w->interval)
574 {
575 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
576
577 if (fabs (diff) >= 1e-4)
578 {
579 evperiodic_stop (w);
580 evperiodic_start (w);
581
582 i = 0; /* restart loop, inefficient, but time jumps should be rare */
583 }
584 }
585 }
586 }
587
588 static void
589 time_update (void)
590 {
591 int i;
592
593 ev_now = ev_time ();
594
595 if (have_monotonic)
596 {
597 ev_tstamp odiff = diff;
598
599 for (i = 4; --i; ) /* loop a few times, before making important decisions */
600 {
601 now = get_clock ();
602 diff = ev_now - now;
603
604 if (fabs (odiff - diff) < MIN_TIMEJUMP)
605 return; /* all is well */
606
607 ev_now = ev_time ();
608 }
609
610 periodics_reschedule (diff - odiff);
611 /* no timer adjustment, as the monotonic clock doesn't jump */
612 }
613 else
614 {
615 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
616 {
617 periodics_reschedule (ev_now - now);
618
619 /* adjust timers. this is easy, as the offset is the same for all */
620 for (i = 0; i < timercnt; ++i)
621 timers [i]->at += diff;
622 }
623
624 now = ev_now;
625 }
626 }
627
628 int ev_loop_done;
629
630 void ev_loop (int flags)
631 {
632 double block;
633 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
634
635 do
636 {
637 /* queue check watchers (and execute them) */
638 if (preparecnt)
639 {
640 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
641 call_pending ();
642 }
643
644 /* update fd-related kernel structures */
645 fd_reify ();
646
647 /* calculate blocking time */
648
649 /* we only need this for !monotonic clockor timers, but as we basically
650 always have timers, we just calculate it always */
651 ev_now = ev_time ();
652
653 if (flags & EVLOOP_NONBLOCK || idlecnt)
654 block = 0.;
655 else
656 {
657 block = MAX_BLOCKTIME;
658
659 if (timercnt)
660 {
661 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
662 if (block > to) block = to;
663 }
664
665 if (periodiccnt)
666 {
667 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
668 if (block > to) block = to;
669 }
670
671 if (block < 0.) block = 0.;
672 }
673
674 method_poll (block);
675
676 /* update ev_now, do magic */
677 time_update ();
678
679 /* queue pending timers and reschedule them */
680 timers_reify (); /* relative timers called last */
681 periodics_reify (); /* absolute timers called first */
682
683 /* queue idle watchers unless io or timers are pending */
684 if (!pendingcnt)
685 queue_events ((W *)idles, idlecnt, EV_IDLE);
686
687 /* queue check watchers, to be executed first */
688 if (checkcnt)
689 queue_events ((W *)checks, checkcnt, EV_CHECK);
690
691 call_pending ();
692 }
693 while (!ev_loop_done);
694
695 if (ev_loop_done != 2)
696 ev_loop_done = 0;
697 }
698
699 /*****************************************************************************/
700
701 static void
702 wlist_add (WL *head, WL elem)
703 {
704 elem->next = *head;
705 *head = elem;
706 }
707
708 static void
709 wlist_del (WL *head, WL elem)
710 {
711 while (*head)
712 {
713 if (*head == elem)
714 {
715 *head = elem->next;
716 return;
717 }
718
719 head = &(*head)->next;
720 }
721 }
722
723 static void
724 ev_clear (W w)
725 {
726 if (w->pending)
727 {
728 pendings [w->pending - 1].w = 0;
729 w->pending = 0;
730 }
731 }
732
733 static void
734 ev_start (W w, int active)
735 {
736 w->active = active;
737 }
738
739 static void
740 ev_stop (W w)
741 {
742 w->active = 0;
743 }
744
745 /*****************************************************************************/
746
747 void
748 evio_start (struct ev_io *w)
749 {
750 if (ev_is_active (w))
751 return;
752
753 int fd = w->fd;
754
755 ev_start ((W)w, 1);
756 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
757 wlist_add ((WL *)&anfds[fd].head, (WL)w);
758
759 fd_change (fd);
760 }
761
762 void
763 evio_stop (struct ev_io *w)
764 {
765 ev_clear ((W)w);
766 if (!ev_is_active (w))
767 return;
768
769 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
770 ev_stop ((W)w);
771
772 fd_change (w->fd);
773 }
774
775 void
776 evtimer_start (struct ev_timer *w)
777 {
778 if (ev_is_active (w))
779 return;
780
781 w->at += now;
782
783 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
784
785 ev_start ((W)w, ++timercnt);
786 array_needsize (timers, timermax, timercnt, );
787 timers [timercnt - 1] = w;
788 upheap ((WT *)timers, timercnt - 1);
789 }
790
791 void
792 evtimer_stop (struct ev_timer *w)
793 {
794 ev_clear ((W)w);
795 if (!ev_is_active (w))
796 return;
797
798 if (w->active < timercnt--)
799 {
800 timers [w->active - 1] = timers [timercnt];
801 downheap ((WT *)timers, timercnt, w->active - 1);
802 }
803
804 w->at = w->repeat;
805
806 ev_stop ((W)w);
807 }
808
809 void
810 evtimer_again (struct ev_timer *w)
811 {
812 if (ev_is_active (w))
813 {
814 if (w->repeat)
815 {
816 w->at = now + w->repeat;
817 downheap ((WT *)timers, timercnt, w->active - 1);
818 }
819 else
820 evtimer_stop (w);
821 }
822 else if (w->repeat)
823 evtimer_start (w);
824 }
825
826 void
827 evperiodic_start (struct ev_periodic *w)
828 {
829 if (ev_is_active (w))
830 return;
831
832 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
833
834 /* this formula differs from the one in periodic_reify because we do not always round up */
835 if (w->interval)
836 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
837
838 ev_start ((W)w, ++periodiccnt);
839 array_needsize (periodics, periodicmax, periodiccnt, );
840 periodics [periodiccnt - 1] = w;
841 upheap ((WT *)periodics, periodiccnt - 1);
842 }
843
844 void
845 evperiodic_stop (struct ev_periodic *w)
846 {
847 ev_clear ((W)w);
848 if (!ev_is_active (w))
849 return;
850
851 if (w->active < periodiccnt--)
852 {
853 periodics [w->active - 1] = periodics [periodiccnt];
854 downheap ((WT *)periodics, periodiccnt, w->active - 1);
855 }
856
857 ev_stop ((W)w);
858 }
859
860 void
861 evsignal_start (struct ev_signal *w)
862 {
863 if (ev_is_active (w))
864 return;
865
866 ev_start ((W)w, 1);
867 array_needsize (signals, signalmax, w->signum, signals_init);
868 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
869
870 if (!w->next)
871 {
872 struct sigaction sa;
873 sa.sa_handler = sighandler;
874 sigfillset (&sa.sa_mask);
875 sa.sa_flags = 0;
876 sigaction (w->signum, &sa, 0);
877 }
878 }
879
880 void
881 evsignal_stop (struct ev_signal *w)
882 {
883 ev_clear ((W)w);
884 if (!ev_is_active (w))
885 return;
886
887 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
888 ev_stop ((W)w);
889
890 if (!signals [w->signum - 1].head)
891 signal (w->signum, SIG_DFL);
892 }
893
894 void evidle_start (struct ev_idle *w)
895 {
896 if (ev_is_active (w))
897 return;
898
899 ev_start ((W)w, ++idlecnt);
900 array_needsize (idles, idlemax, idlecnt, );
901 idles [idlecnt - 1] = w;
902 }
903
904 void evidle_stop (struct ev_idle *w)
905 {
906 ev_clear ((W)w);
907 if (ev_is_active (w))
908 return;
909
910 idles [w->active - 1] = idles [--idlecnt];
911 ev_stop ((W)w);
912 }
913
914 void evprepare_start (struct ev_prepare *w)
915 {
916 if (ev_is_active (w))
917 return;
918
919 ev_start ((W)w, ++preparecnt);
920 array_needsize (prepares, preparemax, preparecnt, );
921 prepares [preparecnt - 1] = w;
922 }
923
924 void evprepare_stop (struct ev_prepare *w)
925 {
926 ev_clear ((W)w);
927 if (ev_is_active (w))
928 return;
929
930 prepares [w->active - 1] = prepares [--preparecnt];
931 ev_stop ((W)w);
932 }
933
934 void evcheck_start (struct ev_check *w)
935 {
936 if (ev_is_active (w))
937 return;
938
939 ev_start ((W)w, ++checkcnt);
940 array_needsize (checks, checkmax, checkcnt, );
941 checks [checkcnt - 1] = w;
942 }
943
944 void evcheck_stop (struct ev_check *w)
945 {
946 ev_clear ((W)w);
947 if (ev_is_active (w))
948 return;
949
950 checks [w->active - 1] = checks [--checkcnt];
951 ev_stop ((W)w);
952 }
953
954 void evchild_start (struct ev_child *w)
955 {
956 if (ev_is_active (w))
957 return;
958
959 ev_start ((W)w, 1);
960 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
961 }
962
963 void evchild_stop (struct ev_child *w)
964 {
965 ev_clear ((W)w);
966 if (ev_is_active (w))
967 return;
968
969 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
970 ev_stop ((W)w);
971 }
972
973 /*****************************************************************************/
974
975 struct ev_once
976 {
977 struct ev_io io;
978 struct ev_timer to;
979 void (*cb)(int revents, void *arg);
980 void *arg;
981 };
982
983 static void
984 once_cb (struct ev_once *once, int revents)
985 {
986 void (*cb)(int revents, void *arg) = once->cb;
987 void *arg = once->arg;
988
989 evio_stop (&once->io);
990 evtimer_stop (&once->to);
991 free (once);
992
993 cb (revents, arg);
994 }
995
996 static void
997 once_cb_io (struct ev_io *w, int revents)
998 {
999 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1000 }
1001
1002 static void
1003 once_cb_to (struct ev_timer *w, int revents)
1004 {
1005 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1006 }
1007
1008 void
1009 ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1010 {
1011 struct ev_once *once = malloc (sizeof (struct ev_once));
1012
1013 if (!once)
1014 cb (EV_ERROR, arg);
1015 else
1016 {
1017 once->cb = cb;
1018 once->arg = arg;
1019
1020 evw_init (&once->io, once_cb_io);
1021
1022 if (fd >= 0)
1023 {
1024 evio_set (&once->io, fd, events);
1025 evio_start (&once->io);
1026 }
1027
1028 evw_init (&once->to, once_cb_to);
1029
1030 if (timeout >= 0.)
1031 {
1032 evtimer_set (&once->to, timeout, 0.);
1033 evtimer_start (&once->to);
1034 }
1035 }
1036 }
1037
1038 /*****************************************************************************/
1039
1040 #if 0
1041
1042 struct ev_io wio;
1043
1044 static void
1045 sin_cb (struct ev_io *w, int revents)
1046 {
1047 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1048 }
1049
1050 static void
1051 ocb (struct ev_timer *w, int revents)
1052 {
1053 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1054 evtimer_stop (w);
1055 evtimer_start (w);
1056 }
1057
1058 static void
1059 scb (struct ev_signal *w, int revents)
1060 {
1061 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1062 evio_stop (&wio);
1063 evio_start (&wio);
1064 }
1065
1066 static void
1067 gcb (struct ev_signal *w, int revents)
1068 {
1069 fprintf (stderr, "generic %x\n", revents);
1070
1071 }
1072
1073 int main (void)
1074 {
1075 ev_init (0);
1076
1077 evio_init (&wio, sin_cb, 0, EV_READ);
1078 evio_start (&wio);
1079
1080 struct ev_timer t[10000];
1081
1082 #if 0
1083 int i;
1084 for (i = 0; i < 10000; ++i)
1085 {
1086 struct ev_timer *w = t + i;
1087 evw_init (w, ocb, i);
1088 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
1089 evtimer_start (w);
1090 if (drand48 () < 0.5)
1091 evtimer_stop (w);
1092 }
1093 #endif
1094
1095 struct ev_timer t1;
1096 evtimer_init (&t1, ocb, 5, 10);
1097 evtimer_start (&t1);
1098
1099 struct ev_signal sig;
1100 evsignal_init (&sig, scb, SIGQUIT);
1101 evsignal_start (&sig);
1102
1103 struct ev_check cw;
1104 evcheck_init (&cw, gcb);
1105 evcheck_start (&cw);
1106
1107 struct ev_idle iw;
1108 evidle_init (&iw, gcb);
1109 evidle_start (&iw);
1110
1111 ev_loop (0);
1112
1113 return 0;
1114 }
1115
1116 #endif
1117
1118
1119
1120