ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/event.c
Revision: 1.31
Committed: Wed Nov 14 05:30:02 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-0_9, rel-1_4, rel-1_1, rel-1_3, rel-1_2, rel-1_5
Changes since 1.30: +17 -12 lines
Log Message:
speed up event emulation

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 root 1.28 x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO);
95 root 1.10 else
96 root 1.28 x_cur = (struct event_base *)ev_default_loop (EVFLAG_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.28 x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_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.29 if (ev_default_loop (EVFLAG_AUTO) != loop)
112 root 1.13 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 root 1.26 ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg);
145 root 1.1 }
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.27 {
167     ev_io_stop (EV_A_ w);
168     ev->ev_flags &= ~EVLIST_INSERTED;
169     }
170 root 1.7
171     x_cb (ev, revents);
172 root 1.1 }
173    
174     static void
175 root 1.7 x_cb_to (EV_P_ struct ev_timer *w, int revents)
176 root 1.1 {
177 root 1.14 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to));
178    
179     event_del (ev);
180    
181     x_cb (ev, revents);
182 root 1.1 }
183    
184     void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
185     {
186 root 1.14 if (events & EV_SIGNAL)
187 root 1.25 ev_init (&ev->iosig.sig, x_cb_sig);
188 root 1.14 else
189 root 1.25 ev_init (&ev->iosig.io, x_cb_io);
190 root 1.4
191 root 1.25 ev_init (&ev->to, x_cb_to);
192 root 1.1
193 root 1.9 ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */
194 root 1.1 ev->ev_fd = fd;
195     ev->ev_events = events;
196     ev->ev_pri = 0;
197     ev->ev_callback = cb;
198     ev->ev_arg = arg;
199     ev->ev_res = 0;
200 root 1.27 ev->ev_flags = EVLIST_INIT;
201 root 1.1 }
202    
203     int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
204     {
205 root 1.5 return event_base_once (x_cur, fd, events, cb, arg, tv);
206 root 1.1 }
207    
208     int event_add (struct event *ev, struct timeval *tv)
209     {
210 root 1.7 dLOOPev;
211    
212 root 1.4 if (ev->ev_events & EV_SIGNAL)
213 root 1.1 {
214 root 1.31 if (!ev_is_active (&ev->iosig.sig))
215     {
216     ev_signal_set (&ev->iosig.sig, ev->ev_fd);
217     ev_signal_start (EV_A_ &ev->iosig.sig);
218 root 1.27
219 root 1.31 ev->ev_flags |= EVLIST_SIGNAL;
220     }
221 root 1.1 }
222 root 1.4 else if (ev->ev_events & (EV_READ | EV_WRITE))
223 root 1.1 {
224 root 1.31 if (!ev_is_active (&ev->iosig.io))
225     {
226     ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE));
227     ev_io_start (EV_A_ &ev->iosig.io);
228 root 1.27
229 root 1.31 ev->ev_flags |= EVLIST_INSERTED;
230     }
231 root 1.1 }
232    
233 root 1.4 if (tv)
234 root 1.1 {
235 root 1.31 ev->to.repeat = tv_get (tv);
236     ev_timer_again (EV_A_ &ev->to);
237 root 1.27 ev->ev_flags |= EVLIST_TIMEOUT;
238 root 1.1 }
239 root 1.31 else
240     if (ev_is_active (&ev->to))
241     ev_timer_stop (EV_A_ &ev->to);
242 root 1.1
243 root 1.27 ev->ev_flags |= EVLIST_ACTIVE;
244    
245 root 1.1 return 0;
246     }
247    
248     int event_del (struct event *ev)
249     {
250 root 1.7 dLOOPev;
251    
252 root 1.4 if (ev->ev_events & EV_SIGNAL)
253     {
254     /* sig */
255     if (ev_is_active (&ev->iosig.sig))
256 root 1.7 ev_signal_stop (EV_A_ &ev->iosig.sig);
257 root 1.4 }
258 root 1.16 else if (ev->ev_events & (EV_READ | EV_WRITE))
259 root 1.1 {
260 root 1.4 /* io */
261     if (ev_is_active (&ev->iosig.io))
262 root 1.7 ev_io_stop (EV_A_ &ev->iosig.io);
263 root 1.1 }
264    
265     if (ev_is_active (&ev->to))
266 root 1.7 ev_timer_stop (EV_A_ &ev->to);
267 root 1.1
268 root 1.27 ev->ev_flags = EVLIST_INIT;
269    
270 root 1.1 return 0;
271     }
272    
273 root 1.21 void event_active (struct event *ev, int res, short ncalls)
274     {
275 root 1.23 dLOOPev;
276    
277 root 1.21 if (res & EV_TIMEOUT)
278 root 1.23 ev_feed_event (EV_A_ &ev->to, res & EV_TIMEOUT);
279 root 1.21
280     if (res & EV_SIGNAL)
281 root 1.23 ev_feed_event (EV_A_ &ev->iosig.sig, res & EV_SIGNAL);
282 root 1.21
283     if (res & (EV_READ | EV_WRITE))
284 root 1.23 ev_feed_event (EV_A_ &ev->iosig.io, res & (EV_READ | EV_WRITE));
285 root 1.21 }
286    
287 root 1.1 int event_pending (struct event *ev, short events, struct timeval *tv)
288     {
289 root 1.18 short revents = 0;
290 root 1.7 dLOOPev;
291    
292 root 1.1
293 root 1.4 if (ev->ev_events & EV_SIGNAL)
294     {
295     /* sig */
296 root 1.14 if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig))
297 root 1.4 revents |= EV_SIGNAL;
298     }
299 root 1.16 else if (ev->ev_events & (EV_READ | EV_WRITE))
300 root 1.4 {
301     /* io */
302 root 1.14 if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io))
303 root 1.4 revents |= ev->ev_events & (EV_READ | EV_WRITE);
304     }
305 root 1.1
306 root 1.14 if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to))
307 root 1.1 {
308     revents |= EV_TIMEOUT;
309    
310     if (tv)
311 root 1.7 tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */
312 root 1.1 }
313    
314     return events & revents;
315     }
316    
317     int event_priority_init (int npri)
318     {
319     return event_base_priority_init (x_cur, npri);
320     }
321    
322     int event_priority_set (struct event *ev, int pri)
323     {
324     ev->ev_pri = pri;
325    
326     return 0;
327     }
328    
329     int event_base_set (struct event_base *base, struct event *ev)
330     {
331     ev->ev_base = base;
332    
333     return 0;
334     }
335    
336     int event_base_loop (struct event_base *base, int flags)
337     {
338 root 1.7 dLOOPbase;
339 root 1.8
340     ev_loop (EV_A_ flags);
341 root 1.1
342     return 0;
343     }
344    
345     int event_base_dispatch (struct event_base *base)
346     {
347     return event_base_loop (base, 0);
348     }
349    
350     static void
351 root 1.9 x_loopexit_cb (int revents, void *base)
352 root 1.1 {
353 root 1.9 dLOOPbase;
354    
355 root 1.30 ev_unloop (EV_A_ EVUNLOOP_ONE);
356 root 1.1 }
357    
358     int event_base_loopexit (struct event_base *base, struct timeval *tv)
359     {
360 root 1.18 ev_tstamp after = tv_get (tv);
361 root 1.7 dLOOPbase;
362 root 1.1
363 root 1.7 ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base);
364 root 1.5
365     return -1;
366 root 1.1 }
367    
368     struct x_once
369     {
370     int fd;
371     void (*cb)(int, short, void *);
372     void *arg;
373     };
374    
375     static void
376     x_once_cb (int revents, void *arg)
377     {
378 root 1.19 struct x_once *once = (struct x_once *)arg;
379 root 1.1
380 root 1.26 once->cb (once->fd, (short)revents, once->arg);
381 root 1.1 free (once);
382     }
383    
384     int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
385     {
386 root 1.19 struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once));
387 root 1.7 dLOOPbase;
388 root 1.1
389     if (!once)
390     return -1;
391    
392     once->fd = fd;
393     once->cb = cb;
394     once->arg = arg;
395    
396 root 1.7 ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once);
397 root 1.1
398     return 0;
399     }
400    
401     int event_base_priority_init (struct event_base *base, int npri)
402     {
403 root 1.13 /*dLOOPbase;*/
404 root 1.7
405 root 1.1 return 0;
406     }
407