ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/event.c
Revision: 1.18
Committed: Tue Nov 6 13:17:55 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-0_5
Changes since 1.17: +7 -4 lines
Log Message:
now port to microsofts goddamn broken pseudo-c-we-do-it-different-to-spite-you so-called c compiler

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