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

Comparing libev/ev.c (file contents):
Revision 1.182 by root, Wed Dec 12 01:27:08 2007 UTC vs.
Revision 1.192 by root, Fri Dec 21 07:55:29 2007 UTC

202#ifndef CLOCK_REALTIME 202#ifndef CLOCK_REALTIME
203# undef EV_USE_REALTIME 203# undef EV_USE_REALTIME
204# define EV_USE_REALTIME 0 204# define EV_USE_REALTIME 0
205#endif 205#endif
206 206
207#if !EV_STAT_ENABLE
208# undef EV_USE_INOTIFY
209# define EV_USE_INOTIFY 0
210#endif
211
212#if EV_USE_INOTIFY
213# include <sys/inotify.h>
214#endif
215
207#if EV_SELECT_IS_WINSOCKET 216#if EV_SELECT_IS_WINSOCKET
208# include <winsock.h> 217# include <winsock.h>
209#endif
210
211#if !EV_STAT_ENABLE
212# define EV_USE_INOTIFY 0
213#endif
214
215#if EV_USE_INOTIFY
216# include <sys/inotify.h>
217#endif 218#endif
218 219
219/**/ 220/**/
220 221
221/* 222/*
230 231
231#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 232#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
232#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 233#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
233/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */ 234/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */
234 235
235#if __GNUC__ >= 3 236#if __GNUC__ >= 4
236# define expect(expr,value) __builtin_expect ((expr),(value)) 237# define expect(expr,value) __builtin_expect ((expr),(value))
237# define noinline __attribute__ ((noinline)) 238# define noinline __attribute__ ((noinline))
238#else 239#else
239# define expect(expr,value) (expr) 240# define expect(expr,value) (expr)
240# define noinline 241# define noinline
533 { 534 {
534 int fd = fdchanges [i]; 535 int fd = fdchanges [i];
535 ANFD *anfd = anfds + fd; 536 ANFD *anfd = anfds + fd;
536 ev_io *w; 537 ev_io *w;
537 538
538 int events = 0; 539 unsigned char events = 0;
539 540
540 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) 541 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
541 events |= w->events; 542 events |= (unsigned char)w->events;
542 543
543#if EV_SELECT_IS_WINSOCKET 544#if EV_SELECT_IS_WINSOCKET
544 if (events) 545 if (events)
545 { 546 {
546 unsigned long argp; 547 unsigned long argp;
547 anfd->handle = _get_osfhandle (fd); 548 anfd->handle = _get_osfhandle (fd);
548 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0)); 549 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
549 } 550 }
550#endif 551#endif
551 552
553 {
554 unsigned char o_events = anfd->events;
555 unsigned char o_reify = anfd->reify;
556
552 anfd->reify = 0; 557 anfd->reify = 0;
553
554 backend_modify (EV_A_ fd, anfd->events, events);
555 anfd->events = events; 558 anfd->events = events;
559
560 if (o_events != events || o_reify & EV_IOFDSET)
561 backend_modify (EV_A_ fd, o_events, events);
562 }
556 } 563 }
557 564
558 fdchangecnt = 0; 565 fdchangecnt = 0;
559} 566}
560 567
561void inline_size 568void inline_size
562fd_change (EV_P_ int fd) 569fd_change (EV_P_ int fd, int flags)
563{ 570{
564 if (expect_false (anfds [fd].reify)) 571 unsigned char reify = anfds [fd].reify;
565 return;
566
567 anfds [fd].reify = 1; 572 anfds [fd].reify |= flags;
568 573
574 if (expect_true (!reify))
575 {
569 ++fdchangecnt; 576 ++fdchangecnt;
570 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 577 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
571 fdchanges [fdchangecnt - 1] = fd; 578 fdchanges [fdchangecnt - 1] = fd;
579 }
572} 580}
573 581
574void inline_speed 582void inline_speed
575fd_kill (EV_P_ int fd) 583fd_kill (EV_P_ int fd)
576{ 584{
627 635
628 for (fd = 0; fd < anfdmax; ++fd) 636 for (fd = 0; fd < anfdmax; ++fd)
629 if (anfds [fd].events) 637 if (anfds [fd].events)
630 { 638 {
631 anfds [fd].events = 0; 639 anfds [fd].events = 0;
632 fd_change (EV_A_ fd); 640 fd_change (EV_A_ fd, EV_IOFDSET | 1);
633 } 641 }
634} 642}
635 643
636/*****************************************************************************/ 644/*****************************************************************************/
637 645
917} 925}
918 926
919unsigned int 927unsigned int
920ev_embeddable_backends (void) 928ev_embeddable_backends (void)
921{ 929{
930 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
922 return EVBACKEND_EPOLL 931 return EVBACKEND_KQUEUE
923 | EVBACKEND_KQUEUE
924 | EVBACKEND_PORT; 932 | EVBACKEND_PORT;
925} 933}
926 934
927unsigned int 935unsigned int
928ev_backend (EV_P) 936ev_backend (EV_P)
1030#if EV_IDLE_ENABLE 1038#if EV_IDLE_ENABLE
1031 array_free (idle, [i]); 1039 array_free (idle, [i]);
1032#endif 1040#endif
1033 } 1041 }
1034 1042
1043 ev_free (anfds); anfdmax = 0;
1044
1035 /* have to use the microsoft-never-gets-it-right macro */ 1045 /* have to use the microsoft-never-gets-it-right macro */
1036 array_free (fdchange, EMPTY); 1046 array_free (fdchange, EMPTY);
1037 array_free (timer, EMPTY); 1047 array_free (timer, EMPTY);
1038#if EV_PERIODIC_ENABLE 1048#if EV_PERIODIC_ENABLE
1039 array_free (periodic, EMPTY); 1049 array_free (periodic, EMPTY);
1050#endif
1051#if EV_FORK_ENABLE
1052 array_free (fork, EMPTY);
1040#endif 1053#endif
1041 array_free (prepare, EMPTY); 1054 array_free (prepare, EMPTY);
1042 array_free (check, EMPTY); 1055 array_free (check, EMPTY);
1043 1056
1044 backend = 0; 1057 backend = 0;
1599 1612
1600 ev_start (EV_A_ (W)w, 1); 1613 ev_start (EV_A_ (W)w, 1);
1601 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); 1614 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1602 wlist_add (&anfds[fd].head, (WL)w); 1615 wlist_add (&anfds[fd].head, (WL)w);
1603 1616
1604 fd_change (EV_A_ fd); 1617 fd_change (EV_A_ fd, w->events & EV_IOFDSET | 1);
1618 w->events &= ~EV_IOFDSET;
1605} 1619}
1606 1620
1607void noinline 1621void noinline
1608ev_io_stop (EV_P_ ev_io *w) 1622ev_io_stop (EV_P_ ev_io *w)
1609{ 1623{
1614 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1628 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1615 1629
1616 wlist_del (&anfds[w->fd].head, (WL)w); 1630 wlist_del (&anfds[w->fd].head, (WL)w);
1617 ev_stop (EV_A_ (W)w); 1631 ev_stop (EV_A_ (W)w);
1618 1632
1619 fd_change (EV_A_ w->fd); 1633 fd_change (EV_A_ w->fd, 1);
1620} 1634}
1621 1635
1622void noinline 1636void noinline
1623ev_timer_start (EV_P_ ev_timer *w) 1637ev_timer_start (EV_P_ ev_timer *w)
1624{ 1638{
2168 2182
2169#if EV_EMBED_ENABLE 2183#if EV_EMBED_ENABLE
2170void noinline 2184void noinline
2171ev_embed_sweep (EV_P_ ev_embed *w) 2185ev_embed_sweep (EV_P_ ev_embed *w)
2172{ 2186{
2173 ev_loop (w->loop, EVLOOP_NONBLOCK); 2187 ev_loop (w->other, EVLOOP_NONBLOCK);
2174} 2188}
2175 2189
2176static void 2190static void
2177embed_cb (EV_P_ ev_io *io, int revents) 2191embed_io_cb (EV_P_ ev_io *io, int revents)
2178{ 2192{
2179 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); 2193 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
2180 2194
2181 if (ev_cb (w)) 2195 if (ev_cb (w))
2182 ev_feed_event (EV_A_ (W)w, EV_EMBED); 2196 ev_feed_event (EV_A_ (W)w, EV_EMBED);
2183 else 2197 else
2184 ev_embed_sweep (loop, w); 2198 ev_embed_sweep (loop, w);
2185} 2199}
2186 2200
2201static void
2202embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents)
2203{
2204 ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare));
2205
2206 fd_reify (w->other);
2207}
2208
2187void 2209void
2188ev_embed_start (EV_P_ ev_embed *w) 2210ev_embed_start (EV_P_ ev_embed *w)
2189{ 2211{
2190 if (expect_false (ev_is_active (w))) 2212 if (expect_false (ev_is_active (w)))
2191 return; 2213 return;
2192 2214
2193 { 2215 {
2194 struct ev_loop *loop = w->loop; 2216 struct ev_loop *loop = w->other;
2195 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); 2217 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
2196 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); 2218 ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ);
2197 } 2219 }
2198 2220
2199 ev_set_priority (&w->io, ev_priority (w)); 2221 ev_set_priority (&w->io, ev_priority (w));
2200 ev_io_start (EV_A_ &w->io); 2222 ev_io_start (EV_A_ &w->io);
2201 2223
2224 ev_prepare_init (&w->prepare, embed_prepare_cb);
2225 ev_set_priority (&w->prepare, EV_MINPRI);
2226 ev_prepare_start (EV_A_ &w->prepare);
2227
2202 ev_start (EV_A_ (W)w, 1); 2228 ev_start (EV_A_ (W)w, 1);
2203} 2229}
2204 2230
2205void 2231void
2206ev_embed_stop (EV_P_ ev_embed *w) 2232ev_embed_stop (EV_P_ ev_embed *w)
2208 clear_pending (EV_A_ (W)w); 2234 clear_pending (EV_A_ (W)w);
2209 if (expect_false (!ev_is_active (w))) 2235 if (expect_false (!ev_is_active (w)))
2210 return; 2236 return;
2211 2237
2212 ev_io_stop (EV_A_ &w->io); 2238 ev_io_stop (EV_A_ &w->io);
2239 ev_prepare_stop (EV_A_ &w->prepare);
2213 2240
2214 ev_stop (EV_A_ (W)w); 2241 ev_stop (EV_A_ (W)w);
2215} 2242}
2216#endif 2243#endif
2217 2244
2306 ev_timer_set (&once->to, timeout, 0.); 2333 ev_timer_set (&once->to, timeout, 0.);
2307 ev_timer_start (EV_A_ &once->to); 2334 ev_timer_start (EV_A_ &once->to);
2308 } 2335 }
2309} 2336}
2310 2337
2338#if EV_MULTIPLICITY
2339 #include "ev_wrap.h"
2340#endif
2341
2311#ifdef __cplusplus 2342#ifdef __cplusplus
2312} 2343}
2313#endif 2344#endif
2314 2345

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines