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 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 |
… | |
… | |
65 | int dummy; |
64 | int dummy; |
66 | }; |
65 | }; |
67 | |
66 | |
68 | static struct event_base *ev_x_cur; |
67 | static struct event_base *ev_x_cur; |
69 | |
68 | |
70 | static void |
|
|
71 | ev_tv_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 | |
|
|
77 | static ev_tstamp |
69 | static ev_tstamp |
78 | ev_tv_get (struct timeval *tv) |
70 | ev_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 | |
88 | const char *event_get_version (void) |
84 | const 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 | |
93 | const char *event_get_method (void) |
90 | const char *event_get_method (void) |
94 | { |
91 | { |
… | |
… | |
101 | if (ev_x_cur) |
98 | if (ev_x_cur) |
102 | ev_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 | ev_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", !ev_x_cur)); |
103 | assert (("libev: multiple event bases not supported when not compiled with EV_MULTIPLICITY", !ev_x_cur)); |
107 | |
104 | |
108 | ev_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 ev_x_cur; |
108 | return ev_x_cur; |
… | |
… | |
144 | } |
141 | } |
145 | |
142 | |
146 | static void |
143 | static void |
147 | ev_x_cb (struct event *ev, int revents) |
144 | ev_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 | |
… | |
… | |
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 | ev_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 | |
… | |
… | |
331 | |
328 | |
332 | int event_base_loop (struct event_base *base, int flags) |
329 | int 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 | |
341 | int event_base_dispatch (struct event_base *base) |
338 | int event_base_dispatch (struct event_base *base) |
… | |
… | |
346 | static void |
343 | static void |
347 | ev_x_loopexit_cb (int revents, void *base) |
344 | ev_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 | |
354 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
351 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
355 | { |
352 | { |
356 | ev_tstamp after = ev_tv_get (tv); |
353 | ev_tstamp after = ev_tv_get (tv); |