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

Comparing libev/event.c (file contents):
Revision 1.6 by root, Sat Nov 3 20:24:42 2007 UTC vs.
Revision 1.33 by root, Tue Dec 25 07:05:46 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines