ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.28
Committed: Thu Nov 1 06:48:49 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.27: +64 -56 lines
Log Message:
the big rename

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 ev_io_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 ev_io_set (&sigev, sigpipe [0], EV_READ);
376 ev_io_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 ev_watcher_init (&sigev, sigcb);
465 siginit ();
466
467 ev_signal_init (&childev, childcb, SIGCHLD);
468 ev_signal_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 ev_io_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 ev_timer_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 ev_periodic_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 ev_periodic_stop (w);
580 ev_periodic_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 ev_io_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 ev_io_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 ev_timer_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 ev_timer_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 ev_timer_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 ev_timer_stop (w);
821 }
822 else if (w->repeat)
823 ev_timer_start (w);
824 }
825
826 void
827 ev_periodic_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 ev_periodic_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 ev_signal_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 ev_signal_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
895 ev_idle_start (struct ev_idle *w)
896 {
897 if (ev_is_active (w))
898 return;
899
900 ev_start ((W)w, ++idlecnt);
901 array_needsize (idles, idlemax, idlecnt, );
902 idles [idlecnt - 1] = w;
903 }
904
905 void
906 ev_idle_stop (struct ev_idle *w)
907 {
908 ev_clear ((W)w);
909 if (ev_is_active (w))
910 return;
911
912 idles [w->active - 1] = idles [--idlecnt];
913 ev_stop ((W)w);
914 }
915
916 void
917 ev_prepare_start (struct ev_prepare *w)
918 {
919 if (ev_is_active (w))
920 return;
921
922 ev_start ((W)w, ++preparecnt);
923 array_needsize (prepares, preparemax, preparecnt, );
924 prepares [preparecnt - 1] = w;
925 }
926
927 void
928 ev_prepare_stop (struct ev_prepare *w)
929 {
930 ev_clear ((W)w);
931 if (ev_is_active (w))
932 return;
933
934 prepares [w->active - 1] = prepares [--preparecnt];
935 ev_stop ((W)w);
936 }
937
938 void
939 ev_check_start (struct ev_check *w)
940 {
941 if (ev_is_active (w))
942 return;
943
944 ev_start ((W)w, ++checkcnt);
945 array_needsize (checks, checkmax, checkcnt, );
946 checks [checkcnt - 1] = w;
947 }
948
949 void
950 ev_check_stop (struct ev_check *w)
951 {
952 ev_clear ((W)w);
953 if (ev_is_active (w))
954 return;
955
956 checks [w->active - 1] = checks [--checkcnt];
957 ev_stop ((W)w);
958 }
959
960 void
961 ev_child_start (struct ev_child *w)
962 {
963 if (ev_is_active (w))
964 return;
965
966 ev_start ((W)w, 1);
967 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
968 }
969
970 void
971 ev_child_stop (struct ev_child *w)
972 {
973 ev_clear ((W)w);
974 if (ev_is_active (w))
975 return;
976
977 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
978 ev_stop ((W)w);
979 }
980
981 /*****************************************************************************/
982
983 struct ev_once
984 {
985 struct ev_io io;
986 struct ev_timer to;
987 void (*cb)(int revents, void *arg);
988 void *arg;
989 };
990
991 static void
992 once_cb (struct ev_once *once, int revents)
993 {
994 void (*cb)(int revents, void *arg) = once->cb;
995 void *arg = once->arg;
996
997 ev_io_stop (&once->io);
998 ev_timer_stop (&once->to);
999 free (once);
1000
1001 cb (revents, arg);
1002 }
1003
1004 static void
1005 once_cb_io (struct ev_io *w, int revents)
1006 {
1007 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1008 }
1009
1010 static void
1011 once_cb_to (struct ev_timer *w, int revents)
1012 {
1013 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1014 }
1015
1016 void
1017 ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1018 {
1019 struct ev_once *once = malloc (sizeof (struct ev_once));
1020
1021 if (!once)
1022 cb (EV_ERROR, arg);
1023 else
1024 {
1025 once->cb = cb;
1026 once->arg = arg;
1027
1028 ev_watcher_init (&once->io, once_cb_io);
1029
1030 if (fd >= 0)
1031 {
1032 ev_io_set (&once->io, fd, events);
1033 ev_io_start (&once->io);
1034 }
1035
1036 ev_watcher_init (&once->to, once_cb_to);
1037
1038 if (timeout >= 0.)
1039 {
1040 ev_timer_set (&once->to, timeout, 0.);
1041 ev_timer_start (&once->to);
1042 }
1043 }
1044 }
1045
1046 /*****************************************************************************/
1047
1048 #if 0
1049
1050 struct ev_io wio;
1051
1052 static void
1053 sin_cb (struct ev_io *w, int revents)
1054 {
1055 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1056 }
1057
1058 static void
1059 ocb (struct ev_timer *w, int revents)
1060 {
1061 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1062 ev_timer_stop (w);
1063 ev_timer_start (w);
1064 }
1065
1066 static void
1067 scb (struct ev_signal *w, int revents)
1068 {
1069 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1070 ev_io_stop (&wio);
1071 ev_io_start (&wio);
1072 }
1073
1074 static void
1075 gcb (struct ev_signal *w, int revents)
1076 {
1077 fprintf (stderr, "generic %x\n", revents);
1078
1079 }
1080
1081 int main (void)
1082 {
1083 ev_init (0);
1084
1085 ev_io_init (&wio, sin_cb, 0, EV_READ);
1086 ev_io_start (&wio);
1087
1088 struct ev_timer t[10000];
1089
1090 #if 0
1091 int i;
1092 for (i = 0; i < 10000; ++i)
1093 {
1094 struct ev_timer *w = t + i;
1095 ev_watcher_init (w, ocb, i);
1096 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1097 ev_timer_start (w);
1098 if (drand48 () < 0.5)
1099 ev_timer_stop (w);
1100 }
1101 #endif
1102
1103 struct ev_timer t1;
1104 ev_timer_init (&t1, ocb, 5, 10);
1105 ev_timer_start (&t1);
1106
1107 struct ev_signal sig;
1108 ev_signal_init (&sig, scb, SIGQUIT);
1109 ev_signal_start (&sig);
1110
1111 struct ev_check cw;
1112 ev_check_init (&cw, gcb);
1113 ev_check_start (&cw);
1114
1115 struct ev_idle iw;
1116 ev_idle_init (&iw, gcb);
1117 ev_idle_start (&iw);
1118
1119 ev_loop (0);
1120
1121 return 0;
1122 }
1123
1124 #endif
1125
1126
1127
1128