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

Comparing libev/event.c (file contents):
Revision 1.35 by root, Fri May 2 07:35:47 2008 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 {
302 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))
303 { 300 {
304 revents |= EV_TIMEOUT; 301 revents |= EV_TIMEOUT;
305 302
306 if (tv) 303 if (tv)
307 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 :) */
308 } 305 }
309 306
310 return events & revents; 307 return events & revents;
311} 308}
312 309
313int event_priority_init (int npri) 310int event_priority_init (int npri)
314{ 311{
315 return event_base_priority_init (x_cur, npri); 312 return event_base_priority_init (ev_x_cur, npri);
316} 313}
317 314
318int event_priority_set (struct event *ev, int pri) 315int event_priority_set (struct event *ev, int pri)
319{ 316{
320 ev->ev_pri = pri; 317 ev->ev_pri = pri;
331 328
332int event_base_loop (struct event_base *base, int flags) 329int event_base_loop (struct event_base *base, int flags)
333{ 330{
334 dLOOPbase; 331 dLOOPbase;
335 332
336 ev_loop (EV_A_ flags); 333 ev_run (EV_A_ flags);
337 334
338 return 0; 335 return 0;
339} 336}
340 337
341int event_base_dispatch (struct event_base *base) 338int event_base_dispatch (struct event_base *base)
342{ 339{
343 return event_base_loop (base, 0); 340 return event_base_loop (base, 0);
344} 341}
345 342
346static void 343static void
347x_loopexit_cb (int revents, void *base) 344ev_x_loopexit_cb (int revents, void *base)
348{ 345{
349 dLOOPbase; 346 dLOOPbase;
350 347
351 ev_unloop (EV_A_ EVUNLOOP_ONE); 348 ev_break (EV_A_ EVBREAK_ONE);
352} 349}
353 350
354int event_base_loopexit (struct event_base *base, struct timeval *tv) 351int event_base_loopexit (struct event_base *base, struct timeval *tv)
355{ 352{
356 ev_tstamp after = tv_get (tv); 353 ev_tstamp after = ev_tv_get (tv);
357 dLOOPbase; 354 dLOOPbase;
358 355
359 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);
360 357
361 return 0; 358 return 0;
362} 359}
363 360
364struct x_once 361struct ev_x_once
365{ 362{
366 int fd; 363 int fd;
367 void (*cb)(int, short, void *); 364 void (*cb)(int, short, void *);
368 void *arg; 365 void *arg;
369}; 366};
370 367
371static void 368static void
372x_once_cb (int revents, void *arg) 369ev_x_once_cb (int revents, void *arg)
373{ 370{
374 struct x_once *once = (struct x_once *)arg; 371 struct ev_x_once *once = (struct ev_x_once *)arg;
375 372
376 once->cb (once->fd, (short)revents, once->arg); 373 once->cb (once->fd, (short)revents, once->arg);
377 free (once); 374 free (once);
378} 375}
379 376
380int 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)
381{ 378{
382 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));
383 dLOOPbase; 380 dLOOPbase;
384 381
385 if (!once) 382 if (!once)
386 return -1; 383 return -1;
387 384
388 once->fd = fd; 385 once->fd = fd;
389 once->cb = cb; 386 once->cb = cb;
390 once->arg = arg; 387 once->arg = arg;
391 388
392 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);
393 390
394 return 0; 391 return 0;
395} 392}
396 393
397int 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