ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/event.c
Revision: 1.25
Committed: Fri Nov 9 21:48:23 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.24: +3 -3 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*
2     * libevent compatibility layer
3     *
4     * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
5     * All rights reserved.
6     *
7     * Redistribution and use in source and binary forms, with or without
8     * modification, are permitted provided that the following conditions are
9     * met:
10     *
11     * * Redistributions of source code must retain the above copyright
12     * notice, this list of conditions and the following disclaimer.
13     *
14     * * Redistributions in binary form must reproduce the above
15     * copyright notice, this list of conditions and the following
16     * disclaimer in the documentation and/or other materials provided
17     * with the distribution.
18     *
19     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20     * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
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     */
31    
32     #include <stddef.h>
33     #include <stdlib.h>
34 root 1.10 #include <assert.h>
35 root 1.1
36 root 1.18 #ifndef WIN32
37     # include <sys/time.h>
38     #endif
39    
40 root 1.24 #ifdef EV_EVENT_H
41     # include EV_EVENT_H
42     #else
43     # include "event.h"
44     #endif
45 root 1.1
46 root 1.9 #if EV_MULTIPLICITY
47     # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base
48     # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base
49 root 1.8 #else
50     # define dLOOPev
51     # define dLOOPbase
52     #endif
53 root 1.7
54 root 1.9 /* never accessed, will always be cast from/to ev_loop */
55 root 1.1 struct event_base
56     {
57     int dummy;
58     };
59    
60 root 1.10 static struct event_base *x_cur;
61 root 1.1
62     static void
63     tv_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    
69     static ev_tstamp
70     tv_get (struct timeval *tv)
71     {
72     if (tv)
73     return tv->tv_sec + tv->tv_usec * 1e-6;
74     else
75     return -1.;
76     }
77    
78     #define EVENT_VERSION(a,b) # a "." # b
79    
80     const char *event_get_version (void)
81     {
82     return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR);
83     }
84    
85     const char *event_get_method (void)
86     {
87     return "libev";
88     }
89    
90     void *event_init (void)
91     {
92 root 1.9 #if EV_MULTIPLICITY
93 root 1.10 if (x_cur)
94     x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO);
95     else
96 root 1.13 x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO);
97 root 1.9 #else
98 root 1.10 assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur));
99    
100 root 1.17 x_cur = (struct event_base *)(long)ev_default_loop (EVMETHOD_AUTO);
101 root 1.9 #endif
102 root 1.1
103 root 1.9 return x_cur;
104 root 1.1 }
105    
106     void event_base_free (struct event_base *base)
107     {
108 root 1.8 dLOOPbase;
109    
110 root 1.9 #if EV_MULTIPLICITY
111 root 1.13 if (ev_default_loop (EVMETHOD_AUTO) != loop)
112     ev_loop_destroy (loop);
113 root 1.9 #endif
114 root 1.1 }
115    
116     int event_dispatch (void)
117     {
118     return event_base_dispatch (x_cur);
119     }
120    
121 root 1.12 #ifdef EV_STANDALONE
122 root 1.1 void event_set_log_callback (event_log_cb cb)
123     {
124     /* nop */
125     }
126 root 1.5 #endif
127 root 1.1
128     int event_loop (int flags)
129     {
130     return event_base_loop (x_cur, flags);
131     }
132    
133     int event_loopexit (struct timeval *tv)
134     {
135 root 1.5 return event_base_loopexit (x_cur, tv);
136 root 1.1 }
137    
138     static void
139     x_cb (struct event *ev, int revents)
140     {
141     revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL;
142    
143     ev->ev_res = revents;
144     ev->ev_callback (ev->ev_fd, revents, ev->ev_arg);
145     }
146    
147     static void
148 root 1.7 x_cb_sig (EV_P_ struct ev_signal *w, int revents)
149 root 1.1 {
150 root 1.20 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig));
151    
152     if (revents & EV_ERROR)
153     event_del (ev);
154    
155     x_cb (ev, revents);
156 root 1.1 }
157    
158     static void
159 root 1.7 x_cb_io (EV_P_ struct ev_io *w, int revents)
160 root 1.1 {
161 root 1.7 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io));
162    
163 root 1.20 if (revents & EV_ERROR)
164     event_del (ev);
165     else if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w))
166 root 1.9 ev_io_stop (EV_A_ w);
167 root 1.7
168     x_cb (ev, revents);
169 root 1.1 }
170    
171     static void
172 root 1.7 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     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.25 ev_init (&ev->iosig.sig, x_cb_sig);
185 root 1.14 else
186 root 1.25 ev_init (&ev->iosig.io, x_cb_io);
187 root 1.4
188 root 1.25 ev_init (&ev->to, x_cb_to);
189 root 1.1
190 root 1.9 ev->ev_base = x_cur; /* not threadsafe, but its like 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     }
198    
199     int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
200     {
201 root 1.5 return event_base_once (x_cur, fd, events, cb, arg, tv);
202 root 1.1 }
203    
204     int event_add (struct event *ev, struct timeval *tv)
205     {
206 root 1.7 dLOOPev;
207    
208 root 1.4 /* disable all watchers */
209     event_del (ev);
210    
211     if (ev->ev_events & EV_SIGNAL)
212 root 1.1 {
213 root 1.4 ev_signal_set (&ev->iosig.sig, ev->ev_fd);
214 root 1.7 ev_signal_start (EV_A_ &ev->iosig.sig);
215 root 1.1 }
216 root 1.4 else if (ev->ev_events & (EV_READ | EV_WRITE))
217 root 1.1 {
218 root 1.4 ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE));
219 root 1.7 ev_io_start (EV_A_ &ev->iosig.io);
220 root 1.1 }
221    
222 root 1.4 if (tv)
223 root 1.1 {
224 root 1.4 ev_timer_set (&ev->to, tv_get (tv), 0.);
225 root 1.7 ev_timer_start (EV_A_ &ev->to);
226 root 1.1 }
227    
228     return 0;
229     }
230    
231     int event_del (struct event *ev)
232     {
233 root 1.7 dLOOPev;
234    
235 root 1.4 if (ev->ev_events & EV_SIGNAL)
236     {
237     /* sig */
238     if (ev_is_active (&ev->iosig.sig))
239 root 1.7 ev_signal_stop (EV_A_ &ev->iosig.sig);
240 root 1.4 }
241 root 1.16 else if (ev->ev_events & (EV_READ | EV_WRITE))
242 root 1.1 {
243 root 1.4 /* io */
244     if (ev_is_active (&ev->iosig.io))
245 root 1.7 ev_io_stop (EV_A_ &ev->iosig.io);
246 root 1.1 }
247    
248     if (ev_is_active (&ev->to))
249 root 1.7 ev_timer_stop (EV_A_ &ev->to);
250 root 1.1
251     return 0;
252     }
253    
254 root 1.21 void event_active (struct event *ev, int res, short ncalls)
255     {
256 root 1.23 dLOOPev;
257    
258 root 1.21 if (res & EV_TIMEOUT)
259 root 1.23 ev_feed_event (EV_A_ &ev->to, res & EV_TIMEOUT);
260 root 1.21
261     if (res & EV_SIGNAL)
262 root 1.23 ev_feed_event (EV_A_ &ev->iosig.sig, res & EV_SIGNAL);
263 root 1.21
264     if (res & (EV_READ | EV_WRITE))
265 root 1.23 ev_feed_event (EV_A_ &ev->iosig.io, res & (EV_READ | EV_WRITE));
266 root 1.21 }
267    
268 root 1.1 int event_pending (struct event *ev, short events, struct timeval *tv)
269     {
270 root 1.18 short revents = 0;
271 root 1.7 dLOOPev;
272    
273 root 1.1
274 root 1.4 if (ev->ev_events & EV_SIGNAL)
275     {
276     /* sig */
277 root 1.14 if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig))
278 root 1.4 revents |= EV_SIGNAL;
279     }
280 root 1.16 else if (ev->ev_events & (EV_READ | EV_WRITE))
281 root 1.4 {
282     /* io */
283 root 1.14 if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io))
284 root 1.4 revents |= ev->ev_events & (EV_READ | EV_WRITE);
285     }
286 root 1.1
287 root 1.14 if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to))
288 root 1.1 {
289     revents |= EV_TIMEOUT;
290    
291     if (tv)
292 root 1.7 tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */
293 root 1.1 }
294    
295     return events & revents;
296     }
297    
298     int event_priority_init (int npri)
299     {
300     return event_base_priority_init (x_cur, npri);
301     }
302    
303     int event_priority_set (struct event *ev, int pri)
304     {
305     ev->ev_pri = pri;
306    
307     return 0;
308     }
309    
310     int event_base_set (struct event_base *base, struct event *ev)
311     {
312     ev->ev_base = base;
313    
314     return 0;
315     }
316    
317     int event_base_loop (struct event_base *base, int flags)
318     {
319 root 1.7 dLOOPbase;
320 root 1.8
321     ev_loop (EV_A_ flags);
322 root 1.1
323     return 0;
324     }
325    
326     int event_base_dispatch (struct event_base *base)
327     {
328     return event_base_loop (base, 0);
329     }
330    
331     static void
332 root 1.9 x_loopexit_cb (int revents, void *base)
333 root 1.1 {
334 root 1.9 dLOOPbase;
335    
336     ev_unloop (EV_A_ EVUNLOOP_ONCE);
337 root 1.1 }
338    
339     int event_base_loopexit (struct event_base *base, struct timeval *tv)
340     {
341 root 1.18 ev_tstamp after = tv_get (tv);
342 root 1.7 dLOOPbase;
343 root 1.1
344 root 1.7 ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base);
345 root 1.5
346     return -1;
347 root 1.1 }
348    
349     struct x_once
350     {
351     int fd;
352     void (*cb)(int, short, void *);
353     void *arg;
354     };
355    
356     static void
357     x_once_cb (int revents, void *arg)
358     {
359 root 1.19 struct x_once *once = (struct x_once *)arg;
360 root 1.1
361     once->cb (once->fd, revents, once->arg);
362     free (once);
363     }
364    
365     int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
366     {
367 root 1.19 struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once));
368 root 1.7 dLOOPbase;
369 root 1.1
370     if (!once)
371     return -1;
372    
373     once->fd = fd;
374     once->cb = cb;
375     once->arg = arg;
376    
377 root 1.7 ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once);
378 root 1.1
379     return 0;
380     }
381    
382     int event_base_priority_init (struct event_base *base, int npri)
383     {
384 root 1.13 /*dLOOPbase;*/
385 root 1.7
386 root 1.1 return 0;
387     }
388