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

Comparing libev/event.c (file contents):
Revision 1.1 by root, Thu Nov 1 15:39:16 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{
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; 149 revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL;
124 150
125 if (revents & EV_TIMEOUT)
126 --x_actives;
127
128 ev->ev_res = revents; 151 ev->ev_res = revents;
129 ev->ev_callback (ev->ev_fd, revents, ev->ev_arg); 152 ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg);
130} 153}
131 154
132static void 155static 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) 156x_cb_sig (EV_P_ struct ev_signal *w, int revents)
146{ 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{
147 x_cb ((struct event *)(((char *)w) - offsetof (struct event, sig)), revents); 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);
148} 185}
149 186
150void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) 187void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
151{ 188{
189 if (events & EV_SIGNAL)
190 ev_init (&ev->iosig.sig, x_cb_sig);
191 else
152 ev_watcher_init (&ev->io, x_cb_io); 192 ev_init (&ev->iosig.io, x_cb_io);
193
153 ev_watcher_init (&ev->to, x_cb_to); 194 ev_init (&ev->to, x_cb_to);
154 ev_watcher_init (&ev->sig, x_cb_sig);
155 195
156 ev->ev_base = x_cur; 196 ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */
157 ev->ev_fd = fd; 197 ev->ev_fd = fd;
158 ev->ev_events = events; 198 ev->ev_events = events;
159 ev->ev_pri = 0; 199 ev->ev_pri = 0;
160 ev->ev_callback = cb; 200 ev->ev_callback = cb;
161 ev->ev_arg = arg; 201 ev->ev_arg = arg;
162 ev->ev_res = 0; 202 ev->ev_res = 0;
203 ev->ev_flags = EVLIST_INIT;
163} 204}
164 205
165int 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)
166{ 207{
167 event_base_once (x_cur, fd, events, cb, arg, tv); 208 return event_base_once (x_cur, fd, events, cb, arg, tv);
168} 209}
169 210
170int event_add (struct event *ev, struct timeval *tv) 211int event_add (struct event *ev, struct timeval *tv)
171{ 212{
213 dLOOPev;
214
215 if (ev->ev_events & EV_SIGNAL)
216 {
217 if (!ev_is_active (&ev->iosig.sig))
218 {
219 ev_signal_set (&ev->iosig.sig, ev->ev_fd);
220 ev_signal_start (EV_A_ &ev->iosig.sig);
221
222 ev->ev_flags |= EVLIST_SIGNAL;
223 }
224 }
225 else if (ev->ev_events & (EV_READ | EV_WRITE))
226 {
227 if (!ev_is_active (&ev->iosig.io))
228 {
229 ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE));
230 ev_io_start (EV_A_ &ev->iosig.io);
231
232 ev->ev_flags |= EVLIST_INSERTED;
233 }
234 }
235
172 if (tv) 236 if (tv)
173 { 237 {
174 if (ev_is_active (&ev->to)) 238 ev->to.repeat = tv_get (tv);
175 { 239 ev_timer_again (EV_A_ &ev->to);
240 ev->ev_flags |= EVLIST_TIMEOUT;
241 }
242 else
243 {
176 ev_timer_stop (&ev->to); 244 ev_timer_stop (EV_A_ &ev->to);
177 --x_actives; 245 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 { 246 }
187 if (ev_is_active (&ev->io))
188 {
189 ev_io_stop (&ev->io);
190 --x_actives;
191 }
192 247
193 ev_io_set (&ev->io, ev->ev_fd, ev->ev_events); 248 ev->ev_flags |= EVLIST_ACTIVE;
194 ev_io_start (&ev->io); 249
195 ++x_actives; 250 return 0;
196 } 251}
252
253int event_del (struct event *ev)
254{
255 dLOOPev;
197 256
198 if (ev->ev_events & EV_SIGNAL) 257 if (ev->ev_events & EV_SIGNAL)
199 {
200 if (ev_is_active (&ev->sig))
201 {
202 ev_signal_stop (&ev->sig); 258 ev_signal_stop (EV_A_ &ev->iosig.sig);
203 --x_actives; 259 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); 260 ev_io_stop (EV_A_ &ev->iosig.io);
219 --x_actives;
220 }
221 261
222 if (ev_is_active (&ev->to)) 262 if (ev_is_active (&ev->to))
223 {
224 ev_timer_stop (&ev->to); 263 ev_timer_stop (EV_A_ &ev->to);
225 --x_actives;
226 }
227 264
228 if (ev_is_active (&ev->sig)) 265 ev->ev_flags = EVLIST_INIT;
229 {
230 ev_signal_stop (&ev->sig);
231 --x_actives;
232 }
233 266
234 return 0; 267 return 0;
235} 268}
236 269
237void event_active (struct event *ev, int fd, short events) 270void event_active (struct event *ev, int res, short ncalls)
238{ 271{
239 /* unclear what this does, likely internal-use */ 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));
240} 282}
241 283
242int event_pending (struct event *ev, short events, struct timeval *tv) 284int event_pending (struct event *ev, short events, struct timeval *tv)
243{ 285{
244 short revents; 286 short revents = 0;
287 dLOOPev;
245 288
246 if (ev->io.pending) 289
290 if (ev->ev_events & EV_SIGNAL)
291 {
292 /* sig */
293 if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig))
294 revents |= EV_SIGNAL;
295 }
296 else if (ev->ev_events & (EV_READ | EV_WRITE))
297 {
298 /* io */
299 if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io))
247 revents |= ev->ev_events & (EV_READ | EV_WRITE); 300 revents |= ev->ev_events & (EV_READ | EV_WRITE);
301 }
248 302
249 if (ev->to.pending) 303 if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to))
250 { 304 {
251 revents |= EV_TIMEOUT; 305 revents |= EV_TIMEOUT;
252 306
253 if (tv) 307 if (tv)
254 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 :) */
255 } 309 }
256
257 if (ev->sig.pending)
258 revents |= EV_SIGNAL;
259 310
260 return events & revents; 311 return events & revents;
261} 312}
262 313
263int event_priority_init (int npri) 314int event_priority_init (int npri)
279 return 0; 330 return 0;
280} 331}
281 332
282int event_base_loop (struct event_base *base, int flags) 333int event_base_loop (struct event_base *base, int flags)
283{ 334{
284 do 335 dLOOPbase;
285 { 336
286 ev_loop (flags | EVLOOP_ONESHOT); 337 ev_loop (EV_A_ flags);
287 }
288 while (!(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) && x_actives && !ev_loop_done);
289 338
290 return 0; 339 return 0;
291} 340}
292 341
293int event_base_dispatch (struct event_base *base) 342int event_base_dispatch (struct event_base *base)
294{ 343{
295 return event_base_loop (base, 0); 344 return event_base_loop (base, 0);
296} 345}
297 346
298static void 347static void
299x_loopexit_cb (int revents, void *arg) 348x_loopexit_cb (int revents, void *base)
300{ 349{
301 ev_loop_done = 2; 350 dLOOPbase;
351
352 ev_unloop (EV_A_ EVUNLOOP_ONE);
302} 353}
303 354
304int event_base_loopexit (struct event_base *base, struct timeval *tv) 355int event_base_loopexit (struct event_base *base, struct timeval *tv)
305{ 356{
306 ev_tstamp after = tv_get (tv); 357 ev_tstamp after = tv_get (tv);
358 dLOOPbase;
307 359
308 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;
309} 363}
310 364
311struct x_once 365struct x_once
312{ 366{
313 int fd; 367 int fd;
316}; 370};
317 371
318static void 372static void
319x_once_cb (int revents, void *arg) 373x_once_cb (int revents, void *arg)
320{ 374{
321 struct x_once *once = arg; 375 struct x_once *once = (struct x_once *)arg;
322 376
323 once->cb (once->fd, revents, once->arg); 377 once->cb (once->fd, (short)revents, once->arg);
324 free (once); 378 free (once);
325} 379}
326 380
327int 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)
328{ 382{
329 struct x_once *once = malloc (sizeof (struct x_once)); 383 struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once));
384 dLOOPbase;
330 385
331 if (!once) 386 if (!once)
332 return -1; 387 return -1;
333 388
334 once->fd = fd; 389 once->fd = fd;
335 once->cb = cb; 390 once->cb = cb;
336 once->arg = arg; 391 once->arg = arg;
337 392
338 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);
339 394
340 return 0; 395 return 0;
341} 396}
342 397
343int event_base_priority_init (struct event_base *base, int npri) 398int event_base_priority_init (struct event_base *base, int npri)
344{ 399{
345 return 0; 400 /*dLOOPbase;*/
346}
347 401
402 return 0;
403}
404

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines