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