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

Comparing libev/event.c (file contents):
Revision 1.7 by root, Sat Nov 3 21:58:51 2007 UTC vs.
Revision 1.13 by root, Sun Nov 4 18:29:44 2007 UTC

30 */ 30 */
31 31
32#include <stddef.h> 32#include <stddef.h>
33#include <stdlib.h> 33#include <stdlib.h>
34#include <sys/time.h> 34#include <sys/time.h>
35#include <assert.h>
35 36
37#include "ev.h"
36#include "event.h" 38#include "event.h"
37 39
40#if EV_MULTIPLICITY
41# define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base
42# define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base
43#else
38#define dLOOPev 44# define dLOOPev
39#define dLOOPbase 45# define dLOOPbase
46#endif
40 47
48/* never accessed, will always be cast from/to ev_loop */
41struct event_base 49struct event_base
42{ 50{
43 int dummy; 51 int dummy;
44}; 52};
45 53
46static struct event_base x_base, *x_cur; 54static struct event_base *x_cur;
47 55
48static void 56static void
49tv_set (struct timeval *tv, ev_tstamp at) 57tv_set (struct timeval *tv, ev_tstamp at)
50{ 58{
51 tv->tv_sec = (long)at; 59 tv->tv_sec = (long)at;
73 return "libev"; 81 return "libev";
74} 82}
75 83
76void *event_init (void) 84void *event_init (void)
77{ 85{
78 if (!x_cur && ev_init (0)) 86#if EV_MULTIPLICITY
79 return x_cur = &x_base; 87 if (x_cur)
88 x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO);
89 else
90 x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO);
91#else
92 assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur));
80 93
94 x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO);
95#endif
96
81 return 0; 97 return x_cur;
82} 98}
83 99
84void event_base_free (struct event_base *base) 100void event_base_free (struct event_base *base)
85{ 101{
86 /* nop */ 102 dLOOPbase;
103
104#if EV_MULTIPLICITY
105 if (ev_default_loop (EVMETHOD_AUTO) != loop)
106 ev_loop_destroy (loop);
107#endif
87} 108}
88 109
89int event_dispatch (void) 110int event_dispatch (void)
90{ 111{
91 return event_base_dispatch (x_cur); 112 return event_base_dispatch (x_cur);
127x_cb_io (EV_P_ struct ev_io *w, int revents) 148x_cb_io (EV_P_ struct ev_io *w, int revents)
128{ 149{
129 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); 150 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io));
130 151
131 if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) 152 if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w))
132 ev_io_stop (w); 153 ev_io_stop (EV_A_ w);
133 154
134 x_cb (ev, revents); 155 x_cb (ev, revents);
135} 156}
136 157
137static void 158static void
140 x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents); 161 x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents);
141} 162}
142 163
143void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) 164void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
144{ 165{
166 if (!ev->initialised)
167 {
168 ev->initialised = 1;
169
145 if (events & EV_SIGNAL) 170 if (events & EV_SIGNAL)
146 ev_watcher_init (&ev->iosig.sig, x_cb_sig); 171 ev_watcher_init (&ev->iosig.sig, x_cb_sig);
147 else 172 else
148 ev_watcher_init (&ev->iosig.io, x_cb_io); 173 ev_watcher_init (&ev->iosig.io, x_cb_io);
149 174
150 ev_watcher_init (&ev->to, x_cb_to); 175 ev_watcher_init (&ev->to, x_cb_to);
176 }
151 177
152 ev->ev_base = x_cur; 178 ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */
153 ev->ev_fd = fd; 179 ev->ev_fd = fd;
154 ev->ev_events = events; 180 ev->ev_events = events;
155 ev->ev_pri = 0; 181 ev->ev_pri = 0;
156 ev->ev_callback = cb; 182 ev->ev_callback = cb;
157 ev->ev_arg = arg; 183 ev->ev_arg = arg;
263} 289}
264 290
265int event_base_loop (struct event_base *base, int flags) 291int event_base_loop (struct event_base *base, int flags)
266{ 292{
267 dLOOPbase; 293 dLOOPbase;
268 ev_loop (EV_A_ flags | EVLOOP_ONESHOT); 294
295 ev_loop (EV_A_ flags);
269 296
270 return 0; 297 return 0;
271} 298}
272 299
273int event_base_dispatch (struct event_base *base) 300int event_base_dispatch (struct event_base *base)
274{ 301{
275 return event_base_loop (base, 0); 302 return event_base_loop (base, 0);
276} 303}
277 304
278static void 305static void
279x_loopexit_cb (EV_P_ int revents, void *arg) 306x_loopexit_cb (int revents, void *base)
280{ 307{
308 dLOOPbase;
309
281 ev_unloop (EV_A_ 2); 310 ev_unloop (EV_A_ EVUNLOOP_ONCE);
282} 311}
283 312
284int event_base_loopexit (struct event_base *base, struct timeval *tv) 313int event_base_loopexit (struct event_base *base, struct timeval *tv)
285{ 314{
286 dLOOPbase; 315 dLOOPbase;
324 return 0; 353 return 0;
325} 354}
326 355
327int event_base_priority_init (struct event_base *base, int npri) 356int event_base_priority_init (struct event_base *base, int npri)
328{ 357{
329 dLOOPbase; 358 /*dLOOPbase;*/
330 359
331 return 0; 360 return 0;
332} 361}
333 362

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines