ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/event.c
Revision: 1.9
Committed: Sun Nov 4 00:24:17 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.8: +29 -19 lines
Log Message:
wow the event api is broken

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     #include <sys/time.h>
35    
36     #include "event.h"
37    
38 root 1.9 #if EV_MULTIPLICITY
39     # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base
40     # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base
41 root 1.8 #else
42     # define dLOOPev
43     # define dLOOPbase
44     #endif
45 root 1.7
46 root 1.9 /* never accessed, will always be cast from/to ev_loop */
47 root 1.1 struct event_base
48     {
49     int dummy;
50     };
51    
52     static struct event_base x_base, *x_cur;
53    
54     static void
55     tv_set (struct timeval *tv, ev_tstamp at)
56     {
57     tv->tv_sec = (long)at;
58     tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6);
59     }
60    
61     static ev_tstamp
62     tv_get (struct timeval *tv)
63     {
64     if (tv)
65     return tv->tv_sec + tv->tv_usec * 1e-6;
66     else
67     return -1.;
68     }
69    
70     #define EVENT_VERSION(a,b) # a "." # b
71    
72     const char *event_get_version (void)
73     {
74     return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR);
75     }
76    
77     const char *event_get_method (void)
78     {
79     return "libev";
80     }
81    
82     void *event_init (void)
83     {
84 root 1.9 #if EV_MULTIPLICITY
85     x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO);
86     #else
87     x_cur = &x_base;
88     #endif
89 root 1.1
90 root 1.9 return x_cur;
91 root 1.1 }
92    
93     void event_base_free (struct event_base *base)
94     {
95 root 1.8 dLOOPbase;
96    
97 root 1.9 #if EV_MULTIPLICITY
98     ev_loop_delete (loop);
99     #endif
100 root 1.1 }
101    
102     int event_dispatch (void)
103     {
104     return event_base_dispatch (x_cur);
105     }
106    
107 root 1.5 #ifdef EV_STANDALONE
108 root 1.1 void event_set_log_callback (event_log_cb cb)
109     {
110     /* nop */
111     }
112 root 1.5 #endif
113 root 1.1
114     int event_loop (int flags)
115     {
116     return event_base_loop (x_cur, flags);
117     }
118    
119     int event_loopexit (struct timeval *tv)
120     {
121 root 1.5 return event_base_loopexit (x_cur, tv);
122 root 1.1 }
123    
124     static void
125     x_cb (struct event *ev, int revents)
126     {
127     revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL;
128    
129     ev->ev_res = revents;
130     ev->ev_callback (ev->ev_fd, revents, ev->ev_arg);
131     }
132    
133     static void
134 root 1.7 x_cb_sig (EV_P_ struct ev_signal *w, int revents)
135 root 1.1 {
136 root 1.7 x_cb ((struct event *)(((char *)w) - offsetof (struct event, iosig.sig)), revents);
137 root 1.1 }
138    
139     static void
140 root 1.7 x_cb_io (EV_P_ struct ev_io *w, int revents)
141 root 1.1 {
142 root 1.7 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io));
143    
144     if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w))
145 root 1.9 ev_io_stop (EV_A_ w);
146 root 1.7
147     x_cb (ev, revents);
148 root 1.1 }
149    
150     static void
151 root 1.7 x_cb_to (EV_P_ struct ev_timer *w, int revents)
152 root 1.1 {
153 root 1.7 x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents);
154 root 1.1 }
155    
156     void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
157     {
158 root 1.9 if (!ev->initialised)
159     {
160     ev->initialised = 1;
161    
162     if (events & EV_SIGNAL)
163     ev_watcher_init (&ev->iosig.sig, x_cb_sig);
164     else
165     ev_watcher_init (&ev->iosig.io, x_cb_io);
166 root 1.4
167 root 1.9 ev_watcher_init (&ev->to, x_cb_to);
168     }
169 root 1.1
170 root 1.9 ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */
171 root 1.1 ev->ev_fd = fd;
172     ev->ev_events = events;
173     ev->ev_pri = 0;
174     ev->ev_callback = cb;
175     ev->ev_arg = arg;
176     ev->ev_res = 0;
177     }
178    
179     int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
180     {
181 root 1.5 return event_base_once (x_cur, fd, events, cb, arg, tv);
182 root 1.1 }
183    
184     int event_add (struct event *ev, struct timeval *tv)
185     {
186 root 1.7 dLOOPev;
187    
188 root 1.4 /* disable all watchers */
189     event_del (ev);
190    
191     if (ev->ev_events & EV_SIGNAL)
192 root 1.1 {
193 root 1.4 ev_signal_set (&ev->iosig.sig, ev->ev_fd);
194 root 1.7 ev_signal_start (EV_A_ &ev->iosig.sig);
195 root 1.1 }
196 root 1.4 else if (ev->ev_events & (EV_READ | EV_WRITE))
197 root 1.1 {
198 root 1.4 ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE));
199 root 1.7 ev_io_start (EV_A_ &ev->iosig.io);
200 root 1.1 }
201    
202 root 1.4 if (tv)
203 root 1.1 {
204 root 1.4 ev_timer_set (&ev->to, tv_get (tv), 0.);
205 root 1.7 ev_timer_start (EV_A_ &ev->to);
206 root 1.1 }
207    
208     return 0;
209     }
210    
211     int event_del (struct event *ev)
212     {
213 root 1.7 dLOOPev;
214    
215 root 1.4 if (ev->ev_events & EV_SIGNAL)
216     {
217     /* sig */
218     if (ev_is_active (&ev->iosig.sig))
219 root 1.7 ev_signal_stop (EV_A_ &ev->iosig.sig);
220 root 1.4 }
221     else
222 root 1.1 {
223 root 1.4 /* io */
224     if (ev_is_active (&ev->iosig.io))
225 root 1.7 ev_io_stop (EV_A_ &ev->iosig.io);
226 root 1.1 }
227    
228     if (ev_is_active (&ev->to))
229 root 1.7 ev_timer_stop (EV_A_ &ev->to);
230 root 1.1
231     return 0;
232     }
233    
234     int event_pending (struct event *ev, short events, struct timeval *tv)
235     {
236 root 1.7 dLOOPev;
237    
238 root 1.5 short revents = 0;
239 root 1.1
240 root 1.4 if (ev->ev_events & EV_SIGNAL)
241     {
242     /* sig */
243     if (ev->iosig.sig.pending)
244     revents |= EV_SIGNAL;
245     }
246     else
247     {
248     /* io */
249     if (ev->iosig.io.pending)
250     revents |= ev->ev_events & (EV_READ | EV_WRITE);
251     }
252 root 1.1
253     if (ev->to.pending)
254     {
255     revents |= EV_TIMEOUT;
256    
257     if (tv)
258 root 1.7 tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */
259 root 1.1 }
260    
261     return events & revents;
262     }
263    
264     int event_priority_init (int npri)
265     {
266     return event_base_priority_init (x_cur, npri);
267     }
268    
269     int event_priority_set (struct event *ev, int pri)
270     {
271     ev->ev_pri = pri;
272    
273     return 0;
274     }
275    
276     int event_base_set (struct event_base *base, struct event *ev)
277     {
278     ev->ev_base = base;
279    
280     return 0;
281     }
282    
283     int event_base_loop (struct event_base *base, int flags)
284     {
285 root 1.7 dLOOPbase;
286 root 1.8
287     ev_loop (EV_A_ flags);
288 root 1.1
289     return 0;
290     }
291    
292     int event_base_dispatch (struct event_base *base)
293     {
294     return event_base_loop (base, 0);
295     }
296    
297     static void
298 root 1.9 x_loopexit_cb (int revents, void *base)
299 root 1.1 {
300 root 1.9 dLOOPbase;
301    
302     ev_unloop (EV_A_ EVUNLOOP_ONCE);
303 root 1.1 }
304    
305     int event_base_loopexit (struct event_base *base, struct timeval *tv)
306     {
307 root 1.7 dLOOPbase;
308 root 1.1 ev_tstamp after = tv_get (tv);
309    
310 root 1.7 ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base);
311 root 1.5
312     return -1;
313 root 1.1 }
314    
315     struct x_once
316     {
317     int fd;
318     void (*cb)(int, short, void *);
319     void *arg;
320     };
321    
322     static void
323     x_once_cb (int revents, void *arg)
324     {
325     struct x_once *once = arg;
326    
327     once->cb (once->fd, revents, once->arg);
328     free (once);
329     }
330    
331     int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
332     {
333 root 1.7 dLOOPbase;
334 root 1.1 struct x_once *once = malloc (sizeof (struct x_once));
335    
336     if (!once)
337     return -1;
338    
339     once->fd = fd;
340     once->cb = cb;
341     once->arg = arg;
342    
343 root 1.7 ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once);
344 root 1.1
345     return 0;
346     }
347    
348     int event_base_priority_init (struct event_base *base, int npri)
349     {
350 root 1.7 dLOOPbase;
351    
352 root 1.1 return 0;
353     }
354