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

Comparing libev/event.c (file contents):
Revision 1.2 by root, Thu Nov 1 18:13:44 2007 UTC vs.
Revision 1.38 by root, Mon Nov 17 03:37:08 2008 UTC

1/* 1/*
2 * libevent compatibility layer 2 * libevent compatibility layer
3 * 3 *
4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007,2008 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>
34#include <sys/time.h> 42#include <assert.h>
35 43
44#ifdef EV_EVENT_H
45# include EV_EVENT_H
46#else
36#include "event.h" 47# include "event.h"
48#endif
37 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 */
38struct event_base 59struct event_base
39{ 60{
40 int dummy; 61 int dummy;
41}; 62};
42 63
43static int x_actives;
44
45static struct event_base x_base, *x_cur; 64static struct event_base *ev_x_cur;
46 65
47static void 66static void
48tv_set (struct timeval *tv, ev_tstamp at) 67ev_tv_set (struct timeval *tv, ev_tstamp at)
49{ 68{
50 tv->tv_sec = (long)at; 69 tv->tv_sec = (long)at;
51 tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6); 70 tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6);
52} 71}
53 72
54static ev_tstamp 73static ev_tstamp
55tv_get (struct timeval *tv) 74ev_tv_get (struct timeval *tv)
56{ 75{
57 if (tv) 76 if (tv)
58 return tv->tv_sec + tv->tv_usec * 1e-6; 77 return tv->tv_sec + tv->tv_usec * 1e-6;
59 else 78 else
60 return -1.; 79 return -1.;
72 return "libev"; 91 return "libev";
73} 92}
74 93
75void *event_init (void) 94void *event_init (void)
76{ 95{
77 if (ev_init (0)) 96#if EV_MULTIPLICITY
78 return x_cur = &x_base; 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));
79 103
80 return 0; 104 ev_x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO);
105#endif
106
107 return ev_x_cur;
81} 108}
82 109
83void event_base_free (struct event_base *base) 110void event_base_free (struct event_base *base)
84{ 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
120int event_dispatch (void)
121{
122 return event_base_dispatch (ev_x_cur);
123}
124
125#ifdef EV_STANDALONE
126void event_set_log_callback (event_log_cb cb)
127{
85 /* nop */ 128 /* nop */
86} 129}
87 130#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 131
98int event_loop (int flags) 132int event_loop (int flags)
99{ 133{
100 return event_base_loop (x_cur, flags); 134 return event_base_loop (ev_x_cur, flags);
101} 135}
102 136
103int event_loopexit (struct timeval *tv) 137int event_loopexit (struct timeval *tv)
104{ 138{
105 event_base_loopexit (x_cur, tv); 139 return event_base_loopexit (ev_x_cur, tv);
106} 140}
107 141
108static void 142static void
109x_cb (struct event *ev, int revents) 143ev_x_cb (struct event *ev, int revents)
110{ 144{
111 if (ev_is_active (&ev->sig))
112 {
113 ev_signal_stop (&ev->sig);
114 --x_actives;
115 }
116
117 if (!(ev->ev_events & EV_PERSIST) && ev_is_active (&ev->io))
118 {
119 ev_io_stop (&ev->io);
120 --x_actives;
121 }
122
123 revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL; 145 revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL;
124 146
125 if (revents & EV_TIMEOUT)
126 --x_actives;
127
128 ev->ev_res = revents; 147 ev->ev_res = revents;
129 ev->ev_callback (ev->ev_fd, revents, ev->ev_arg); 148 ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg);
130} 149}
131 150
132static void 151static void
133x_cb_io (struct ev_io *w, int revents)
134{
135 x_cb ((struct event *)(((char *)w) - offsetof (struct event, io)), revents);
136}
137
138static void
139x_cb_to (struct ev_timer *w, int revents)
140{
141 x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents);
142}
143
144static void
145x_cb_sig (struct ev_signal *w, int revents) 152ev_x_cb_sig (EV_P_ struct ev_signal *w, int revents)
146{ 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
162static void
163ev_x_cb_io (EV_P_ struct ev_io *w, int revents)
164{
147 x_cb ((struct event *)(((char *)w) - offsetof (struct event, sig)), revents); 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
173static void
174ev_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);
148} 181}
149 182
150void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) 183void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
151{ 184{
185 if (events & EV_SIGNAL)
186 ev_init (&ev->iosig.sig, ev_x_cb_sig);
187 else
152 ev_watcher_init (&ev->io, x_cb_io); 188 ev_init (&ev->iosig.io, ev_x_cb_io);
189
153 ev_watcher_init (&ev->to, x_cb_to); 190 ev_init (&ev->to, ev_x_cb_to);
154 ev_watcher_init (&ev->sig, x_cb_sig);
155 191
156 ev->ev_base = x_cur; 192 ev->ev_base = ev_x_cur; /* not threadsafe, but it's how libevent works */
157 ev->ev_fd = fd; 193 ev->ev_fd = fd;
158 ev->ev_events = events; 194 ev->ev_events = events;
159 ev->ev_pri = 0; 195 ev->ev_pri = 0;
160 ev->ev_callback = cb; 196 ev->ev_callback = cb;
161 ev->ev_arg = arg; 197 ev->ev_arg = arg;
162 ev->ev_res = 0; 198 ev->ev_res = 0;
199 ev->ev_flags = EVLIST_INIT;
163} 200}
164 201
165int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) 202int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
166{ 203{
167 event_base_once (x_cur, fd, events, cb, arg, tv); 204 return event_base_once (ev_x_cur, fd, events, cb, arg, tv);
168} 205}
169 206
170int event_add (struct event *ev, struct timeval *tv) 207int event_add (struct event *ev, struct timeval *tv)
171{ 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
172 if (tv) 232 if (tv)
173 { 233 {
174 if (ev_is_active (&ev->to)) 234 ev->to.repeat = ev_tv_get (tv);
175 { 235 ev_timer_again (EV_A_ &ev->to);
236 ev->ev_flags |= EVLIST_TIMEOUT;
237 }
238 else
239 {
176 ev_timer_stop (&ev->to); 240 ev_timer_stop (EV_A_ &ev->to);
177 --x_actives; 241 ev->ev_flags &= ~EVLIST_TIMEOUT;
178 }
179
180 ev_timer_set (&ev->to, tv_get (tv), 0.);
181 ev_timer_start (&ev->to);
182 ++x_actives;
183 }
184
185 if (ev->ev_events & (EV_READ | EV_WRITE))
186 { 242 }
187 if (ev_is_active (&ev->io))
188 {
189 ev_io_stop (&ev->io);
190 --x_actives;
191 }
192 243
193 ev_io_set (&ev->io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE)); 244 ev->ev_flags |= EVLIST_ACTIVE;
194 ev_io_start (&ev->io); 245
195 ++x_actives; 246 return 0;
196 } 247}
248
249int event_del (struct event *ev)
250{
251 dLOOPev;
197 252
198 if (ev->ev_events & EV_SIGNAL) 253 if (ev->ev_events & EV_SIGNAL)
199 {
200 if (ev_is_active (&ev->sig))
201 {
202 ev_signal_stop (&ev->sig); 254 ev_signal_stop (EV_A_ &ev->iosig.sig);
203 --x_actives; 255 else if (ev->ev_events & (EV_READ | EV_WRITE))
204 }
205
206 ev_signal_set (&ev->sig, ev->ev_fd);
207 ev_signal_start (&ev->sig);
208 ++x_actives;
209 }
210
211 return 0;
212}
213
214int event_del (struct event *ev)
215{
216 if (ev_is_active (&ev->io))
217 {
218 ev_io_stop (&ev->io); 256 ev_io_stop (EV_A_ &ev->iosig.io);
219 --x_actives;
220 }
221 257
222 if (ev_is_active (&ev->to)) 258 if (ev_is_active (&ev->to))
223 {
224 ev_timer_stop (&ev->to); 259 ev_timer_stop (EV_A_ &ev->to);
225 --x_actives;
226 }
227 260
228 if (ev_is_active (&ev->sig)) 261 ev->ev_flags = EVLIST_INIT;
229 {
230 ev_signal_stop (&ev->sig);
231 --x_actives;
232 }
233 262
234 return 0; 263 return 0;
235} 264}
236 265
237void event_active (struct event *ev, int fd, short events) 266void event_active (struct event *ev, int res, short ncalls)
238{ 267{
239 /* unclear what this does, likely internal-use */ 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));
240} 278}
241 279
242int event_pending (struct event *ev, short events, struct timeval *tv) 280int event_pending (struct event *ev, short events, struct timeval *tv)
243{ 281{
244 short revents; 282 short revents = 0;
283 dLOOPev;
245 284
246 if (ev->io.pending) 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))
247 revents |= ev->ev_events & (EV_READ | EV_WRITE); 295 revents |= ev->ev_events & (EV_READ | EV_WRITE);
296 }
248 297
249 if (ev->to.pending) 298 if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to))
250 { 299 {
251 revents |= EV_TIMEOUT; 300 revents |= EV_TIMEOUT;
252 301
253 if (tv) 302 if (tv)
254 tv_set (tv, ev_now); /* not sure if this is right :) */ 303 ev_tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */
255 } 304 }
256
257 if (ev->sig.pending)
258 revents |= EV_SIGNAL;
259 305
260 return events & revents; 306 return events & revents;
261} 307}
262 308
263int event_priority_init (int npri) 309int event_priority_init (int npri)
264{ 310{
265 return event_base_priority_init (x_cur, npri); 311 return event_base_priority_init (ev_x_cur, npri);
266} 312}
267 313
268int event_priority_set (struct event *ev, int pri) 314int event_priority_set (struct event *ev, int pri)
269{ 315{
270 ev->ev_pri = pri; 316 ev->ev_pri = pri;
279 return 0; 325 return 0;
280} 326}
281 327
282int event_base_loop (struct event_base *base, int flags) 328int event_base_loop (struct event_base *base, int flags)
283{ 329{
284 do 330 dLOOPbase;
285 { 331
286 ev_loop (flags | EVLOOP_ONESHOT); 332 ev_loop (EV_A_ flags);
287 }
288 while (!(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) && x_actives && !ev_loop_done);
289 333
290 return 0; 334 return 0;
291} 335}
292 336
293int event_base_dispatch (struct event_base *base) 337int event_base_dispatch (struct event_base *base)
294{ 338{
295 return event_base_loop (base, 0); 339 return event_base_loop (base, 0);
296} 340}
297 341
298static void 342static void
299x_loopexit_cb (int revents, void *arg) 343ev_x_loopexit_cb (int revents, void *base)
300{ 344{
301 ev_loop_done = 2; 345 dLOOPbase;
346
347 ev_unloop (EV_A_ EVUNLOOP_ONE);
302} 348}
303 349
304int event_base_loopexit (struct event_base *base, struct timeval *tv) 350int event_base_loopexit (struct event_base *base, struct timeval *tv)
305{ 351{
306 ev_tstamp after = tv_get (tv); 352 ev_tstamp after = ev_tv_get (tv);
353 dLOOPbase;
307 354
308 ev_once (-1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); 355 ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., ev_x_loopexit_cb, (void *)base);
309}
310 356
357 return 0;
358}
359
311struct x_once 360struct ev_x_once
312{ 361{
313 int fd; 362 int fd;
314 void (*cb)(int, short, void *); 363 void (*cb)(int, short, void *);
315 void *arg; 364 void *arg;
316}; 365};
317 366
318static void 367static void
319x_once_cb (int revents, void *arg) 368ev_x_once_cb (int revents, void *arg)
320{ 369{
321 struct x_once *once = arg; 370 struct ev_x_once *once = (struct ev_x_once *)arg;
322 371
323 once->cb (once->fd, revents, once->arg); 372 once->cb (once->fd, (short)revents, once->arg);
324 free (once); 373 free (once);
325} 374}
326 375
327int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) 376int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
328{ 377{
329 struct x_once *once = malloc (sizeof (struct x_once)); 378 struct ev_x_once *once = (struct ev_x_once *)malloc (sizeof (struct ev_x_once));
379 dLOOPbase;
330 380
331 if (!once) 381 if (!once)
332 return -1; 382 return -1;
333 383
334 once->fd = fd; 384 once->fd = fd;
335 once->cb = cb; 385 once->cb = cb;
336 once->arg = arg; 386 once->arg = arg;
337 387
338 ev_once (fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once); 388 ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), ev_tv_get (tv), ev_x_once_cb, (void *)once);
339 389
340 return 0; 390 return 0;
341} 391}
342 392
343int event_base_priority_init (struct event_base *base, int npri) 393int event_base_priority_init (struct event_base *base, int npri)
344{ 394{
345 return 0; 395 /*dLOOPbase;*/
346}
347 396
397 return 0;
398}
399

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines