ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/event.c
Revision: 1.7
Committed: Sat Nov 3 21:58:51 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.6: +37 -58 lines
Log Message:
prepare for multiple bases

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
36 #include "event.h"
37
38 #define dLOOPev
39 #define dLOOPbase
40
41 struct event_base
42 {
43 int dummy;
44 };
45
46 static struct event_base x_base, *x_cur;
47
48 static void
49 tv_set (struct timeval *tv, ev_tstamp at)
50 {
51 tv->tv_sec = (long)at;
52 tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6);
53 }
54
55 static ev_tstamp
56 tv_get (struct timeval *tv)
57 {
58 if (tv)
59 return tv->tv_sec + tv->tv_usec * 1e-6;
60 else
61 return -1.;
62 }
63
64 #define EVENT_VERSION(a,b) # a "." # b
65
66 const char *event_get_version (void)
67 {
68 return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR);
69 }
70
71 const char *event_get_method (void)
72 {
73 return "libev";
74 }
75
76 void *event_init (void)
77 {
78 if (!x_cur && ev_init (0))
79 return x_cur = &x_base;
80
81 return 0;
82 }
83
84 void event_base_free (struct event_base *base)
85 {
86 /* nop */
87 }
88
89 int event_dispatch (void)
90 {
91 return event_base_dispatch (x_cur);
92 }
93
94 #ifdef EV_STANDALONE
95 void event_set_log_callback (event_log_cb cb)
96 {
97 /* nop */
98 }
99 #endif
100
101 int event_loop (int flags)
102 {
103 return event_base_loop (x_cur, flags);
104 }
105
106 int event_loopexit (struct timeval *tv)
107 {
108 return event_base_loopexit (x_cur, tv);
109 }
110
111 static void
112 x_cb (struct event *ev, int revents)
113 {
114 revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL;
115
116 ev->ev_res = revents;
117 ev->ev_callback (ev->ev_fd, revents, ev->ev_arg);
118 }
119
120 static void
121 x_cb_sig (EV_P_ struct ev_signal *w, int revents)
122 {
123 x_cb ((struct event *)(((char *)w) - offsetof (struct event, iosig.sig)), revents);
124 }
125
126 static void
127 x_cb_io (EV_P_ struct ev_io *w, int revents)
128 {
129 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io));
130
131 if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w))
132 ev_io_stop (w);
133
134 x_cb (ev, revents);
135 }
136
137 static void
138 x_cb_to (EV_P_ struct ev_timer *w, int revents)
139 {
140 x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents);
141 }
142
143 void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
144 {
145 if (events & EV_SIGNAL)
146 ev_watcher_init (&ev->iosig.sig, x_cb_sig);
147 else
148 ev_watcher_init (&ev->iosig.io, x_cb_io);
149
150 ev_watcher_init (&ev->to, x_cb_to);
151
152 ev->ev_base = x_cur;
153 ev->ev_fd = fd;
154 ev->ev_events = events;
155 ev->ev_pri = 0;
156 ev->ev_callback = cb;
157 ev->ev_arg = arg;
158 ev->ev_res = 0;
159 }
160
161 int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
162 {
163 return event_base_once (x_cur, fd, events, cb, arg, tv);
164 }
165
166 int event_add (struct event *ev, struct timeval *tv)
167 {
168 dLOOPev;
169
170 /* disable all watchers */
171 event_del (ev);
172
173 if (ev->ev_events & EV_SIGNAL)
174 {
175 ev_signal_set (&ev->iosig.sig, ev->ev_fd);
176 ev_signal_start (EV_A_ &ev->iosig.sig);
177 }
178 else if (ev->ev_events & (EV_READ | EV_WRITE))
179 {
180 ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE));
181 ev_io_start (EV_A_ &ev->iosig.io);
182 }
183
184 if (tv)
185 {
186 ev_timer_set (&ev->to, tv_get (tv), 0.);
187 ev_timer_start (EV_A_ &ev->to);
188 }
189
190 return 0;
191 }
192
193 int event_del (struct event *ev)
194 {
195 dLOOPev;
196
197 if (ev->ev_events & EV_SIGNAL)
198 {
199 /* sig */
200 if (ev_is_active (&ev->iosig.sig))
201 ev_signal_stop (EV_A_ &ev->iosig.sig);
202 }
203 else
204 {
205 /* io */
206 if (ev_is_active (&ev->iosig.io))
207 ev_io_stop (EV_A_ &ev->iosig.io);
208 }
209
210 if (ev_is_active (&ev->to))
211 ev_timer_stop (EV_A_ &ev->to);
212
213 return 0;
214 }
215
216 int event_pending (struct event *ev, short events, struct timeval *tv)
217 {
218 dLOOPev;
219
220 short revents = 0;
221
222 if (ev->ev_events & EV_SIGNAL)
223 {
224 /* sig */
225 if (ev->iosig.sig.pending)
226 revents |= EV_SIGNAL;
227 }
228 else
229 {
230 /* io */
231 if (ev->iosig.io.pending)
232 revents |= ev->ev_events & (EV_READ | EV_WRITE);
233 }
234
235 if (ev->to.pending)
236 {
237 revents |= EV_TIMEOUT;
238
239 if (tv)
240 tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */
241 }
242
243 return events & revents;
244 }
245
246 int event_priority_init (int npri)
247 {
248 return event_base_priority_init (x_cur, npri);
249 }
250
251 int event_priority_set (struct event *ev, int pri)
252 {
253 ev->ev_pri = pri;
254
255 return 0;
256 }
257
258 int event_base_set (struct event_base *base, struct event *ev)
259 {
260 ev->ev_base = base;
261
262 return 0;
263 }
264
265 int event_base_loop (struct event_base *base, int flags)
266 {
267 dLOOPbase;
268 ev_loop (EV_A_ flags | EVLOOP_ONESHOT);
269
270 return 0;
271 }
272
273 int event_base_dispatch (struct event_base *base)
274 {
275 return event_base_loop (base, 0);
276 }
277
278 static void
279 x_loopexit_cb (EV_P_ int revents, void *arg)
280 {
281 ev_unloop (EV_A_ 2);
282 }
283
284 int event_base_loopexit (struct event_base *base, struct timeval *tv)
285 {
286 dLOOPbase;
287 ev_tstamp after = tv_get (tv);
288
289 ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base);
290
291 return -1;
292 }
293
294 struct x_once
295 {
296 int fd;
297 void (*cb)(int, short, void *);
298 void *arg;
299 };
300
301 static void
302 x_once_cb (int revents, void *arg)
303 {
304 struct x_once *once = arg;
305
306 once->cb (once->fd, revents, once->arg);
307 free (once);
308 }
309
310 int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
311 {
312 dLOOPbase;
313 struct x_once *once = malloc (sizeof (struct x_once));
314
315 if (!once)
316 return -1;
317
318 once->fd = fd;
319 once->cb = cb;
320 once->arg = arg;
321
322 ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once);
323
324 return 0;
325 }
326
327 int event_base_priority_init (struct event_base *base, int npri)
328 {
329 dLOOPbase;
330
331 return 0;
332 }
333