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

Comparing libev/event.c (file contents):
Revision 1.32 by root, Thu Nov 29 12:21:06 2007 UTC vs.
Revision 1.46 by root, Sun Oct 24 17:19:47 2010 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,2009,2010 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 <assert.h> 42#include <assert.h>
35
36#ifndef WIN32
37# include <sys/time.h>
38#endif
39 43
40#ifdef EV_EVENT_H 44#ifdef EV_EVENT_H
41# include EV_EVENT_H 45# include EV_EVENT_H
42#else 46#else
43# include "event.h" 47# include "event.h"
44#endif 48#endif
49
50/* same definition as in ev.c */
51#define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0)
45 52
46#if EV_MULTIPLICITY 53#if EV_MULTIPLICITY
47# define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base 54# define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base
48# define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base 55# define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base
49#else 56#else
55struct event_base 62struct event_base
56{ 63{
57 int dummy; 64 int dummy;
58}; 65};
59 66
60static struct event_base *x_cur; 67static struct event_base *ev_x_cur;
61
62static void
63tv_set (struct timeval *tv, ev_tstamp at)
64{
65 tv->tv_sec = (long)at;
66 tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6);
67}
68 68
69static ev_tstamp 69static ev_tstamp
70tv_get (struct timeval *tv) 70ev_tv_get (struct timeval *tv)
71{ 71{
72 if (tv) 72 if (tv)
73 {
73 return tv->tv_sec + tv->tv_usec * 1e-6; 74 ev_tstamp after = tv->tv_sec + tv->tv_usec * 1e-6;
75 return after ? after : 1e-6;
76 }
74 else 77 else
75 return -1.; 78 return -1.;
76} 79}
77 80
78#define EVENT_VERSION(a,b) # a "." # b 81#define EVENT_STRINGIFY(s) # s
82#define EVENT_VERSION(a,b) EVENT_STRINGIFY (a) "." EVENT_STRINGIFY (b)
79 83
80const char *event_get_version (void) 84const char *event_get_version (void)
81{ 85{
86 /* returns ABI, not API or library, version */
82 return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR); 87 return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR);
83} 88}
84 89
85const char *event_get_method (void) 90const char *event_get_method (void)
86{ 91{
88} 93}
89 94
90void *event_init (void) 95void *event_init (void)
91{ 96{
92#if EV_MULTIPLICITY 97#if EV_MULTIPLICITY
93 if (x_cur) 98 if (ev_x_cur)
94 x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO); 99 ev_x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO);
95 else 100 else
96 x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO); 101 ev_x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO);
97#else 102#else
98 assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur)); 103 assert (("libev: multiple event bases not supported when not compiled with EV_MULTIPLICITY", !ev_x_cur));
99 104
100 x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO); 105 ev_x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO);
101#endif 106#endif
102 107
103 return x_cur; 108 return ev_x_cur;
104} 109}
105 110
106void event_base_free (struct event_base *base) 111void event_base_free (struct event_base *base)
107{ 112{
108 dLOOPbase; 113 dLOOPbase;
113#endif 118#endif
114} 119}
115 120
116int event_dispatch (void) 121int event_dispatch (void)
117{ 122{
118 return event_base_dispatch (x_cur); 123 return event_base_dispatch (ev_x_cur);
119} 124}
120 125
121#ifdef EV_STANDALONE 126#ifdef EV_STANDALONE
122void event_set_log_callback (event_log_cb cb) 127void event_set_log_callback (event_log_cb cb)
123{ 128{
125} 130}
126#endif 131#endif
127 132
128int event_loop (int flags) 133int event_loop (int flags)
129{ 134{
130 return event_base_loop (x_cur, flags); 135 return event_base_loop (ev_x_cur, flags);
131} 136}
132 137
133int event_loopexit (struct timeval *tv) 138int event_loopexit (struct timeval *tv)
134{ 139{
135 return event_base_loopexit (x_cur, tv); 140 return event_base_loopexit (ev_x_cur, tv);
136} 141}
137 142
138static void 143static void
139x_cb (struct event *ev, int revents) 144ev_x_cb (struct event *ev, int revents)
140{ 145{
141 revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL; 146 revents &= EV_READ | EV_WRITE | EV_TIMER | EV_SIGNAL;
142 147
143 ev->ev_res = revents; 148 ev->ev_res = revents;
144 ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg); 149 ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg);
145} 150}
146 151
147static void 152static void
148x_cb_sig (EV_P_ struct ev_signal *w, int revents) 153ev_x_cb_sig (EV_P_ struct ev_signal *w, int revents)
149{ 154{
150 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig)); 155 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig));
151 156
152 if (revents & EV_ERROR) 157 if (revents & EV_ERROR)
153 event_del (ev); 158 event_del (ev);
154 159
155 x_cb (ev, revents); 160 ev_x_cb (ev, revents);
156} 161}
157 162
158static void 163static void
159x_cb_io (EV_P_ struct ev_io *w, int revents) 164ev_x_cb_io (EV_P_ struct ev_io *w, int revents)
160{ 165{
161 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); 166 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io));
162 167
163 if ((revents & EV_ERROR) || !(ev->ev_events & EV_PERSIST)) 168 if ((revents & EV_ERROR) || !(ev->ev_events & EV_PERSIST))
164 event_del (ev); 169 event_del (ev);
165 170
166 x_cb (ev, revents); 171 ev_x_cb (ev, revents);
167} 172}
168 173
169static void 174static void
170x_cb_to (EV_P_ struct ev_timer *w, int revents) 175ev_x_cb_to (EV_P_ struct ev_timer *w, int revents)
171{ 176{
172 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); 177 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to));
173 178
174 event_del (ev); 179 event_del (ev);
175 180
176 x_cb (ev, revents); 181 ev_x_cb (ev, revents);
177} 182}
178 183
179void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) 184void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
180{ 185{
181 if (events & EV_SIGNAL) 186 if (events & EV_SIGNAL)
182 ev_init (&ev->iosig.sig, x_cb_sig); 187 ev_init (&ev->iosig.sig, ev_x_cb_sig);
183 else 188 else
184 ev_init (&ev->iosig.io, x_cb_io); 189 ev_init (&ev->iosig.io, ev_x_cb_io);
185 190
186 ev_init (&ev->to, x_cb_to); 191 ev_init (&ev->to, ev_x_cb_to);
187 192
188 ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ 193 ev->ev_base = ev_x_cur; /* not threadsafe, but it's how libevent works */
189 ev->ev_fd = fd; 194 ev->ev_fd = fd;
190 ev->ev_events = events; 195 ev->ev_events = events;
191 ev->ev_pri = 0; 196 ev->ev_pri = 0;
192 ev->ev_callback = cb; 197 ev->ev_callback = cb;
193 ev->ev_arg = arg; 198 ev->ev_arg = arg;
195 ev->ev_flags = EVLIST_INIT; 200 ev->ev_flags = EVLIST_INIT;
196} 201}
197 202
198int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) 203int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
199{ 204{
200 return event_base_once (x_cur, fd, events, cb, arg, tv); 205 return event_base_once (ev_x_cur, fd, events, cb, arg, tv);
201} 206}
202 207
203int event_add (struct event *ev, struct timeval *tv) 208int event_add (struct event *ev, struct timeval *tv)
204{ 209{
205 dLOOPev; 210 dLOOPev;
225 } 230 }
226 } 231 }
227 232
228 if (tv) 233 if (tv)
229 { 234 {
230 ev->to.repeat = tv_get (tv); 235 ev->to.repeat = ev_tv_get (tv);
231 ev_timer_again (EV_A_ &ev->to); 236 ev_timer_again (EV_A_ &ev->to);
232 ev->ev_flags |= EVLIST_TIMEOUT; 237 ev->ev_flags |= EVLIST_TIMEOUT;
233 } 238 }
234 else 239 else
235 { 240 {
276int event_pending (struct event *ev, short events, struct timeval *tv) 281int event_pending (struct event *ev, short events, struct timeval *tv)
277{ 282{
278 short revents = 0; 283 short revents = 0;
279 dLOOPev; 284 dLOOPev;
280 285
281
282 if (ev->ev_events & EV_SIGNAL) 286 if (ev->ev_events & EV_SIGNAL)
283 { 287 {
284 /* sig */ 288 /* sig */
285 if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) 289 if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig))
286 revents |= EV_SIGNAL; 290 revents |= EV_SIGNAL;
295 if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) 299 if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to))
296 { 300 {
297 revents |= EV_TIMEOUT; 301 revents |= EV_TIMEOUT;
298 302
299 if (tv) 303 if (tv)
300 tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ 304 EV_TV_SET (tv, ev_now (EV_A)); /* not sure if this is right :) */
301 } 305 }
302 306
303 return events & revents; 307 return events & revents;
304} 308}
305 309
306int event_priority_init (int npri) 310int event_priority_init (int npri)
307{ 311{
308 return event_base_priority_init (x_cur, npri); 312 return event_base_priority_init (ev_x_cur, npri);
309} 313}
310 314
311int event_priority_set (struct event *ev, int pri) 315int event_priority_set (struct event *ev, int pri)
312{ 316{
313 ev->ev_pri = pri; 317 ev->ev_pri = pri;
324 328
325int event_base_loop (struct event_base *base, int flags) 329int event_base_loop (struct event_base *base, int flags)
326{ 330{
327 dLOOPbase; 331 dLOOPbase;
328 332
329 ev_loop (EV_A_ flags); 333 ev_run (EV_A_ flags);
330 334
331 return 0; 335 return 0;
332} 336}
333 337
334int event_base_dispatch (struct event_base *base) 338int event_base_dispatch (struct event_base *base)
335{ 339{
336 return event_base_loop (base, 0); 340 return event_base_loop (base, 0);
337} 341}
338 342
339static void 343static void
340x_loopexit_cb (int revents, void *base) 344ev_x_loopexit_cb (int revents, void *base)
341{ 345{
342 dLOOPbase; 346 dLOOPbase;
343 347
344 ev_unloop (EV_A_ EVUNLOOP_ONE); 348 ev_break (EV_A_ EVBREAK_ONE);
345} 349}
346 350
347int event_base_loopexit (struct event_base *base, struct timeval *tv) 351int event_base_loopexit (struct event_base *base, struct timeval *tv)
348{ 352{
349 ev_tstamp after = tv_get (tv); 353 ev_tstamp after = ev_tv_get (tv);
350 dLOOPbase; 354 dLOOPbase;
351 355
352 ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); 356 ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., ev_x_loopexit_cb, (void *)base);
353 357
354 return -1; 358 return 0;
355} 359}
356 360
357struct x_once 361struct ev_x_once
358{ 362{
359 int fd; 363 int fd;
360 void (*cb)(int, short, void *); 364 void (*cb)(int, short, void *);
361 void *arg; 365 void *arg;
362}; 366};
363 367
364static void 368static void
365x_once_cb (int revents, void *arg) 369ev_x_once_cb (int revents, void *arg)
366{ 370{
367 struct x_once *once = (struct x_once *)arg; 371 struct ev_x_once *once = (struct ev_x_once *)arg;
368 372
369 once->cb (once->fd, (short)revents, once->arg); 373 once->cb (once->fd, (short)revents, once->arg);
370 free (once); 374 free (once);
371} 375}
372 376
373int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) 377int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
374{ 378{
375 struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once)); 379 struct ev_x_once *once = (struct ev_x_once *)malloc (sizeof (struct ev_x_once));
376 dLOOPbase; 380 dLOOPbase;
377 381
378 if (!once) 382 if (!once)
379 return -1; 383 return -1;
380 384
381 once->fd = fd; 385 once->fd = fd;
382 once->cb = cb; 386 once->cb = cb;
383 once->arg = arg; 387 once->arg = arg;
384 388
385 ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once); 389 ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), ev_tv_get (tv), ev_x_once_cb, (void *)once);
386 390
387 return 0; 391 return 0;
388} 392}
389 393
390int event_base_priority_init (struct event_base *base, int npri) 394int event_base_priority_init (struct event_base *base, int npri)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines