ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/event.c
Revision: 1.13
Committed: Sun Nov 4 18:29:44 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.12: +4 -3 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * libevent compatibility layer
3 *
4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <stddef.h>
33 #include <stdlib.h>
34 #include <sys/time.h>
35 #include <assert.h>
36
37 #include "ev.h"
38 #include "event.h"
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
44 # define dLOOPev
45 # define dLOOPbase
46 #endif
47
48 /* never accessed, will always be cast from/to ev_loop */
49 struct event_base
50 {
51 int dummy;
52 };
53
54 static struct event_base *x_cur;
55
56 static void
57 tv_set (struct timeval *tv, ev_tstamp at)
58 {
59 tv->tv_sec = (long)at;
60 tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6);
61 }
62
63 static ev_tstamp
64 tv_get (struct timeval *tv)
65 {
66 if (tv)
67 return tv->tv_sec + tv->tv_usec * 1e-6;
68 else
69 return -1.;
70 }
71
72 #define EVENT_VERSION(a,b) # a "." # b
73
74 const char *event_get_version (void)
75 {
76 return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR);
77 }
78
79 const char *event_get_method (void)
80 {
81 return "libev";
82 }
83
84 void *event_init (void)
85 {
86 #if EV_MULTIPLICITY
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));
93
94 x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO);
95 #endif
96
97 return x_cur;
98 }
99
100 void event_base_free (struct event_base *base)
101 {
102 dLOOPbase;
103
104 #if EV_MULTIPLICITY
105 if (ev_default_loop (EVMETHOD_AUTO) != loop)
106 ev_loop_destroy (loop);
107 #endif
108 }
109
110 int event_dispatch (void)
111 {
112 return event_base_dispatch (x_cur);
113 }
114
115 #ifdef EV_STANDALONE
116 void event_set_log_callback (event_log_cb cb)
117 {
118 /* nop */
119 }
120 #endif
121
122 int event_loop (int flags)
123 {
124 return event_base_loop (x_cur, flags);
125 }
126
127 int event_loopexit (struct timeval *tv)
128 {
129 return event_base_loopexit (x_cur, tv);
130 }
131
132 static void
133 x_cb (struct event *ev, int revents)
134 {
135 revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL;
136
137 ev->ev_res = revents;
138 ev->ev_callback (ev->ev_fd, revents, ev->ev_arg);
139 }
140
141 static void
142 x_cb_sig (EV_P_ struct ev_signal *w, int revents)
143 {
144 x_cb ((struct event *)(((char *)w) - offsetof (struct event, iosig.sig)), revents);
145 }
146
147 static void
148 x_cb_io (EV_P_ struct ev_io *w, int revents)
149 {
150 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io));
151
152 if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w))
153 ev_io_stop (EV_A_ w);
154
155 x_cb (ev, revents);
156 }
157
158 static void
159 x_cb_to (EV_P_ struct ev_timer *w, int revents)
160 {
161 x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents);
162 }
163
164 void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
165 {
166 if (!ev->initialised)
167 {
168 ev->initialised = 1;
169
170 if (events & EV_SIGNAL)
171 ev_watcher_init (&ev->iosig.sig, x_cb_sig);
172 else
173 ev_watcher_init (&ev->iosig.io, x_cb_io);
174
175 ev_watcher_init (&ev->to, x_cb_to);
176 }
177
178 ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */
179 ev->ev_fd = fd;
180 ev->ev_events = events;
181 ev->ev_pri = 0;
182 ev->ev_callback = cb;
183 ev->ev_arg = arg;
184 ev->ev_res = 0;
185 }
186
187 int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
188 {
189 return event_base_once (x_cur, fd, events, cb, arg, tv);
190 }
191
192 int event_add (struct event *ev, struct timeval *tv)
193 {
194 dLOOPev;
195
196 /* disable all watchers */
197 event_del (ev);
198
199 if (ev->ev_events & EV_SIGNAL)
200 {
201 ev_signal_set (&ev->iosig.sig, ev->ev_fd);
202 ev_signal_start (EV_A_ &ev->iosig.sig);
203 }
204 else if (ev->ev_events & (EV_READ | EV_WRITE))
205 {
206 ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE));
207 ev_io_start (EV_A_ &ev->iosig.io);
208 }
209
210 if (tv)
211 {
212 ev_timer_set (&ev->to, tv_get (tv), 0.);
213 ev_timer_start (EV_A_ &ev->to);
214 }
215
216 return 0;
217 }
218
219 int event_del (struct event *ev)
220 {
221 dLOOPev;
222
223 if (ev->ev_events & EV_SIGNAL)
224 {
225 /* sig */
226 if (ev_is_active (&ev->iosig.sig))
227 ev_signal_stop (EV_A_ &ev->iosig.sig);
228 }
229 else
230 {
231 /* io */
232 if (ev_is_active (&ev->iosig.io))
233 ev_io_stop (EV_A_ &ev->iosig.io);
234 }
235
236 if (ev_is_active (&ev->to))
237 ev_timer_stop (EV_A_ &ev->to);
238
239 return 0;
240 }
241
242 int event_pending (struct event *ev, short events, struct timeval *tv)
243 {
244 dLOOPev;
245
246 short revents = 0;
247
248 if (ev->ev_events & EV_SIGNAL)
249 {
250 /* sig */
251 if (ev->iosig.sig.pending)
252 revents |= EV_SIGNAL;
253 }
254 else
255 {
256 /* io */
257 if (ev->iosig.io.pending)
258 revents |= ev->ev_events & (EV_READ | EV_WRITE);
259 }
260
261 if (ev->to.pending)
262 {
263 revents |= EV_TIMEOUT;
264
265 if (tv)
266 tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */
267 }
268
269 return events & revents;
270 }
271
272 int event_priority_init (int npri)
273 {
274 return event_base_priority_init (x_cur, npri);
275 }
276
277 int event_priority_set (struct event *ev, int pri)
278 {
279 ev->ev_pri = pri;
280
281 return 0;
282 }
283
284 int event_base_set (struct event_base *base, struct event *ev)
285 {
286 ev->ev_base = base;
287
288 return 0;
289 }
290
291 int event_base_loop (struct event_base *base, int flags)
292 {
293 dLOOPbase;
294
295 ev_loop (EV_A_ flags);
296
297 return 0;
298 }
299
300 int event_base_dispatch (struct event_base *base)
301 {
302 return event_base_loop (base, 0);
303 }
304
305 static void
306 x_loopexit_cb (int revents, void *base)
307 {
308 dLOOPbase;
309
310 ev_unloop (EV_A_ EVUNLOOP_ONCE);
311 }
312
313 int event_base_loopexit (struct event_base *base, struct timeval *tv)
314 {
315 dLOOPbase;
316 ev_tstamp after = tv_get (tv);
317
318 ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base);
319
320 return -1;
321 }
322
323 struct x_once
324 {
325 int fd;
326 void (*cb)(int, short, void *);
327 void *arg;
328 };
329
330 static void
331 x_once_cb (int revents, void *arg)
332 {
333 struct x_once *once = arg;
334
335 once->cb (once->fd, revents, once->arg);
336 free (once);
337 }
338
339 int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
340 {
341 dLOOPbase;
342 struct x_once *once = malloc (sizeof (struct x_once));
343
344 if (!once)
345 return -1;
346
347 once->fd = fd;
348 once->cb = cb;
349 once->arg = arg;
350
351 ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once);
352
353 return 0;
354 }
355
356 int event_base_priority_init (struct event_base *base, int npri)
357 {
358 /*dLOOPbase;*/
359
360 return 0;
361 }
362