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

Comparing libev/ev.c (file contents):
Revision 1.7 by root, Wed Oct 31 00:24:16 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>
3#include <unistd.h> 32#include <unistd.h>
4#include <fcntl.h> 33#include <fcntl.h>
5#include <signal.h> 34#include <signal.h>
35#include <stddef.h>
6 36
7#include <stdio.h> 37#include <stdio.h>
8 38
9#include <assert.h> 39#include <assert.h>
10#include <errno.h> 40#include <errno.h>
41#include <sys/types.h>
42#include <sys/wait.h>
11#include <sys/time.h> 43#include <sys/time.h>
12#include <time.h> 44#include <time.h>
13 45
46#ifndef HAVE_MONOTONIC
14#ifdef CLOCK_MONOTONIC 47# ifdef CLOCK_MONOTONIC
15# define HAVE_MONOTONIC 1 48# define HAVE_MONOTONIC 1
16#endif 49# endif
50#endif
17 51
18#define HAVE_REALTIME 1 52#ifndef HAVE_SELECT
19#define HAVE_EPOLL 1
20#define HAVE_SELECT 1 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
21 63
22#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) */
23#define MAX_BLOCKTIME 60. 65#define MAX_BLOCKTIME 60.
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
24 67
25#include "ev.h" 68#include "ev.h"
26 69
27struct ev_watcher { 70typedef struct ev_watcher *W;
28 EV_WATCHER (ev_watcher);
29};
30
31struct ev_watcher_list { 71typedef struct ev_watcher_list *WL;
32 EV_WATCHER_LIST (ev_watcher_list); 72typedef struct ev_watcher_time *WT;
33};
34 73
35static ev_tstamp now, diff; /* monotonic clock */ 74static ev_tstamp now, diff; /* monotonic clock */
36ev_tstamp ev_now; 75ev_tstamp ev_now;
37int ev_method; 76int ev_method;
38 77
39static int have_monotonic; /* runtime */ 78static int have_monotonic; /* runtime */
40 79
41static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 80static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
42static void (*method_modify)(int fd, int oev, int nev); 81static void (*method_modify)(int fd, int oev, int nev);
43static void (*method_poll)(ev_tstamp timeout); 82static void (*method_poll)(ev_tstamp timeout);
83
84/*****************************************************************************/
44 85
45ev_tstamp 86ev_tstamp
46ev_time (void) 87ev_time (void)
47{ 88{
48#if HAVE_REALTIME 89#if HAVE_REALTIME
72} 113}
73 114
74#define array_needsize(base,cur,cnt,init) \ 115#define array_needsize(base,cur,cnt,init) \
75 if ((cnt) > cur) \ 116 if ((cnt) > cur) \
76 { \ 117 { \
77 int newcnt = cur ? cur << 1 : 16; \ 118 int newcnt = cur; \
78 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 \
79 base = realloc (base, sizeof (*base) * (newcnt)); \ 125 base = realloc (base, sizeof (*base) * (newcnt)); \
80 init (base + cur, newcnt - cur); \ 126 init (base + cur, newcnt - cur); \
81 cur = newcnt; \ 127 cur = newcnt; \
82 } 128 }
83 129
130/*****************************************************************************/
131
84typedef struct 132typedef struct
85{ 133{
86 struct ev_io *head; 134 struct ev_io *head;
87 unsigned char wev, rev; /* want, received event set */ 135 unsigned char wev, rev; /* want, received event set */
88} ANFD; 136} ANFD;
104 } 152 }
105} 153}
106 154
107typedef struct 155typedef struct
108{ 156{
109 struct ev_watcher *w; 157 W w;
110 int events; 158 int events;
111} ANPENDING; 159} ANPENDING;
112 160
113static ANPENDING *pendings; 161static ANPENDING *pendings;
114static int pendingmax, pendingcnt; 162static int pendingmax, pendingcnt;
115 163
116static void 164static void
117event (struct ev_watcher *w, int events) 165event (W w, int events)
118{ 166{
167 if (w->active)
168 {
119 w->pending = ++pendingcnt; 169 w->pending = ++pendingcnt;
120 array_needsize (pendings, pendingmax, pendingcnt, ); 170 array_needsize (pendings, pendingmax, pendingcnt, );
121 pendings [pendingcnt - 1].w = w; 171 pendings [pendingcnt - 1].w = w;
122 pendings [pendingcnt - 1].events = events; 172 pendings [pendingcnt - 1].events = events;
173 }
123} 174}
124 175
125static void 176static void
126fd_event (int fd, int events) 177fd_event (int fd, int events)
127{ 178{
131 for (w = anfd->head; w; w = w->next) 182 for (w = anfd->head; w; w = w->next)
132 { 183 {
133 int ev = w->events & events; 184 int ev = w->events & events;
134 185
135 if (ev) 186 if (ev)
136 event ((struct ev_watcher *)w, ev); 187 event ((W)w, ev);
137 } 188 }
138} 189}
139 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/*****************************************************************************/
217
140static struct ev_timer **atimers; 218static struct ev_timer **timers;
141static int atimermax, atimercnt; 219static int timermax, timercnt;
142 220
143static struct ev_timer **rtimers; 221static struct ev_periodic **periodics;
144static int rtimermax, rtimercnt; 222static int periodicmax, periodiccnt;
145 223
146static void 224static void
147upheap (struct ev_timer **timers, int k) 225upheap (WT *timers, int k)
148{ 226{
149 struct ev_timer *w = timers [k]; 227 WT w = timers [k];
150 228
151 while (k && timers [k >> 1]->at > w->at) 229 while (k && timers [k >> 1]->at > w->at)
152 { 230 {
153 timers [k] = timers [k >> 1]; 231 timers [k] = timers [k >> 1];
154 timers [k]->active = k + 1; 232 timers [k]->active = k + 1;
159 timers [k]->active = k + 1; 237 timers [k]->active = k + 1;
160 238
161} 239}
162 240
163static void 241static void
164downheap (struct ev_timer **timers, int N, int k) 242downheap (WT *timers, int N, int k)
165{ 243{
166 struct ev_timer *w = timers [k]; 244 WT w = timers [k];
167 245
168 while (k < (N >> 1)) 246 while (k < (N >> 1))
169 { 247 {
170 int j = k << 1; 248 int j = k << 1;
171 249
181 } 259 }
182 260
183 timers [k] = w; 261 timers [k] = w;
184 timers [k]->active = k + 1; 262 timers [k]->active = k + 1;
185} 263}
264
265/*****************************************************************************/
186 266
187typedef struct 267typedef struct
188{ 268{
189 struct ev_signal *head; 269 struct ev_signal *head;
190 sig_atomic_t gotsig; 270 sig_atomic_t gotsig;
233 if (signals [sig].gotsig) 313 if (signals [sig].gotsig)
234 { 314 {
235 signals [sig].gotsig = 0; 315 signals [sig].gotsig = 0;
236 316
237 for (w = signals [sig].head; w; w = w->next) 317 for (w = signals [sig].head; w; w = w->next)
238 event ((struct ev_watcher *)w, EV_SIGNAL); 318 event ((W)w, EV_SIGNAL);
239 } 319 }
240} 320}
241 321
242static void 322static void
243siginit (void) 323siginit (void)
251 331
252 evio_set (&sigev, sigpipe [0], EV_READ); 332 evio_set (&sigev, sigpipe [0], EV_READ);
253 evio_start (&sigev); 333 evio_start (&sigev);
254} 334}
255 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/*****************************************************************************/
372
256#if HAVE_EPOLL 373#if HAVE_EPOLL
257# include "ev_epoll.c" 374# include "ev_epoll.c"
258#endif 375#endif
259#if HAVE_SELECT 376#if HAVE_SELECT
260# include "ev_select.c" 377# include "ev_select.c"
261#endif 378#endif
262 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
263int ev_init (int flags) 392int ev_init (int flags)
264{ 393{
394 if (!ev_method)
395 {
265#if HAVE_MONOTONIC 396#if HAVE_MONOTONIC
266 { 397 {
267 struct timespec ts; 398 struct timespec ts;
268 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 399 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
269 have_monotonic = 1; 400 have_monotonic = 1;
270 } 401 }
271#endif 402#endif
272 403
273 ev_now = ev_time (); 404 ev_now = ev_time ();
274 now = get_clock (); 405 now = get_clock ();
275 diff = ev_now - now; 406 diff = ev_now - now;
276 407
277 if (pipe (sigpipe)) 408 if (pipe (sigpipe))
278 return 0; 409 return 0;
279 410
280 ev_method = EVMETHOD_NONE; 411 ev_method = EVMETHOD_NONE;
281#if HAVE_EPOLL 412#if HAVE_EPOLL
282 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 413 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
283#endif 414#endif
284#if HAVE_SELECT 415#if HAVE_SELECT
285 if (ev_method == EVMETHOD_NONE) select_init (flags); 416 if (ev_method == EVMETHOD_NONE) select_init (flags);
286#endif 417#endif
287 418
288 if (ev_method) 419 if (ev_method)
289 { 420 {
290 evw_init (&sigev, sigcb, 0); 421 evw_init (&sigev, sigcb);
291 siginit (); 422 siginit ();
423
424 evsignal_init (&childev, childcb, SIGCHLD);
425 evsignal_start (&childev);
426 }
292 } 427 }
293 428
294 return ev_method; 429 return ev_method;
295} 430}
296 431
432/*****************************************************************************/
433
434void
297void ev_prefork (void) 435ev_prefork (void)
298{ 436{
437 /* nop */
299} 438}
300 439
440void
301void ev_postfork_parent (void) 441ev_postfork_parent (void)
302{ 442{
443 /* nop */
303} 444}
304 445
446void
305void ev_postfork_child (void) 447ev_postfork_child (void)
306{ 448{
307#if HAVE_EPOLL 449#if HAVE_EPOLL
308 if (ev_method == EVMETHOD_EPOLL) 450 if (ev_method == EVMETHOD_EPOLL)
309 epoll_postfork_child (); 451 epoll_postfork_child ();
310#endif 452#endif
314 close (sigpipe [1]); 456 close (sigpipe [1]);
315 pipe (sigpipe); 457 pipe (sigpipe);
316 siginit (); 458 siginit ();
317} 459}
318 460
461/*****************************************************************************/
462
319static void 463static void
320fd_reify (void) 464fd_reify (void)
321{ 465{
322 int i; 466 int i;
323 467
341 485
342 fdchangecnt = 0; 486 fdchangecnt = 0;
343} 487}
344 488
345static void 489static void
346call_pending () 490call_pending (void)
347{ 491{
348 int i; 492 while (pendingcnt)
349
350 for (i = 0; i < pendingcnt; ++i)
351 { 493 {
352 ANPENDING *p = pendings + i; 494 ANPENDING *p = pendings + --pendingcnt;
353 495
354 if (p->w) 496 if (p->w)
355 { 497 {
356 p->w->pending = 0; 498 p->w->pending = 0;
357 p->w->cb (p->w, p->events); 499 p->w->cb (p->w, p->events);
358 } 500 }
359 } 501 }
360
361 pendingcnt = 0;
362} 502}
363 503
364static void 504static void
365timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) 505timers_reify (void)
366{ 506{
367 while (timercnt && timers [0]->at <= now) 507 while (timercnt && timers [0]->at <= now)
368 { 508 {
369 struct ev_timer *w = timers [0]; 509 struct ev_timer *w = timers [0];
510
511 event ((W)w, EV_TIMEOUT);
370 512
371 /* first reschedule or stop timer */ 513 /* first reschedule or stop timer */
372 if (w->repeat) 514 if (w->repeat)
373 { 515 {
374 if (w->is_abs)
375 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
376 else
377 w->at = now + w->repeat; 516 w->at = now + w->repeat;
378 517 assert (("timer timeout in the past, negative repeat?", w->at > now));
379 assert (w->at > now);
380
381 downheap (timers, timercnt, 0); 518 downheap ((WT *)timers, timercnt, 0);
382 } 519 }
383 else 520 else
384 {
385 evtimer_stop (w); /* nonrepeating: stop timer */ 521 evtimer_stop (w); /* nonrepeating: stop timer */
386 --timercnt; /* maybe pass by reference instead? */ 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)
387 } 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 */
388 541
389 event ((struct ev_watcher *)w, EV_TIMEOUT); 542 event ((W)w, EV_TIMEOUT);
390 } 543 }
391} 544}
392 545
393static void 546static void
394time_update () 547periodics_reschedule (ev_tstamp diff)
395{ 548{
396 int i; 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
397 ev_now = ev_time (); 576 ev_now = ev_time ();
398 577
399 if (have_monotonic) 578 if (have_monotonic)
400 { 579 {
401 ev_tstamp odiff = diff; 580 ev_tstamp odiff = diff;
402 581
403 /* detecting time jumps is much more difficult */
404 for (i = 2; --i; ) /* loop a few times, before making important decisions */ 582 for (i = 4; --i; ) /* loop a few times, before making important decisions */
405 { 583 {
406 now = get_clock (); 584 now = get_clock ();
407 diff = ev_now - now; 585 diff = ev_now - now;
408 586
409 if (fabs (odiff - diff) < MIN_TIMEJUMP) 587 if (fabs (odiff - diff) < MIN_TIMEJUMP)
410 return; /* all is well */ 588 return; /* all is well */
411 589
412 ev_now = ev_time (); 590 ev_now = ev_time ();
413 } 591 }
414 592
415 /* time jump detected, reschedule atimers */ 593 periodics_reschedule (diff - odiff);
416 for (i = 0; i < atimercnt; ++i) 594 /* no timer adjustment, as the monotonic clock doesn't jump */
417 {
418 struct ev_timer *w = atimers [i];
419 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
420 }
421 } 595 }
422 else 596 else
423 { 597 {
424 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 598 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
425 /* time jump detected, adjust rtimers */ 599 {
600 periodics_reschedule (ev_now - now);
601
602 /* adjust timers. this is easy, as the offset is the same for all */
426 for (i = 0; i < rtimercnt; ++i) 603 for (i = 0; i < timercnt; ++i)
427 rtimers [i]->at += ev_now - now; 604 timers [i]->at += diff;
605 }
428 606
429 now = ev_now; 607 now = ev_now;
430 } 608 }
431} 609}
432 610
433int ev_loop_done; 611int ev_loop_done;
434 612
435void ev_loop (int flags) 613void ev_loop (int flags)
436{ 614{
437 double block; 615 double block;
438 ev_loop_done = flags & EVLOOP_ONESHOT; 616 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
439 617
440 do 618 do
441 { 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
442 /* update fd-related kernel structures */ 627 /* update fd-related kernel structures */
443 fd_reify (); 628 fd_reify ();
444 629
445 /* 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 */
634 ev_now = ev_time ();
635
446 if (flags & EVLOOP_NONBLOCK) 636 if (flags & EVLOOP_NONBLOCK || idlecnt)
447 block = 0.; 637 block = 0.;
448 else 638 else
449 { 639 {
450 block = MAX_BLOCKTIME; 640 block = MAX_BLOCKTIME;
451 641
452 if (rtimercnt) 642 if (timercnt)
453 { 643 {
454 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; 644 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
455 if (block > to) block = to; 645 if (block > to) block = to;
456 } 646 }
457 647
458 if (atimercnt) 648 if (periodiccnt)
459 { 649 {
460 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; 650 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
461 if (block > to) block = to; 651 if (block > to) block = to;
462 } 652 }
463 653
464 if (block < 0.) block = 0.; 654 if (block < 0.) block = 0.;
465 } 655 }
467 method_poll (block); 657 method_poll (block);
468 658
469 /* update ev_now, do magic */ 659 /* update ev_now, do magic */
470 time_update (); 660 time_update ();
471 661
472 /* put pending timers into pendign queue and reschedule them */ 662 /* queue pending timers and reschedule them */
473 /* absolute timers first */ 663 timers_reify (); /* relative timers called last */
474 timers_reify (atimers, atimercnt, ev_now); 664 periodics_reify (); /* absolute timers called first */
475 /* relative timers second */ 665
476 timers_reify (rtimers, rtimercnt, now); 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);
477 673
478 call_pending (); 674 call_pending ();
479 } 675 }
480 while (!ev_loop_done); 676 while (!ev_loop_done);
481}
482 677
678 if (ev_loop_done != 2)
679 ev_loop_done = 0;
680}
681
682/*****************************************************************************/
683
483static void 684static void
484wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 685wlist_add (WL *head, WL elem)
485{ 686{
486 elem->next = *head; 687 elem->next = *head;
487 *head = elem; 688 *head = elem;
488} 689}
489 690
490static void 691static void
491wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 692wlist_del (WL *head, WL elem)
492{ 693{
493 while (*head) 694 while (*head)
494 { 695 {
495 if (*head == elem) 696 if (*head == elem)
496 { 697 {
501 head = &(*head)->next; 702 head = &(*head)->next;
502 } 703 }
503} 704}
504 705
505static void 706static void
506ev_start (struct ev_watcher *w, int active) 707ev_clear (W w)
507{ 708{
709 if (w->pending)
710 {
711 pendings [w->pending - 1].w = 0;
508 w->pending = 0; 712 w->pending = 0;
713 }
714}
715
716static void
717ev_start (W w, int active)
718{
509 w->active = active; 719 w->active = active;
510} 720}
511 721
512static void 722static void
513ev_stop (struct ev_watcher *w) 723ev_stop (W w)
514{ 724{
515 if (w->pending)
516 pendings [w->pending - 1].w = 0;
517
518 w->active = 0; 725 w->active = 0;
519 /* nop */
520} 726}
727
728/*****************************************************************************/
521 729
522void 730void
523evio_start (struct ev_io *w) 731evio_start (struct ev_io *w)
524{ 732{
525 if (ev_is_active (w)) 733 if (ev_is_active (w))
526 return; 734 return;
527 735
528 int fd = w->fd; 736 int fd = w->fd;
529 737
530 ev_start ((struct ev_watcher *)w, 1); 738 ev_start ((W)w, 1);
531 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 739 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
532 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 740 wlist_add ((WL *)&anfds[fd].head, (WL)w);
533 741
534 ++fdchangecnt; 742 ++fdchangecnt;
535 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 743 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
536 fdchanges [fdchangecnt - 1] = fd; 744 fdchanges [fdchangecnt - 1] = fd;
537} 745}
538 746
539void 747void
540evio_stop (struct ev_io *w) 748evio_stop (struct ev_io *w)
541{ 749{
750 ev_clear ((W)w);
542 if (!ev_is_active (w)) 751 if (!ev_is_active (w))
543 return; 752 return;
544 753
545 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);
546 ev_stop ((struct ev_watcher *)w); 755 ev_stop ((W)w);
547 756
548 ++fdchangecnt; 757 ++fdchangecnt;
549 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 758 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
550 fdchanges [fdchangecnt - 1] = w->fd; 759 fdchanges [fdchangecnt - 1] = w->fd;
551} 760}
554evtimer_start (struct ev_timer *w) 763evtimer_start (struct ev_timer *w)
555{ 764{
556 if (ev_is_active (w)) 765 if (ev_is_active (w))
557 return; 766 return;
558 767
559 if (w->is_abs) 768 w->at += now;
769
770 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
771
772 ev_start ((W)w, ++timercnt);
773 array_needsize (timers, timermax, timercnt, );
774 timers [timercnt - 1] = w;
775 upheap ((WT *)timers, timercnt - 1);
776}
777
778void
779evtimer_stop (struct ev_timer *w)
780{
781 ev_clear ((W)w);
782 if (!ev_is_active (w))
783 return;
784
785 if (w->active < timercnt--)
786 {
787 timers [w->active - 1] = timers [timercnt];
788 downheap ((WT *)timers, timercnt, w->active - 1);
560 { 789 }
561 /* this formula differs from the one in timer_reify becuse we do not round up */ 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))
800 {
562 if (w->repeat) 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}
812
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)
563 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; 823 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
564 824
565 ev_start ((struct ev_watcher *)w, ++atimercnt); 825 ev_start ((W)w, ++periodiccnt);
566 array_needsize (atimers, atimermax, atimercnt, ); 826 array_needsize (periodics, periodicmax, periodiccnt, );
567 atimers [atimercnt - 1] = w; 827 periodics [periodiccnt - 1] = w;
568 upheap (atimers, atimercnt - 1); 828 upheap ((WT *)periodics, periodiccnt - 1);
569 }
570 else
571 {
572 w->at += now;
573
574 ev_start ((struct ev_watcher *)w, ++rtimercnt);
575 array_needsize (rtimers, rtimermax, rtimercnt, );
576 rtimers [rtimercnt - 1] = w;
577 upheap (rtimers, rtimercnt - 1);
578 }
579
580} 829}
581 830
582void 831void
583evtimer_stop (struct ev_timer *w) 832evperiodic_stop (struct ev_periodic *w)
584{ 833{
834 ev_clear ((W)w);
585 if (!ev_is_active (w)) 835 if (!ev_is_active (w))
586 return; 836 return;
587 837
588 if (w->is_abs)
589 {
590 if (w->active < atimercnt--) 838 if (w->active < periodiccnt--)
591 {
592 atimers [w->active - 1] = atimers [atimercnt];
593 downheap (atimers, atimercnt, w->active - 1);
594 }
595 } 839 {
596 else 840 periodics [w->active - 1] = periodics [periodiccnt];
841 downheap ((WT *)periodics, periodiccnt, w->active - 1);
597 { 842 }
598 if (w->active < rtimercnt--)
599 {
600 rtimers [w->active - 1] = rtimers [rtimercnt];
601 downheap (rtimers, rtimercnt, w->active - 1);
602 }
603 }
604 843
605 ev_stop ((struct ev_watcher *)w); 844 ev_stop ((W)w);
606} 845}
607 846
608void 847void
609evsignal_start (struct ev_signal *w) 848evsignal_start (struct ev_signal *w)
610{ 849{
611 if (ev_is_active (w)) 850 if (ev_is_active (w))
612 return; 851 return;
613 852
614 ev_start ((struct ev_watcher *)w, 1); 853 ev_start ((W)w, 1);
615 array_needsize (signals, signalmax, w->signum, signals_init); 854 array_needsize (signals, signalmax, w->signum, signals_init);
616 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); 855 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
617 856
618 if (!w->next) 857 if (!w->next)
619 { 858 {
620 struct sigaction sa; 859 struct sigaction sa;
621 sa.sa_handler = sighandler; 860 sa.sa_handler = sighandler;
626} 865}
627 866
628void 867void
629evsignal_stop (struct ev_signal *w) 868evsignal_stop (struct ev_signal *w)
630{ 869{
870 ev_clear ((W)w);
631 if (!ev_is_active (w)) 871 if (!ev_is_active (w))
632 return; 872 return;
633 873
634 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); 874 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
635 ev_stop ((struct ev_watcher *)w); 875 ev_stop ((W)w);
636 876
637 if (!signals [w->signum - 1].head) 877 if (!signals [w->signum - 1].head)
638 signal (w->signum, SIG_DFL); 878 signal (w->signum, SIG_DFL);
639} 879}
640 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
641/*****************************************************************************/ 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
642#if 1 1027#if 0
1028
1029struct ev_io wio;
643 1030
644static void 1031static void
645sin_cb (struct ev_io *w, int revents) 1032sin_cb (struct ev_io *w, int revents)
646{ 1033{
647 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 1034 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
657 1044
658static void 1045static void
659scb (struct ev_signal *w, int revents) 1046scb (struct ev_signal *w, int revents)
660{ 1047{
661 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 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
662} 1058}
663 1059
664int main (void) 1060int main (void)
665{ 1061{
666 struct ev_io sin;
667
668 ev_init (0); 1062 ev_init (0);
669 1063
670 evw_init (&sin, sin_cb, 55);
671 evio_set (&sin, 0, EV_READ); 1064 evio_init (&wio, sin_cb, 0, EV_READ);
672 evio_start (&sin); 1065 evio_start (&wio);
673 1066
674 struct ev_timer t[10000]; 1067 struct ev_timer t[10000];
675 1068
676#if 0 1069#if 0
677 int i; 1070 int i;
678 for (i = 0; i < 10000; ++i) 1071 for (i = 0; i < 10000; ++i)
679 { 1072 {
680 struct ev_timer *w = t + i; 1073 struct ev_timer *w = t + i;
681 evw_init (w, ocb, i); 1074 evw_init (w, ocb, i);
682 evtimer_set_abs (w, drand48 (), 0.99775533); 1075 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
683 evtimer_start (w); 1076 evtimer_start (w);
684 if (drand48 () < 0.5) 1077 if (drand48 () < 0.5)
685 evtimer_stop (w); 1078 evtimer_stop (w);
686 } 1079 }
687#endif 1080#endif
688 1081
689 struct ev_timer t1; 1082 struct ev_timer t1;
690 evw_init (&t1, ocb, 0); 1083 evtimer_init (&t1, ocb, 5, 10);
691 evtimer_set_abs (&t1, 5, 10);
692 evtimer_start (&t1); 1084 evtimer_start (&t1);
693 1085
694 struct ev_signal sig; 1086 struct ev_signal sig;
695 evw_init (&sig, scb, 65535);
696 evsignal_set (&sig, SIGQUIT); 1087 evsignal_init (&sig, scb, SIGQUIT);
697 evsignal_start (&sig); 1088 evsignal_start (&sig);
698 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);
1097
699 ev_loop (0); 1098 ev_loop (0);
700 1099
701 return 0; 1100 return 0;
702} 1101}
703 1102

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines