ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/event.c
Revision: 1.38
Committed: Mon Nov 17 03:37:08 2008 UTC (15 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_51, rel-3_49
Changes since 1.37: +0 -4 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * libevent compatibility layer
3 *
4 * Copyright (c) 2007,2008 Marc Alexander Lehmann <libev@schmorp.de>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
25 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26 * OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU General Public License ("GPL") version 2 or any later version,
30 * in which case the provisions of the GPL are applicable instead of
31 * the above. If you wish to allow the use of your version of this file
32 * only under the terms of the GPL and not to allow others to use your
33 * version of this file under the BSD license, indicate your decision
34 * by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL. If you do not delete the
36 * provisions above, a recipient may use your version of this file under
37 * either the BSD or the GPL.
38 */
39
40 #include <stddef.h>
41 #include <stdlib.h>
42 #include <assert.h>
43
44 #ifdef EV_EVENT_H
45 # include EV_EVENT_H
46 #else
47 # include "event.h"
48 #endif
49
50 #if EV_MULTIPLICITY
51 # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base
52 # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base
53 #else
54 # define dLOOPev
55 # define dLOOPbase
56 #endif
57
58 /* never accessed, will always be cast from/to ev_loop */
59 struct event_base
60 {
61 int dummy;
62 };
63
64 static struct event_base *ev_x_cur;
65
66 static void
67 ev_tv_set (struct timeval *tv, ev_tstamp at)
68 {
69 tv->tv_sec = (long)at;
70 tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6);
71 }
72
73 static ev_tstamp
74 ev_tv_get (struct timeval *tv)
75 {
76 if (tv)
77 return tv->tv_sec + tv->tv_usec * 1e-6;
78 else
79 return -1.;
80 }
81
82 #define EVENT_VERSION(a,b) # a "." # b
83
84 const char *event_get_version (void)
85 {
86 return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR);
87 }
88
89 const char *event_get_method (void)
90 {
91 return "libev";
92 }
93
94 void *event_init (void)
95 {
96 #if EV_MULTIPLICITY
97 if (ev_x_cur)
98 ev_x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO);
99 else
100 ev_x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO);
101 #else
102 assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !ev_x_cur));
103
104 ev_x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO);
105 #endif
106
107 return ev_x_cur;
108 }
109
110 void event_base_free (struct event_base *base)
111 {
112 dLOOPbase;
113
114 #if EV_MULTIPLICITY
115 if (ev_default_loop (EVFLAG_AUTO) != loop)
116 ev_loop_destroy (loop);
117 #endif
118 }
119
120 int event_dispatch (void)
121 {
122 return event_base_dispatch (ev_x_cur);
123 }
124
125 #ifdef EV_STANDALONE
126 void event_set_log_callback (event_log_cb cb)
127 {
128 /* nop */
129 }
130 #endif
131
132 int event_loop (int flags)
133 {
134 return event_base_loop (ev_x_cur, flags);
135 }
136
137 int event_loopexit (struct timeval *tv)
138 {
139 return event_base_loopexit (ev_x_cur, tv);
140 }
141
142 static void
143 ev_x_cb (struct event *ev, int revents)
144 {
145 revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL;
146
147 ev->ev_res = revents;
148 ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg);
149 }
150
151 static void
152 ev_x_cb_sig (EV_P_ struct ev_signal *w, int revents)
153 {
154 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig));
155
156 if (revents & EV_ERROR)
157 event_del (ev);
158
159 ev_x_cb (ev, revents);
160 }
161
162 static void
163 ev_x_cb_io (EV_P_ struct ev_io *w, int revents)
164 {
165 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io));
166
167 if ((revents & EV_ERROR) || !(ev->ev_events & EV_PERSIST))
168 event_del (ev);
169
170 ev_x_cb (ev, revents);
171 }
172
173 static void
174 ev_x_cb_to (EV_P_ struct ev_timer *w, int revents)
175 {
176 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to));
177
178 event_del (ev);
179
180 ev_x_cb (ev, revents);
181 }
182
183 void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
184 {
185 if (events & EV_SIGNAL)
186 ev_init (&ev->iosig.sig, ev_x_cb_sig);
187 else
188 ev_init (&ev->iosig.io, ev_x_cb_io);
189
190 ev_init (&ev->to, ev_x_cb_to);
191
192 ev->ev_base = ev_x_cur; /* not threadsafe, but it's how libevent works */
193 ev->ev_fd = fd;
194 ev->ev_events = events;
195 ev->ev_pri = 0;
196 ev->ev_callback = cb;
197 ev->ev_arg = arg;
198 ev->ev_res = 0;
199 ev->ev_flags = EVLIST_INIT;
200 }
201
202 int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
203 {
204 return event_base_once (ev_x_cur, fd, events, cb, arg, tv);
205 }
206
207 int event_add (struct event *ev, struct timeval *tv)
208 {
209 dLOOPev;
210
211 if (ev->ev_events & EV_SIGNAL)
212 {
213 if (!ev_is_active (&ev->iosig.sig))
214 {
215 ev_signal_set (&ev->iosig.sig, ev->ev_fd);
216 ev_signal_start (EV_A_ &ev->iosig.sig);
217
218 ev->ev_flags |= EVLIST_SIGNAL;
219 }
220 }
221 else if (ev->ev_events & (EV_READ | EV_WRITE))
222 {
223 if (!ev_is_active (&ev->iosig.io))
224 {
225 ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE));
226 ev_io_start (EV_A_ &ev->iosig.io);
227
228 ev->ev_flags |= EVLIST_INSERTED;
229 }
230 }
231
232 if (tv)
233 {
234 ev->to.repeat = ev_tv_get (tv);
235 ev_timer_again (EV_A_ &ev->to);
236 ev->ev_flags |= EVLIST_TIMEOUT;
237 }
238 else
239 {
240 ev_timer_stop (EV_A_ &ev->to);
241 ev->ev_flags &= ~EVLIST_TIMEOUT;
242 }
243
244 ev->ev_flags |= EVLIST_ACTIVE;
245
246 return 0;
247 }
248
249 int event_del (struct event *ev)
250 {
251 dLOOPev;
252
253 if (ev->ev_events & EV_SIGNAL)
254 ev_signal_stop (EV_A_ &ev->iosig.sig);
255 else if (ev->ev_events & (EV_READ | EV_WRITE))
256 ev_io_stop (EV_A_ &ev->iosig.io);
257
258 if (ev_is_active (&ev->to))
259 ev_timer_stop (EV_A_ &ev->to);
260
261 ev->ev_flags = EVLIST_INIT;
262
263 return 0;
264 }
265
266 void event_active (struct event *ev, int res, short ncalls)
267 {
268 dLOOPev;
269
270 if (res & EV_TIMEOUT)
271 ev_feed_event (EV_A_ &ev->to, res & EV_TIMEOUT);
272
273 if (res & EV_SIGNAL)
274 ev_feed_event (EV_A_ &ev->iosig.sig, res & EV_SIGNAL);
275
276 if (res & (EV_READ | EV_WRITE))
277 ev_feed_event (EV_A_ &ev->iosig.io, res & (EV_READ | EV_WRITE));
278 }
279
280 int event_pending (struct event *ev, short events, struct timeval *tv)
281 {
282 short revents = 0;
283 dLOOPev;
284
285 if (ev->ev_events & EV_SIGNAL)
286 {
287 /* sig */
288 if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig))
289 revents |= EV_SIGNAL;
290 }
291 else if (ev->ev_events & (EV_READ | EV_WRITE))
292 {
293 /* io */
294 if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io))
295 revents |= ev->ev_events & (EV_READ | EV_WRITE);
296 }
297
298 if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to))
299 {
300 revents |= EV_TIMEOUT;
301
302 if (tv)
303 ev_tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */
304 }
305
306 return events & revents;
307 }
308
309 int event_priority_init (int npri)
310 {
311 return event_base_priority_init (ev_x_cur, npri);
312 }
313
314 int event_priority_set (struct event *ev, int pri)
315 {
316 ev->ev_pri = pri;
317
318 return 0;
319 }
320
321 int event_base_set (struct event_base *base, struct event *ev)
322 {
323 ev->ev_base = base;
324
325 return 0;
326 }
327
328 int event_base_loop (struct event_base *base, int flags)
329 {
330 dLOOPbase;
331
332 ev_loop (EV_A_ flags);
333
334 return 0;
335 }
336
337 int event_base_dispatch (struct event_base *base)
338 {
339 return event_base_loop (base, 0);
340 }
341
342 static void
343 ev_x_loopexit_cb (int revents, void *base)
344 {
345 dLOOPbase;
346
347 ev_unloop (EV_A_ EVUNLOOP_ONE);
348 }
349
350 int event_base_loopexit (struct event_base *base, struct timeval *tv)
351 {
352 ev_tstamp after = ev_tv_get (tv);
353 dLOOPbase;
354
355 ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., ev_x_loopexit_cb, (void *)base);
356
357 return 0;
358 }
359
360 struct ev_x_once
361 {
362 int fd;
363 void (*cb)(int, short, void *);
364 void *arg;
365 };
366
367 static void
368 ev_x_once_cb (int revents, void *arg)
369 {
370 struct ev_x_once *once = (struct ev_x_once *)arg;
371
372 once->cb (once->fd, (short)revents, once->arg);
373 free (once);
374 }
375
376 int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
377 {
378 struct ev_x_once *once = (struct ev_x_once *)malloc (sizeof (struct ev_x_once));
379 dLOOPbase;
380
381 if (!once)
382 return -1;
383
384 once->fd = fd;
385 once->cb = cb;
386 once->arg = arg;
387
388 ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), ev_tv_get (tv), ev_x_once_cb, (void *)once);
389
390 return 0;
391 }
392
393 int event_base_priority_init (struct event_base *base, int npri)
394 {
395 /*dLOOPbase;*/
396
397 return 0;
398 }
399