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

Comparing libev/ev.c (file contents):
Revision 1.28 by root, Thu Nov 1 06:48:49 2007 UTC vs.
Revision 1.32 by root, Thu Nov 1 09:21:51 2007 UTC

24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 28 */
29#if EV_USE_CONFIG_H
30# include "config.h"
31#endif
29 32
30#include <math.h> 33#include <math.h>
31#include <stdlib.h> 34#include <stdlib.h>
32#include <unistd.h> 35#include <unistd.h>
33#include <fcntl.h> 36#include <fcntl.h>
41#include <sys/types.h> 44#include <sys/types.h>
42#include <sys/wait.h> 45#include <sys/wait.h>
43#include <sys/time.h> 46#include <sys/time.h>
44#include <time.h> 47#include <time.h>
45 48
46#ifndef HAVE_MONOTONIC 49#ifndef EV_USE_MONOTONIC
47# ifdef CLOCK_MONOTONIC 50# ifdef CLOCK_MONOTONIC
48# define HAVE_MONOTONIC 1 51# define EV_USE_MONOTONIC 1
49# endif 52# endif
50#endif 53#endif
51 54
52#ifndef HAVE_SELECT 55#ifndef EV_USE_SELECT
53# define HAVE_SELECT 1 56# define EV_USE_SELECT 1
54#endif 57#endif
55 58
56#ifndef HAVE_EPOLL 59#ifndef EV_USE_EPOLL
57# define HAVE_EPOLL 0 60# define EV_USE_EPOLL 0
58#endif 61#endif
59 62
63#ifndef CLOCK_REALTIME
64# define EV_USE_REALTIME 0
65#endif
60#ifndef HAVE_REALTIME 66#ifndef EV_USE_REALTIME
61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 67# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */
62#endif 68#endif
63 69
64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 70#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
65#define MAX_BLOCKTIME 60. 71#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ 72#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
73#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
67 74
68#include "ev.h" 75#include "ev.h"
69 76
70typedef struct ev_watcher *W; 77typedef struct ev_watcher *W;
71typedef struct ev_watcher_list *WL; 78typedef struct ev_watcher_list *WL;
84/*****************************************************************************/ 91/*****************************************************************************/
85 92
86ev_tstamp 93ev_tstamp
87ev_time (void) 94ev_time (void)
88{ 95{
89#if HAVE_REALTIME 96#if EV_USE_REALTIME
90 struct timespec ts; 97 struct timespec ts;
91 clock_gettime (CLOCK_REALTIME, &ts); 98 clock_gettime (CLOCK_REALTIME, &ts);
92 return ts.tv_sec + ts.tv_nsec * 1e-9; 99 return ts.tv_sec + ts.tv_nsec * 1e-9;
93#else 100#else
94 struct timeval tv; 101 struct timeval tv;
98} 105}
99 106
100static ev_tstamp 107static ev_tstamp
101get_clock (void) 108get_clock (void)
102{ 109{
103#if HAVE_MONOTONIC 110#if EV_USE_MONOTONIC
104 if (have_monotonic) 111 if (have_monotonic)
105 { 112 {
106 struct timespec ts; 113 struct timespec ts;
107 clock_gettime (CLOCK_MONOTONIC, &ts); 114 clock_gettime (CLOCK_MONOTONIC, &ts);
108 return ts.tv_sec + ts.tv_nsec * 1e-9; 115 return ts.tv_sec + ts.tv_nsec * 1e-9;
109 } 116 }
110#endif 117#endif
111 118
112 return ev_time (); 119 return ev_time ();
113} 120}
121
122#define array_roundsize(base,n) ((n) | 4 & ~3)
114 123
115#define array_needsize(base,cur,cnt,init) \ 124#define array_needsize(base,cur,cnt,init) \
116 if ((cnt) > cur) \ 125 if ((cnt) > cur) \
117 { \ 126 { \
118 int newcnt = cur; \ 127 int newcnt = cur; \
119 do \ 128 do \
120 { \ 129 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \ 130 newcnt = array_roundsize (base, newcnt << 1); \
122 } \ 131 } \
123 while ((cnt) > newcnt); \ 132 while ((cnt) > newcnt); \
124 \ 133 \
125 base = realloc (base, sizeof (*base) * (newcnt)); \ 134 base = realloc (base, sizeof (*base) * (newcnt)); \
126 init (base + cur, newcnt - cur); \ 135 init (base + cur, newcnt - cur); \
159static int pendingmax, pendingcnt; 168static int pendingmax, pendingcnt;
160 169
161static void 170static void
162event (W w, int events) 171event (W w, int events)
163{ 172{
164 if (w->active) 173 if (w->pending)
174 {
175 pendings [w->pending - 1].events |= events;
176 return;
165 { 177 }
178
166 w->pending = ++pendingcnt; 179 w->pending = ++pendingcnt;
167 array_needsize (pendings, pendingmax, pendingcnt, ); 180 array_needsize (pendings, pendingmax, pendingcnt, );
168 pendings [pendingcnt - 1].w = w; 181 pendings [pendingcnt - 1].w = w;
169 pendings [pendingcnt - 1].events = events; 182 pendings [pendingcnt - 1].events = events;
170 }
171} 183}
172 184
173static void 185static void
174queue_events (W *events, int eventcnt, int type) 186queue_events (W *events, int eventcnt, int type)
175{ 187{
228} 240}
229 241
230static void 242static void
231fd_change (int fd) 243fd_change (int fd)
232{ 244{
233 if (anfds [fd].events & EV_REIFY) 245 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0)
234 return; 246 return;
235 247
236 anfds [fd].events |= EV_REIFY; 248 anfds [fd].events |= EV_REIFY;
237 249
238 ++fdchangecnt; 250 ++fdchangecnt;
249 for (fd = 0; fd < anfdmax; ++fd) 261 for (fd = 0; fd < anfdmax; ++fd)
250 if (anfds [fd].events) 262 if (anfds [fd].events)
251 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 263 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
252 while (anfds [fd].head) 264 while (anfds [fd].head)
253 { 265 {
254 event ((W)anfds [fd].head, EV_ERROR);
255 ev_io_stop (anfds [fd].head); 266 ev_io_stop (anfds [fd].head);
267 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT);
256 } 268 }
257} 269}
258 270
259/*****************************************************************************/ 271/*****************************************************************************/
260 272
411 } 423 }
412} 424}
413 425
414/*****************************************************************************/ 426/*****************************************************************************/
415 427
416#if HAVE_EPOLL 428#if EV_USE_EPOLL
417# include "ev_epoll.c" 429# include "ev_epoll.c"
418#endif 430#endif
419#if HAVE_SELECT 431#if EV_USE_SELECT
420# include "ev_select.c" 432# include "ev_select.c"
421#endif 433#endif
422 434
423int 435int
424ev_version_major (void) 436ev_version_major (void)
434 446
435int ev_init (int flags) 447int ev_init (int flags)
436{ 448{
437 if (!ev_method) 449 if (!ev_method)
438 { 450 {
439#if HAVE_MONOTONIC 451#if EV_USE_MONOTONIC
440 { 452 {
441 struct timespec ts; 453 struct timespec ts;
442 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 454 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
443 have_monotonic = 1; 455 have_monotonic = 1;
444 } 456 }
450 462
451 if (pipe (sigpipe)) 463 if (pipe (sigpipe))
452 return 0; 464 return 0;
453 465
454 ev_method = EVMETHOD_NONE; 466 ev_method = EVMETHOD_NONE;
455#if HAVE_EPOLL 467#if EV_USE_EPOLL
456 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 468 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
457#endif 469#endif
458#if HAVE_SELECT 470#if EV_USE_SELECT
459 if (ev_method == EVMETHOD_NONE) select_init (flags); 471 if (ev_method == EVMETHOD_NONE) select_init (flags);
460#endif 472#endif
461 473
462 if (ev_method) 474 if (ev_method)
463 { 475 {
487} 499}
488 500
489void 501void
490ev_postfork_child (void) 502ev_postfork_child (void)
491{ 503{
492#if HAVE_EPOLL 504#if EV_USE_EPOLL
493 if (ev_method == EVMETHOD_EPOLL) 505 if (ev_method == EVMETHOD_EPOLL)
494 epoll_postfork_child (); 506 epoll_postfork_child ();
495#endif 507#endif
496 508
497 ev_io_stop (&sigev); 509 ev_io_stop (&sigev);
523{ 535{
524 while (timercnt && timers [0]->at <= now) 536 while (timercnt && timers [0]->at <= now)
525 { 537 {
526 struct ev_timer *w = timers [0]; 538 struct ev_timer *w = timers [0];
527 539
528 event ((W)w, EV_TIMEOUT);
529
530 /* first reschedule or stop timer */ 540 /* first reschedule or stop timer */
531 if (w->repeat) 541 if (w->repeat)
532 { 542 {
533 w->at = now + w->repeat; 543 w->at = now + w->repeat;
534 assert (("timer timeout in the past, negative repeat?", w->at > now)); 544 assert (("timer timeout in the past, negative repeat?", w->at > now));
535 downheap ((WT *)timers, timercnt, 0); 545 downheap ((WT *)timers, timercnt, 0);
536 } 546 }
537 else 547 else
538 ev_timer_stop (w); /* nonrepeating: stop timer */ 548 ev_timer_stop (w); /* nonrepeating: stop timer */
549
550 event ((W)w, EV_TIMEOUT);
539 } 551 }
540} 552}
541 553
542static void 554static void
543periodics_reify (void) 555periodics_reify (void)
1017ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1029ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1018{ 1030{
1019 struct ev_once *once = malloc (sizeof (struct ev_once)); 1031 struct ev_once *once = malloc (sizeof (struct ev_once));
1020 1032
1021 if (!once) 1033 if (!once)
1022 cb (EV_ERROR, arg); 1034 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1023 else 1035 else
1024 { 1036 {
1025 once->cb = cb; 1037 once->cb = cb;
1026 once->arg = arg; 1038 once->arg = arg;
1027 1039
1028 ev_watcher_init (&once->io, once_cb_io); 1040 ev_watcher_init (&once->io, once_cb_io);
1029
1030 if (fd >= 0) 1041 if (fd >= 0)
1031 { 1042 {
1032 ev_io_set (&once->io, fd, events); 1043 ev_io_set (&once->io, fd, events);
1033 ev_io_start (&once->io); 1044 ev_io_start (&once->io);
1034 } 1045 }
1035 1046
1036 ev_watcher_init (&once->to, once_cb_to); 1047 ev_watcher_init (&once->to, once_cb_to);
1037
1038 if (timeout >= 0.) 1048 if (timeout >= 0.)
1039 { 1049 {
1040 ev_timer_set (&once->to, timeout, 0.); 1050 ev_timer_set (&once->to, timeout, 0.);
1041 ev_timer_start (&once->to); 1051 ev_timer_start (&once->to);
1042 } 1052 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines