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.11 by root, Sun Nov 4 16:52:52 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#ifndef EV_EMBED
36#include "event.h" 38# include "event.h"
39#endif
37 40
38#ifdef EV_MULTIPLICITY 41#if EV_MULTIPLICITY
39# define dLOOPev struct ev_loop *loop = ev->ev_base->loop 42# define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base
40# define dLOOPbase struct ev_loop *loop = base->loop 43# define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base
41#else 44#else
42# define dLOOPev 45# define dLOOPev
43# define dLOOPbase 46# define dLOOPbase
44#endif 47#endif
45 48
49/* never accessed, will always be cast from/to ev_loop */
46struct event_base 50struct event_base
47{ 51{
48#ifdef EV_MULTIPLICITY
49 struct ev_loop *loop;
50#endif
51 int dummy; 52 int dummy;
52}; 53};
53 54
54static struct event_base x_base, *x_cur; 55static struct event_base *x_cur;
55 56
56static void 57static void
57tv_set (struct timeval *tv, ev_tstamp at) 58tv_set (struct timeval *tv, ev_tstamp at)
58{ 59{
59 tv->tv_sec = (long)at; 60 tv->tv_sec = (long)at;
81 return "libev"; 82 return "libev";
82} 83}
83 84
84void *event_init (void) 85void *event_init (void)
85{ 86{
86 if (!x_cur && ev_init (0)) 87#if EV_MULTIPLICITY
87 return x_cur = &x_base; 88 if (x_cur)
89 x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO);
90 else
91 x_cur = ev_default_loop (EVMETHOD_AUTO);
92#else
93 assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur));
88 94
95 x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO);
96#endif
97
89 return 0; 98 return x_cur;
90} 99}
91 100
92void event_base_free (struct event_base *base) 101void event_base_free (struct event_base *base)
93{ 102{
94 dLOOPbase; 103 dLOOPbase;
95 104
96 /* nop */ 105#if EV_MULTIPLICITY
106 ev_loop_delete (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);
102} 113}
103 114
104#ifdef EV_STANDALONE 115#ifdef EV_EMBED
105void event_set_log_callback (event_log_cb cb) 116void event_set_log_callback (event_log_cb cb)
106{ 117{
107 /* nop */ 118 /* nop */
108} 119}
109#endif 120#endif
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;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines