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

Comparing libev/ev.c (file contents):
Revision 1.3 by root, Tue Oct 30 21:45:00 2007 UTC vs.
Revision 1.27 by root, Wed Oct 31 22:16:36 2007 UTC

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
1#include <math.h> 30#include <math.h>
2#include <stdlib.h> 31#include <stdlib.h>
32#include <unistd.h>
33#include <fcntl.h>
34#include <signal.h>
35#include <stddef.h>
3 36
4#include <stdio.h> 37#include <stdio.h>
5 38
39#include <assert.h>
6#include <errno.h> 40#include <errno.h>
41#include <sys/types.h>
42#include <sys/wait.h>
7#include <sys/time.h> 43#include <sys/time.h>
8#include <time.h> 44#include <time.h>
9 45
46#ifndef HAVE_MONOTONIC
10#ifdef CLOCK_MONOTONIC 47# ifdef CLOCK_MONOTONIC
11# define HAVE_MONOTONIC 1 48# define HAVE_MONOTONIC 1
12#endif 49# endif
50#endif
13 51
14#define HAVE_EPOLL 1 52#ifndef HAVE_SELECT
15#define HAVE_REALTIME 1
16#define HAVE_SELECT 0 53# define HAVE_SELECT 1
54#endif
17 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) */
18#define MAX_BLOCKTIME 60. 65#define MAX_BLOCKTIME 60.
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
19 67
20#include "ev.h" 68#include "ev.h"
21 69
22struct ev_watcher { 70typedef struct ev_watcher *W;
23 EV_WATCHER (ev_watcher);
24};
25
26struct ev_watcher_list { 71typedef struct ev_watcher_list *WL;
27 EV_WATCHER_LIST (ev_watcher_list); 72typedef struct ev_watcher_time *WT;
28};
29 73
74static ev_tstamp now, diff; /* monotonic clock */
30ev_tstamp ev_now; 75ev_tstamp ev_now;
31int ev_method; 76int ev_method;
32 77
33static int have_monotonic; /* runtime */ 78static int have_monotonic; /* runtime */
34 79
35static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 80static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
36static void (*method_reify)(void); 81static void (*method_modify)(int fd, int oev, int nev);
37static void (*method_poll)(ev_tstamp timeout); 82static void (*method_poll)(ev_tstamp timeout);
83
84/*****************************************************************************/
38 85
39ev_tstamp 86ev_tstamp
40ev_time (void) 87ev_time (void)
41{ 88{
42#if HAVE_REALTIME 89#if HAVE_REALTIME
66} 113}
67 114
68#define array_needsize(base,cur,cnt,init) \ 115#define array_needsize(base,cur,cnt,init) \
69 if ((cnt) > cur) \ 116 if ((cnt) > cur) \
70 { \ 117 { \
71 int newcnt = cur ? cur << 1 : 16; \ 118 int newcnt = cur; \
72 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ 119 do \
120 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \
122 } \
123 while ((cnt) > newcnt); \
124 \
73 base = realloc (base, sizeof (*base) * (newcnt)); \ 125 base = realloc (base, sizeof (*base) * (newcnt)); \
74 init (base + cur, newcnt - cur); \ 126 init (base + cur, newcnt - cur); \
75 cur = newcnt; \ 127 cur = newcnt; \
76 } 128 }
77 129
130/*****************************************************************************/
131
78typedef struct 132typedef struct
79{ 133{
80 struct ev_io *head; 134 struct ev_io *head;
81 unsigned char wev, rev; /* want, received event set */ 135 int events;
82} ANFD; 136} ANFD;
83 137
84static ANFD *anfds; 138static ANFD *anfds;
85static int anfdmax; 139static int anfdmax;
86 140
141static void
142anfds_init (ANFD *base, int count)
143{
144 while (count--)
145 {
146 base->head = 0;
147 base->events = EV_NONE;
148 ++base;
149 }
150}
151
152typedef struct
153{
154 W w;
155 int events;
156} ANPENDING;
157
158static ANPENDING *pendings;
159static int pendingmax, pendingcnt;
160
161static void
162event (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
173static void
174queue_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
182static void
183fd_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
87static int *fdchanges; 199static int *fdchanges;
88static int fdchangemax, fdchangecnt; 200static int fdchangemax, fdchangecnt;
89 201
90static void 202static void
91anfds_init (ANFD *base, int count) 203fd_reify (void)
92{ 204{
93 while (count--) 205 int i;
94 { 206
95 base->head = 0; 207 for (i = 0; i < fdchangecnt; ++i)
96 base->wev = base->rev = EV_NONE;
97 ++base;
98 } 208 {
99} 209 int fd = fdchanges [i];
100
101typedef struct
102{
103 struct ev_watcher *w;
104 int events;
105} ANPENDING;
106
107static ANPENDING *pendings;
108static int pendingmax, pendingcnt;
109
110static void
111event (struct ev_watcher *w, int events)
112{
113 w->pending = ++pendingcnt;
114 array_needsize (pendings, pendingmax, pendingcnt, );
115 pendings [pendingcnt - 1].w = w;
116 pendings [pendingcnt - 1].events = events;
117}
118
119static void
120fd_event (int fd, int events)
121{
122 ANFD *anfd = anfds + fd; 210 ANFD *anfd = anfds + fd;
123 struct ev_io *w; 211 struct ev_io *w;
124 212
213 int events = 0;
214
125 for (w = anfd->head; w; w = w->next) 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 }
126 { 225 }
127 int ev = w->events & events;
128 226
129 if (ev) 227 fdchangecnt = 0;
130 event ((struct ev_watcher *)w, ev);
131 }
132} 228}
229
230static void
231fd_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 */
244static void
245fd_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/*****************************************************************************/
133 260
134static struct ev_timer **timers; 261static struct ev_timer **timers;
135static int timermax, timercnt; 262static int timermax, timercnt;
136 263
264static struct ev_periodic **periodics;
265static int periodicmax, periodiccnt;
266
137static void 267static void
138upheap (int k) 268upheap (WT *timers, int k)
139{ 269{
140 struct ev_timer *w = timers [k]; 270 WT w = timers [k];
141 271
142 while (k && timers [k >> 1]->at > w->at) 272 while (k && timers [k >> 1]->at > w->at)
143 { 273 {
144 timers [k] = timers [k >> 1]; 274 timers [k] = timers [k >> 1];
145 timers [k]->active = k + 1; 275 timers [k]->active = k + 1;
150 timers [k]->active = k + 1; 280 timers [k]->active = k + 1;
151 281
152} 282}
153 283
154static void 284static void
155downheap (int k) 285downheap (WT *timers, int N, int k)
156{ 286{
157 struct ev_timer *w = timers [k]; 287 WT w = timers [k];
158 288
159 while (k < (timercnt >> 1)) 289 while (k < (N >> 1))
160 { 290 {
161 int j = k << 1; 291 int j = k << 1;
162 292
163 if (j + 1 < timercnt && timers [j]->at > timers [j + 1]->at) 293 if (j + 1 < N && timers [j]->at > timers [j + 1]->at)
164 ++j; 294 ++j;
165 295
166 if (w->at <= timers [j]->at) 296 if (w->at <= timers [j]->at)
167 break; 297 break;
168 298
173 303
174 timers [k] = w; 304 timers [k] = w;
175 timers [k]->active = k + 1; 305 timers [k]->active = k + 1;
176} 306}
177 307
178static struct ev_signal **signals; 308/*****************************************************************************/
309
310typedef struct
311{
312 struct ev_signal *head;
313 sig_atomic_t gotsig;
314} ANSIG;
315
316static ANSIG *signals;
179static int signalmax, signalcnt; 317static int signalmax;
180 318
319static int sigpipe [2];
320static sig_atomic_t gotsig;
321static struct ev_io sigev;
322
181static void 323static void
182signals_init (struct ev_signal **base, int count) 324signals_init (ANSIG *base, int count)
183{ 325{
184 while (count--) 326 while (count--)
185 *base++ = 0; 327 {
328 base->head = 0;
329 base->gotsig = 0;
330 ++base;
331 }
186} 332}
333
334static void
335sighandler (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
346static void
347sigcb (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
365static void
366siginit (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
381static struct ev_idle **idles;
382static int idlemax, idlecnt;
383
384static struct ev_prepare **prepares;
385static int preparemax, preparecnt;
386
387static struct ev_check **checks;
388static int checkmax, checkcnt;
389
390/*****************************************************************************/
391
392static struct ev_child *childs [PID_HASHSIZE];
393static struct ev_signal childev;
394
395#ifndef WCONTINUED
396# define WCONTINUED 0
397#endif
398
399static void
400childcb (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/*****************************************************************************/
187 415
188#if HAVE_EPOLL 416#if HAVE_EPOLL
189# include "ev_epoll.c" 417# include "ev_epoll.c"
190#endif 418#endif
191#if HAVE_SELECT 419#if HAVE_SELECT
192# include "ev_select.c" 420# include "ev_select.c"
193#endif 421#endif
194 422
423int
424ev_version_major (void)
425{
426 return EV_VERSION_MAJOR;
427}
428
429int
430ev_version_minor (void)
431{
432 return EV_VERSION_MINOR;
433}
434
195int ev_init (int flags) 435int ev_init (int flags)
196{ 436{
437 if (!ev_method)
438 {
197#if HAVE_MONOTONIC 439#if HAVE_MONOTONIC
198 { 440 {
199 struct timespec ts; 441 struct timespec ts;
200 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 442 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
201 have_monotonic = 1; 443 have_monotonic = 1;
202 } 444 }
203#endif 445#endif
204 446
205 ev_now = ev_time (); 447 ev_now = ev_time ();
448 now = get_clock ();
449 diff = ev_now - now;
206 450
451 if (pipe (sigpipe))
452 return 0;
453
454 ev_method = EVMETHOD_NONE;
207#if HAVE_EPOLL 455#if HAVE_EPOLL
208 if (epoll_init (flags)) 456 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
209 return ev_method;
210#endif 457#endif
211#if HAVE_SELECT 458#if HAVE_SELECT
212 if (select_init (flags)) 459 if (ev_method == EVMETHOD_NONE) select_init (flags);
213 return ev_method;
214#endif 460#endif
215 461
216 ev_method = EVMETHOD_NONE; 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
217 return ev_method; 472 return ev_method;
218} 473}
219 474
475/*****************************************************************************/
476
477void
220void ev_prefork (void) 478ev_prefork (void)
221{ 479{
480 /* nop */
222} 481}
223 482
483void
224void ev_postfork_parent (void) 484ev_postfork_parent (void)
225{ 485{
486 /* nop */
226} 487}
227 488
489void
228void ev_postfork_child (void) 490ev_postfork_child (void)
229{ 491{
230#if HAVE_EPOLL 492#if HAVE_EPOLL
493 if (ev_method == EVMETHOD_EPOLL)
231 epoll_postfork_child (); 494 epoll_postfork_child ();
232#endif 495#endif
233}
234 496
497 evio_stop (&sigev);
498 close (sigpipe [0]);
499 close (sigpipe [1]);
500 pipe (sigpipe);
501 siginit ();
502}
503
504/*****************************************************************************/
505
235static void 506static void
236call_pending () 507call_pending (void)
237{ 508{
238 int i; 509 while (pendingcnt)
239
240 for (i = 0; i < pendingcnt; ++i)
241 { 510 {
242 ANPENDING *p = pendings + i; 511 ANPENDING *p = pendings + --pendingcnt;
243 512
244 if (p->w) 513 if (p->w)
245 { 514 {
246 p->w->pending = 0; 515 p->w->pending = 0;
247 p->w->cb (p->w, p->events); 516 p->w->cb (p->w, p->events);
248 } 517 }
249 } 518 }
250
251 pendingcnt = 0;
252} 519}
253 520
254static void 521static void
255timer_reify (void) 522timers_reify (void)
256{ 523{
257 while (timercnt && timers [0]->at <= ev_now) 524 while (timercnt && timers [0]->at <= now)
258 { 525 {
259 struct ev_timer *w = timers [0]; 526 struct ev_timer *w = timers [0];
260 527
528 event ((W)w, EV_TIMEOUT);
529
261 /* first reschedule timer */ 530 /* first reschedule or stop timer */
262 if (w->repeat) 531 if (w->repeat)
263 { 532 {
264 if (w->is_abs)
265 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
266 else
267 w->at = ev_now + w->repeat; 533 w->at = now + w->repeat;
268 534 assert (("timer timeout in the past, negative repeat?", w->at > now));
269 downheap (0); 535 downheap ((WT *)timers, timercnt, 0);
270 } 536 }
271 else 537 else
272 evtimer_stop (w); /* nonrepeating: stop timer */ 538 evtimer_stop (w); /* nonrepeating: stop timer */
539 }
540}
273 541
542static void
543periodics_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
274 event ((struct ev_watcher *)w, EV_TIMEOUT); 559 event ((W)w, EV_TIMEOUT);
560 }
561}
562
563static void
564periodics_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
588static void
589time_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;
275 } 625 }
276} 626}
277 627
278int ev_loop_done; 628int ev_loop_done;
279 629
280int ev_loop (int flags) 630void ev_loop (int flags)
281{ 631{
282 double block; 632 double block;
283 ev_loop_done = flags & EVLOOP_ONESHOT; 633 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
284 634
285 do 635 do
286 { 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
287 /* update fd-related kernel structures */ 644 /* update fd-related kernel structures */
288 method_reify (); fdchangecnt = 0; 645 fd_reify ();
289 646
290 /* calculate blocking time */ 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 */
291 ev_now = ev_time (); 651 ev_now = ev_time ();
292 652
293 if (flags & EVLOOP_NONBLOCK) 653 if (flags & EVLOOP_NONBLOCK || idlecnt)
294 block = 0.; 654 block = 0.;
295 else if (!timercnt)
296 block = MAX_BLOCKTIME;
297 else 655 else
298 { 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 {
299 block = timers [0]->at - ev_now + method_fudge; 667 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
668 if (block > to) block = to;
669 }
670
300 if (block < 0.) block = 0.; 671 if (block < 0.) block = 0.;
301 else if (block > MAX_BLOCKTIME) block = MAX_BLOCKTIME;
302 } 672 }
303 673
304 method_poll (block); 674 method_poll (block);
305 675
676 /* update ev_now, do magic */
677 time_update ();
678
306 /* put pending timers into pendign queue and reschedule them */ 679 /* queue pending timers and reschedule them */
307 timer_reify (); 680 timers_reify (); /* relative timers called last */
681 periodics_reify (); /* absolute timers called first */
308 682
309 ev_now = ev_time (); 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
310 call_pending (); 691 call_pending ();
311 } 692 }
312 while (!ev_loop_done); 693 while (!ev_loop_done);
313}
314 694
695 if (ev_loop_done != 2)
696 ev_loop_done = 0;
697}
698
699/*****************************************************************************/
700
315static void 701static void
316wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 702wlist_add (WL *head, WL elem)
317{ 703{
318 elem->next = *head; 704 elem->next = *head;
319 *head = elem; 705 *head = elem;
320} 706}
321 707
322static void 708static void
323wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 709wlist_del (WL *head, WL elem)
324{ 710{
325 while (*head) 711 while (*head)
326 { 712 {
327 if (*head == elem) 713 if (*head == elem)
328 { 714 {
333 head = &(*head)->next; 719 head = &(*head)->next;
334 } 720 }
335} 721}
336 722
337static void 723static void
338ev_start (struct ev_watcher *w, int active) 724ev_clear (W w)
339{ 725{
726 if (w->pending)
727 {
728 pendings [w->pending - 1].w = 0;
340 w->pending = 0; 729 w->pending = 0;
730 }
731}
732
733static void
734ev_start (W w, int active)
735{
341 w->active = active; 736 w->active = active;
342} 737}
343 738
344static void 739static void
345ev_stop (struct ev_watcher *w) 740ev_stop (W w)
346{ 741{
347 if (w->pending)
348 pendings [w->pending - 1].w = 0;
349
350 w->active = 0; 742 w->active = 0;
351 /* nop */
352} 743}
744
745/*****************************************************************************/
353 746
354void 747void
355evio_start (struct ev_io *w) 748evio_start (struct ev_io *w)
356{ 749{
357 if (ev_is_active (w)) 750 if (ev_is_active (w))
358 return; 751 return;
359 752
360 int fd = w->fd; 753 int fd = w->fd;
361 754
362 ev_start ((struct ev_watcher *)w, 1); 755 ev_start ((W)w, 1);
363 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 756 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
364 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 757 wlist_add ((WL *)&anfds[fd].head, (WL)w);
365 758
366 ++fdchangecnt; 759 fd_change (fd);
367 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
368 fdchanges [fdchangecnt - 1] = fd;
369} 760}
370 761
371void 762void
372evio_stop (struct ev_io *w) 763evio_stop (struct ev_io *w)
373{ 764{
765 ev_clear ((W)w);
374 if (!ev_is_active (w)) 766 if (!ev_is_active (w))
375 return; 767 return;
376 768
377 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); 769 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
378 ev_stop ((struct ev_watcher *)w); 770 ev_stop ((W)w);
379 771
380 ++fdchangecnt; 772 fd_change (w->fd);
381 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
382 fdchanges [fdchangecnt - 1] = w->fd;
383} 773}
384 774
385void 775void
386evtimer_start (struct ev_timer *w) 776evtimer_start (struct ev_timer *w)
387{ 777{
388 if (ev_is_active (w)) 778 if (ev_is_active (w))
389 return; 779 return;
390 780
391 if (w->is_abs)
392 {
393 /* this formula differs from the one in timer_reify becuse we do not round up */
394 if (w->repeat)
395 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
396 }
397 else
398 w->at += ev_now; 781 w->at += now;
399 782
400 ev_start ((struct ev_watcher *)w, ++timercnt); 783 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
784
785 ev_start ((W)w, ++timercnt);
401 array_needsize (timers, timermax, timercnt, ); 786 array_needsize (timers, timermax, timercnt, );
402 timers [timercnt - 1] = w; 787 timers [timercnt - 1] = w;
403 upheap (timercnt - 1); 788 upheap ((WT *)timers, timercnt - 1);
404} 789}
405 790
406void 791void
407evtimer_stop (struct ev_timer *w) 792evtimer_stop (struct ev_timer *w)
408{ 793{
794 ev_clear ((W)w);
409 if (!ev_is_active (w)) 795 if (!ev_is_active (w))
410 return; 796 return;
411 797
412 if (w->active < timercnt--) 798 if (w->active < timercnt--)
413 { 799 {
414 timers [w->active - 1] = timers [timercnt]; 800 timers [w->active - 1] = timers [timercnt];
415 downheap (w->active - 1); 801 downheap ((WT *)timers, timercnt, w->active - 1);
802 }
803
804 w->at = w->repeat;
805
806 ev_stop ((W)w);
807}
808
809void
810evtimer_again (struct ev_timer *w)
811{
812 if (ev_is_active (w))
416 } 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}
417 825
418 ev_stop ((struct ev_watcher *)w); 826void
827evperiodic_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
844void
845evperiodic_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);
419} 858}
420 859
421void 860void
422evsignal_start (struct ev_signal *w) 861evsignal_start (struct ev_signal *w)
423{ 862{
424 if (ev_is_active (w)) 863 if (ev_is_active (w))
425 return; 864 return;
426 865
427 ev_start ((struct ev_watcher *)w, 1); 866 ev_start ((W)w, 1);
428 array_needsize (signals, signalmax, w->signum, signals_init); 867 array_needsize (signals, signalmax, w->signum, signals_init);
429 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); 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 }
430} 878}
431 879
432void 880void
433evsignal_stop (struct ev_signal *w) 881evsignal_stop (struct ev_signal *w)
434{ 882{
883 ev_clear ((W)w);
435 if (!ev_is_active (w)) 884 if (!ev_is_active (w))
436 return; 885 return;
437 886
438 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); 887 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
439 ev_stop ((struct ev_watcher *)w); 888 ev_stop ((W)w);
440}
441 889
890 if (!signals [w->signum - 1].head)
891 signal (w->signum, SIG_DFL);
892}
893
894void 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
904void 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
914void 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
924void 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
934void 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
944void 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
954void 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
963void 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
442/*****************************************************************************/ 973/*****************************************************************************/
974
975struct 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
983static void
984once_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
996static void
997once_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
1002static void
1003once_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
1008void
1009ev_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
443#if 1 1040#if 0
1041
1042struct ev_io wio;
444 1043
445static void 1044static void
446sin_cb (struct ev_io *w, int revents) 1045sin_cb (struct ev_io *w, int revents)
447{ 1046{
448 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 1047 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
449} 1048}
450 1049
451static void 1050static void
452ocb (struct ev_timer *w, int revents) 1051ocb (struct ev_timer *w, int revents)
453{ 1052{
454 fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 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
1058static void
1059scb (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
1066static void
1067gcb (struct ev_signal *w, int revents)
1068{
1069 fprintf (stderr, "generic %x\n", revents);
1070
455} 1071}
456 1072
457int main (void) 1073int main (void)
458{ 1074{
459 struct ev_io sin;
460
461 ev_init (0); 1075 ev_init (0);
462 1076
463 evw_init (&sin, sin_cb, 55);
464 evio_set (&sin, 0, EV_READ); 1077 evio_init (&wio, sin_cb, 0, EV_READ);
465 evio_start (&sin); 1078 evio_start (&wio);
466 1079
467 struct ev_timer t[1000]; 1080 struct ev_timer t[10000];
468 1081
1082#if 0
469 int i; 1083 int i;
470 for (i = 0; i < 1000; ++i) 1084 for (i = 0; i < 10000; ++i)
471 { 1085 {
472 struct ev_timer *w = t + i; 1086 struct ev_timer *w = t + i;
473 evw_init (w, ocb, i); 1087 evw_init (w, ocb, i);
474 evtimer_set_rel (w, drand48 (), 0); 1088 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
475 evtimer_start (w); 1089 evtimer_start (w);
476 if (drand48 () < 0.5) 1090 if (drand48 () < 0.5)
477 evtimer_stop (w); 1091 evtimer_stop (w);
478 } 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);
479 1110
480 ev_loop (0); 1111 ev_loop (0);
481 1112
482 return 0; 1113 return 0;
483} 1114}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines