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

Comparing libev/event.c (file contents):
Revision 1.4 by root, Fri Nov 2 13:08:51 2007 UTC vs.
Revision 1.34 by root, Wed Apr 9 21:09:10 2008 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 (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{
116 dLOOPbase;
117
118#if EV_MULTIPLICITY
119 if (ev_default_loop (EVFLAG_AUTO) != loop)
120 ev_loop_destroy (loop);
121#endif
122}
123
124int event_dispatch (void)
125{
126 return event_base_dispatch (x_cur);
127}
128
129#ifdef EV_STANDALONE
130void event_set_log_callback (event_log_cb cb)
131{
85 /* nop */ 132 /* nop */
86} 133}
87 134#endif
88int event_dispatch (void)
89{
90 return event_base_dispatch (x_cur);
91}
92
93void event_set_log_callback (event_log_cb cb)
94{
95 /* nop */
96}
97 135
98int event_loop (int flags) 136int event_loop (int flags)
99{ 137{
100 return event_base_loop (x_cur, flags); 138 return event_base_loop (x_cur, flags);
101} 139}
102 140
103int event_loopexit (struct timeval *tv) 141int event_loopexit (struct timeval *tv)
104{ 142{
105 event_base_loopexit (x_cur, tv); 143 return event_base_loopexit (x_cur, tv);
106} 144}
107 145
108static void 146static void
109x_cb (struct event *ev, int revents) 147x_cb (struct event *ev, int revents)
110{ 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{
111 if (ev->ev_events & EV_SIGNAL) 189 if (events & EV_SIGNAL)
112 { 190 ev_init (&ev->iosig.sig, x_cb_sig);
113 /* sig */
114 if (ev_is_active (&ev->iosig.sig))
115 {
116 ev_signal_stop (&ev->iosig.sig);
117 --x_actives;
118 }
119 }
120 else 191 else
121 {
122 /* io */
123 if (!(ev->ev_events & EV_PERSIST) && ev_is_active (&ev->iosig.io))
124 {
125 ev_io_stop (&ev->iosig.io);
126 --x_actives;
127 }
128 }
129
130 revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL;
131
132 if (revents & EV_TIMEOUT)
133 --x_actives;
134
135 ev->ev_res = revents;
136 ev->ev_callback (ev->ev_fd, revents, ev->ev_arg);
137}
138
139static void
140x_cb_io (struct ev_io *w, int revents)
141{
142 x_cb ((struct event *)(((char *)w) - offsetof (struct event, iosig.io)), revents);
143}
144
145static void
146x_cb_to (struct ev_timer *w, int revents)
147{
148 x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents);
149}
150
151static void
152x_cb_sig (struct ev_signal *w, int revents)
153{
154 x_cb ((struct event *)(((char *)w) - offsetof (struct event, iosig.sig)), revents);
155}
156
157void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
158{
159 if (events & EV_SIGNAL)
160 ev_watcher_init (&ev->iosig.sig, x_cb_sig);
161 else
162 ev_watcher_init (&ev->iosig.io, x_cb_io); 192 ev_init (&ev->iosig.io, x_cb_io);
163 193
164 ev_watcher_init (&ev->to, x_cb_to); 194 ev_init (&ev->to, x_cb_to);
165 195
166 ev->ev_base = x_cur; 196 ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */
167 ev->ev_fd = fd; 197 ev->ev_fd = fd;
168 ev->ev_events = events; 198 ev->ev_events = events;
169 ev->ev_pri = 0; 199 ev->ev_pri = 0;
170 ev->ev_callback = cb; 200 ev->ev_callback = cb;
171 ev->ev_arg = arg; 201 ev->ev_arg = arg;
172 ev->ev_res = 0; 202 ev->ev_res = 0;
203 ev->ev_flags = EVLIST_INIT;
173} 204}
174 205
175int 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)
176{ 207{
177 event_base_once (x_cur, fd, events, cb, arg, tv); 208 return event_base_once (x_cur, fd, events, cb, arg, tv);
178} 209}
179 210
180int event_add (struct event *ev, struct timeval *tv) 211int event_add (struct event *ev, struct timeval *tv)
181{ 212{
182 /* disable all watchers */ 213 dLOOPev;
183 event_del (ev);
184 214
185 if (ev->ev_events & EV_SIGNAL) 215 if (ev->ev_events & EV_SIGNAL)
186 { 216 {
217 if (!ev_is_active (&ev->iosig.sig))
218 {
187 ev_signal_set (&ev->iosig.sig, ev->ev_fd); 219 ev_signal_set (&ev->iosig.sig, ev->ev_fd);
188 ev_signal_start (&ev->iosig.sig); 220 ev_signal_start (EV_A_ &ev->iosig.sig);
189 ++x_actives; 221
222 ev->ev_flags |= EVLIST_SIGNAL;
223 }
190 } 224 }
191 else if (ev->ev_events & (EV_READ | EV_WRITE)) 225 else if (ev->ev_events & (EV_READ | EV_WRITE))
192 { 226 {
227 if (!ev_is_active (&ev->iosig.io))
228 {
193 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));
194 ev_io_start (&ev->iosig.io); 230 ev_io_start (EV_A_ &ev->iosig.io);
195 ++x_actives; 231
232 ev->ev_flags |= EVLIST_INSERTED;
233 }
196 } 234 }
197 235
198 if (tv) 236 if (tv)
199 { 237 {
200 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 {
201 ev_timer_start (&ev->to); 244 ev_timer_stop (EV_A_ &ev->to);
202 ++x_actives; 245 ev->ev_flags &= ~EVLIST_TIMEOUT;
203 } 246 }
247
248 ev->ev_flags |= EVLIST_ACTIVE;
204 249
205 return 0; 250 return 0;
206} 251}
207 252
208int event_del (struct event *ev) 253int event_del (struct event *ev)
209{ 254{
255 dLOOPev;
256
210 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)
211 { 291 {
212 /* sig */ 292 /* sig */
213 if (ev_is_active (&ev->iosig.sig)) 293 if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig))
214 { 294 revents |= EV_SIGNAL;
215 ev_signal_stop (&ev->iosig.sig);
216 --x_actives;
217 }
218 } 295 }
219 else 296 else if (ev->ev_events & (EV_READ | EV_WRITE))
220 { 297 {
221 /* io */ 298 /* io */
222 if (ev_is_active (&ev->iosig.io)) 299 if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io))
223 {
224 ev_io_stop (&ev->iosig.io);
225 --x_actives;
226 }
227 }
228
229 if (ev_is_active (&ev->to))
230 {
231 ev_timer_stop (&ev->to);
232 --x_actives;
233 }
234
235 return 0;
236}
237
238int event_pending (struct event *ev, short events, struct timeval *tv)
239{
240 short revents;
241
242 if (ev->ev_events & EV_SIGNAL)
243 {
244 /* sig */
245 if (ev->iosig.sig.pending)
246 revents |= EV_SIGNAL;
247 }
248 else
249 {
250 /* io */
251 if (ev->iosig.io.pending)
252 revents |= ev->ev_events & (EV_READ | EV_WRITE); 300 revents |= ev->ev_events & (EV_READ | EV_WRITE);
253 } 301 }
254 302
255 if (ev->to.pending) 303 if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to))
256 { 304 {
257 revents |= EV_TIMEOUT; 305 revents |= EV_TIMEOUT;
258 306
259 if (tv) 307 if (tv)
260 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 :) */
261 } 309 }
262 310
263 return events & revents; 311 return events & revents;
264} 312}
265 313
282 return 0; 330 return 0;
283} 331}
284 332
285int event_base_loop (struct event_base *base, int flags) 333int event_base_loop (struct event_base *base, int flags)
286{ 334{
287 do 335 dLOOPbase;
288 { 336
289 ev_loop (flags | EVLOOP_ONESHOT); 337 ev_loop (EV_A_ flags);
290 }
291 while (!(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) && x_actives && !ev_loop_done);
292 338
293 return 0; 339 return 0;
294} 340}
295 341
296int event_base_dispatch (struct event_base *base) 342int event_base_dispatch (struct event_base *base)
297{ 343{
298 return event_base_loop (base, 0); 344 return event_base_loop (base, 0);
299} 345}
300 346
301static void 347static void
302x_loopexit_cb (int revents, void *arg) 348x_loopexit_cb (int revents, void *base)
303{ 349{
304 ev_loop_done = 2; 350 dLOOPbase;
351
352 ev_unloop (EV_A_ EVUNLOOP_ONE);
305} 353}
306 354
307int event_base_loopexit (struct event_base *base, struct timeval *tv) 355int event_base_loopexit (struct event_base *base, struct timeval *tv)
308{ 356{
309 ev_tstamp after = tv_get (tv); 357 ev_tstamp after = tv_get (tv);
358 dLOOPbase;
310 359
311 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);
361
362 return 0;
312} 363}
313 364
314struct x_once 365struct x_once
315{ 366{
316 int fd; 367 int fd;
319}; 370};
320 371
321static void 372static void
322x_once_cb (int revents, void *arg) 373x_once_cb (int revents, void *arg)
323{ 374{
324 struct x_once *once = arg; 375 struct x_once *once = (struct x_once *)arg;
325 376
326 once->cb (once->fd, revents, once->arg); 377 once->cb (once->fd, (short)revents, once->arg);
327 free (once); 378 free (once);
328} 379}
329 380
330int 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)
331{ 382{
332 struct x_once *once = malloc (sizeof (struct x_once)); 383 struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once));
384 dLOOPbase;
333 385
334 if (!once) 386 if (!once)
335 return -1; 387 return -1;
336 388
337 once->fd = fd; 389 once->fd = fd;
338 once->cb = cb; 390 once->cb = cb;
339 once->arg = arg; 391 once->arg = arg;
340 392
341 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);
342 394
343 return 0; 395 return 0;
344} 396}
345 397
346int event_base_priority_init (struct event_base *base, int npri) 398int event_base_priority_init (struct event_base *base, int npri)
347{ 399{
348 return 0; 400 /*dLOOPbase;*/
349}
350 401
402 return 0;
403}
404

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines