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

Comparing libev/event.c (file contents):
Revision 1.8 by root, Sat Nov 3 22:10:39 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
38#ifdef EV_MULTIPLICITY 40#if EV_MULTIPLICITY
39# define dLOOPev struct ev_loop *loop = ev->ev_base->loop 41# define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base
40# define dLOOPbase struct ev_loop *loop = base->loop 42# define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base
41#else 43#else
42# define dLOOPev 44# define dLOOPev
43# define dLOOPbase 45# define dLOOPbase
44#endif 46#endif
45 47
48/* never accessed, will always be cast from/to ev_loop */
46struct event_base 49struct event_base
47{ 50{
48#ifdef EV_MULTIPLICITY
49 struct ev_loop *loop;
50#endif
51 int dummy; 51 int dummy;
52}; 52};
53 53
54static struct event_base x_base, *x_cur; 54static struct event_base *x_cur;
55 55
56static void 56static void
57tv_set (struct timeval *tv, ev_tstamp at) 57tv_set (struct timeval *tv, ev_tstamp at)
58{ 58{
59 tv->tv_sec = (long)at; 59 tv->tv_sec = (long)at;
81 return "libev"; 81 return "libev";
82} 82}
83 83
84void *event_init (void) 84void *event_init (void)
85{ 85{
86 if (!x_cur && ev_init (0)) 86#if EV_MULTIPLICITY
87 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));
88 93
94 x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO);
95#endif
96
89 return 0; 97 return x_cur;
90} 98}
91 99
92void event_base_free (struct event_base *base) 100void event_base_free (struct event_base *base)
93{ 101{
94 dLOOPbase; 102 dLOOPbase;
95 103
96 /* nop */ 104#if EV_MULTIPLICITY
105 if (ev_default_loop (EVMETHOD_AUTO) != loop)
106 ev_loop_destroy (loop);
107#endif
97} 108}
98 109
99int event_dispatch (void) 110int event_dispatch (void)
100{ 111{
101 return event_base_dispatch (x_cur); 112 return event_base_dispatch (x_cur);
137x_cb_io (EV_P_ struct ev_io *w, int revents) 148x_cb_io (EV_P_ struct ev_io *w, int revents)
138{ 149{
139 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));
140 151
141 if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) 152 if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w))
142 ev_io_stop (w); 153 ev_io_stop (EV_A_ w);
143 154
144 x_cb (ev, revents); 155 x_cb (ev, revents);
145} 156}
146 157
147static void 158static void
150 x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents); 161 x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents);
151} 162}
152 163
153void 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)
154{ 165{
166 if (!ev->initialised)
167 {
168 ev->initialised = 1;
169
155 if (events & EV_SIGNAL) 170 if (events & EV_SIGNAL)
156 ev_watcher_init (&ev->iosig.sig, x_cb_sig); 171 ev_watcher_init (&ev->iosig.sig, x_cb_sig);
157 else 172 else
158 ev_watcher_init (&ev->iosig.io, x_cb_io); 173 ev_watcher_init (&ev->iosig.io, x_cb_io);
159 174
160 ev_watcher_init (&ev->to, x_cb_to); 175 ev_watcher_init (&ev->to, x_cb_to);
176 }
161 177
162 ev->ev_base = x_cur; 178 ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */
163 ev->ev_fd = fd; 179 ev->ev_fd = fd;
164 ev->ev_events = events; 180 ev->ev_events = events;
165 ev->ev_pri = 0; 181 ev->ev_pri = 0;
166 ev->ev_callback = cb; 182 ev->ev_callback = cb;
167 ev->ev_arg = arg; 183 ev->ev_arg = arg;
285{ 301{
286 return event_base_loop (base, 0); 302 return event_base_loop (base, 0);
287} 303}
288 304
289static void 305static void
290x_loopexit_cb (EV_P_ int revents, void *arg) 306x_loopexit_cb (int revents, void *base)
291{ 307{
308 dLOOPbase;
309
292 ev_unloop (EV_A_ 2); 310 ev_unloop (EV_A_ EVUNLOOP_ONCE);
293} 311}
294 312
295int event_base_loopexit (struct event_base *base, struct timeval *tv) 313int event_base_loopexit (struct event_base *base, struct timeval *tv)
296{ 314{
297 dLOOPbase; 315 dLOOPbase;
335 return 0; 353 return 0;
336} 354}
337 355
338int event_base_priority_init (struct event_base *base, int npri) 356int event_base_priority_init (struct event_base *base, int npri)
339{ 357{
340 dLOOPbase; 358 /*dLOOPbase;*/
341 359
342 return 0; 360 return 0;
343} 361}
344 362

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines