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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines