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

Comparing libev/ev.c (file contents):
Revision 1.4 by root, Tue Oct 30 23:10:33 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
6#include <assert.h> 39#include <assert.h>
7#include <errno.h> 40#include <errno.h>
41#include <sys/types.h>
42#include <sys/wait.h>
8#include <sys/time.h> 43#include <sys/time.h>
9#include <time.h> 44#include <time.h>
10 45
46#ifndef HAVE_MONOTONIC
11#ifdef CLOCK_MONOTONIC 47# ifdef CLOCK_MONOTONIC
12# define HAVE_MONOTONIC 1 48# define HAVE_MONOTONIC 1
13#endif 49# endif
50#endif
14 51
15#define HAVE_EPOLL 1 52#ifndef HAVE_SELECT
16#define HAVE_REALTIME 1
17#define HAVE_SELECT 0 53# define HAVE_SELECT 1
54#endif
55
56#ifndef HAVE_EPOLL
57# define HAVE_EPOLL 0
58#endif
59
60#ifndef HAVE_REALTIME
61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */
62#endif
18 63
19#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
20#define MAX_BLOCKTIME 60. 65#define MAX_BLOCKTIME 60.
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
21 67
22#include "ev.h" 68#include "ev.h"
23 69
24struct ev_watcher { 70typedef struct ev_watcher *W;
25 EV_WATCHER (ev_watcher);
26};
27
28struct ev_watcher_list { 71typedef struct ev_watcher_list *WL;
29 EV_WATCHER_LIST (ev_watcher_list); 72typedef struct ev_watcher_time *WT;
30};
31 73
32static ev_tstamp now, diff; /* monotonic clock */ 74static ev_tstamp now, diff; /* monotonic clock */
33ev_tstamp ev_now; 75ev_tstamp ev_now;
34int ev_method; 76int ev_method;
35 77
36static int have_monotonic; /* runtime */ 78static int have_monotonic; /* runtime */
37 79
38static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 80static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
39static void (*method_reify)(void); 81static void (*method_modify)(int fd, int oev, int nev);
40static void (*method_poll)(ev_tstamp timeout); 82static void (*method_poll)(ev_tstamp timeout);
83
84/*****************************************************************************/
41 85
42ev_tstamp 86ev_tstamp
43ev_time (void) 87ev_time (void)
44{ 88{
45#if HAVE_REALTIME 89#if HAVE_REALTIME
70 114
71#define array_needsize(base,cur,cnt,init) \ 115#define array_needsize(base,cur,cnt,init) \
72 if ((cnt) > cur) \ 116 if ((cnt) > cur) \
73 { \ 117 { \
74 int newcnt = cur ? cur << 1 : 16; \ 118 int newcnt = cur ? cur << 1 : 16; \
75 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\
76 base = realloc (base, sizeof (*base) * (newcnt)); \ 119 base = realloc (base, sizeof (*base) * (newcnt)); \
77 init (base + cur, newcnt - cur); \ 120 init (base + cur, newcnt - cur); \
78 cur = newcnt; \ 121 cur = newcnt; \
79 } 122 }
80 123
124/*****************************************************************************/
125
81typedef struct 126typedef struct
82{ 127{
83 struct ev_io *head; 128 struct ev_io *head;
84 unsigned char wev, rev; /* want, received event set */ 129 unsigned char wev, rev; /* want, received event set */
85} ANFD; 130} ANFD;
101 } 146 }
102} 147}
103 148
104typedef struct 149typedef struct
105{ 150{
106 struct ev_watcher *w; 151 W w;
107 int events; 152 int events;
108} ANPENDING; 153} ANPENDING;
109 154
110static ANPENDING *pendings; 155static ANPENDING *pendings;
111static int pendingmax, pendingcnt; 156static int pendingmax, pendingcnt;
112 157
113static void 158static void
114event (struct ev_watcher *w, int events) 159event (W w, int events)
115{ 160{
161 if (w->active)
162 {
116 w->pending = ++pendingcnt; 163 w->pending = ++pendingcnt;
117 array_needsize (pendings, pendingmax, pendingcnt, ); 164 array_needsize (pendings, pendingmax, pendingcnt, );
118 pendings [pendingcnt - 1].w = w; 165 pendings [pendingcnt - 1].w = w;
119 pendings [pendingcnt - 1].events = events; 166 pendings [pendingcnt - 1].events = events;
167 }
120} 168}
121 169
122static void 170static void
123fd_event (int fd, int events) 171fd_event (int fd, int events)
124{ 172{
128 for (w = anfd->head; w; w = w->next) 176 for (w = anfd->head; w; w = w->next)
129 { 177 {
130 int ev = w->events & events; 178 int ev = w->events & events;
131 179
132 if (ev) 180 if (ev)
133 event ((struct ev_watcher *)w, ev); 181 event ((W)w, ev);
134 } 182 }
135} 183}
136 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/*****************************************************************************/
208
137static struct ev_timer **atimers; 209static struct ev_timer **timers;
138static int atimermax, atimercnt; 210static int timermax, timercnt;
139 211
140static struct ev_timer **rtimers; 212static struct ev_periodic **periodics;
141static int rtimermax, rtimercnt; 213static int periodicmax, periodiccnt;
142 214
143static void 215static void
144upheap (struct ev_timer **timers, int k) 216upheap (WT *timers, int k)
145{ 217{
146 struct ev_timer *w = timers [k]; 218 WT w = timers [k];
147 219
148 while (k && timers [k >> 1]->at > w->at) 220 while (k && timers [k >> 1]->at > w->at)
149 { 221 {
150 timers [k] = timers [k >> 1]; 222 timers [k] = timers [k >> 1];
151 timers [k]->active = k + 1; 223 timers [k]->active = k + 1;
156 timers [k]->active = k + 1; 228 timers [k]->active = k + 1;
157 229
158} 230}
159 231
160static void 232static void
161downheap (struct ev_timer **timers, int N, int k) 233downheap (WT *timers, int N, int k)
162{ 234{
163 struct ev_timer *w = timers [k]; 235 WT w = timers [k];
164 236
165 while (k < (N >> 1)) 237 while (k < (N >> 1))
166 { 238 {
167 int j = k << 1; 239 int j = k << 1;
168 240
179 251
180 timers [k] = w; 252 timers [k] = w;
181 timers [k]->active = k + 1; 253 timers [k]->active = k + 1;
182} 254}
183 255
184static 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;
185static int signalmax; 265static int signalmax;
186 266
267static int sigpipe [2];
268static sig_atomic_t gotsig;
269static struct ev_io sigev;
270
187static void 271static void
188signals_init (struct ev_signal **base, int count) 272signals_init (ANSIG *base, int count)
189{ 273{
190 while (count--) 274 while (count--)
191 *base++ = 0; 275 {
276 base->head = 0;
277 base->gotsig = 0;
278 ++base;
279 }
192} 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/*****************************************************************************/
193 363
194#if HAVE_EPOLL 364#if HAVE_EPOLL
195# include "ev_epoll.c" 365# include "ev_epoll.c"
196#endif 366#endif
197#if HAVE_SELECT 367#if HAVE_SELECT
210 380
211 ev_now = ev_time (); 381 ev_now = ev_time ();
212 now = get_clock (); 382 now = get_clock ();
213 diff = ev_now - now; 383 diff = ev_now - now;
214 384
385 if (pipe (sigpipe))
386 return 0;
387
388 ev_method = EVMETHOD_NONE;
215#if HAVE_EPOLL 389#if HAVE_EPOLL
216 if (epoll_init (flags)) 390 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
217 return ev_method;
218#endif 391#endif
219#if HAVE_SELECT 392#if HAVE_SELECT
220 if (select_init (flags)) 393 if (ev_method == EVMETHOD_NONE) select_init (flags);
221 return ev_method;
222#endif 394#endif
223 395
224 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
225 return ev_method; 405 return ev_method;
226} 406}
227 407
408/*****************************************************************************/
409
228void ev_prefork (void) 410void ev_prefork (void)
229{ 411{
412 /* nop */
230} 413}
231 414
232void ev_postfork_parent (void) 415void ev_postfork_parent (void)
233{ 416{
417 /* nop */
234} 418}
235 419
236void ev_postfork_child (void) 420void ev_postfork_child (void)
237{ 421{
238#if HAVE_EPOLL 422#if HAVE_EPOLL
423 if (ev_method == EVMETHOD_EPOLL)
239 epoll_postfork_child (); 424 epoll_postfork_child ();
240#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;
241} 460}
242 461
243static void 462static void
244call_pending () 463call_pending ()
245{ 464{
246 int i; 465 while (pendingcnt)
247
248 for (i = 0; i < pendingcnt; ++i)
249 { 466 {
250 ANPENDING *p = pendings + i; 467 ANPENDING *p = pendings + --pendingcnt;
251 468
252 if (p->w) 469 if (p->w)
253 { 470 {
254 p->w->pending = 0; 471 p->w->pending = 0;
255 p->w->cb (p->w, p->events); 472 p->w->cb (p->w, p->events);
256 } 473 }
257 } 474 }
258
259 pendingcnt = 0;
260} 475}
261 476
262static void 477static void
263timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) 478timers_reify ()
264{ 479{
265 while (timercnt && timers [0]->at <= now) 480 while (timercnt && timers [0]->at <= now)
266 { 481 {
267 struct ev_timer *w = timers [0]; 482 struct ev_timer *w = timers [0];
483
484 event ((W)w, EV_TIMEOUT);
268 485
269 /* first reschedule or stop timer */ 486 /* first reschedule or stop timer */
270 if (w->repeat) 487 if (w->repeat)
271 { 488 {
272 if (w->is_abs)
273 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
274 else
275 w->at = now + w->repeat; 489 w->at = now + w->repeat;
276 490 assert (("timer timeout in the past, negative repeat?", w->at > now));
277 assert (w->at > now);
278
279 downheap (timers, timercnt, 0); 491 downheap ((WT *)timers, timercnt, 0);
280 } 492 }
281 else 493 else
282 {
283 evtimer_stop (w); /* nonrepeating: stop timer */ 494 evtimer_stop (w); /* nonrepeating: stop timer */
284 --timercnt; /* maybe pass by reference instead? */ 495 }
496}
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)
285 } 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 */
286 514
287 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 }
288 } 541 }
289} 542}
290 543
291static void 544static void
292time_update () 545time_update ()
293{ 546{
294 int i; 547 int i;
548
295 ev_now = ev_time (); 549 ev_now = ev_time ();
296 550
297 if (have_monotonic) 551 if (have_monotonic)
298 { 552 {
299 ev_tstamp odiff = diff; 553 ev_tstamp odiff = diff;
300 554
301 /* detecting time jumps is much more difficult */
302 for (i = 2; --i; ) /* loop a few times, before making important decisions */ 555 for (i = 4; --i; ) /* loop a few times, before making important decisions */
303 { 556 {
304 now = get_clock (); 557 now = get_clock ();
305 diff = ev_now - now; 558 diff = ev_now - now;
306 559
307 if (fabs (odiff - diff) < MIN_TIMEJUMP) 560 if (fabs (odiff - diff) < MIN_TIMEJUMP)
308 return; /* all is well */ 561 return; /* all is well */
309 562
310 ev_now = ev_time (); 563 ev_now = ev_time ();
311 } 564 }
312 565
313 /* time jump detected, reschedule atimers */ 566 periodics_reschedule (diff - odiff);
314 for (i = 0; i < atimercnt; ++i) 567 /* no timer adjustment, as the monotonic clock doesn't jump */
315 {
316 struct ev_timer *w = atimers [i];
317 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
318 }
319 } 568 }
320 else 569 else
321 { 570 {
322 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 571 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
323 /* time jump detected, adjust rtimers */ 572 {
573 periodics_reschedule (ev_now - now);
574
575 /* adjust timers. this is easy, as the offset is the same for all */
324 for (i = 0; i < rtimercnt; ++i) 576 for (i = 0; i < timercnt; ++i)
325 rtimers [i]->at += ev_now - now; 577 timers [i]->at += diff;
578 }
326 579
327 now = ev_now; 580 now = ev_now;
328 } 581 }
329} 582}
330 583
331int ev_loop_done; 584int ev_loop_done;
332 585
333void ev_loop (int flags) 586void ev_loop (int flags)
334{ 587{
335 double block; 588 double block;
336 ev_loop_done = flags & EVLOOP_ONESHOT; 589 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
337 590
338 do 591 do
339 { 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
340 /* update fd-related kernel structures */ 600 /* update fd-related kernel structures */
341 method_reify (); fdchangecnt = 0; 601 fd_reify ();
342 602
343 /* 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 */
607 ev_now = ev_time ();
608
344 if (flags & EVLOOP_NONBLOCK) 609 if (flags & EVLOOP_NONBLOCK || idlecnt)
345 block = 0.; 610 block = 0.;
346 else 611 else
347 { 612 {
348 block = MAX_BLOCKTIME; 613 block = MAX_BLOCKTIME;
349 614
350 if (rtimercnt) 615 if (timercnt)
351 { 616 {
352 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; 617 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
353 if (block > to) block = to; 618 if (block > to) block = to;
354 } 619 }
355 620
356 if (atimercnt) 621 if (periodiccnt)
357 { 622 {
358 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; 623 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
359 if (block > to) block = to; 624 if (block > to) block = to;
360 } 625 }
361 626
362 if (block < 0.) block = 0.; 627 if (block < 0.) block = 0.;
363 } 628 }
365 method_poll (block); 630 method_poll (block);
366 631
367 /* update ev_now, do magic */ 632 /* update ev_now, do magic */
368 time_update (); 633 time_update ();
369 634
370 /* put pending timers into pendign queue and reschedule them */ 635 /* queue pending timers and reschedule them */
371 /* absolute timers first */ 636 timers_reify (); /* relative timers called last */
372 timers_reify (atimers, atimercnt, ev_now); 637 periodics_reify (); /* absolute timers called first */
373 /* relative timers second */ 638
374 timers_reify (rtimers, rtimercnt, now); 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);
375 646
376 call_pending (); 647 call_pending ();
377 } 648 }
378 while (!ev_loop_done); 649 while (!ev_loop_done);
379}
380 650
651 if (ev_loop_done != 2)
652 ev_loop_done = 0;
653}
654
655/*****************************************************************************/
656
381static void 657static void
382wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 658wlist_add (WL *head, WL elem)
383{ 659{
384 elem->next = *head; 660 elem->next = *head;
385 *head = elem; 661 *head = elem;
386} 662}
387 663
388static void 664static void
389wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 665wlist_del (WL *head, WL elem)
390{ 666{
391 while (*head) 667 while (*head)
392 { 668 {
393 if (*head == elem) 669 if (*head == elem)
394 { 670 {
399 head = &(*head)->next; 675 head = &(*head)->next;
400 } 676 }
401} 677}
402 678
403static void 679static void
404ev_start (struct ev_watcher *w, int active) 680ev_clear (W w)
405{ 681{
682 if (w->pending)
683 {
684 pendings [w->pending - 1].w = 0;
406 w->pending = 0; 685 w->pending = 0;
686 }
687}
688
689static void
690ev_start (W w, int active)
691{
407 w->active = active; 692 w->active = active;
408} 693}
409 694
410static void 695static void
411ev_stop (struct ev_watcher *w) 696ev_stop (W w)
412{ 697{
413 if (w->pending)
414 pendings [w->pending - 1].w = 0;
415
416 w->active = 0; 698 w->active = 0;
417 /* nop */
418} 699}
700
701/*****************************************************************************/
419 702
420void 703void
421evio_start (struct ev_io *w) 704evio_start (struct ev_io *w)
422{ 705{
423 if (ev_is_active (w)) 706 if (ev_is_active (w))
424 return; 707 return;
425 708
426 int fd = w->fd; 709 int fd = w->fd;
427 710
428 ev_start ((struct ev_watcher *)w, 1); 711 ev_start ((W)w, 1);
429 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 712 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
430 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 713 wlist_add ((WL *)&anfds[fd].head, (WL)w);
431 714
432 ++fdchangecnt; 715 ++fdchangecnt;
433 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 716 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
434 fdchanges [fdchangecnt - 1] = fd; 717 fdchanges [fdchangecnt - 1] = fd;
435} 718}
436 719
437void 720void
438evio_stop (struct ev_io *w) 721evio_stop (struct ev_io *w)
439{ 722{
723 ev_clear ((W)w);
440 if (!ev_is_active (w)) 724 if (!ev_is_active (w))
441 return; 725 return;
442 726
443 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);
444 ev_stop ((struct ev_watcher *)w); 728 ev_stop ((W)w);
445 729
446 ++fdchangecnt; 730 ++fdchangecnt;
447 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 731 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
448 fdchanges [fdchangecnt - 1] = w->fd; 732 fdchanges [fdchangecnt - 1] = w->fd;
449} 733}
452evtimer_start (struct ev_timer *w) 736evtimer_start (struct ev_timer *w)
453{ 737{
454 if (ev_is_active (w)) 738 if (ev_is_active (w))
455 return; 739 return;
456 740
457 if (w->is_abs) 741 w->at += now;
458 {
459 /* this formula differs from the one in timer_reify becuse we do not round up */
460 if (w->repeat)
461 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
462 742
463 ev_start ((struct ev_watcher *)w, ++atimercnt); 743 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
744
745 ev_start ((W)w, ++timercnt);
464 array_needsize (atimers, atimermax, atimercnt, ); 746 array_needsize (timers, timermax, timercnt, );
465 atimers [atimercnt - 1] = w; 747 timers [timercnt - 1] = w;
466 upheap (atimers, atimercnt - 1); 748 upheap ((WT *)timers, timercnt - 1);
467 }
468 else
469 {
470 w->at += now;
471
472 ev_start ((struct ev_watcher *)w, ++rtimercnt);
473 array_needsize (rtimers, rtimermax, rtimercnt, );
474 rtimers [rtimercnt - 1] = w;
475 upheap (rtimers, rtimercnt - 1);
476 }
477
478} 749}
479 750
480void 751void
481evtimer_stop (struct ev_timer *w) 752evtimer_stop (struct ev_timer *w)
482{ 753{
754 ev_clear ((W)w);
483 if (!ev_is_active (w)) 755 if (!ev_is_active (w))
484 return; 756 return;
485 757
486 if (w->is_abs)
487 {
488 if (w->active < atimercnt--) 758 if (w->active < timercnt--)
489 { 759 {
490 atimers [w->active - 1] = atimers [atimercnt]; 760 timers [w->active - 1] = timers [timercnt];
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))
773 {
774 if (w->repeat)
775 {
776 w->at = now + w->repeat;
491 downheap (atimers, atimercnt, w->active - 1); 777 downheap ((WT *)timers, timercnt, w->active - 1);
492 }
493 }
494 else
495 {
496 if (w->active < rtimercnt--)
497 { 778 }
498 rtimers [w->active - 1] = rtimers [rtimercnt]; 779 else
499 downheap (rtimers, rtimercnt, w->active - 1); 780 evtimer_stop (w);
500 } 781 }
782 else if (w->repeat)
783 evtimer_start (w);
784}
785
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--)
501 } 812 {
813 periodics [w->active - 1] = periodics [periodiccnt];
814 downheap ((WT *)periodics, periodiccnt, w->active - 1);
815 }
502 816
503 ev_stop ((struct ev_watcher *)w); 817 ev_stop ((W)w);
504} 818}
505 819
506void 820void
507evsignal_start (struct ev_signal *w) 821evsignal_start (struct ev_signal *w)
508{ 822{
509 if (ev_is_active (w)) 823 if (ev_is_active (w))
510 return; 824 return;
511 825
512 ev_start ((struct ev_watcher *)w, 1); 826 ev_start ((W)w, 1);
513 array_needsize (signals, signalmax, w->signum, signals_init); 827 array_needsize (signals, signalmax, w->signum, signals_init);
514 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 }
515} 838}
516 839
517void 840void
518evsignal_stop (struct ev_signal *w) 841evsignal_stop (struct ev_signal *w)
519{ 842{
843 ev_clear ((W)w);
520 if (!ev_is_active (w)) 844 if (!ev_is_active (w))
521 return; 845 return;
522 846
523 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);
524 ev_stop ((struct ev_watcher *)w); 848 ev_stop ((W)w);
525}
526 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
527/*****************************************************************************/ 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
528#if 1 1000#if 0
1001
1002struct ev_io wio;
529 1003
530static void 1004static void
531sin_cb (struct ev_io *w, int revents) 1005sin_cb (struct ev_io *w, int revents)
532{ 1006{
533 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 1007 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
539 //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);
540 evtimer_stop (w); 1014 evtimer_stop (w);
541 evtimer_start (w); 1015 evtimer_start (w);
542} 1016}
543 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
1031}
1032
544int main (void) 1033int main (void)
545{ 1034{
546 struct ev_io sin;
547
548 ev_init (0); 1035 ev_init (0);
549 1036
550 evw_init (&sin, sin_cb, 55);
551 evio_set (&sin, 0, EV_READ); 1037 evio_init (&wio, sin_cb, 0, EV_READ);
552 evio_start (&sin); 1038 evio_start (&wio);
553 1039
554 struct ev_timer t[10000]; 1040 struct ev_timer t[10000];
555 1041
556#if 1 1042#if 0
557 int i; 1043 int i;
558 for (i = 0; i < 10000; ++i) 1044 for (i = 0; i < 10000; ++i)
559 { 1045 {
560 struct ev_timer *w = t + i; 1046 struct ev_timer *w = t + i;
561 evw_init (w, ocb, i); 1047 evw_init (w, ocb, i);
562 evtimer_set_abs (w, drand48 (), 0.99775533); 1048 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
563 evtimer_start (w); 1049 evtimer_start (w);
564 if (drand48 () < 0.5) 1050 if (drand48 () < 0.5)
565 evtimer_stop (w); 1051 evtimer_stop (w);
566 } 1052 }
567#endif 1053#endif
568 1054
569 struct ev_timer t1; 1055 struct ev_timer t1;
570 evw_init (&t1, ocb, 0); 1056 evtimer_init (&t1, ocb, 5, 10);
571 evtimer_set_abs (&t1, 5, 10);
572 evtimer_start (&t1); 1057 evtimer_start (&t1);
573 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);
1070
574 ev_loop (0); 1071 ev_loop (0);
575 1072
576 return 0; 1073 return 0;
577} 1074}
578 1075

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines