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

Comparing libev/event.c (file contents):
Revision 1.36 by root, Fri May 23 16:37:38 2008 UTC vs.
Revision 1.52 by root, Mon Apr 2 23:14:41 2012 UTC

1/* 1/*
2 * libevent compatibility layer 2 * libevent compatibility layer
3 * 3 *
4 * Copyright (c) 2007,2008 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007,2008,2009,2010,2012 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 *
10 * 1. Redistributions of source code must retain the above copyright notice, 10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer. 11 * this list of conditions and the following disclaimer.
12 * 12 *
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution. 15 * documentation and/or other materials provided with the distribution.
16 * 16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
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
81const char *
88const char *event_get_version (void) 82event_get_version (void)
89{ 83{
84 /* returns ABI, not API or library, version */
90 return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR); 85 return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR);
91} 86}
92 87
88const char *
93const char *event_get_method (void) 89event_get_method (void)
94{ 90{
95 return "libev"; 91 return "libev";
96} 92}
97 93
98void *event_init (void) 94void *event_init (void)
99{ 95{
100#if EV_MULTIPLICITY 96#if EV_MULTIPLICITY
101 if (x_cur) 97 if (ev_x_cur)
102 x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO); 98 ev_x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO);
103 else 99 else
104 x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO); 100 ev_x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO);
105#else 101#else
106 assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur)); 102 assert (("libev: multiple event bases not supported when not compiled with EV_MULTIPLICITY", !ev_x_cur));
107 103
108 x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO); 104 ev_x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO);
109#endif 105#endif
110 106
111 return x_cur; 107 return ev_x_cur;
108}
109
110const char *
111event_base_get_method (const struct event_base *base)
112{
113 return "libev";
114}
115
116struct event_base *
117event_base_new (void)
118{
119#if EV_MULTIPLICITY
120 return (struct event_base *)ev_loop_new (EVFLAG_AUTO);
121#else
122 assert (("libev: multiple event bases not supported when not compiled with EV_MULTIPLICITY"));
123 return NULL;
124#endif
112} 125}
113 126
114void event_base_free (struct event_base *base) 127void event_base_free (struct event_base *base)
115{ 128{
116 dLOOPbase; 129 dLOOPbase;
117 130
118#if EV_MULTIPLICITY 131#if EV_MULTIPLICITY
119 if (ev_default_loop (EVFLAG_AUTO) != loop) 132 if (!ev_is_default_loop (loop))
120 ev_loop_destroy (loop); 133 ev_loop_destroy (loop);
121#endif 134#endif
122} 135}
123 136
124int event_dispatch (void) 137int event_dispatch (void)
125{ 138{
126 return event_base_dispatch (x_cur); 139 return event_base_dispatch (ev_x_cur);
127} 140}
128 141
129#ifdef EV_STANDALONE 142#ifdef EV_STANDALONE
130void event_set_log_callback (event_log_cb cb) 143void event_set_log_callback (event_log_cb cb)
131{ 144{
133} 146}
134#endif 147#endif
135 148
136int event_loop (int flags) 149int event_loop (int flags)
137{ 150{
138 return event_base_loop (x_cur, flags); 151 return event_base_loop (ev_x_cur, flags);
139} 152}
140 153
141int event_loopexit (struct timeval *tv) 154int event_loopexit (struct timeval *tv)
142{ 155{
143 return event_base_loopexit (x_cur, tv); 156 return event_base_loopexit (ev_x_cur, tv);
144} 157}
145 158
159event_callback_fn event_get_callback
160(const struct event *ev)
161{
162 return ev->ev_callback;
163}
164
146static void 165static void
147x_cb (struct event *ev, int revents) 166ev_x_cb (struct event *ev, int revents)
148{ 167{
149 revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL; 168 revents &= EV_READ | EV_WRITE | EV_TIMER | EV_SIGNAL;
150 169
151 ev->ev_res = revents; 170 ev->ev_res = revents;
152 ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg); 171 ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg);
153} 172}
154 173
155static void 174static void
156x_cb_sig (EV_P_ struct ev_signal *w, int revents) 175ev_x_cb_sig (EV_P_ struct ev_signal *w, int revents)
157{ 176{
158 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig)); 177 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig));
159 178
160 if (revents & EV_ERROR) 179 if (revents & EV_ERROR)
161 event_del (ev); 180 event_del (ev);
162 181
163 x_cb (ev, revents); 182 ev_x_cb (ev, revents);
164} 183}
165 184
166static void 185static void
167x_cb_io (EV_P_ struct ev_io *w, int revents) 186ev_x_cb_io (EV_P_ struct ev_io *w, int revents)
168{ 187{
169 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); 188 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io));
170 189
171 if ((revents & EV_ERROR) || !(ev->ev_events & EV_PERSIST)) 190 if ((revents & EV_ERROR) || !(ev->ev_events & EV_PERSIST))
172 event_del (ev); 191 event_del (ev);
173 192
174 x_cb (ev, revents); 193 ev_x_cb (ev, revents);
175} 194}
176 195
177static void 196static void
178x_cb_to (EV_P_ struct ev_timer *w, int revents) 197ev_x_cb_to (EV_P_ struct ev_timer *w, int revents)
179{ 198{
180 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); 199 struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to));
181 200
182 event_del (ev); 201 event_del (ev);
183 202
184 x_cb (ev, revents); 203 ev_x_cb (ev, revents);
185} 204}
186 205
187void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) 206void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
188{ 207{
189 if (events & EV_SIGNAL) 208 if (events & EV_SIGNAL)
190 ev_init (&ev->iosig.sig, x_cb_sig); 209 ev_init (&ev->iosig.sig, ev_x_cb_sig);
191 else 210 else
192 ev_init (&ev->iosig.io, x_cb_io); 211 ev_init (&ev->iosig.io, ev_x_cb_io);
193 212
194 ev_init (&ev->to, x_cb_to); 213 ev_init (&ev->to, ev_x_cb_to);
195 214
196 ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ 215 ev->ev_base = ev_x_cur; /* not threadsafe, but it's how libevent works */
197 ev->ev_fd = fd; 216 ev->ev_fd = fd;
198 ev->ev_events = events; 217 ev->ev_events = events;
199 ev->ev_pri = 0; 218 ev->ev_pri = 0;
200 ev->ev_callback = cb; 219 ev->ev_callback = cb;
201 ev->ev_arg = arg; 220 ev->ev_arg = arg;
203 ev->ev_flags = EVLIST_INIT; 222 ev->ev_flags = EVLIST_INIT;
204} 223}
205 224
206int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) 225int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
207{ 226{
208 return event_base_once (x_cur, fd, events, cb, arg, tv); 227 return event_base_once (ev_x_cur, fd, events, cb, arg, tv);
209} 228}
210 229
211int event_add (struct event *ev, struct timeval *tv) 230int event_add (struct event *ev, struct timeval *tv)
212{ 231{
213 dLOOPev; 232 dLOOPev;
233 } 252 }
234 } 253 }
235 254
236 if (tv) 255 if (tv)
237 { 256 {
238 ev->to.repeat = tv_get (tv); 257 ev->to.repeat = ev_tv_get (tv);
239 ev_timer_again (EV_A_ &ev->to); 258 ev_timer_again (EV_A_ &ev->to);
240 ev->ev_flags |= EVLIST_TIMEOUT; 259 ev->ev_flags |= EVLIST_TIMEOUT;
241 } 260 }
242 else 261 else
243 { 262 {
302 if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) 321 if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to))
303 { 322 {
304 revents |= EV_TIMEOUT; 323 revents |= EV_TIMEOUT;
305 324
306 if (tv) 325 if (tv)
307 tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ 326 {
327 ev_tstamp at = ev_now (EV_A);
328
329 tv->tv_sec = (long)at;
330 tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6);
331 }
308 } 332 }
309 333
310 return events & revents; 334 return events & revents;
311} 335}
312 336
313int event_priority_init (int npri) 337int event_priority_init (int npri)
314{ 338{
315 return event_base_priority_init (x_cur, npri); 339 return event_base_priority_init (ev_x_cur, npri);
316} 340}
317 341
318int event_priority_set (struct event *ev, int pri) 342int event_priority_set (struct event *ev, int pri)
319{ 343{
320 ev->ev_pri = pri; 344 ev->ev_pri = pri;
331 355
332int event_base_loop (struct event_base *base, int flags) 356int event_base_loop (struct event_base *base, int flags)
333{ 357{
334 dLOOPbase; 358 dLOOPbase;
335 359
336 ev_loop (EV_A_ flags); 360 return !ev_run (EV_A_ flags);
337
338 return 0;
339} 361}
340 362
341int event_base_dispatch (struct event_base *base) 363int event_base_dispatch (struct event_base *base)
342{ 364{
343 return event_base_loop (base, 0); 365 return event_base_loop (base, 0);
344} 366}
345 367
346static void 368static void
347x_loopexit_cb (int revents, void *base) 369ev_x_loopexit_cb (int revents, void *base)
348{ 370{
349 dLOOPbase; 371 dLOOPbase;
350 372
351 ev_unloop (EV_A_ EVUNLOOP_ONE); 373 ev_break (EV_A_ EVBREAK_ONE);
352} 374}
353 375
354int event_base_loopexit (struct event_base *base, struct timeval *tv) 376int event_base_loopexit (struct event_base *base, struct timeval *tv)
355{ 377{
356 ev_tstamp after = tv_get (tv); 378 ev_tstamp after = ev_tv_get (tv);
357 dLOOPbase; 379 dLOOPbase;
358 380
359 ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); 381 ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., ev_x_loopexit_cb, (void *)base);
360 382
361 return 0; 383 return 0;
362} 384}
363 385
364struct x_once 386struct ev_x_once
365{ 387{
366 int fd; 388 int fd;
367 void (*cb)(int, short, void *); 389 void (*cb)(int, short, void *);
368 void *arg; 390 void *arg;
369}; 391};
370 392
371static void 393static void
372x_once_cb (int revents, void *arg) 394ev_x_once_cb (int revents, void *arg)
373{ 395{
374 struct x_once *once = (struct x_once *)arg; 396 struct ev_x_once *once = (struct ev_x_once *)arg;
375 397
376 once->cb (once->fd, (short)revents, once->arg); 398 once->cb (once->fd, (short)revents, once->arg);
377 free (once); 399 free (once);
378} 400}
379 401
380int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) 402int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
381{ 403{
382 struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once)); 404 struct ev_x_once *once = (struct ev_x_once *)malloc (sizeof (struct ev_x_once));
383 dLOOPbase; 405 dLOOPbase;
384 406
385 if (!once) 407 if (!once)
386 return -1; 408 return -1;
387 409
388 once->fd = fd; 410 once->fd = fd;
389 once->cb = cb; 411 once->cb = cb;
390 once->arg = arg; 412 once->arg = arg;
391 413
392 ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once); 414 ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), ev_tv_get (tv), ev_x_once_cb, (void *)once);
393 415
394 return 0; 416 return 0;
395} 417}
396 418
397int event_base_priority_init (struct event_base *base, int npri) 419int event_base_priority_init (struct event_base *base, int npri)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines