ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/event.c
Revision: 1.15
Committed: Sun Nov 4 20:38:07 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.14: +4 -0 lines
Log Message:
need to rethinkg design, maybe use 'proper' struct subclassing due to aliasing restrictions in C

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.14 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to));
162    
163     event_del (ev);
164    
165     x_cb (ev, revents);
166 root 1.1 }
167    
168     void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
169     {
170 root 1.15 printf ("event set %p\n", ev);//D
171 root 1.14 if (events & EV_SIGNAL)
172     ev_watcher_init (&ev->iosig.sig, x_cb_sig);
173     else
174     ev_watcher_init (&ev->iosig.io, x_cb_io);
175 root 1.4
176 root 1.14 ev_watcher_init (&ev->to, x_cb_to);
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.15 printf ("event add %p %p\n", ev, &ev->to);//D
195 root 1.7 dLOOPev;
196    
197 root 1.4 /* disable all watchers */
198     event_del (ev);
199    
200     if (ev->ev_events & EV_SIGNAL)
201 root 1.1 {
202 root 1.4 ev_signal_set (&ev->iosig.sig, ev->ev_fd);
203 root 1.7 ev_signal_start (EV_A_ &ev->iosig.sig);
204 root 1.1 }
205 root 1.4 else if (ev->ev_events & (EV_READ | EV_WRITE))
206 root 1.1 {
207 root 1.4 ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE));
208 root 1.7 ev_io_start (EV_A_ &ev->iosig.io);
209 root 1.1 }
210    
211 root 1.4 if (tv)
212 root 1.1 {
213 root 1.4 ev_timer_set (&ev->to, tv_get (tv), 0.);
214 root 1.7 ev_timer_start (EV_A_ &ev->to);
215 root 1.1 }
216    
217     return 0;
218     }
219    
220     int event_del (struct event *ev)
221     {
222 root 1.15 printf ("event del %p\n", ev);//D
223 root 1.7 dLOOPev;
224    
225 root 1.4 if (ev->ev_events & EV_SIGNAL)
226     {
227     /* sig */
228     if (ev_is_active (&ev->iosig.sig))
229 root 1.7 ev_signal_stop (EV_A_ &ev->iosig.sig);
230 root 1.4 }
231     else
232 root 1.1 {
233 root 1.4 /* io */
234     if (ev_is_active (&ev->iosig.io))
235 root 1.7 ev_io_stop (EV_A_ &ev->iosig.io);
236 root 1.1 }
237    
238 root 1.15 printf ("to %p %d\n", &ev->to, ev->to.active);//D
239 root 1.1 if (ev_is_active (&ev->to))
240 root 1.7 ev_timer_stop (EV_A_ &ev->to);
241 root 1.1
242     return 0;
243     }
244    
245     int event_pending (struct event *ev, short events, struct timeval *tv)
246     {
247 root 1.7 dLOOPev;
248    
249 root 1.5 short revents = 0;
250 root 1.1
251 root 1.4 if (ev->ev_events & EV_SIGNAL)
252     {
253     /* sig */
254 root 1.14 if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig))
255 root 1.4 revents |= EV_SIGNAL;
256     }
257     else
258     {
259     /* io */
260 root 1.14 if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io))
261 root 1.4 revents |= ev->ev_events & (EV_READ | EV_WRITE);
262     }
263 root 1.1
264 root 1.14 if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to))
265 root 1.1 {
266     revents |= EV_TIMEOUT;
267    
268     if (tv)
269 root 1.7 tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */
270 root 1.1 }
271    
272     return events & revents;
273     }
274    
275     int event_priority_init (int npri)
276     {
277     return event_base_priority_init (x_cur, npri);
278     }
279    
280     int event_priority_set (struct event *ev, int pri)
281     {
282     ev->ev_pri = pri;
283    
284     return 0;
285     }
286    
287     int event_base_set (struct event_base *base, struct event *ev)
288     {
289     ev->ev_base = base;
290    
291     return 0;
292     }
293    
294     int event_base_loop (struct event_base *base, int flags)
295     {
296 root 1.7 dLOOPbase;
297 root 1.8
298     ev_loop (EV_A_ flags);
299 root 1.1
300     return 0;
301     }
302    
303     int event_base_dispatch (struct event_base *base)
304     {
305     return event_base_loop (base, 0);
306     }
307    
308     static void
309 root 1.9 x_loopexit_cb (int revents, void *base)
310 root 1.1 {
311 root 1.9 dLOOPbase;
312    
313     ev_unloop (EV_A_ EVUNLOOP_ONCE);
314 root 1.1 }
315    
316     int event_base_loopexit (struct event_base *base, struct timeval *tv)
317     {
318 root 1.7 dLOOPbase;
319 root 1.1 ev_tstamp after = tv_get (tv);
320    
321 root 1.7 ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base);
322 root 1.5
323     return -1;
324 root 1.1 }
325    
326     struct x_once
327     {
328     int fd;
329     void (*cb)(int, short, void *);
330     void *arg;
331     };
332    
333     static void
334     x_once_cb (int revents, void *arg)
335     {
336     struct x_once *once = arg;
337    
338     once->cb (once->fd, revents, once->arg);
339     free (once);
340     }
341    
342     int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
343     {
344 root 1.7 dLOOPbase;
345 root 1.1 struct x_once *once = malloc (sizeof (struct x_once));
346    
347     if (!once)
348     return -1;
349    
350     once->fd = fd;
351     once->cb = cb;
352     once->arg = arg;
353    
354 root 1.7 ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once);
355 root 1.1
356     return 0;
357     }
358    
359     int event_base_priority_init (struct event_base *base, int npri)
360     {
361 root 1.13 /*dLOOPbase;*/
362 root 1.7
363 root 1.1 return 0;
364     }
365