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

Comparing libev/ev.c (file contents):
Revision 1.16 by root, Wed Oct 31 13:57:34 2007 UTC vs.
Revision 1.23 by root, Wed Oct 31 20:10:17 2007 UTC

1/*
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
1#include <math.h> 30#include <math.h>
2#include <stdlib.h> 31#include <stdlib.h>
3#include <unistd.h> 32#include <unistd.h>
4#include <fcntl.h> 33#include <fcntl.h>
5#include <signal.h> 34#include <signal.h>
7 36
8#include <stdio.h> 37#include <stdio.h>
9 38
10#include <assert.h> 39#include <assert.h>
11#include <errno.h> 40#include <errno.h>
41#include <sys/types.h>
42#include <sys/wait.h>
12#include <sys/time.h> 43#include <sys/time.h>
13#include <time.h> 44#include <time.h>
14
15#define HAVE_EPOLL 1
16 45
17#ifndef HAVE_MONOTONIC 46#ifndef HAVE_MONOTONIC
18# ifdef CLOCK_MONOTONIC 47# ifdef CLOCK_MONOTONIC
19# define HAVE_MONOTONIC 1 48# define HAVE_MONOTONIC 1
20# endif 49# endif
32# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */
33#endif 62#endif
34 63
35#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) */
36#define MAX_BLOCKTIME 60. 65#define MAX_BLOCKTIME 60.
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
37 67
38#include "ev.h" 68#include "ev.h"
39 69
40typedef struct ev_watcher *W; 70typedef struct ev_watcher *W;
41typedef struct ev_watcher_list *WL; 71typedef struct ev_watcher_list *WL;
83} 113}
84 114
85#define array_needsize(base,cur,cnt,init) \ 115#define array_needsize(base,cur,cnt,init) \
86 if ((cnt) > cur) \ 116 if ((cnt) > cur) \
87 { \ 117 { \
88 int newcnt = cur ? cur << 1 : 16; \ 118 int newcnt = cur; \
119 do \
120 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \
122 } \
123 while ((cnt) > newcnt); \
124 \
89 base = realloc (base, sizeof (*base) * (newcnt)); \ 125 base = realloc (base, sizeof (*base) * (newcnt)); \
90 init (base + cur, newcnt - cur); \ 126 init (base + cur, newcnt - cur); \
91 cur = newcnt; \ 127 cur = newcnt; \
92 } 128 }
93 129
159 195
160 for (i = 0; i < eventcnt; ++i) 196 for (i = 0; i < eventcnt; ++i)
161 event (events [i], type); 197 event (events [i], type);
162} 198}
163 199
200/* called on EBADF to verify fds */
201static void
202fd_recheck ()
203{
204 int fd;
205
206 for (fd = 0; fd < anfdmax; ++fd)
207 if (anfds [fd].wev)
208 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
209 while (anfds [fd].head)
210 evio_stop (anfds [fd].head);
211}
212
164/*****************************************************************************/ 213/*****************************************************************************/
165 214
166static struct ev_timer **timers; 215static struct ev_timer **timers;
167static int timermax, timercnt; 216static int timermax, timercnt;
168 217
284/*****************************************************************************/ 333/*****************************************************************************/
285 334
286static struct ev_idle **idles; 335static struct ev_idle **idles;
287static int idlemax, idlecnt; 336static int idlemax, idlecnt;
288 337
338static struct ev_prepare **prepares;
339static int preparemax, preparecnt;
340
289static struct ev_check **checks; 341static struct ev_check **checks;
290static int checkmax, checkcnt; 342static int checkmax, checkcnt;
291 343
292/*****************************************************************************/ 344/*****************************************************************************/
293 345
346static struct ev_child *childs [PID_HASHSIZE];
347static struct ev_signal childev;
348
349#ifndef WCONTINUED
350# define WCONTINUED 0
351#endif
352
353static void
354childcb (struct ev_signal *sw, int revents)
355{
356 struct ev_child *w;
357 int pid, status;
358
359 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
360 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
361 if (w->pid == pid || w->pid == -1)
362 {
363 w->status = status;
364 event ((W)w, EV_CHILD);
365 }
366}
367
368/*****************************************************************************/
369
294#if HAVE_EPOLL 370#if HAVE_EPOLL
295# include "ev_epoll.c" 371# include "ev_epoll.c"
296#endif 372#endif
297#if HAVE_SELECT 373#if HAVE_SELECT
298# include "ev_select.c" 374# include "ev_select.c"
299#endif 375#endif
300 376
301int ev_init (int flags) 377int ev_init (int flags)
302{ 378{
379 if (!ev_method)
380 {
303#if HAVE_MONOTONIC 381#if HAVE_MONOTONIC
304 { 382 {
305 struct timespec ts; 383 struct timespec ts;
306 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 384 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
307 have_monotonic = 1; 385 have_monotonic = 1;
308 } 386 }
309#endif 387#endif
310 388
311 ev_now = ev_time (); 389 ev_now = ev_time ();
312 now = get_clock (); 390 now = get_clock ();
313 diff = ev_now - now; 391 diff = ev_now - now;
314 392
315 if (pipe (sigpipe)) 393 if (pipe (sigpipe))
316 return 0; 394 return 0;
317 395
318 ev_method = EVMETHOD_NONE; 396 ev_method = EVMETHOD_NONE;
319#if HAVE_EPOLL 397#if HAVE_EPOLL
320 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 398 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
321#endif 399#endif
322#if HAVE_SELECT 400#if HAVE_SELECT
323 if (ev_method == EVMETHOD_NONE) select_init (flags); 401 if (ev_method == EVMETHOD_NONE) select_init (flags);
324#endif 402#endif
325 403
326 if (ev_method) 404 if (ev_method)
327 { 405 {
328 evw_init (&sigev, sigcb); 406 evw_init (&sigev, sigcb);
329 siginit (); 407 siginit ();
408
409 evsignal_init (&childev, childcb, SIGCHLD);
410 evsignal_start (&childev);
411 }
330 } 412 }
331 413
332 return ev_method; 414 return ev_method;
333} 415}
334 416
387} 469}
388 470
389static void 471static void
390call_pending () 472call_pending ()
391{ 473{
392 int i; 474 while (pendingcnt)
393
394 for (i = 0; i < pendingcnt; ++i)
395 { 475 {
396 ANPENDING *p = pendings + i; 476 ANPENDING *p = pendings + --pendingcnt;
397 477
398 if (p->w) 478 if (p->w)
399 { 479 {
400 p->w->pending = 0; 480 p->w->pending = 0;
401 p->w->cb (p->w, p->events); 481 p->w->cb (p->w, p->events);
402 } 482 }
403 } 483 }
404
405 pendingcnt = 0;
406} 484}
407 485
408static void 486static void
409timers_reify () 487timers_reify ()
410{ 488{
517void ev_loop (int flags) 595void ev_loop (int flags)
518{ 596{
519 double block; 597 double block;
520 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 598 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
521 599
522 if (checkcnt)
523 {
524 queue_events ((W *)checks, checkcnt, EV_CHECK);
525 call_pending ();
526 }
527
528 do 600 do
529 { 601 {
602 /* queue check watchers (and execute them) */
603 if (preparecnt)
604 {
605 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
606 call_pending ();
607 }
608
530 /* update fd-related kernel structures */ 609 /* update fd-related kernel structures */
531 fd_reify (); 610 fd_reify ();
532 611
533 /* calculate blocking time */ 612 /* calculate blocking time */
534 613
535 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ 614 /* we only need this for !monotonic clockor timers, but as we basically
615 always have timers, we just calculate it always */
536 ev_now = ev_time (); 616 ev_now = ev_time ();
537 617
538 if (flags & EVLOOP_NONBLOCK || idlecnt) 618 if (flags & EVLOOP_NONBLOCK || idlecnt)
539 block = 0.; 619 block = 0.;
540 else 620 else
560 640
561 /* update ev_now, do magic */ 641 /* update ev_now, do magic */
562 time_update (); 642 time_update ();
563 643
564 /* queue pending timers and reschedule them */ 644 /* queue pending timers and reschedule them */
645 timers_reify (); /* relative timers called last */
565 periodics_reify (); /* absolute timers first */ 646 periodics_reify (); /* absolute timers called first */
566 timers_reify (); /* relative timers second */
567 647
568 /* queue idle watchers unless io or timers are pending */ 648 /* queue idle watchers unless io or timers are pending */
569 if (!pendingcnt) 649 if (!pendingcnt)
570 queue_events ((W *)idles, idlecnt, EV_IDLE); 650 queue_events ((W *)idles, idlecnt, EV_IDLE);
571 651
572 /* queue check and possibly idle watchers */ 652 /* queue check watchers, to be executed first */
653 if (checkcnt)
573 queue_events ((W *)checks, checkcnt, EV_CHECK); 654 queue_events ((W *)checks, checkcnt, EV_CHECK);
574 655
575 call_pending (); 656 call_pending ();
576 } 657 }
577 while (!ev_loop_done); 658 while (!ev_loop_done);
578 659
797 878
798 idles [w->active - 1] = idles [--idlecnt]; 879 idles [w->active - 1] = idles [--idlecnt];
799 ev_stop ((W)w); 880 ev_stop ((W)w);
800} 881}
801 882
883void evprepare_start (struct ev_prepare *w)
884{
885 if (ev_is_active (w))
886 return;
887
888 ev_start ((W)w, ++preparecnt);
889 array_needsize (prepares, preparemax, preparecnt, );
890 prepares [preparecnt - 1] = w;
891}
892
893void evprepare_stop (struct ev_prepare *w)
894{
895 ev_clear ((W)w);
896 if (ev_is_active (w))
897 return;
898
899 prepares [w->active - 1] = prepares [--preparecnt];
900 ev_stop ((W)w);
901}
902
802void evcheck_start (struct ev_check *w) 903void evcheck_start (struct ev_check *w)
803{ 904{
804 if (ev_is_active (w)) 905 if (ev_is_active (w))
805 return; 906 return;
806 907
814 ev_clear ((W)w); 915 ev_clear ((W)w);
815 if (ev_is_active (w)) 916 if (ev_is_active (w))
816 return; 917 return;
817 918
818 checks [w->active - 1] = checks [--checkcnt]; 919 checks [w->active - 1] = checks [--checkcnt];
920 ev_stop ((W)w);
921}
922
923void evchild_start (struct ev_child *w)
924{
925 if (ev_is_active (w))
926 return;
927
928 ev_start ((W)w, 1);
929 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
930}
931
932void evchild_stop (struct ev_child *w)
933{
934 ev_clear ((W)w);
935 if (ev_is_active (w))
936 return;
937
938 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
819 ev_stop ((W)w); 939 ev_stop ((W)w);
820} 940}
821 941
822/*****************************************************************************/ 942/*****************************************************************************/
823 943

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines