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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines