ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/event.c
Revision: 1.49
Committed: Tue Jan 4 01:28:56 2011 UTC (13 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-4_03
Changes since 1.48: +1 -1 lines
Log Message:
d'oh

File Contents

# User Rev Content
1 root 1.1 /*
2     * libevent compatibility layer
3     *
4 root 1.44 * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libev@schmorp.de>
5 root 1.1 * All rights reserved.
6     *
7 root 1.33 * Redistribution and use in source and binary forms, with or without modifica-
8     * tion, are permitted provided that the following conditions are 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.
27 root 1.1 *
28 root 1.33 * Alternatively, the contents of this file may be used under the terms of
29     * the GNU General Public License ("GPL") version 2 or any later version,
30     * in which case the provisions of the GPL are applicable instead of
31     * the above. If you wish to allow the use of your version of this file
32     * only under the terms of the GPL and not to allow others to use your
33     * version of this file under the BSD license, indicate your decision
34     * by deleting the provisions above and replace them with the notice
35     * and other provisions required by the GPL. If you do not delete the
36     * provisions above, a recipient may use your version of this file under
37     * either the BSD or the GPL.
38 root 1.1 */
39    
40     #include <stddef.h>
41     #include <stdlib.h>
42 root 1.10 #include <assert.h>
43 root 1.1
44 root 1.24 #ifdef EV_EVENT_H
45     # include EV_EVENT_H
46     #else
47     # include "event.h"
48     #endif
49 root 1.1
50 root 1.9 #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 root 1.8 #else
54     # define dLOOPev
55     # define dLOOPbase
56     #endif
57 root 1.7
58 root 1.9 /* never accessed, will always be cast from/to ev_loop */
59 root 1.1 struct event_base
60     {
61     int dummy;
62     };
63    
64 root 1.37 static struct event_base *ev_x_cur;
65 root 1.1
66     static ev_tstamp
67 root 1.37 ev_tv_get (struct timeval *tv)
68 root 1.1 {
69     if (tv)
70 root 1.42 {
71     ev_tstamp after = tv->tv_sec + tv->tv_usec * 1e-6;
72     return after ? after : 1e-6;
73     }
74 root 1.1 else
75     return -1.;
76     }
77    
78 root 1.40 #define EVENT_STRINGIFY(s) # s
79     #define EVENT_VERSION(a,b) EVENT_STRINGIFY (a) "." EVENT_STRINGIFY (b)
80 root 1.1
81     const char *event_get_version (void)
82     {
83 root 1.40 /* returns ABI, not API or library, version */
84 root 1.1 return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR);
85     }
86    
87     const char *event_get_method (void)
88     {
89     return "libev";
90     }
91    
92     void *event_init (void)
93     {
94 root 1.9 #if EV_MULTIPLICITY
95 root 1.37 if (ev_x_cur)
96     ev_x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO);
97 root 1.10 else
98 root 1.37 ev_x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO);
99 root 1.9 #else
100 root 1.39 assert (("libev: multiple event bases not supported when not compiled with EV_MULTIPLICITY", !ev_x_cur));
101 root 1.10
102 root 1.37 ev_x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO);
103 root 1.9 #endif
104 root 1.1
105 root 1.37 return ev_x_cur;
106 root 1.1 }
107    
108     void event_base_free (struct event_base *base)
109     {
110 root 1.8 dLOOPbase;
111    
112 root 1.9 #if EV_MULTIPLICITY
113 root 1.49 if (!ev_is_default_loop (loop))
114 root 1.13 ev_loop_destroy (loop);
115 root 1.9 #endif
116 root 1.1 }
117    
118     int event_dispatch (void)
119     {
120 root 1.37 return event_base_dispatch (ev_x_cur);
121 root 1.1 }
122    
123 root 1.12 #ifdef EV_STANDALONE
124 root 1.1 void event_set_log_callback (event_log_cb cb)
125     {
126     /* nop */
127     }
128 root 1.5 #endif
129 root 1.1
130     int event_loop (int flags)
131     {
132 root 1.37 return event_base_loop (ev_x_cur, flags);
133 root 1.1 }
134    
135     int event_loopexit (struct timeval *tv)
136     {
137 root 1.37 return event_base_loopexit (ev_x_cur, tv);
138 root 1.1 }
139    
140     static void
141 root 1.37 ev_x_cb (struct event *ev, int revents)
142 root 1.1 {
143 root 1.45 revents &= EV_READ | EV_WRITE | EV_TIMER | EV_SIGNAL;
144 root 1.1
145     ev->ev_res = revents;
146 root 1.26 ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg);
147 root 1.1 }
148    
149     static void
150 root 1.37 ev_x_cb_sig (EV_P_ struct ev_signal *w, int revents)
151 root 1.1 {
152 root 1.20 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig));
153    
154     if (revents & EV_ERROR)
155     event_del (ev);
156    
157 root 1.37 ev_x_cb (ev, revents);
158 root 1.1 }
159    
160     static void
161 root 1.37 ev_x_cb_io (EV_P_ struct ev_io *w, int revents)
162 root 1.1 {
163 root 1.7 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io));
164    
165 root 1.32 if ((revents & EV_ERROR) || !(ev->ev_events & EV_PERSIST))
166 root 1.20 event_del (ev);
167 root 1.7
168 root 1.37 ev_x_cb (ev, revents);
169 root 1.1 }
170    
171     static void
172 root 1.37 ev_x_cb_to (EV_P_ struct ev_timer *w, int revents)
173 root 1.1 {
174 root 1.14 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to));
175    
176     event_del (ev);
177    
178 root 1.37 ev_x_cb (ev, revents);
179 root 1.1 }
180    
181     void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
182     {
183 root 1.14 if (events & EV_SIGNAL)
184 root 1.37 ev_init (&ev->iosig.sig, ev_x_cb_sig);
185 root 1.14 else
186 root 1.37 ev_init (&ev->iosig.io, ev_x_cb_io);
187 root 1.4
188 root 1.37 ev_init (&ev->to, ev_x_cb_to);
189 root 1.1
190 root 1.37 ev->ev_base = ev_x_cur; /* not threadsafe, but it's how libevent works */
191 root 1.1 ev->ev_fd = fd;
192     ev->ev_events = events;
193     ev->ev_pri = 0;
194     ev->ev_callback = cb;
195     ev->ev_arg = arg;
196     ev->ev_res = 0;
197 root 1.27 ev->ev_flags = EVLIST_INIT;
198 root 1.1 }
199    
200     int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
201     {
202 root 1.37 return event_base_once (ev_x_cur, fd, events, cb, arg, tv);
203 root 1.1 }
204    
205     int event_add (struct event *ev, struct timeval *tv)
206     {
207 root 1.7 dLOOPev;
208    
209 root 1.4 if (ev->ev_events & EV_SIGNAL)
210 root 1.1 {
211 root 1.31 if (!ev_is_active (&ev->iosig.sig))
212     {
213     ev_signal_set (&ev->iosig.sig, ev->ev_fd);
214     ev_signal_start (EV_A_ &ev->iosig.sig);
215 root 1.27
216 root 1.31 ev->ev_flags |= EVLIST_SIGNAL;
217     }
218 root 1.1 }
219 root 1.4 else if (ev->ev_events & (EV_READ | EV_WRITE))
220 root 1.1 {
221 root 1.31 if (!ev_is_active (&ev->iosig.io))
222     {
223     ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE));
224     ev_io_start (EV_A_ &ev->iosig.io);
225 root 1.27
226 root 1.31 ev->ev_flags |= EVLIST_INSERTED;
227     }
228 root 1.1 }
229    
230 root 1.4 if (tv)
231 root 1.1 {
232 root 1.37 ev->to.repeat = ev_tv_get (tv);
233 root 1.31 ev_timer_again (EV_A_ &ev->to);
234 root 1.27 ev->ev_flags |= EVLIST_TIMEOUT;
235 root 1.1 }
236 root 1.31 else
237 root 1.32 {
238 root 1.31 ev_timer_stop (EV_A_ &ev->to);
239 root 1.32 ev->ev_flags &= ~EVLIST_TIMEOUT;
240     }
241 root 1.1
242 root 1.27 ev->ev_flags |= EVLIST_ACTIVE;
243    
244 root 1.1 return 0;
245     }
246    
247     int event_del (struct event *ev)
248     {
249 root 1.7 dLOOPev;
250    
251 root 1.4 if (ev->ev_events & EV_SIGNAL)
252 root 1.32 ev_signal_stop (EV_A_ &ev->iosig.sig);
253 root 1.16 else if (ev->ev_events & (EV_READ | EV_WRITE))
254 root 1.32 ev_io_stop (EV_A_ &ev->iosig.io);
255 root 1.1
256     if (ev_is_active (&ev->to))
257 root 1.7 ev_timer_stop (EV_A_ &ev->to);
258 root 1.1
259 root 1.27 ev->ev_flags = EVLIST_INIT;
260    
261 root 1.1 return 0;
262     }
263    
264 root 1.21 void event_active (struct event *ev, int res, short ncalls)
265     {
266 root 1.23 dLOOPev;
267    
268 root 1.21 if (res & EV_TIMEOUT)
269 root 1.23 ev_feed_event (EV_A_ &ev->to, res & EV_TIMEOUT);
270 root 1.21
271     if (res & EV_SIGNAL)
272 root 1.23 ev_feed_event (EV_A_ &ev->iosig.sig, res & EV_SIGNAL);
273 root 1.21
274     if (res & (EV_READ | EV_WRITE))
275 root 1.23 ev_feed_event (EV_A_ &ev->iosig.io, res & (EV_READ | EV_WRITE));
276 root 1.21 }
277    
278 root 1.1 int event_pending (struct event *ev, short events, struct timeval *tv)
279     {
280 root 1.18 short revents = 0;
281 root 1.7 dLOOPev;
282    
283 root 1.4 if (ev->ev_events & EV_SIGNAL)
284     {
285     /* sig */
286 root 1.14 if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig))
287 root 1.4 revents |= EV_SIGNAL;
288     }
289 root 1.16 else if (ev->ev_events & (EV_READ | EV_WRITE))
290 root 1.4 {
291     /* io */
292 root 1.14 if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io))
293 root 1.4 revents |= ev->ev_events & (EV_READ | EV_WRITE);
294     }
295 root 1.1
296 root 1.14 if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to))
297 root 1.1 {
298     revents |= EV_TIMEOUT;
299    
300     if (tv)
301 root 1.47 {
302     ev_tstamp at = ev_now (EV_A);
303    
304     tv->tv_sec = (long)at;
305     tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6);
306     }
307 root 1.1 }
308    
309     return events & revents;
310     }
311    
312     int event_priority_init (int npri)
313     {
314 root 1.37 return event_base_priority_init (ev_x_cur, npri);
315 root 1.1 }
316    
317     int event_priority_set (struct event *ev, int pri)
318     {
319     ev->ev_pri = pri;
320    
321     return 0;
322     }
323    
324     int event_base_set (struct event_base *base, struct event *ev)
325     {
326     ev->ev_base = base;
327    
328     return 0;
329     }
330    
331     int event_base_loop (struct event_base *base, int flags)
332     {
333 root 1.7 dLOOPbase;
334 root 1.8
335 root 1.45 ev_run (EV_A_ flags);
336 root 1.1
337     return 0;
338     }
339    
340     int event_base_dispatch (struct event_base *base)
341     {
342     return event_base_loop (base, 0);
343     }
344    
345     static void
346 root 1.37 ev_x_loopexit_cb (int revents, void *base)
347 root 1.1 {
348 root 1.9 dLOOPbase;
349    
350 root 1.45 ev_break (EV_A_ EVBREAK_ONE);
351 root 1.1 }
352    
353     int event_base_loopexit (struct event_base *base, struct timeval *tv)
354     {
355 root 1.37 ev_tstamp after = ev_tv_get (tv);
356 root 1.7 dLOOPbase;
357 root 1.1
358 root 1.37 ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., ev_x_loopexit_cb, (void *)base);
359 root 1.5
360 root 1.34 return 0;
361 root 1.1 }
362    
363 root 1.37 struct ev_x_once
364 root 1.1 {
365     int fd;
366     void (*cb)(int, short, void *);
367     void *arg;
368     };
369    
370     static void
371 root 1.37 ev_x_once_cb (int revents, void *arg)
372 root 1.1 {
373 root 1.37 struct ev_x_once *once = (struct ev_x_once *)arg;
374 root 1.1
375 root 1.26 once->cb (once->fd, (short)revents, once->arg);
376 root 1.1 free (once);
377     }
378    
379     int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
380     {
381 root 1.37 struct ev_x_once *once = (struct ev_x_once *)malloc (sizeof (struct ev_x_once));
382 root 1.7 dLOOPbase;
383 root 1.1
384     if (!once)
385     return -1;
386    
387     once->fd = fd;
388     once->cb = cb;
389     once->arg = arg;
390    
391 root 1.37 ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), ev_tv_get (tv), ev_x_once_cb, (void *)once);
392 root 1.1
393     return 0;
394     }
395    
396     int event_base_priority_init (struct event_base *base, int npri)
397     {
398 root 1.13 /*dLOOPbase;*/
399 root 1.7
400 root 1.1 return 0;
401     }
402