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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines