ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/event.c
(Generate patch)

Comparing libev/event.c (file contents):
Revision 1.33 by root, Tue Dec 25 07:05:46 2007 UTC vs.
Revision 1.45 by root, Thu Oct 21 12:32:48 2010 UTC

1/* 1/*
2 * libevent compatibility layer 2 * libevent compatibility layer
3 * 3 *
4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libev@schmorp.de>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without modifica- 7 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met: 8 * tion, are permitted provided that the following conditions are met:
9 * 9 *
39 39
40#include <stddef.h> 40#include <stddef.h>
41#include <stdlib.h> 41#include <stdlib.h>
42#include <assert.h> 42#include <assert.h>
43 43
44#ifndef WIN32
45# include <sys/time.h>
46#endif
47
48#ifdef EV_EVENT_H 44#ifdef EV_EVENT_H
49# include EV_EVENT_H 45# include EV_EVENT_H
50#else 46#else
51# include "event.h" 47# include "event.h"
52#endif 48#endif
63struct event_base 59struct event_base
64{ 60{
65 int dummy; 61 int dummy;
66}; 62};
67 63
68static struct event_base *x_cur; 64static struct event_base *ev_x_cur;
69
70static void
71tv_set (struct timeval *tv, ev_tstamp at)
72{
73 tv->tv_sec = (long)at;
74 tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6);
75}
76 65
77static ev_tstamp 66static ev_tstamp
78tv_get (struct timeval *tv) 67ev_tv_get (struct timeval *tv)
79{ 68{
80 if (tv) 69 if (tv)
70 {
81 return tv->tv_sec + tv->tv_usec * 1e-6; 71 ev_tstamp after = tv->tv_sec + tv->tv_usec * 1e-6;
72 return after ? after : 1e-6;
73 }
82 else 74 else
83 return -1.; 75 return -1.;
84} 76}
85 77
86#define EVENT_VERSION(a,b) # a "." # b 78#define EVENT_STRINGIFY(s) # s
79#define EVENT_VERSION(a,b) EVENT_STRINGIFY (a) "." EVENT_STRINGIFY (b)
87 80
88const char *event_get_version (void) 81const char *event_get_version (void)
89{ 82{
83 /* returns ABI, not API or library, version */
90 return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR); 84 return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR);
91} 85}
92 86
93const char *event_get_method (void) 87const char *event_get_method (void)
94{ 88{
96} 90}
97 91
98void *event_init (void) 92void *event_init (void)
99{ 93{
100#if EV_MULTIPLICITY 94#if EV_MULTIPLICITY
101 if (x_cur) 95 if (ev_x_cur)
102 x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO); 96 ev_x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO);
103 else 97 else
104 x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO); 98 ev_x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO);
105#else 99#else
106 assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur)); 100 assert (("libev: multiple event bases not supported when not compiled with EV_MULTIPLICITY", !ev_x_cur));
107 101
108 x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO); 102 ev_x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO);
109#endif 103#endif
110 104
111 return x_cur; 105 return ev_x_cur;
112} 106}
113 107
114void event_base_free (struct event_base *base) 108void event_base_free (struct event_base *base)
115{ 109{
116 dLOOPbase; 110 dLOOPbase;
121#endif 115#endif
122} 116}
123 117
124int event_dispatch (void) 118int event_dispatch (void)
125{ 119{
126 return event_base_dispatch (x_cur); 120 return event_base_dispatch (ev_x_cur);
127} 121}
128 122
129#ifdef EV_STANDALONE 123#ifdef EV_STANDALONE
130void event_set_log_callback (event_log_cb cb) 124void event_set_log_callback (event_log_cb cb)
131{ 125{
133} 127}
134#endif 128#endif
135 129
136int event_loop (int flags) 130int event_loop (int flags)
137{ 131{
138 return event_base_loop (x_cur, flags); 132 return event_base_loop (ev_x_cur, flags);
139} 133}
140 134
141int event_loopexit (struct timeval *tv) 135int event_loopexit (struct timeval *tv)
142{ 136{
143 return event_base_loopexit (x_cur, tv); 137 return event_base_loopexit (ev_x_cur, tv);
144} 138}
145 139
146static void 140static void
147x_cb (struct event *ev, int revents) 141ev_x_cb (struct event *ev, int revents)
148{ 142{
149 revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL; 143 revents &= EV_READ | EV_WRITE | EV_TIMER | EV_SIGNAL;
150 144
151 ev->ev_res = revents; 145 ev->ev_res = revents;
152 ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg); 146 ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg);
153} 147}
154 148
155static void 149static void
156x_cb_sig (EV_P_ struct ev_signal *w, int revents) 150ev_x_cb_sig (EV_P_ struct ev_signal *w, int revents)
157{ 151{
158 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig)); 152 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig));
159 153
160 if (revents & EV_ERROR) 154 if (revents & EV_ERROR)
161 event_del (ev); 155 event_del (ev);
162 156
163 x_cb (ev, revents); 157 ev_x_cb (ev, revents);
164} 158}
165 159
166static void 160static void
167x_cb_io (EV_P_ struct ev_io *w, int revents) 161ev_x_cb_io (EV_P_ struct ev_io *w, int revents)
168{ 162{
169 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); 163 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io));
170 164
171 if ((revents & EV_ERROR) || !(ev->ev_events & EV_PERSIST)) 165 if ((revents & EV_ERROR) || !(ev->ev_events & EV_PERSIST))
172 event_del (ev); 166 event_del (ev);
173 167
174 x_cb (ev, revents); 168 ev_x_cb (ev, revents);
175} 169}
176 170
177static void 171static void
178x_cb_to (EV_P_ struct ev_timer *w, int revents) 172ev_x_cb_to (EV_P_ struct ev_timer *w, int revents)
179{ 173{
180 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); 174 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to));
181 175
182 event_del (ev); 176 event_del (ev);
183 177
184 x_cb (ev, revents); 178 ev_x_cb (ev, revents);
185} 179}
186 180
187void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) 181void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
188{ 182{
189 if (events & EV_SIGNAL) 183 if (events & EV_SIGNAL)
190 ev_init (&ev->iosig.sig, x_cb_sig); 184 ev_init (&ev->iosig.sig, ev_x_cb_sig);
191 else 185 else
192 ev_init (&ev->iosig.io, x_cb_io); 186 ev_init (&ev->iosig.io, ev_x_cb_io);
193 187
194 ev_init (&ev->to, x_cb_to); 188 ev_init (&ev->to, ev_x_cb_to);
195 189
196 ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ 190 ev->ev_base = ev_x_cur; /* not threadsafe, but it's how libevent works */
197 ev->ev_fd = fd; 191 ev->ev_fd = fd;
198 ev->ev_events = events; 192 ev->ev_events = events;
199 ev->ev_pri = 0; 193 ev->ev_pri = 0;
200 ev->ev_callback = cb; 194 ev->ev_callback = cb;
201 ev->ev_arg = arg; 195 ev->ev_arg = arg;
203 ev->ev_flags = EVLIST_INIT; 197 ev->ev_flags = EVLIST_INIT;
204} 198}
205 199
206int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) 200int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
207{ 201{
208 return event_base_once (x_cur, fd, events, cb, arg, tv); 202 return event_base_once (ev_x_cur, fd, events, cb, arg, tv);
209} 203}
210 204
211int event_add (struct event *ev, struct timeval *tv) 205int event_add (struct event *ev, struct timeval *tv)
212{ 206{
213 dLOOPev; 207 dLOOPev;
233 } 227 }
234 } 228 }
235 229
236 if (tv) 230 if (tv)
237 { 231 {
238 ev->to.repeat = tv_get (tv); 232 ev->to.repeat = ev_tv_get (tv);
239 ev_timer_again (EV_A_ &ev->to); 233 ev_timer_again (EV_A_ &ev->to);
240 ev->ev_flags |= EVLIST_TIMEOUT; 234 ev->ev_flags |= EVLIST_TIMEOUT;
241 } 235 }
242 else 236 else
243 { 237 {
284int event_pending (struct event *ev, short events, struct timeval *tv) 278int event_pending (struct event *ev, short events, struct timeval *tv)
285{ 279{
286 short revents = 0; 280 short revents = 0;
287 dLOOPev; 281 dLOOPev;
288 282
289
290 if (ev->ev_events & EV_SIGNAL) 283 if (ev->ev_events & EV_SIGNAL)
291 { 284 {
292 /* sig */ 285 /* sig */
293 if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) 286 if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig))
294 revents |= EV_SIGNAL; 287 revents |= EV_SIGNAL;
303 if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) 296 if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to))
304 { 297 {
305 revents |= EV_TIMEOUT; 298 revents |= EV_TIMEOUT;
306 299
307 if (tv) 300 if (tv)
308 tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ 301 EV_TV_SET (tv, ev_now (EV_A)); /* not sure if this is right :) */
309 } 302 }
310 303
311 return events & revents; 304 return events & revents;
312} 305}
313 306
314int event_priority_init (int npri) 307int event_priority_init (int npri)
315{ 308{
316 return event_base_priority_init (x_cur, npri); 309 return event_base_priority_init (ev_x_cur, npri);
317} 310}
318 311
319int event_priority_set (struct event *ev, int pri) 312int event_priority_set (struct event *ev, int pri)
320{ 313{
321 ev->ev_pri = pri; 314 ev->ev_pri = pri;
332 325
333int event_base_loop (struct event_base *base, int flags) 326int event_base_loop (struct event_base *base, int flags)
334{ 327{
335 dLOOPbase; 328 dLOOPbase;
336 329
337 ev_loop (EV_A_ flags); 330 ev_run (EV_A_ flags);
338 331
339 return 0; 332 return 0;
340} 333}
341 334
342int event_base_dispatch (struct event_base *base) 335int event_base_dispatch (struct event_base *base)
343{ 336{
344 return event_base_loop (base, 0); 337 return event_base_loop (base, 0);
345} 338}
346 339
347static void 340static void
348x_loopexit_cb (int revents, void *base) 341ev_x_loopexit_cb (int revents, void *base)
349{ 342{
350 dLOOPbase; 343 dLOOPbase;
351 344
352 ev_unloop (EV_A_ EVUNLOOP_ONE); 345 ev_break (EV_A_ EVBREAK_ONE);
353} 346}
354 347
355int event_base_loopexit (struct event_base *base, struct timeval *tv) 348int event_base_loopexit (struct event_base *base, struct timeval *tv)
356{ 349{
357 ev_tstamp after = tv_get (tv); 350 ev_tstamp after = ev_tv_get (tv);
358 dLOOPbase; 351 dLOOPbase;
359 352
360 ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); 353 ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., ev_x_loopexit_cb, (void *)base);
361 354
362 return -1; 355 return 0;
363} 356}
364 357
365struct x_once 358struct ev_x_once
366{ 359{
367 int fd; 360 int fd;
368 void (*cb)(int, short, void *); 361 void (*cb)(int, short, void *);
369 void *arg; 362 void *arg;
370}; 363};
371 364
372static void 365static void
373x_once_cb (int revents, void *arg) 366ev_x_once_cb (int revents, void *arg)
374{ 367{
375 struct x_once *once = (struct x_once *)arg; 368 struct ev_x_once *once = (struct ev_x_once *)arg;
376 369
377 once->cb (once->fd, (short)revents, once->arg); 370 once->cb (once->fd, (short)revents, once->arg);
378 free (once); 371 free (once);
379} 372}
380 373
381int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) 374int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
382{ 375{
383 struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once)); 376 struct ev_x_once *once = (struct ev_x_once *)malloc (sizeof (struct ev_x_once));
384 dLOOPbase; 377 dLOOPbase;
385 378
386 if (!once) 379 if (!once)
387 return -1; 380 return -1;
388 381
389 once->fd = fd; 382 once->fd = fd;
390 once->cb = cb; 383 once->cb = cb;
391 once->arg = arg; 384 once->arg = arg;
392 385
393 ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once); 386 ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), ev_tv_get (tv), ev_x_once_cb, (void *)once);
394 387
395 return 0; 388 return 0;
396} 389}
397 390
398int event_base_priority_init (struct event_base *base, int npri) 391int event_base_priority_init (struct event_base *base, int npri)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines