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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines