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