… | |
… | |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
30 | */ |
|
|
31 | |
|
|
32 | #ifdef __cplusplus |
|
|
33 | extern "C" { |
|
|
34 | #endif |
|
|
35 | |
31 | #ifndef EV_STANDALONE |
36 | #ifndef EV_STANDALONE |
|
|
37 | # ifdef EV_CONFIG_H |
|
|
38 | # include EV_CONFIG_H |
|
|
39 | # else |
32 | # include "config.h" |
40 | # include "config.h" |
|
|
41 | # endif |
|
|
42 | |
|
|
43 | # if HAVE_CLOCK_GETTIME |
|
|
44 | # ifndef EV_USE_MONOTONIC |
|
|
45 | # define EV_USE_MONOTONIC 1 |
|
|
46 | # endif |
|
|
47 | # ifndef EV_USE_REALTIME |
|
|
48 | # define EV_USE_REALTIME 1 |
|
|
49 | # endif |
|
|
50 | # else |
|
|
51 | # ifndef EV_USE_MONOTONIC |
|
|
52 | # define EV_USE_MONOTONIC 0 |
|
|
53 | # endif |
|
|
54 | # ifndef EV_USE_REALTIME |
|
|
55 | # define EV_USE_REALTIME 0 |
|
|
56 | # endif |
|
|
57 | # endif |
|
|
58 | |
|
|
59 | # ifndef EV_USE_SELECT |
|
|
60 | # if HAVE_SELECT && HAVE_SYS_SELECT_H |
|
|
61 | # define EV_USE_SELECT 1 |
|
|
62 | # else |
|
|
63 | # define EV_USE_SELECT 0 |
|
|
64 | # endif |
|
|
65 | # endif |
|
|
66 | |
|
|
67 | # ifndef EV_USE_POLL |
|
|
68 | # if HAVE_POLL && HAVE_POLL_H |
|
|
69 | # define EV_USE_POLL 1 |
|
|
70 | # else |
|
|
71 | # define EV_USE_POLL 0 |
|
|
72 | # endif |
|
|
73 | # endif |
|
|
74 | |
|
|
75 | # ifndef EV_USE_EPOLL |
|
|
76 | # if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
|
|
77 | # define EV_USE_EPOLL 1 |
|
|
78 | # else |
|
|
79 | # define EV_USE_EPOLL 0 |
|
|
80 | # endif |
|
|
81 | # endif |
|
|
82 | |
|
|
83 | # ifndef EV_USE_KQUEUE |
|
|
84 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
|
|
85 | # define EV_USE_KQUEUE 1 |
|
|
86 | # else |
|
|
87 | # define EV_USE_KQUEUE 0 |
|
|
88 | # endif |
|
|
89 | # endif |
|
|
90 | |
|
|
91 | # ifndef EV_USE_PORT |
|
|
92 | # if HAVE_PORT_H && HAVE_PORT_CREATE |
|
|
93 | # define EV_USE_PORT 1 |
|
|
94 | # else |
|
|
95 | # define EV_USE_PORT 0 |
|
|
96 | # endif |
|
|
97 | # endif |
|
|
98 | |
33 | #endif |
99 | #endif |
34 | |
100 | |
35 | #include <math.h> |
101 | #include <math.h> |
36 | #include <stdlib.h> |
102 | #include <stdlib.h> |
37 | #include <unistd.h> |
|
|
38 | #include <fcntl.h> |
103 | #include <fcntl.h> |
39 | #include <signal.h> |
|
|
40 | #include <stddef.h> |
104 | #include <stddef.h> |
41 | |
105 | |
42 | #include <stdio.h> |
106 | #include <stdio.h> |
43 | |
107 | |
44 | #include <assert.h> |
108 | #include <assert.h> |
45 | #include <errno.h> |
109 | #include <errno.h> |
46 | #include <sys/types.h> |
110 | #include <sys/types.h> |
|
|
111 | #include <time.h> |
|
|
112 | |
|
|
113 | #include <signal.h> |
|
|
114 | |
47 | #ifndef WIN32 |
115 | #ifndef _WIN32 |
|
|
116 | # include <sys/time.h> |
48 | # include <sys/wait.h> |
117 | # include <sys/wait.h> |
|
|
118 | # include <unistd.h> |
|
|
119 | #else |
|
|
120 | # define WIN32_LEAN_AND_MEAN |
|
|
121 | # include <windows.h> |
|
|
122 | # ifndef EV_SELECT_IS_WINSOCKET |
|
|
123 | # define EV_SELECT_IS_WINSOCKET 1 |
49 | #endif |
124 | # endif |
50 | #include <sys/time.h> |
125 | #endif |
51 | #include <time.h> |
|
|
52 | |
126 | |
53 | /**/ |
127 | /**/ |
54 | |
128 | |
55 | #ifndef EV_USE_MONOTONIC |
129 | #ifndef EV_USE_MONOTONIC |
56 | # define EV_USE_MONOTONIC 1 |
130 | # define EV_USE_MONOTONIC 0 |
|
|
131 | #endif |
|
|
132 | |
|
|
133 | #ifndef EV_USE_REALTIME |
|
|
134 | # define EV_USE_REALTIME 0 |
57 | #endif |
135 | #endif |
58 | |
136 | |
59 | #ifndef EV_USE_SELECT |
137 | #ifndef EV_USE_SELECT |
60 | # define EV_USE_SELECT 1 |
138 | # define EV_USE_SELECT 1 |
61 | #endif |
139 | #endif |
62 | |
140 | |
63 | #ifndef EV_USEV_POLL |
141 | #ifndef EV_USE_POLL |
64 | # define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ |
142 | # ifdef _WIN32 |
|
|
143 | # define EV_USE_POLL 0 |
|
|
144 | # else |
|
|
145 | # define EV_USE_POLL 1 |
|
|
146 | # endif |
65 | #endif |
147 | #endif |
66 | |
148 | |
67 | #ifndef EV_USE_EPOLL |
149 | #ifndef EV_USE_EPOLL |
68 | # define EV_USE_EPOLL 0 |
150 | # define EV_USE_EPOLL 0 |
69 | #endif |
151 | #endif |
70 | |
152 | |
71 | #ifndef EV_USE_KQUEUE |
153 | #ifndef EV_USE_KQUEUE |
72 | # define EV_USE_KQUEUE 0 |
154 | # define EV_USE_KQUEUE 0 |
73 | #endif |
155 | #endif |
74 | |
156 | |
75 | #ifndef EV_USE_REALTIME |
157 | #ifndef EV_USE_PORT |
76 | # define EV_USE_REALTIME 1 |
158 | # define EV_USE_PORT 0 |
77 | #endif |
159 | #endif |
78 | |
160 | |
79 | /**/ |
161 | /**/ |
80 | |
162 | |
81 | #ifndef CLOCK_MONOTONIC |
163 | #ifndef CLOCK_MONOTONIC |
… | |
… | |
86 | #ifndef CLOCK_REALTIME |
168 | #ifndef CLOCK_REALTIME |
87 | # undef EV_USE_REALTIME |
169 | # undef EV_USE_REALTIME |
88 | # define EV_USE_REALTIME 0 |
170 | # define EV_USE_REALTIME 0 |
89 | #endif |
171 | #endif |
90 | |
172 | |
|
|
173 | #if EV_SELECT_IS_WINSOCKET |
|
|
174 | # include <winsock.h> |
|
|
175 | #endif |
|
|
176 | |
91 | /**/ |
177 | /**/ |
92 | |
178 | |
93 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
179 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
94 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
180 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
95 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
181 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
96 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
182 | /*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
97 | |
183 | |
|
|
184 | #ifdef EV_H |
|
|
185 | # include EV_H |
|
|
186 | #else |
98 | #include "ev.h" |
187 | # include "ev.h" |
|
|
188 | #endif |
99 | |
189 | |
100 | #if __GNUC__ >= 3 |
190 | #if __GNUC__ >= 3 |
101 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
191 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
192 | # define inline_size static inline /* inline for codesize */ |
|
|
193 | # if EV_MINIMAL |
102 | # define inline inline |
194 | # define noinline __attribute__ ((noinline)) |
|
|
195 | # define inline_speed static noinline |
|
|
196 | # else |
|
|
197 | # define noinline |
|
|
198 | # define inline_speed static inline |
|
|
199 | # endif |
103 | #else |
200 | #else |
104 | # define expect(expr,value) (expr) |
201 | # define expect(expr,value) (expr) |
105 | # define inline static |
202 | # define inline_speed static |
|
|
203 | # define inline_minimal static |
|
|
204 | # define noinline |
106 | #endif |
205 | #endif |
107 | |
206 | |
108 | #define expect_false(expr) expect ((expr) != 0, 0) |
207 | #define expect_false(expr) expect ((expr) != 0, 0) |
109 | #define expect_true(expr) expect ((expr) != 0, 1) |
208 | #define expect_true(expr) expect ((expr) != 0, 1) |
110 | |
209 | |
111 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
210 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
112 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
211 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
113 | |
212 | |
|
|
213 | #define EMPTY0 /* required for microsofts broken pseudo-c compiler */ |
|
|
214 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
|
|
215 | |
114 | typedef struct ev_watcher *W; |
216 | typedef ev_watcher *W; |
115 | typedef struct ev_watcher_list *WL; |
217 | typedef ev_watcher_list *WL; |
116 | typedef struct ev_watcher_time *WT; |
218 | typedef ev_watcher_time *WT; |
117 | |
219 | |
118 | static ev_tstamp now_floor, mn_now, diff; /* monotonic clock */ |
220 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
119 | static ev_tstamp rt_now; |
|
|
120 | static int method; |
|
|
121 | |
221 | |
122 | static int have_monotonic; /* runtime */ |
222 | #ifdef _WIN32 |
123 | |
223 | # include "ev_win32.c" |
124 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
|
|
125 | static void (*method_modify)(EV_P_ int fd, int oev, int nev); |
|
|
126 | static void (*method_poll)(EV_P_ ev_tstamp timeout); |
|
|
127 | |
|
|
128 | static int activecnt; /* number of active events */ |
|
|
129 | |
|
|
130 | #if EV_USE_SELECT |
|
|
131 | static unsigned char *vec_ri, *vec_ro, *vec_wi, *vec_wo; |
|
|
132 | static int vec_max; |
|
|
133 | #endif |
|
|
134 | |
|
|
135 | #if EV_USEV_POLL |
|
|
136 | static struct pollfd *polls; |
|
|
137 | static int pollmax, pollcnt; |
|
|
138 | static int *pollidxs; /* maps fds into structure indices */ |
|
|
139 | static int pollidxmax; |
|
|
140 | #endif |
|
|
141 | |
|
|
142 | #if EV_USE_EPOLL |
|
|
143 | static int epoll_fd = -1; |
|
|
144 | |
|
|
145 | static struct epoll_event *events; |
|
|
146 | static int eventmax; |
|
|
147 | #endif |
|
|
148 | |
|
|
149 | #if EV_USE_KQUEUE |
|
|
150 | static int kqueue_fd; |
|
|
151 | static struct kevent *kqueue_changes; |
|
|
152 | static int kqueue_changemax, kqueue_changecnt; |
|
|
153 | static struct kevent *kqueue_events; |
|
|
154 | static int kqueue_eventmax; |
|
|
155 | #endif |
224 | #endif |
156 | |
225 | |
157 | /*****************************************************************************/ |
226 | /*****************************************************************************/ |
158 | |
227 | |
159 | inline ev_tstamp |
228 | static void (*syserr_cb)(const char *msg); |
|
|
229 | |
|
|
230 | void |
|
|
231 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
|
|
232 | { |
|
|
233 | syserr_cb = cb; |
|
|
234 | } |
|
|
235 | |
|
|
236 | static void noinline |
|
|
237 | syserr (const char *msg) |
|
|
238 | { |
|
|
239 | if (!msg) |
|
|
240 | msg = "(libev) system error"; |
|
|
241 | |
|
|
242 | if (syserr_cb) |
|
|
243 | syserr_cb (msg); |
|
|
244 | else |
|
|
245 | { |
|
|
246 | perror (msg); |
|
|
247 | abort (); |
|
|
248 | } |
|
|
249 | } |
|
|
250 | |
|
|
251 | static void *(*alloc)(void *ptr, long size); |
|
|
252 | |
|
|
253 | void |
|
|
254 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
|
|
255 | { |
|
|
256 | alloc = cb; |
|
|
257 | } |
|
|
258 | |
|
|
259 | static void * |
|
|
260 | ev_realloc (void *ptr, long size) |
|
|
261 | { |
|
|
262 | ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); |
|
|
263 | |
|
|
264 | if (!ptr && size) |
|
|
265 | { |
|
|
266 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
|
|
267 | abort (); |
|
|
268 | } |
|
|
269 | |
|
|
270 | return ptr; |
|
|
271 | } |
|
|
272 | |
|
|
273 | #define ev_malloc(size) ev_realloc (0, (size)) |
|
|
274 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
|
|
275 | |
|
|
276 | /*****************************************************************************/ |
|
|
277 | |
|
|
278 | typedef struct |
|
|
279 | { |
|
|
280 | WL head; |
|
|
281 | unsigned char events; |
|
|
282 | unsigned char reify; |
|
|
283 | #if EV_SELECT_IS_WINSOCKET |
|
|
284 | SOCKET handle; |
|
|
285 | #endif |
|
|
286 | } ANFD; |
|
|
287 | |
|
|
288 | typedef struct |
|
|
289 | { |
|
|
290 | W w; |
|
|
291 | int events; |
|
|
292 | } ANPENDING; |
|
|
293 | |
|
|
294 | #if EV_MULTIPLICITY |
|
|
295 | |
|
|
296 | struct ev_loop |
|
|
297 | { |
|
|
298 | ev_tstamp ev_rt_now; |
|
|
299 | #define ev_rt_now ((loop)->ev_rt_now) |
|
|
300 | #define VAR(name,decl) decl; |
|
|
301 | #include "ev_vars.h" |
|
|
302 | #undef VAR |
|
|
303 | }; |
|
|
304 | #include "ev_wrap.h" |
|
|
305 | |
|
|
306 | static struct ev_loop default_loop_struct; |
|
|
307 | struct ev_loop *ev_default_loop_ptr; |
|
|
308 | |
|
|
309 | #else |
|
|
310 | |
|
|
311 | ev_tstamp ev_rt_now; |
|
|
312 | #define VAR(name,decl) static decl; |
|
|
313 | #include "ev_vars.h" |
|
|
314 | #undef VAR |
|
|
315 | |
|
|
316 | static int ev_default_loop_ptr; |
|
|
317 | |
|
|
318 | #endif |
|
|
319 | |
|
|
320 | /*****************************************************************************/ |
|
|
321 | |
|
|
322 | ev_tstamp |
160 | ev_time (void) |
323 | ev_time (void) |
161 | { |
324 | { |
162 | #if EV_USE_REALTIME |
325 | #if EV_USE_REALTIME |
163 | struct timespec ts; |
326 | struct timespec ts; |
164 | clock_gettime (CLOCK_REALTIME, &ts); |
327 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
168 | gettimeofday (&tv, 0); |
331 | gettimeofday (&tv, 0); |
169 | return tv.tv_sec + tv.tv_usec * 1e-6; |
332 | return tv.tv_sec + tv.tv_usec * 1e-6; |
170 | #endif |
333 | #endif |
171 | } |
334 | } |
172 | |
335 | |
173 | inline ev_tstamp |
336 | ev_tstamp inline_size |
174 | get_clock (void) |
337 | get_clock (void) |
175 | { |
338 | { |
176 | #if EV_USE_MONOTONIC |
339 | #if EV_USE_MONOTONIC |
177 | if (expect_true (have_monotonic)) |
340 | if (expect_true (have_monotonic)) |
178 | { |
341 | { |
… | |
… | |
183 | #endif |
346 | #endif |
184 | |
347 | |
185 | return ev_time (); |
348 | return ev_time (); |
186 | } |
349 | } |
187 | |
350 | |
|
|
351 | #if EV_MULTIPLICITY |
188 | ev_tstamp |
352 | ev_tstamp |
189 | ev_now (EV_P) |
353 | ev_now (EV_P) |
190 | { |
354 | { |
191 | return rt_now; |
355 | return ev_rt_now; |
192 | } |
356 | } |
|
|
357 | #endif |
193 | |
358 | |
194 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
359 | #define array_roundsize(type,n) (((n) | 4) & ~3) |
195 | |
360 | |
196 | #define array_needsize(base,cur,cnt,init) \ |
361 | #define array_needsize(type,base,cur,cnt,init) \ |
197 | if (expect_false ((cnt) > cur)) \ |
362 | if (expect_false ((cnt) > cur)) \ |
198 | { \ |
363 | { \ |
199 | int newcnt = cur; \ |
364 | int newcnt = cur; \ |
200 | do \ |
365 | do \ |
201 | { \ |
366 | { \ |
202 | newcnt = array_roundsize (base, newcnt << 1); \ |
367 | newcnt = array_roundsize (type, newcnt << 1); \ |
203 | } \ |
368 | } \ |
204 | while ((cnt) > newcnt); \ |
369 | while ((cnt) > newcnt); \ |
205 | \ |
370 | \ |
206 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
371 | base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
207 | init (base + cur, newcnt - cur); \ |
372 | init (base + cur, newcnt - cur); \ |
208 | cur = newcnt; \ |
373 | cur = newcnt; \ |
209 | } |
374 | } |
|
|
375 | |
|
|
376 | #define array_slim(type,stem) \ |
|
|
377 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
|
|
378 | { \ |
|
|
379 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
|
|
380 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
|
|
381 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
|
|
382 | } |
|
|
383 | |
|
|
384 | #define array_free(stem, idx) \ |
|
|
385 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
210 | |
386 | |
211 | /*****************************************************************************/ |
387 | /*****************************************************************************/ |
212 | |
388 | |
213 | typedef struct |
389 | void noinline |
|
|
390 | ev_feed_event (EV_P_ void *w, int revents) |
214 | { |
391 | { |
215 | struct ev_watcher_list *head; |
392 | W w_ = (W)w; |
216 | unsigned char events; |
|
|
217 | unsigned char reify; |
|
|
218 | } ANFD; |
|
|
219 | |
393 | |
220 | static ANFD *anfds; |
394 | if (expect_false (w_->pending)) |
221 | static int anfdmax; |
395 | { |
|
|
396 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
|
|
397 | return; |
|
|
398 | } |
222 | |
399 | |
223 | static void |
400 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
|
|
401 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
|
|
402 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
|
|
403 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
|
|
404 | } |
|
|
405 | |
|
|
406 | void inline_size |
|
|
407 | queue_events (EV_P_ W *events, int eventcnt, int type) |
|
|
408 | { |
|
|
409 | int i; |
|
|
410 | |
|
|
411 | for (i = 0; i < eventcnt; ++i) |
|
|
412 | ev_feed_event (EV_A_ events [i], type); |
|
|
413 | } |
|
|
414 | |
|
|
415 | /*****************************************************************************/ |
|
|
416 | |
|
|
417 | void inline_size |
224 | anfds_init (ANFD *base, int count) |
418 | anfds_init (ANFD *base, int count) |
225 | { |
419 | { |
226 | while (count--) |
420 | while (count--) |
227 | { |
421 | { |
228 | base->head = 0; |
422 | base->head = 0; |
… | |
… | |
231 | |
425 | |
232 | ++base; |
426 | ++base; |
233 | } |
427 | } |
234 | } |
428 | } |
235 | |
429 | |
236 | typedef struct |
430 | void inline_speed |
237 | { |
|
|
238 | W w; |
|
|
239 | int events; |
|
|
240 | } ANPENDING; |
|
|
241 | |
|
|
242 | static ANPENDING *pendings [NUMPRI]; |
|
|
243 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
|
|
244 | |
|
|
245 | static void |
|
|
246 | event (EV_P_ W w, int events) |
|
|
247 | { |
|
|
248 | if (w->pending) |
|
|
249 | { |
|
|
250 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
|
|
251 | return; |
|
|
252 | } |
|
|
253 | |
|
|
254 | w->pending = ++pendingcnt [ABSPRI (w)]; |
|
|
255 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
|
|
256 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
|
|
257 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
|
|
258 | } |
|
|
259 | |
|
|
260 | static void |
|
|
261 | queue_events (EV_P_ W *events, int eventcnt, int type) |
|
|
262 | { |
|
|
263 | int i; |
|
|
264 | |
|
|
265 | for (i = 0; i < eventcnt; ++i) |
|
|
266 | event (EV_A_ events [i], type); |
|
|
267 | } |
|
|
268 | |
|
|
269 | static void |
|
|
270 | fd_event (EV_P_ int fd, int events) |
431 | fd_event (EV_P_ int fd, int revents) |
271 | { |
432 | { |
272 | ANFD *anfd = anfds + fd; |
433 | ANFD *anfd = anfds + fd; |
273 | struct ev_io *w; |
434 | ev_io *w; |
274 | |
435 | |
275 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
436 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
276 | { |
437 | { |
277 | int ev = w->events & events; |
438 | int ev = w->events & revents; |
278 | |
439 | |
279 | if (ev) |
440 | if (ev) |
280 | event (EV_A_ (W)w, ev); |
441 | ev_feed_event (EV_A_ (W)w, ev); |
281 | } |
442 | } |
282 | } |
443 | } |
283 | |
444 | |
284 | /*****************************************************************************/ |
445 | void |
|
|
446 | ev_feed_fd_event (EV_P_ int fd, int revents) |
|
|
447 | { |
|
|
448 | fd_event (EV_A_ fd, revents); |
|
|
449 | } |
285 | |
450 | |
286 | static int *fdchanges; |
451 | void inline_size |
287 | static int fdchangemax, fdchangecnt; |
|
|
288 | |
|
|
289 | static void |
|
|
290 | fd_reify (EV_P) |
452 | fd_reify (EV_P) |
291 | { |
453 | { |
292 | int i; |
454 | int i; |
293 | |
455 | |
294 | for (i = 0; i < fdchangecnt; ++i) |
456 | for (i = 0; i < fdchangecnt; ++i) |
295 | { |
457 | { |
296 | int fd = fdchanges [i]; |
458 | int fd = fdchanges [i]; |
297 | ANFD *anfd = anfds + fd; |
459 | ANFD *anfd = anfds + fd; |
298 | struct ev_io *w; |
460 | ev_io *w; |
299 | |
461 | |
300 | int events = 0; |
462 | int events = 0; |
301 | |
463 | |
302 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
464 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
303 | events |= w->events; |
465 | events |= w->events; |
304 | |
466 | |
|
|
467 | #if EV_SELECT_IS_WINSOCKET |
|
|
468 | if (events) |
|
|
469 | { |
|
|
470 | unsigned long argp; |
|
|
471 | anfd->handle = _get_osfhandle (fd); |
|
|
472 | assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0)); |
|
|
473 | } |
|
|
474 | #endif |
|
|
475 | |
305 | anfd->reify = 0; |
476 | anfd->reify = 0; |
306 | |
477 | |
307 | if (anfd->events != events) |
|
|
308 | { |
|
|
309 | method_modify (EV_A_ fd, anfd->events, events); |
478 | backend_modify (EV_A_ fd, anfd->events, events); |
310 | anfd->events = events; |
479 | anfd->events = events; |
311 | } |
|
|
312 | } |
480 | } |
313 | |
481 | |
314 | fdchangecnt = 0; |
482 | fdchangecnt = 0; |
315 | } |
483 | } |
316 | |
484 | |
317 | static void |
485 | void inline_size |
318 | fd_change (EV_P_ int fd) |
486 | fd_change (EV_P_ int fd) |
319 | { |
487 | { |
320 | if (anfds [fd].reify || fdchangecnt < 0) |
488 | if (expect_false (anfds [fd].reify)) |
321 | return; |
489 | return; |
322 | |
490 | |
323 | anfds [fd].reify = 1; |
491 | anfds [fd].reify = 1; |
324 | |
492 | |
325 | ++fdchangecnt; |
493 | ++fdchangecnt; |
326 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
494 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
327 | fdchanges [fdchangecnt - 1] = fd; |
495 | fdchanges [fdchangecnt - 1] = fd; |
328 | } |
496 | } |
329 | |
497 | |
330 | static void |
498 | void inline_speed |
331 | fd_kill (EV_P_ int fd) |
499 | fd_kill (EV_P_ int fd) |
332 | { |
500 | { |
333 | struct ev_io *w; |
501 | ev_io *w; |
334 | |
502 | |
335 | while ((w = (struct ev_io *)anfds [fd].head)) |
503 | while ((w = (ev_io *)anfds [fd].head)) |
336 | { |
504 | { |
337 | ev_io_stop (EV_A_ w); |
505 | ev_io_stop (EV_A_ w); |
338 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
506 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
339 | } |
507 | } |
|
|
508 | } |
|
|
509 | |
|
|
510 | int inline_size |
|
|
511 | fd_valid (int fd) |
|
|
512 | { |
|
|
513 | #ifdef _WIN32 |
|
|
514 | return _get_osfhandle (fd) != -1; |
|
|
515 | #else |
|
|
516 | return fcntl (fd, F_GETFD) != -1; |
|
|
517 | #endif |
340 | } |
518 | } |
341 | |
519 | |
342 | /* called on EBADF to verify fds */ |
520 | /* called on EBADF to verify fds */ |
343 | static void |
521 | static void noinline |
344 | fd_ebadf (EV_P) |
522 | fd_ebadf (EV_P) |
345 | { |
523 | { |
346 | int fd; |
524 | int fd; |
347 | |
525 | |
348 | for (fd = 0; fd < anfdmax; ++fd) |
526 | for (fd = 0; fd < anfdmax; ++fd) |
349 | if (anfds [fd].events) |
527 | if (anfds [fd].events) |
350 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
528 | if (!fd_valid (fd) == -1 && errno == EBADF) |
351 | fd_kill (EV_A_ fd); |
529 | fd_kill (EV_A_ fd); |
352 | } |
530 | } |
353 | |
531 | |
354 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
532 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
355 | static void |
533 | static void noinline |
356 | fd_enomem (EV_P) |
534 | fd_enomem (EV_P) |
357 | { |
535 | { |
358 | int fd = anfdmax; |
536 | int fd; |
359 | |
537 | |
360 | while (fd--) |
538 | for (fd = anfdmax; fd--; ) |
361 | if (anfds [fd].events) |
539 | if (anfds [fd].events) |
362 | { |
540 | { |
363 | close (fd); |
|
|
364 | fd_kill (EV_A_ fd); |
541 | fd_kill (EV_A_ fd); |
365 | return; |
542 | return; |
366 | } |
543 | } |
367 | } |
544 | } |
368 | |
545 | |
|
|
546 | /* usually called after fork if backend needs to re-arm all fds from scratch */ |
|
|
547 | static void noinline |
|
|
548 | fd_rearm_all (EV_P) |
|
|
549 | { |
|
|
550 | int fd; |
|
|
551 | |
|
|
552 | /* this should be highly optimised to not do anything but set a flag */ |
|
|
553 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
554 | if (anfds [fd].events) |
|
|
555 | { |
|
|
556 | anfds [fd].events = 0; |
|
|
557 | fd_change (EV_A_ fd); |
|
|
558 | } |
|
|
559 | } |
|
|
560 | |
369 | /*****************************************************************************/ |
561 | /*****************************************************************************/ |
370 | |
562 | |
371 | static struct ev_timer **timers; |
563 | void inline_speed |
372 | static int timermax, timercnt; |
|
|
373 | |
|
|
374 | static struct ev_periodic **periodics; |
|
|
375 | static int periodicmax, periodiccnt; |
|
|
376 | |
|
|
377 | static void |
|
|
378 | upheap (WT *timers, int k) |
564 | upheap (WT *heap, int k) |
379 | { |
565 | { |
380 | WT w = timers [k]; |
566 | WT w = heap [k]; |
381 | |
567 | |
382 | while (k && timers [k >> 1]->at > w->at) |
568 | while (k && heap [k >> 1]->at > w->at) |
383 | { |
569 | { |
384 | timers [k] = timers [k >> 1]; |
570 | heap [k] = heap [k >> 1]; |
385 | timers [k]->active = k + 1; |
571 | ((W)heap [k])->active = k + 1; |
386 | k >>= 1; |
572 | k >>= 1; |
387 | } |
573 | } |
388 | |
574 | |
389 | timers [k] = w; |
575 | heap [k] = w; |
390 | timers [k]->active = k + 1; |
576 | ((W)heap [k])->active = k + 1; |
391 | |
577 | |
392 | } |
578 | } |
393 | |
579 | |
394 | static void |
580 | void inline_speed |
395 | downheap (WT *timers, int N, int k) |
581 | downheap (WT *heap, int N, int k) |
396 | { |
582 | { |
397 | WT w = timers [k]; |
583 | WT w = heap [k]; |
398 | |
584 | |
399 | while (k < (N >> 1)) |
585 | while (k < (N >> 1)) |
400 | { |
586 | { |
401 | int j = k << 1; |
587 | int j = k << 1; |
402 | |
588 | |
403 | if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
589 | if (j + 1 < N && heap [j]->at > heap [j + 1]->at) |
404 | ++j; |
590 | ++j; |
405 | |
591 | |
406 | if (w->at <= timers [j]->at) |
592 | if (w->at <= heap [j]->at) |
407 | break; |
593 | break; |
408 | |
594 | |
409 | timers [k] = timers [j]; |
595 | heap [k] = heap [j]; |
410 | timers [k]->active = k + 1; |
596 | ((W)heap [k])->active = k + 1; |
411 | k = j; |
597 | k = j; |
412 | } |
598 | } |
413 | |
599 | |
414 | timers [k] = w; |
600 | heap [k] = w; |
415 | timers [k]->active = k + 1; |
601 | ((W)heap [k])->active = k + 1; |
|
|
602 | } |
|
|
603 | |
|
|
604 | void inline_size |
|
|
605 | adjustheap (WT *heap, int N, int k) |
|
|
606 | { |
|
|
607 | upheap (heap, k); |
|
|
608 | downheap (heap, N, k); |
416 | } |
609 | } |
417 | |
610 | |
418 | /*****************************************************************************/ |
611 | /*****************************************************************************/ |
419 | |
612 | |
420 | typedef struct |
613 | typedef struct |
421 | { |
614 | { |
422 | struct ev_watcher_list *head; |
615 | WL head; |
423 | sig_atomic_t volatile gotsig; |
616 | sig_atomic_t volatile gotsig; |
424 | } ANSIG; |
617 | } ANSIG; |
425 | |
618 | |
426 | static ANSIG *signals; |
619 | static ANSIG *signals; |
427 | static int signalmax; |
620 | static int signalmax; |
428 | |
621 | |
429 | static int sigpipe [2]; |
622 | static int sigpipe [2]; |
430 | static sig_atomic_t volatile gotsig; |
623 | static sig_atomic_t volatile gotsig; |
431 | static struct ev_io sigev; |
624 | static ev_io sigev; |
432 | |
625 | |
433 | static void |
626 | void inline_size |
434 | signals_init (ANSIG *base, int count) |
627 | signals_init (ANSIG *base, int count) |
435 | { |
628 | { |
436 | while (count--) |
629 | while (count--) |
437 | { |
630 | { |
438 | base->head = 0; |
631 | base->head = 0; |
… | |
… | |
443 | } |
636 | } |
444 | |
637 | |
445 | static void |
638 | static void |
446 | sighandler (int signum) |
639 | sighandler (int signum) |
447 | { |
640 | { |
|
|
641 | #if _WIN32 |
|
|
642 | signal (signum, sighandler); |
|
|
643 | #endif |
|
|
644 | |
448 | signals [signum - 1].gotsig = 1; |
645 | signals [signum - 1].gotsig = 1; |
449 | |
646 | |
450 | if (!gotsig) |
647 | if (!gotsig) |
451 | { |
648 | { |
452 | int old_errno = errno; |
649 | int old_errno = errno; |
… | |
… | |
454 | write (sigpipe [1], &signum, 1); |
651 | write (sigpipe [1], &signum, 1); |
455 | errno = old_errno; |
652 | errno = old_errno; |
456 | } |
653 | } |
457 | } |
654 | } |
458 | |
655 | |
|
|
656 | void noinline |
|
|
657 | ev_feed_signal_event (EV_P_ int signum) |
|
|
658 | { |
|
|
659 | WL w; |
|
|
660 | |
|
|
661 | #if EV_MULTIPLICITY |
|
|
662 | assert (("feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr)); |
|
|
663 | #endif |
|
|
664 | |
|
|
665 | --signum; |
|
|
666 | |
|
|
667 | if (signum < 0 || signum >= signalmax) |
|
|
668 | return; |
|
|
669 | |
|
|
670 | signals [signum].gotsig = 0; |
|
|
671 | |
|
|
672 | for (w = signals [signum].head; w; w = w->next) |
|
|
673 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
|
|
674 | } |
|
|
675 | |
459 | static void |
676 | static void |
460 | sigcb (EV_P_ struct ev_io *iow, int revents) |
677 | sigcb (EV_P_ ev_io *iow, int revents) |
461 | { |
678 | { |
462 | struct ev_watcher_list *w; |
|
|
463 | int signum; |
679 | int signum; |
464 | |
680 | |
465 | read (sigpipe [0], &revents, 1); |
681 | read (sigpipe [0], &revents, 1); |
466 | gotsig = 0; |
682 | gotsig = 0; |
467 | |
683 | |
468 | for (signum = signalmax; signum--; ) |
684 | for (signum = signalmax; signum--; ) |
469 | if (signals [signum].gotsig) |
685 | if (signals [signum].gotsig) |
|
|
686 | ev_feed_signal_event (EV_A_ signum + 1); |
|
|
687 | } |
|
|
688 | |
|
|
689 | void inline_size |
|
|
690 | fd_intern (int fd) |
|
|
691 | { |
|
|
692 | #ifdef _WIN32 |
|
|
693 | int arg = 1; |
|
|
694 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
|
|
695 | #else |
|
|
696 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
697 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
698 | #endif |
|
|
699 | } |
|
|
700 | |
|
|
701 | static void noinline |
|
|
702 | siginit (EV_P) |
|
|
703 | { |
|
|
704 | fd_intern (sigpipe [0]); |
|
|
705 | fd_intern (sigpipe [1]); |
|
|
706 | |
|
|
707 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
|
|
708 | ev_io_start (EV_A_ &sigev); |
|
|
709 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
|
|
710 | } |
|
|
711 | |
|
|
712 | /*****************************************************************************/ |
|
|
713 | |
|
|
714 | static ev_child *childs [PID_HASHSIZE]; |
|
|
715 | |
|
|
716 | #ifndef _WIN32 |
|
|
717 | |
|
|
718 | static ev_signal childev; |
|
|
719 | |
|
|
720 | void inline_speed |
|
|
721 | child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) |
|
|
722 | { |
|
|
723 | ev_child *w; |
|
|
724 | |
|
|
725 | for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
|
|
726 | if (w->pid == pid || !w->pid) |
470 | { |
727 | { |
471 | signals [signum].gotsig = 0; |
728 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
472 | |
729 | w->rpid = pid; |
473 | for (w = signals [signum].head; w; w = w->next) |
730 | w->rstatus = status; |
474 | event (EV_A_ (W)w, EV_SIGNAL); |
731 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
475 | } |
732 | } |
476 | } |
733 | } |
477 | |
|
|
478 | static void |
|
|
479 | siginit (EV_P) |
|
|
480 | { |
|
|
481 | #ifndef WIN32 |
|
|
482 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
|
|
483 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
|
|
484 | |
|
|
485 | /* rather than sort out wether we really need nb, set it */ |
|
|
486 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
|
|
487 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
488 | #endif |
|
|
489 | |
|
|
490 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
|
|
491 | ev_io_start (&sigev); |
|
|
492 | } |
|
|
493 | |
|
|
494 | /*****************************************************************************/ |
|
|
495 | |
|
|
496 | static struct ev_idle **idles; |
|
|
497 | static int idlemax, idlecnt; |
|
|
498 | |
|
|
499 | static struct ev_prepare **prepares; |
|
|
500 | static int preparemax, preparecnt; |
|
|
501 | |
|
|
502 | static struct ev_check **checks; |
|
|
503 | static int checkmax, checkcnt; |
|
|
504 | |
|
|
505 | /*****************************************************************************/ |
|
|
506 | |
|
|
507 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
508 | static struct ev_signal childev; |
|
|
509 | |
|
|
510 | #ifndef WIN32 |
|
|
511 | |
734 | |
512 | #ifndef WCONTINUED |
735 | #ifndef WCONTINUED |
513 | # define WCONTINUED 0 |
736 | # define WCONTINUED 0 |
514 | #endif |
737 | #endif |
515 | |
738 | |
516 | static void |
739 | static void |
517 | child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) |
|
|
518 | { |
|
|
519 | struct ev_child *w; |
|
|
520 | |
|
|
521 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
|
|
522 | if (w->pid == pid || !w->pid) |
|
|
523 | { |
|
|
524 | w->priority = sw->priority; /* need to do it *now* */ |
|
|
525 | w->rpid = pid; |
|
|
526 | w->rstatus = status; |
|
|
527 | event (EV_A_ (W)w, EV_CHILD); |
|
|
528 | } |
|
|
529 | } |
|
|
530 | |
|
|
531 | static void |
|
|
532 | childcb (EV_P_ struct ev_signal *sw, int revents) |
740 | childcb (EV_P_ ev_signal *sw, int revents) |
533 | { |
741 | { |
534 | int pid, status; |
742 | int pid, status; |
535 | |
743 | |
|
|
744 | /* some systems define WCONTINUED but then fail to support it (linux 2.4) */ |
536 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
745 | if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
537 | { |
746 | if (!WCONTINUED |
|
|
747 | || errno != EINVAL |
|
|
748 | || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED))) |
|
|
749 | return; |
|
|
750 | |
538 | /* make sure we are called again until all childs have been reaped */ |
751 | /* make sure we are called again until all childs have been reaped */ |
|
|
752 | /* we need to do it this way so that the callback gets called before we continue */ |
539 | event (EV_A_ (W)sw, EV_SIGNAL); |
753 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
540 | |
754 | |
541 | child_reap (EV_A_ sw, pid, pid, status); |
755 | child_reap (EV_A_ sw, pid, pid, status); |
542 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
756 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ |
543 | } |
|
|
544 | } |
757 | } |
545 | |
758 | |
546 | #endif |
759 | #endif |
547 | |
760 | |
548 | /*****************************************************************************/ |
761 | /*****************************************************************************/ |
549 | |
762 | |
|
|
763 | #if EV_USE_PORT |
|
|
764 | # include "ev_port.c" |
|
|
765 | #endif |
550 | #if EV_USE_KQUEUE |
766 | #if EV_USE_KQUEUE |
551 | # include "ev_kqueue.c" |
767 | # include "ev_kqueue.c" |
552 | #endif |
768 | #endif |
553 | #if EV_USE_EPOLL |
769 | #if EV_USE_EPOLL |
554 | # include "ev_epoll.c" |
770 | # include "ev_epoll.c" |
555 | #endif |
771 | #endif |
556 | #if EV_USEV_POLL |
772 | #if EV_USE_POLL |
557 | # include "ev_poll.c" |
773 | # include "ev_poll.c" |
558 | #endif |
774 | #endif |
559 | #if EV_USE_SELECT |
775 | #if EV_USE_SELECT |
560 | # include "ev_select.c" |
776 | # include "ev_select.c" |
561 | #endif |
777 | #endif |
… | |
… | |
571 | { |
787 | { |
572 | return EV_VERSION_MINOR; |
788 | return EV_VERSION_MINOR; |
573 | } |
789 | } |
574 | |
790 | |
575 | /* return true if we are running with elevated privileges and should ignore env variables */ |
791 | /* return true if we are running with elevated privileges and should ignore env variables */ |
576 | static int |
792 | int inline_size |
577 | enable_secure (void) |
793 | enable_secure (void) |
578 | { |
794 | { |
579 | #ifdef WIN32 |
795 | #ifdef _WIN32 |
580 | return 0; |
796 | return 0; |
581 | #else |
797 | #else |
582 | return getuid () != geteuid () |
798 | return getuid () != geteuid () |
583 | || getgid () != getegid (); |
799 | || getgid () != getegid (); |
584 | #endif |
800 | #endif |
585 | } |
801 | } |
586 | |
802 | |
587 | int |
803 | unsigned int |
588 | ev_method (EV_P) |
804 | ev_supported_backends (void) |
589 | { |
805 | { |
590 | return method; |
806 | unsigned int flags = 0; |
591 | } |
|
|
592 | |
807 | |
593 | int |
808 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
594 | ev_init (EV_P_ int methods) |
809 | if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; |
|
|
810 | if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL; |
|
|
811 | if (EV_USE_POLL ) flags |= EVBACKEND_POLL; |
|
|
812 | if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; |
|
|
813 | |
|
|
814 | return flags; |
|
|
815 | } |
|
|
816 | |
|
|
817 | unsigned int |
|
|
818 | ev_recommended_backends (void) |
595 | { |
819 | { |
596 | if (!method) |
820 | unsigned int flags = ev_supported_backends (); |
|
|
821 | |
|
|
822 | #ifndef __NetBSD__ |
|
|
823 | /* kqueue is borked on everything but netbsd apparently */ |
|
|
824 | /* it usually doesn't work correctly on anything but sockets and pipes */ |
|
|
825 | flags &= ~EVBACKEND_KQUEUE; |
|
|
826 | #endif |
|
|
827 | #ifdef __APPLE__ |
|
|
828 | // flags &= ~EVBACKEND_KQUEUE; for documentation |
|
|
829 | flags &= ~EVBACKEND_POLL; |
|
|
830 | #endif |
|
|
831 | |
|
|
832 | return flags; |
|
|
833 | } |
|
|
834 | |
|
|
835 | unsigned int |
|
|
836 | ev_embeddable_backends (void) |
|
|
837 | { |
|
|
838 | return EVBACKEND_EPOLL |
|
|
839 | | EVBACKEND_KQUEUE |
|
|
840 | | EVBACKEND_PORT; |
|
|
841 | } |
|
|
842 | |
|
|
843 | unsigned int |
|
|
844 | ev_backend (EV_P) |
|
|
845 | { |
|
|
846 | return backend; |
|
|
847 | } |
|
|
848 | |
|
|
849 | static void |
|
|
850 | loop_init (EV_P_ unsigned int flags) |
|
|
851 | { |
|
|
852 | if (!backend) |
597 | { |
853 | { |
598 | #if EV_USE_MONOTONIC |
854 | #if EV_USE_MONOTONIC |
599 | { |
855 | { |
600 | struct timespec ts; |
856 | struct timespec ts; |
601 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
857 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
602 | have_monotonic = 1; |
858 | have_monotonic = 1; |
603 | } |
859 | } |
604 | #endif |
860 | #endif |
605 | |
861 | |
606 | rt_now = ev_time (); |
862 | ev_rt_now = ev_time (); |
607 | mn_now = get_clock (); |
863 | mn_now = get_clock (); |
608 | now_floor = mn_now; |
864 | now_floor = mn_now; |
609 | diff = rt_now - mn_now; |
865 | rtmn_diff = ev_rt_now - mn_now; |
610 | |
866 | |
611 | if (pipe (sigpipe)) |
867 | if (!(flags & EVFLAG_NOENV) |
612 | return 0; |
868 | && !enable_secure () |
|
|
869 | && getenv ("LIBEV_FLAGS")) |
|
|
870 | flags = atoi (getenv ("LIBEV_FLAGS")); |
613 | |
871 | |
614 | if (methods == EVMETHOD_AUTO) |
872 | if (!(flags & 0x0000ffffUL)) |
615 | if (!enable_secure () && getenv ("LIBmethodS")) |
873 | flags |= ev_recommended_backends (); |
616 | methods = atoi (getenv ("LIBmethodS")); |
|
|
617 | else |
|
|
618 | methods = EVMETHOD_ANY; |
|
|
619 | |
874 | |
620 | method = 0; |
875 | backend = 0; |
|
|
876 | #if EV_USE_PORT |
|
|
877 | if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); |
|
|
878 | #endif |
621 | #if EV_USE_KQUEUE |
879 | #if EV_USE_KQUEUE |
622 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
880 | if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags); |
623 | #endif |
881 | #endif |
624 | #if EV_USE_EPOLL |
882 | #if EV_USE_EPOLL |
625 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
883 | if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags); |
626 | #endif |
884 | #endif |
627 | #if EV_USEV_POLL |
885 | #if EV_USE_POLL |
628 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
886 | if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags); |
629 | #endif |
887 | #endif |
630 | #if EV_USE_SELECT |
888 | #if EV_USE_SELECT |
631 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
889 | if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); |
632 | #endif |
890 | #endif |
633 | |
891 | |
634 | if (method) |
892 | ev_init (&sigev, sigcb); |
|
|
893 | ev_set_priority (&sigev, EV_MAXPRI); |
|
|
894 | } |
|
|
895 | } |
|
|
896 | |
|
|
897 | static void |
|
|
898 | loop_destroy (EV_P) |
|
|
899 | { |
|
|
900 | int i; |
|
|
901 | |
|
|
902 | #if EV_USE_PORT |
|
|
903 | if (backend == EVBACKEND_PORT ) port_destroy (EV_A); |
|
|
904 | #endif |
|
|
905 | #if EV_USE_KQUEUE |
|
|
906 | if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A); |
|
|
907 | #endif |
|
|
908 | #if EV_USE_EPOLL |
|
|
909 | if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A); |
|
|
910 | #endif |
|
|
911 | #if EV_USE_POLL |
|
|
912 | if (backend == EVBACKEND_POLL ) poll_destroy (EV_A); |
|
|
913 | #endif |
|
|
914 | #if EV_USE_SELECT |
|
|
915 | if (backend == EVBACKEND_SELECT) select_destroy (EV_A); |
|
|
916 | #endif |
|
|
917 | |
|
|
918 | for (i = NUMPRI; i--; ) |
|
|
919 | array_free (pending, [i]); |
|
|
920 | |
|
|
921 | /* have to use the microsoft-never-gets-it-right macro */ |
|
|
922 | array_free (fdchange, EMPTY0); |
|
|
923 | array_free (timer, EMPTY0); |
|
|
924 | #if EV_PERIODIC_ENABLE |
|
|
925 | array_free (periodic, EMPTY0); |
|
|
926 | #endif |
|
|
927 | array_free (idle, EMPTY0); |
|
|
928 | array_free (prepare, EMPTY0); |
|
|
929 | array_free (check, EMPTY0); |
|
|
930 | |
|
|
931 | backend = 0; |
|
|
932 | } |
|
|
933 | |
|
|
934 | static void |
|
|
935 | loop_fork (EV_P) |
|
|
936 | { |
|
|
937 | #if EV_USE_PORT |
|
|
938 | if (backend == EVBACKEND_PORT ) port_fork (EV_A); |
|
|
939 | #endif |
|
|
940 | #if EV_USE_KQUEUE |
|
|
941 | if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A); |
|
|
942 | #endif |
|
|
943 | #if EV_USE_EPOLL |
|
|
944 | if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); |
|
|
945 | #endif |
|
|
946 | |
|
|
947 | if (ev_is_active (&sigev)) |
|
|
948 | { |
|
|
949 | /* default loop */ |
|
|
950 | |
|
|
951 | ev_ref (EV_A); |
|
|
952 | ev_io_stop (EV_A_ &sigev); |
|
|
953 | close (sigpipe [0]); |
|
|
954 | close (sigpipe [1]); |
|
|
955 | |
|
|
956 | while (pipe (sigpipe)) |
|
|
957 | syserr ("(libev) error creating pipe"); |
|
|
958 | |
|
|
959 | siginit (EV_A); |
|
|
960 | } |
|
|
961 | |
|
|
962 | postfork = 0; |
|
|
963 | } |
|
|
964 | |
|
|
965 | #if EV_MULTIPLICITY |
|
|
966 | struct ev_loop * |
|
|
967 | ev_loop_new (unsigned int flags) |
|
|
968 | { |
|
|
969 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
|
|
970 | |
|
|
971 | memset (loop, 0, sizeof (struct ev_loop)); |
|
|
972 | |
|
|
973 | loop_init (EV_A_ flags); |
|
|
974 | |
|
|
975 | if (ev_backend (EV_A)) |
|
|
976 | return loop; |
|
|
977 | |
|
|
978 | return 0; |
|
|
979 | } |
|
|
980 | |
|
|
981 | void |
|
|
982 | ev_loop_destroy (EV_P) |
|
|
983 | { |
|
|
984 | loop_destroy (EV_A); |
|
|
985 | ev_free (loop); |
|
|
986 | } |
|
|
987 | |
|
|
988 | void |
|
|
989 | ev_loop_fork (EV_P) |
|
|
990 | { |
|
|
991 | postfork = 1; |
|
|
992 | } |
|
|
993 | |
|
|
994 | #endif |
|
|
995 | |
|
|
996 | #if EV_MULTIPLICITY |
|
|
997 | struct ev_loop * |
|
|
998 | ev_default_loop_init (unsigned int flags) |
|
|
999 | #else |
|
|
1000 | int |
|
|
1001 | ev_default_loop (unsigned int flags) |
|
|
1002 | #endif |
|
|
1003 | { |
|
|
1004 | if (sigpipe [0] == sigpipe [1]) |
|
|
1005 | if (pipe (sigpipe)) |
|
|
1006 | return 0; |
|
|
1007 | |
|
|
1008 | if (!ev_default_loop_ptr) |
|
|
1009 | { |
|
|
1010 | #if EV_MULTIPLICITY |
|
|
1011 | struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct; |
|
|
1012 | #else |
|
|
1013 | ev_default_loop_ptr = 1; |
|
|
1014 | #endif |
|
|
1015 | |
|
|
1016 | loop_init (EV_A_ flags); |
|
|
1017 | |
|
|
1018 | if (ev_backend (EV_A)) |
635 | { |
1019 | { |
636 | ev_watcher_init (&sigev, sigcb); |
|
|
637 | ev_set_priority (&sigev, EV_MAXPRI); |
|
|
638 | siginit (EV_A); |
1020 | siginit (EV_A); |
639 | |
1021 | |
640 | #ifndef WIN32 |
1022 | #ifndef _WIN32 |
641 | ev_signal_init (&childev, childcb, SIGCHLD); |
1023 | ev_signal_init (&childev, childcb, SIGCHLD); |
642 | ev_set_priority (&childev, EV_MAXPRI); |
1024 | ev_set_priority (&childev, EV_MAXPRI); |
643 | ev_signal_start (EV_A_ &childev); |
1025 | ev_signal_start (EV_A_ &childev); |
|
|
1026 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
644 | #endif |
1027 | #endif |
645 | } |
1028 | } |
|
|
1029 | else |
|
|
1030 | ev_default_loop_ptr = 0; |
646 | } |
1031 | } |
647 | |
1032 | |
648 | return method; |
1033 | return ev_default_loop_ptr; |
|
|
1034 | } |
|
|
1035 | |
|
|
1036 | void |
|
|
1037 | ev_default_destroy (void) |
|
|
1038 | { |
|
|
1039 | #if EV_MULTIPLICITY |
|
|
1040 | struct ev_loop *loop = ev_default_loop_ptr; |
|
|
1041 | #endif |
|
|
1042 | |
|
|
1043 | #ifndef _WIN32 |
|
|
1044 | ev_ref (EV_A); /* child watcher */ |
|
|
1045 | ev_signal_stop (EV_A_ &childev); |
|
|
1046 | #endif |
|
|
1047 | |
|
|
1048 | ev_ref (EV_A); /* signal watcher */ |
|
|
1049 | ev_io_stop (EV_A_ &sigev); |
|
|
1050 | |
|
|
1051 | close (sigpipe [0]); sigpipe [0] = 0; |
|
|
1052 | close (sigpipe [1]); sigpipe [1] = 0; |
|
|
1053 | |
|
|
1054 | loop_destroy (EV_A); |
|
|
1055 | } |
|
|
1056 | |
|
|
1057 | void |
|
|
1058 | ev_default_fork (void) |
|
|
1059 | { |
|
|
1060 | #if EV_MULTIPLICITY |
|
|
1061 | struct ev_loop *loop = ev_default_loop_ptr; |
|
|
1062 | #endif |
|
|
1063 | |
|
|
1064 | if (backend) |
|
|
1065 | postfork = 1; |
649 | } |
1066 | } |
650 | |
1067 | |
651 | /*****************************************************************************/ |
1068 | /*****************************************************************************/ |
652 | |
1069 | |
653 | void |
1070 | int inline_size |
654 | ev_fork_prepare (void) |
1071 | any_pending (EV_P) |
655 | { |
1072 | { |
656 | /* nop */ |
1073 | int pri; |
657 | } |
|
|
658 | |
1074 | |
659 | void |
1075 | for (pri = NUMPRI; pri--; ) |
660 | ev_fork_parent (void) |
1076 | if (pendingcnt [pri]) |
661 | { |
1077 | return 1; |
662 | /* nop */ |
|
|
663 | } |
|
|
664 | |
1078 | |
665 | void |
1079 | return 0; |
666 | ev_fork_child (void) |
|
|
667 | { |
|
|
668 | #if EV_USE_EPOLL |
|
|
669 | if (method == EVMETHOD_EPOLL) |
|
|
670 | epoll_postfork_child (); |
|
|
671 | #endif |
|
|
672 | |
|
|
673 | ev_io_stop (&sigev); |
|
|
674 | close (sigpipe [0]); |
|
|
675 | close (sigpipe [1]); |
|
|
676 | pipe (sigpipe); |
|
|
677 | siginit (); |
|
|
678 | } |
1080 | } |
679 | |
1081 | |
680 | /*****************************************************************************/ |
1082 | void inline_speed |
681 | |
|
|
682 | static void |
|
|
683 | call_pending (EV_P) |
1083 | call_pending (EV_P) |
684 | { |
1084 | { |
685 | int pri; |
1085 | int pri; |
686 | |
1086 | |
687 | for (pri = NUMPRI; pri--; ) |
1087 | for (pri = NUMPRI; pri--; ) |
688 | while (pendingcnt [pri]) |
1088 | while (pendingcnt [pri]) |
689 | { |
1089 | { |
690 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1090 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
691 | |
1091 | |
692 | if (p->w) |
1092 | if (expect_true (p->w)) |
693 | { |
1093 | { |
|
|
1094 | assert (("non-pending watcher on pending list", p->w->pending)); |
|
|
1095 | |
694 | p->w->pending = 0; |
1096 | p->w->pending = 0; |
695 | p->w->cb (EV_A_ p->w, p->events); |
1097 | EV_CB_INVOKE (p->w, p->events); |
696 | } |
1098 | } |
697 | } |
1099 | } |
698 | } |
1100 | } |
699 | |
1101 | |
700 | static void |
1102 | void inline_size |
701 | timers_reify (EV_P) |
1103 | timers_reify (EV_P) |
702 | { |
1104 | { |
703 | while (timercnt && timers [0]->at <= mn_now) |
1105 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
704 | { |
1106 | { |
705 | struct ev_timer *w = timers [0]; |
1107 | ev_timer *w = timers [0]; |
|
|
1108 | |
|
|
1109 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
706 | |
1110 | |
707 | /* first reschedule or stop timer */ |
1111 | /* first reschedule or stop timer */ |
708 | if (w->repeat) |
1112 | if (w->repeat) |
709 | { |
1113 | { |
710 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
1114 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
|
|
1115 | |
711 | w->at = mn_now + w->repeat; |
1116 | ((WT)w)->at += w->repeat; |
|
|
1117 | if (((WT)w)->at < mn_now) |
|
|
1118 | ((WT)w)->at = mn_now; |
|
|
1119 | |
712 | downheap ((WT *)timers, timercnt, 0); |
1120 | downheap ((WT *)timers, timercnt, 0); |
713 | } |
1121 | } |
714 | else |
1122 | else |
715 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1123 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
716 | |
1124 | |
717 | event ((W)w, EV_TIMEOUT); |
1125 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
718 | } |
1126 | } |
719 | } |
1127 | } |
720 | |
1128 | |
721 | static void |
1129 | #if EV_PERIODIC_ENABLE |
|
|
1130 | void inline_size |
722 | periodics_reify (EV_P) |
1131 | periodics_reify (EV_P) |
723 | { |
1132 | { |
724 | while (periodiccnt && periodics [0]->at <= rt_now) |
1133 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
725 | { |
1134 | { |
726 | struct ev_periodic *w = periodics [0]; |
1135 | ev_periodic *w = periodics [0]; |
|
|
1136 | |
|
|
1137 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
727 | |
1138 | |
728 | /* first reschedule or stop timer */ |
1139 | /* first reschedule or stop timer */ |
729 | if (w->interval) |
1140 | if (w->reschedule_cb) |
730 | { |
1141 | { |
|
|
1142 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
|
|
1143 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
|
|
1144 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
1145 | } |
|
|
1146 | else if (w->interval) |
|
|
1147 | { |
731 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
1148 | ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
732 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
1149 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
733 | downheap ((WT *)periodics, periodiccnt, 0); |
1150 | downheap ((WT *)periodics, periodiccnt, 0); |
734 | } |
1151 | } |
735 | else |
1152 | else |
736 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1153 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
737 | |
1154 | |
738 | event (EV_A_ (W)w, EV_PERIODIC); |
1155 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
739 | } |
1156 | } |
740 | } |
1157 | } |
741 | |
1158 | |
742 | static void |
1159 | static void noinline |
743 | periodics_reschedule (EV_P_ ev_tstamp diff) |
1160 | periodics_reschedule (EV_P) |
744 | { |
1161 | { |
745 | int i; |
1162 | int i; |
746 | |
1163 | |
747 | /* adjust periodics after time jump */ |
1164 | /* adjust periodics after time jump */ |
748 | for (i = 0; i < periodiccnt; ++i) |
1165 | for (i = 0; i < periodiccnt; ++i) |
749 | { |
1166 | { |
750 | struct ev_periodic *w = periodics [i]; |
1167 | ev_periodic *w = periodics [i]; |
751 | |
1168 | |
|
|
1169 | if (w->reschedule_cb) |
|
|
1170 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
752 | if (w->interval) |
1171 | else if (w->interval) |
753 | { |
|
|
754 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
1172 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
755 | |
|
|
756 | if (fabs (diff) >= 1e-4) |
|
|
757 | { |
|
|
758 | ev_periodic_stop (EV_A_ w); |
|
|
759 | ev_periodic_start (EV_A_ w); |
|
|
760 | |
|
|
761 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
762 | } |
|
|
763 | } |
|
|
764 | } |
1173 | } |
765 | } |
|
|
766 | |
1174 | |
767 | inline int |
1175 | /* now rebuild the heap */ |
|
|
1176 | for (i = periodiccnt >> 1; i--; ) |
|
|
1177 | downheap ((WT *)periodics, periodiccnt, i); |
|
|
1178 | } |
|
|
1179 | #endif |
|
|
1180 | |
|
|
1181 | int inline_size |
768 | time_update_monotonic (EV_P) |
1182 | time_update_monotonic (EV_P) |
769 | { |
1183 | { |
770 | mn_now = get_clock (); |
1184 | mn_now = get_clock (); |
771 | |
1185 | |
772 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1186 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
773 | { |
1187 | { |
774 | rt_now = mn_now + diff; |
1188 | ev_rt_now = rtmn_diff + mn_now; |
775 | return 0; |
1189 | return 0; |
776 | } |
1190 | } |
777 | else |
1191 | else |
778 | { |
1192 | { |
779 | now_floor = mn_now; |
1193 | now_floor = mn_now; |
780 | rt_now = ev_time (); |
1194 | ev_rt_now = ev_time (); |
781 | return 1; |
1195 | return 1; |
782 | } |
1196 | } |
783 | } |
1197 | } |
784 | |
1198 | |
785 | static void |
1199 | void inline_size |
786 | time_update (EV_P) |
1200 | time_update (EV_P) |
787 | { |
1201 | { |
788 | int i; |
1202 | int i; |
789 | |
1203 | |
790 | #if EV_USE_MONOTONIC |
1204 | #if EV_USE_MONOTONIC |
791 | if (expect_true (have_monotonic)) |
1205 | if (expect_true (have_monotonic)) |
792 | { |
1206 | { |
793 | if (time_update_monotonic (EV_A)) |
1207 | if (time_update_monotonic (EV_A)) |
794 | { |
1208 | { |
795 | ev_tstamp odiff = diff; |
1209 | ev_tstamp odiff = rtmn_diff; |
796 | |
1210 | |
797 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1211 | /* loop a few times, before making important decisions. |
|
|
1212 | * on the choice of "4": one iteration isn't enough, |
|
|
1213 | * in case we get preempted during the calls to |
|
|
1214 | * ev_time and get_clock. a second call is almost guarenteed |
|
|
1215 | * to succeed in that case, though. and looping a few more times |
|
|
1216 | * doesn't hurt either as we only do this on time-jumps or |
|
|
1217 | * in the unlikely event of getting preempted here. |
|
|
1218 | */ |
|
|
1219 | for (i = 4; --i; ) |
798 | { |
1220 | { |
799 | diff = rt_now - mn_now; |
1221 | rtmn_diff = ev_rt_now - mn_now; |
800 | |
1222 | |
801 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
1223 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
802 | return; /* all is well */ |
1224 | return; /* all is well */ |
803 | |
1225 | |
804 | rt_now = ev_time (); |
1226 | ev_rt_now = ev_time (); |
805 | mn_now = get_clock (); |
1227 | mn_now = get_clock (); |
806 | now_floor = mn_now; |
1228 | now_floor = mn_now; |
807 | } |
1229 | } |
808 | |
1230 | |
|
|
1231 | # if EV_PERIODIC_ENABLE |
809 | periodics_reschedule (EV_A_ diff - odiff); |
1232 | periodics_reschedule (EV_A); |
|
|
1233 | # endif |
810 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1234 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
1235 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
811 | } |
1236 | } |
812 | } |
1237 | } |
813 | else |
1238 | else |
814 | #endif |
1239 | #endif |
815 | { |
1240 | { |
816 | rt_now = ev_time (); |
1241 | ev_rt_now = ev_time (); |
817 | |
1242 | |
818 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1243 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
819 | { |
1244 | { |
|
|
1245 | #if EV_PERIODIC_ENABLE |
820 | periodics_reschedule (EV_A_ rt_now - mn_now); |
1246 | periodics_reschedule (EV_A); |
|
|
1247 | #endif |
821 | |
1248 | |
822 | /* adjust timers. this is easy, as the offset is the same for all */ |
1249 | /* adjust timers. this is easy, as the offset is the same for all */ |
823 | for (i = 0; i < timercnt; ++i) |
1250 | for (i = 0; i < timercnt; ++i) |
824 | timers [i]->at += diff; |
1251 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
825 | } |
1252 | } |
826 | |
1253 | |
827 | mn_now = rt_now; |
1254 | mn_now = ev_rt_now; |
828 | } |
1255 | } |
829 | } |
1256 | } |
830 | |
1257 | |
831 | void |
1258 | void |
832 | ev_ref (EV_P) |
1259 | ev_ref (EV_P) |
… | |
… | |
843 | static int loop_done; |
1270 | static int loop_done; |
844 | |
1271 | |
845 | void |
1272 | void |
846 | ev_loop (EV_P_ int flags) |
1273 | ev_loop (EV_P_ int flags) |
847 | { |
1274 | { |
848 | double block; |
|
|
849 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
1275 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) |
|
|
1276 | ? EVUNLOOP_ONE |
|
|
1277 | : EVUNLOOP_CANCEL; |
850 | |
1278 | |
851 | do |
1279 | while (activecnt) |
852 | { |
1280 | { |
853 | /* queue check watchers (and execute them) */ |
1281 | /* queue check watchers (and execute them) */ |
854 | if (expect_false (preparecnt)) |
1282 | if (expect_false (preparecnt)) |
855 | { |
1283 | { |
856 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1284 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
857 | call_pending (EV_A); |
1285 | call_pending (EV_A); |
858 | } |
1286 | } |
859 | |
1287 | |
|
|
1288 | /* we might have forked, so reify kernel state if necessary */ |
|
|
1289 | if (expect_false (postfork)) |
|
|
1290 | loop_fork (EV_A); |
|
|
1291 | |
860 | /* update fd-related kernel structures */ |
1292 | /* update fd-related kernel structures */ |
861 | fd_reify (EV_A); |
1293 | fd_reify (EV_A); |
862 | |
1294 | |
863 | /* calculate blocking time */ |
1295 | /* calculate blocking time */ |
|
|
1296 | { |
|
|
1297 | double block; |
864 | |
1298 | |
865 | /* we only need this for !monotonic clockor timers, but as we basically |
1299 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
866 | always have timers, we just calculate it always */ |
1300 | block = 0.; /* do not block at all */ |
|
|
1301 | else |
|
|
1302 | { |
|
|
1303 | /* update time to cancel out callback processing overhead */ |
867 | #if EV_USE_MONOTONIC |
1304 | #if EV_USE_MONOTONIC |
868 | if (expect_true (have_monotonic)) |
1305 | if (expect_true (have_monotonic)) |
869 | time_update_monotonic (EV_A); |
1306 | time_update_monotonic (EV_A); |
870 | else |
1307 | else |
871 | #endif |
1308 | #endif |
872 | { |
1309 | { |
873 | rt_now = ev_time (); |
1310 | ev_rt_now = ev_time (); |
874 | mn_now = rt_now; |
1311 | mn_now = ev_rt_now; |
875 | } |
1312 | } |
876 | |
1313 | |
877 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
|
|
878 | block = 0.; |
|
|
879 | else |
|
|
880 | { |
|
|
881 | block = MAX_BLOCKTIME; |
1314 | block = MAX_BLOCKTIME; |
882 | |
1315 | |
883 | if (timercnt) |
1316 | if (timercnt) |
884 | { |
1317 | { |
885 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
1318 | ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; |
886 | if (block > to) block = to; |
1319 | if (block > to) block = to; |
887 | } |
1320 | } |
888 | |
1321 | |
|
|
1322 | #if EV_PERIODIC_ENABLE |
889 | if (periodiccnt) |
1323 | if (periodiccnt) |
890 | { |
1324 | { |
891 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
1325 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; |
892 | if (block > to) block = to; |
1326 | if (block > to) block = to; |
893 | } |
1327 | } |
|
|
1328 | #endif |
894 | |
1329 | |
895 | if (block < 0.) block = 0.; |
1330 | if (expect_false (block < 0.)) block = 0.; |
896 | } |
1331 | } |
897 | |
1332 | |
898 | method_poll (EV_A_ block); |
1333 | backend_poll (EV_A_ block); |
|
|
1334 | } |
899 | |
1335 | |
900 | /* update rt_now, do magic */ |
1336 | /* update ev_rt_now, do magic */ |
901 | time_update (EV_A); |
1337 | time_update (EV_A); |
902 | |
1338 | |
903 | /* queue pending timers and reschedule them */ |
1339 | /* queue pending timers and reschedule them */ |
904 | timers_reify (EV_A); /* relative timers called last */ |
1340 | timers_reify (EV_A); /* relative timers called last */ |
|
|
1341 | #if EV_PERIODIC_ENABLE |
905 | periodics_reify (EV_A); /* absolute timers called first */ |
1342 | periodics_reify (EV_A); /* absolute timers called first */ |
|
|
1343 | #endif |
906 | |
1344 | |
907 | /* queue idle watchers unless io or timers are pending */ |
1345 | /* queue idle watchers unless other events are pending */ |
908 | if (!pendingcnt) |
1346 | if (idlecnt && !any_pending (EV_A)) |
909 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1347 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
910 | |
1348 | |
911 | /* queue check watchers, to be executed first */ |
1349 | /* queue check watchers, to be executed first */ |
912 | if (checkcnt) |
1350 | if (expect_false (checkcnt)) |
913 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1351 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
914 | |
1352 | |
915 | call_pending (EV_A); |
1353 | call_pending (EV_A); |
916 | printf ("activecnt %d\n", activecnt);//D |
|
|
917 | } |
|
|
918 | while (activecnt && !loop_done); |
|
|
919 | |
1354 | |
920 | if (loop_done != 2) |
1355 | if (expect_false (loop_done)) |
921 | loop_done = 0; |
1356 | break; |
|
|
1357 | } |
|
|
1358 | |
|
|
1359 | if (loop_done == EVUNLOOP_ONE) |
|
|
1360 | loop_done = EVUNLOOP_CANCEL; |
922 | } |
1361 | } |
923 | |
1362 | |
924 | void |
1363 | void |
925 | ev_unloop (EV_P_ int how) |
1364 | ev_unloop (EV_P_ int how) |
926 | { |
1365 | { |
927 | loop_done = how; |
1366 | loop_done = how; |
928 | } |
1367 | } |
929 | |
1368 | |
930 | /*****************************************************************************/ |
1369 | /*****************************************************************************/ |
931 | |
1370 | |
932 | inline void |
1371 | void inline_size |
933 | wlist_add (WL *head, WL elem) |
1372 | wlist_add (WL *head, WL elem) |
934 | { |
1373 | { |
935 | elem->next = *head; |
1374 | elem->next = *head; |
936 | *head = elem; |
1375 | *head = elem; |
937 | } |
1376 | } |
938 | |
1377 | |
939 | inline void |
1378 | void inline_size |
940 | wlist_del (WL *head, WL elem) |
1379 | wlist_del (WL *head, WL elem) |
941 | { |
1380 | { |
942 | while (*head) |
1381 | while (*head) |
943 | { |
1382 | { |
944 | if (*head == elem) |
1383 | if (*head == elem) |
… | |
… | |
949 | |
1388 | |
950 | head = &(*head)->next; |
1389 | head = &(*head)->next; |
951 | } |
1390 | } |
952 | } |
1391 | } |
953 | |
1392 | |
954 | inline void |
1393 | void inline_speed |
955 | ev_clear_pending (EV_P_ W w) |
1394 | ev_clear_pending (EV_P_ W w) |
956 | { |
1395 | { |
957 | if (w->pending) |
1396 | if (w->pending) |
958 | { |
1397 | { |
959 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1398 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
960 | w->pending = 0; |
1399 | w->pending = 0; |
961 | } |
1400 | } |
962 | } |
1401 | } |
963 | |
1402 | |
964 | inline void |
1403 | void inline_speed |
965 | ev_start (EV_P_ W w, int active) |
1404 | ev_start (EV_P_ W w, int active) |
966 | { |
1405 | { |
967 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1406 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
968 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
1407 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
969 | |
1408 | |
970 | w->active = active; |
1409 | w->active = active; |
971 | ev_ref (EV_A); |
1410 | ev_ref (EV_A); |
972 | } |
1411 | } |
973 | |
1412 | |
974 | inline void |
1413 | void inline_size |
975 | ev_stop (EV_P_ W w) |
1414 | ev_stop (EV_P_ W w) |
976 | { |
1415 | { |
977 | ev_unref (EV_A); |
1416 | ev_unref (EV_A); |
978 | w->active = 0; |
1417 | w->active = 0; |
979 | } |
1418 | } |
980 | |
1419 | |
981 | /*****************************************************************************/ |
1420 | /*****************************************************************************/ |
982 | |
1421 | |
983 | void |
1422 | void |
984 | ev_io_start (EV_P_ struct ev_io *w) |
1423 | ev_io_start (EV_P_ ev_io *w) |
985 | { |
1424 | { |
986 | int fd = w->fd; |
1425 | int fd = w->fd; |
987 | |
1426 | |
988 | if (ev_is_active (w)) |
1427 | if (expect_false (ev_is_active (w))) |
989 | return; |
1428 | return; |
990 | |
1429 | |
991 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1430 | assert (("ev_io_start called with negative fd", fd >= 0)); |
992 | |
1431 | |
993 | ev_start (EV_A_ (W)w, 1); |
1432 | ev_start (EV_A_ (W)w, 1); |
994 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1433 | array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
995 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1434 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
996 | |
1435 | |
997 | fd_change (EV_A_ fd); |
1436 | fd_change (EV_A_ fd); |
998 | } |
1437 | } |
999 | |
1438 | |
1000 | void |
1439 | void |
1001 | ev_io_stop (EV_P_ struct ev_io *w) |
1440 | ev_io_stop (EV_P_ ev_io *w) |
1002 | { |
1441 | { |
1003 | ev_clear_pending (EV_A_ (W)w); |
1442 | ev_clear_pending (EV_A_ (W)w); |
1004 | if (!ev_is_active (w)) |
1443 | if (expect_false (!ev_is_active (w))) |
1005 | return; |
1444 | return; |
|
|
1445 | |
|
|
1446 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
1006 | |
1447 | |
1007 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1448 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1008 | ev_stop (EV_A_ (W)w); |
1449 | ev_stop (EV_A_ (W)w); |
1009 | |
1450 | |
1010 | fd_change (EV_A_ w->fd); |
1451 | fd_change (EV_A_ w->fd); |
1011 | } |
1452 | } |
1012 | |
1453 | |
1013 | void |
1454 | void |
1014 | ev_timer_start (EV_P_ struct ev_timer *w) |
1455 | ev_timer_start (EV_P_ ev_timer *w) |
1015 | { |
1456 | { |
1016 | if (ev_is_active (w)) |
1457 | if (expect_false (ev_is_active (w))) |
1017 | return; |
1458 | return; |
1018 | |
1459 | |
1019 | w->at += mn_now; |
1460 | ((WT)w)->at += mn_now; |
1020 | |
1461 | |
1021 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1462 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1022 | |
1463 | |
1023 | ev_start (EV_A_ (W)w, ++timercnt); |
1464 | ev_start (EV_A_ (W)w, ++timercnt); |
1024 | array_needsize (timers, timermax, timercnt, ); |
1465 | array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); |
1025 | timers [timercnt - 1] = w; |
1466 | timers [timercnt - 1] = w; |
1026 | upheap ((WT *)timers, timercnt - 1); |
1467 | upheap ((WT *)timers, timercnt - 1); |
1027 | } |
|
|
1028 | |
1468 | |
|
|
1469 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1470 | } |
|
|
1471 | |
1029 | void |
1472 | void |
1030 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1473 | ev_timer_stop (EV_P_ ev_timer *w) |
1031 | { |
1474 | { |
1032 | ev_clear_pending (EV_A_ (W)w); |
1475 | ev_clear_pending (EV_A_ (W)w); |
1033 | if (!ev_is_active (w)) |
1476 | if (expect_false (!ev_is_active (w))) |
1034 | return; |
1477 | return; |
1035 | |
1478 | |
|
|
1479 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1480 | |
1036 | if (w->active < timercnt--) |
1481 | if (expect_true (((W)w)->active < timercnt--)) |
1037 | { |
1482 | { |
1038 | timers [w->active - 1] = timers [timercnt]; |
1483 | timers [((W)w)->active - 1] = timers [timercnt]; |
1039 | downheap ((WT *)timers, timercnt, w->active - 1); |
1484 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1040 | } |
1485 | } |
1041 | |
1486 | |
1042 | w->at = w->repeat; |
1487 | ((WT)w)->at -= mn_now; |
1043 | |
1488 | |
1044 | ev_stop (EV_A_ (W)w); |
1489 | ev_stop (EV_A_ (W)w); |
1045 | } |
1490 | } |
1046 | |
1491 | |
1047 | void |
1492 | void |
1048 | ev_timer_again (EV_P_ struct ev_timer *w) |
1493 | ev_timer_again (EV_P_ ev_timer *w) |
1049 | { |
1494 | { |
1050 | if (ev_is_active (w)) |
1495 | if (ev_is_active (w)) |
1051 | { |
1496 | { |
1052 | if (w->repeat) |
1497 | if (w->repeat) |
1053 | { |
1498 | { |
1054 | w->at = mn_now + w->repeat; |
1499 | ((WT)w)->at = mn_now + w->repeat; |
1055 | downheap ((WT *)timers, timercnt, w->active - 1); |
1500 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1056 | } |
1501 | } |
1057 | else |
1502 | else |
1058 | ev_timer_stop (EV_A_ w); |
1503 | ev_timer_stop (EV_A_ w); |
1059 | } |
1504 | } |
1060 | else if (w->repeat) |
1505 | else if (w->repeat) |
|
|
1506 | { |
|
|
1507 | w->at = w->repeat; |
1061 | ev_timer_start (EV_A_ w); |
1508 | ev_timer_start (EV_A_ w); |
|
|
1509 | } |
1062 | } |
1510 | } |
1063 | |
1511 | |
|
|
1512 | #if EV_PERIODIC_ENABLE |
1064 | void |
1513 | void |
1065 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1514 | ev_periodic_start (EV_P_ ev_periodic *w) |
1066 | { |
1515 | { |
1067 | if (ev_is_active (w)) |
1516 | if (expect_false (ev_is_active (w))) |
1068 | return; |
1517 | return; |
1069 | |
1518 | |
|
|
1519 | if (w->reschedule_cb) |
|
|
1520 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
|
|
1521 | else if (w->interval) |
|
|
1522 | { |
1070 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1523 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1071 | |
|
|
1072 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1524 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1073 | if (w->interval) |
|
|
1074 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
1525 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1526 | } |
1075 | |
1527 | |
1076 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1528 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1077 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1529 | array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1078 | periodics [periodiccnt - 1] = w; |
1530 | periodics [periodiccnt - 1] = w; |
1079 | upheap ((WT *)periodics, periodiccnt - 1); |
1531 | upheap ((WT *)periodics, periodiccnt - 1); |
1080 | } |
|
|
1081 | |
1532 | |
|
|
1533 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1534 | } |
|
|
1535 | |
1082 | void |
1536 | void |
1083 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1537 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1084 | { |
1538 | { |
1085 | ev_clear_pending (EV_A_ (W)w); |
1539 | ev_clear_pending (EV_A_ (W)w); |
1086 | if (!ev_is_active (w)) |
1540 | if (expect_false (!ev_is_active (w))) |
1087 | return; |
1541 | return; |
1088 | |
1542 | |
|
|
1543 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1544 | |
1089 | if (w->active < periodiccnt--) |
1545 | if (expect_true (((W)w)->active < periodiccnt--)) |
1090 | { |
1546 | { |
1091 | periodics [w->active - 1] = periodics [periodiccnt]; |
1547 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1092 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1548 | adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
|
|
1549 | } |
|
|
1550 | |
|
|
1551 | ev_stop (EV_A_ (W)w); |
|
|
1552 | } |
|
|
1553 | |
|
|
1554 | void |
|
|
1555 | ev_periodic_again (EV_P_ ev_periodic *w) |
|
|
1556 | { |
|
|
1557 | /* TODO: use adjustheap and recalculation */ |
|
|
1558 | ev_periodic_stop (EV_A_ w); |
|
|
1559 | ev_periodic_start (EV_A_ w); |
|
|
1560 | } |
|
|
1561 | #endif |
|
|
1562 | |
|
|
1563 | void |
|
|
1564 | ev_idle_start (EV_P_ ev_idle *w) |
|
|
1565 | { |
|
|
1566 | if (expect_false (ev_is_active (w))) |
|
|
1567 | return; |
|
|
1568 | |
|
|
1569 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1570 | array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
|
|
1571 | idles [idlecnt - 1] = w; |
|
|
1572 | } |
|
|
1573 | |
|
|
1574 | void |
|
|
1575 | ev_idle_stop (EV_P_ ev_idle *w) |
|
|
1576 | { |
|
|
1577 | ev_clear_pending (EV_A_ (W)w); |
|
|
1578 | if (expect_false (!ev_is_active (w))) |
|
|
1579 | return; |
|
|
1580 | |
|
|
1581 | { |
|
|
1582 | int active = ((W)w)->active; |
|
|
1583 | idles [active - 1] = idles [--idlecnt]; |
|
|
1584 | ((W)idles [active - 1])->active = active; |
1093 | } |
1585 | } |
|
|
1586 | |
|
|
1587 | ev_stop (EV_A_ (W)w); |
|
|
1588 | } |
|
|
1589 | |
|
|
1590 | void |
|
|
1591 | ev_prepare_start (EV_P_ ev_prepare *w) |
|
|
1592 | { |
|
|
1593 | if (expect_false (ev_is_active (w))) |
|
|
1594 | return; |
|
|
1595 | |
|
|
1596 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1597 | array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
|
|
1598 | prepares [preparecnt - 1] = w; |
|
|
1599 | } |
|
|
1600 | |
|
|
1601 | void |
|
|
1602 | ev_prepare_stop (EV_P_ ev_prepare *w) |
|
|
1603 | { |
|
|
1604 | ev_clear_pending (EV_A_ (W)w); |
|
|
1605 | if (expect_false (!ev_is_active (w))) |
|
|
1606 | return; |
|
|
1607 | |
|
|
1608 | { |
|
|
1609 | int active = ((W)w)->active; |
|
|
1610 | prepares [active - 1] = prepares [--preparecnt]; |
|
|
1611 | ((W)prepares [active - 1])->active = active; |
|
|
1612 | } |
|
|
1613 | |
|
|
1614 | ev_stop (EV_A_ (W)w); |
|
|
1615 | } |
|
|
1616 | |
|
|
1617 | void |
|
|
1618 | ev_check_start (EV_P_ ev_check *w) |
|
|
1619 | { |
|
|
1620 | if (expect_false (ev_is_active (w))) |
|
|
1621 | return; |
|
|
1622 | |
|
|
1623 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1624 | array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2); |
|
|
1625 | checks [checkcnt - 1] = w; |
|
|
1626 | } |
|
|
1627 | |
|
|
1628 | void |
|
|
1629 | ev_check_stop (EV_P_ ev_check *w) |
|
|
1630 | { |
|
|
1631 | ev_clear_pending (EV_A_ (W)w); |
|
|
1632 | if (expect_false (!ev_is_active (w))) |
|
|
1633 | return; |
|
|
1634 | |
|
|
1635 | { |
|
|
1636 | int active = ((W)w)->active; |
|
|
1637 | checks [active - 1] = checks [--checkcnt]; |
|
|
1638 | ((W)checks [active - 1])->active = active; |
|
|
1639 | } |
1094 | |
1640 | |
1095 | ev_stop (EV_A_ (W)w); |
1641 | ev_stop (EV_A_ (W)w); |
1096 | } |
1642 | } |
1097 | |
1643 | |
1098 | #ifndef SA_RESTART |
1644 | #ifndef SA_RESTART |
1099 | # define SA_RESTART 0 |
1645 | # define SA_RESTART 0 |
1100 | #endif |
1646 | #endif |
1101 | |
1647 | |
1102 | void |
1648 | void |
1103 | ev_signal_start (EV_P_ struct ev_signal *w) |
1649 | ev_signal_start (EV_P_ ev_signal *w) |
1104 | { |
1650 | { |
|
|
1651 | #if EV_MULTIPLICITY |
|
|
1652 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
|
|
1653 | #endif |
1105 | if (ev_is_active (w)) |
1654 | if (expect_false (ev_is_active (w))) |
1106 | return; |
1655 | return; |
1107 | |
1656 | |
1108 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1657 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1109 | |
1658 | |
1110 | ev_start (EV_A_ (W)w, 1); |
1659 | ev_start (EV_A_ (W)w, 1); |
1111 | array_needsize (signals, signalmax, w->signum, signals_init); |
1660 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1112 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1661 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1113 | |
1662 | |
1114 | if (!w->next) |
1663 | if (!((WL)w)->next) |
1115 | { |
1664 | { |
|
|
1665 | #if _WIN32 |
|
|
1666 | signal (w->signum, sighandler); |
|
|
1667 | #else |
1116 | struct sigaction sa; |
1668 | struct sigaction sa; |
1117 | sa.sa_handler = sighandler; |
1669 | sa.sa_handler = sighandler; |
1118 | sigfillset (&sa.sa_mask); |
1670 | sigfillset (&sa.sa_mask); |
1119 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1671 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1120 | sigaction (w->signum, &sa, 0); |
1672 | sigaction (w->signum, &sa, 0); |
|
|
1673 | #endif |
1121 | } |
1674 | } |
1122 | } |
1675 | } |
1123 | |
1676 | |
1124 | void |
1677 | void |
1125 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1678 | ev_signal_stop (EV_P_ ev_signal *w) |
1126 | { |
1679 | { |
1127 | ev_clear_pending (EV_A_ (W)w); |
1680 | ev_clear_pending (EV_A_ (W)w); |
1128 | if (!ev_is_active (w)) |
1681 | if (expect_false (!ev_is_active (w))) |
1129 | return; |
1682 | return; |
1130 | |
1683 | |
1131 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1684 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1132 | ev_stop (EV_A_ (W)w); |
1685 | ev_stop (EV_A_ (W)w); |
1133 | |
1686 | |
1134 | if (!signals [w->signum - 1].head) |
1687 | if (!signals [w->signum - 1].head) |
1135 | signal (w->signum, SIG_DFL); |
1688 | signal (w->signum, SIG_DFL); |
1136 | } |
1689 | } |
1137 | |
1690 | |
1138 | void |
1691 | void |
1139 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1140 | { |
|
|
1141 | if (ev_is_active (w)) |
|
|
1142 | return; |
|
|
1143 | |
|
|
1144 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1145 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1146 | idles [idlecnt - 1] = w; |
|
|
1147 | } |
|
|
1148 | |
|
|
1149 | void |
|
|
1150 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1151 | { |
|
|
1152 | ev_clear_pending (EV_A_ (W)w); |
|
|
1153 | if (ev_is_active (w)) |
|
|
1154 | return; |
|
|
1155 | |
|
|
1156 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
1157 | ev_stop (EV_A_ (W)w); |
|
|
1158 | } |
|
|
1159 | |
|
|
1160 | void |
|
|
1161 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1162 | { |
|
|
1163 | if (ev_is_active (w)) |
|
|
1164 | return; |
|
|
1165 | |
|
|
1166 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1167 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1168 | prepares [preparecnt - 1] = w; |
|
|
1169 | } |
|
|
1170 | |
|
|
1171 | void |
|
|
1172 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1173 | { |
|
|
1174 | ev_clear_pending (EV_A_ (W)w); |
|
|
1175 | if (ev_is_active (w)) |
|
|
1176 | return; |
|
|
1177 | |
|
|
1178 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1179 | ev_stop (EV_A_ (W)w); |
|
|
1180 | } |
|
|
1181 | |
|
|
1182 | void |
|
|
1183 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1184 | { |
|
|
1185 | if (ev_is_active (w)) |
|
|
1186 | return; |
|
|
1187 | |
|
|
1188 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1189 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1190 | checks [checkcnt - 1] = w; |
|
|
1191 | } |
|
|
1192 | |
|
|
1193 | void |
|
|
1194 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1195 | { |
|
|
1196 | ev_clear_pending (EV_A_ (W)w); |
|
|
1197 | if (ev_is_active (w)) |
|
|
1198 | return; |
|
|
1199 | |
|
|
1200 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1201 | ev_stop (EV_A_ (W)w); |
|
|
1202 | } |
|
|
1203 | |
|
|
1204 | void |
|
|
1205 | ev_child_start (EV_P_ struct ev_child *w) |
1692 | ev_child_start (EV_P_ ev_child *w) |
1206 | { |
1693 | { |
|
|
1694 | #if EV_MULTIPLICITY |
|
|
1695 | assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
|
|
1696 | #endif |
1207 | if (ev_is_active (w)) |
1697 | if (expect_false (ev_is_active (w))) |
1208 | return; |
1698 | return; |
1209 | |
1699 | |
1210 | ev_start (EV_A_ (W)w, 1); |
1700 | ev_start (EV_A_ (W)w, 1); |
1211 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1701 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1212 | } |
1702 | } |
1213 | |
1703 | |
1214 | void |
1704 | void |
1215 | ev_child_stop (EV_P_ struct ev_child *w) |
1705 | ev_child_stop (EV_P_ ev_child *w) |
1216 | { |
1706 | { |
1217 | ev_clear_pending (EV_A_ (W)w); |
1707 | ev_clear_pending (EV_A_ (W)w); |
1218 | if (ev_is_active (w)) |
1708 | if (expect_false (!ev_is_active (w))) |
1219 | return; |
1709 | return; |
1220 | |
1710 | |
1221 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1711 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1222 | ev_stop (EV_A_ (W)w); |
1712 | ev_stop (EV_A_ (W)w); |
1223 | } |
1713 | } |
1224 | |
1714 | |
|
|
1715 | #if EV_EMBED_ENABLE |
|
|
1716 | void noinline |
|
|
1717 | ev_embed_sweep (EV_P_ ev_embed *w) |
|
|
1718 | { |
|
|
1719 | ev_loop (w->loop, EVLOOP_NONBLOCK); |
|
|
1720 | } |
|
|
1721 | |
|
|
1722 | static void |
|
|
1723 | embed_cb (EV_P_ ev_io *io, int revents) |
|
|
1724 | { |
|
|
1725 | ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); |
|
|
1726 | |
|
|
1727 | if (ev_cb (w)) |
|
|
1728 | ev_feed_event (EV_A_ (W)w, EV_EMBED); |
|
|
1729 | else |
|
|
1730 | ev_embed_sweep (loop, w); |
|
|
1731 | } |
|
|
1732 | |
|
|
1733 | void |
|
|
1734 | ev_embed_start (EV_P_ ev_embed *w) |
|
|
1735 | { |
|
|
1736 | if (expect_false (ev_is_active (w))) |
|
|
1737 | return; |
|
|
1738 | |
|
|
1739 | { |
|
|
1740 | struct ev_loop *loop = w->loop; |
|
|
1741 | assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); |
|
|
1742 | ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); |
|
|
1743 | } |
|
|
1744 | |
|
|
1745 | ev_set_priority (&w->io, ev_priority (w)); |
|
|
1746 | ev_io_start (EV_A_ &w->io); |
|
|
1747 | |
|
|
1748 | ev_start (EV_A_ (W)w, 1); |
|
|
1749 | } |
|
|
1750 | |
|
|
1751 | void |
|
|
1752 | ev_embed_stop (EV_P_ ev_embed *w) |
|
|
1753 | { |
|
|
1754 | ev_clear_pending (EV_A_ (W)w); |
|
|
1755 | if (expect_false (!ev_is_active (w))) |
|
|
1756 | return; |
|
|
1757 | |
|
|
1758 | ev_io_stop (EV_A_ &w->io); |
|
|
1759 | |
|
|
1760 | ev_stop (EV_A_ (W)w); |
|
|
1761 | } |
|
|
1762 | #endif |
|
|
1763 | |
|
|
1764 | #if EV_STAT_ENABLE |
|
|
1765 | |
|
|
1766 | # ifdef _WIN32 |
|
|
1767 | # define lstat(a,b) stat(a,b) |
|
|
1768 | # endif |
|
|
1769 | |
|
|
1770 | void |
|
|
1771 | ev_stat_stat (EV_P_ ev_stat *w) |
|
|
1772 | { |
|
|
1773 | if (lstat (w->path, &w->attr) < 0) |
|
|
1774 | w->attr.st_nlink = 0; |
|
|
1775 | else if (!w->attr.st_nlink) |
|
|
1776 | w->attr.st_nlink = 1; |
|
|
1777 | } |
|
|
1778 | |
|
|
1779 | static void |
|
|
1780 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
|
|
1781 | { |
|
|
1782 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
|
|
1783 | |
|
|
1784 | /* we copy this here each the time so that */ |
|
|
1785 | /* prev has the old value when the callback gets invoked */ |
|
|
1786 | w->prev = w->attr; |
|
|
1787 | ev_stat_stat (EV_A_ w); |
|
|
1788 | |
|
|
1789 | if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata))) |
|
|
1790 | ev_feed_event (EV_A_ w, EV_STAT); |
|
|
1791 | } |
|
|
1792 | |
|
|
1793 | void |
|
|
1794 | ev_stat_start (EV_P_ ev_stat *w) |
|
|
1795 | { |
|
|
1796 | if (expect_false (ev_is_active (w))) |
|
|
1797 | return; |
|
|
1798 | |
|
|
1799 | /* since we use memcmp, we need to clear any padding data etc. */ |
|
|
1800 | memset (&w->prev, 0, sizeof (ev_statdata)); |
|
|
1801 | memset (&w->attr, 0, sizeof (ev_statdata)); |
|
|
1802 | |
|
|
1803 | ev_stat_stat (EV_A_ w); |
|
|
1804 | |
|
|
1805 | ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval); |
|
|
1806 | ev_set_priority (&w->timer, ev_priority (w)); |
|
|
1807 | ev_timer_start (EV_A_ &w->timer); |
|
|
1808 | |
|
|
1809 | ev_start (EV_A_ (W)w, 1); |
|
|
1810 | } |
|
|
1811 | |
|
|
1812 | void |
|
|
1813 | ev_stat_stop (EV_P_ ev_stat *w) |
|
|
1814 | { |
|
|
1815 | ev_clear_pending (EV_A_ (W)w); |
|
|
1816 | if (expect_false (!ev_is_active (w))) |
|
|
1817 | return; |
|
|
1818 | |
|
|
1819 | ev_timer_stop (EV_A_ &w->timer); |
|
|
1820 | |
|
|
1821 | ev_stop (EV_A_ (W)w); |
|
|
1822 | } |
|
|
1823 | #endif |
|
|
1824 | |
1225 | /*****************************************************************************/ |
1825 | /*****************************************************************************/ |
1226 | |
1826 | |
1227 | struct ev_once |
1827 | struct ev_once |
1228 | { |
1828 | { |
1229 | struct ev_io io; |
1829 | ev_io io; |
1230 | struct ev_timer to; |
1830 | ev_timer to; |
1231 | void (*cb)(int revents, void *arg); |
1831 | void (*cb)(int revents, void *arg); |
1232 | void *arg; |
1832 | void *arg; |
1233 | }; |
1833 | }; |
1234 | |
1834 | |
1235 | static void |
1835 | static void |
… | |
… | |
1238 | void (*cb)(int revents, void *arg) = once->cb; |
1838 | void (*cb)(int revents, void *arg) = once->cb; |
1239 | void *arg = once->arg; |
1839 | void *arg = once->arg; |
1240 | |
1840 | |
1241 | ev_io_stop (EV_A_ &once->io); |
1841 | ev_io_stop (EV_A_ &once->io); |
1242 | ev_timer_stop (EV_A_ &once->to); |
1842 | ev_timer_stop (EV_A_ &once->to); |
1243 | free (once); |
1843 | ev_free (once); |
1244 | |
1844 | |
1245 | cb (revents, arg); |
1845 | cb (revents, arg); |
1246 | } |
1846 | } |
1247 | |
1847 | |
1248 | static void |
1848 | static void |
1249 | once_cb_io (EV_P_ struct ev_io *w, int revents) |
1849 | once_cb_io (EV_P_ ev_io *w, int revents) |
1250 | { |
1850 | { |
1251 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1851 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1252 | } |
1852 | } |
1253 | |
1853 | |
1254 | static void |
1854 | static void |
1255 | once_cb_to (EV_P_ struct ev_timer *w, int revents) |
1855 | once_cb_to (EV_P_ ev_timer *w, int revents) |
1256 | { |
1856 | { |
1257 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1857 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1258 | } |
1858 | } |
1259 | |
1859 | |
1260 | void |
1860 | void |
1261 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1861 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1262 | { |
1862 | { |
1263 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1863 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1264 | |
1864 | |
1265 | if (!once) |
1865 | if (expect_false (!once)) |
|
|
1866 | { |
1266 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1867 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1267 | else |
1868 | return; |
1268 | { |
1869 | } |
|
|
1870 | |
1269 | once->cb = cb; |
1871 | once->cb = cb; |
1270 | once->arg = arg; |
1872 | once->arg = arg; |
1271 | |
1873 | |
1272 | ev_watcher_init (&once->io, once_cb_io); |
1874 | ev_init (&once->io, once_cb_io); |
1273 | if (fd >= 0) |
1875 | if (fd >= 0) |
1274 | { |
1876 | { |
1275 | ev_io_set (&once->io, fd, events); |
1877 | ev_io_set (&once->io, fd, events); |
1276 | ev_io_start (EV_A_ &once->io); |
1878 | ev_io_start (EV_A_ &once->io); |
1277 | } |
1879 | } |
1278 | |
1880 | |
1279 | ev_watcher_init (&once->to, once_cb_to); |
1881 | ev_init (&once->to, once_cb_to); |
1280 | if (timeout >= 0.) |
1882 | if (timeout >= 0.) |
1281 | { |
1883 | { |
1282 | ev_timer_set (&once->to, timeout, 0.); |
1884 | ev_timer_set (&once->to, timeout, 0.); |
1283 | ev_timer_start (EV_A_ &once->to); |
1885 | ev_timer_start (EV_A_ &once->to); |
1284 | } |
|
|
1285 | } |
|
|
1286 | } |
|
|
1287 | |
|
|
1288 | /*****************************************************************************/ |
|
|
1289 | |
|
|
1290 | #if 0 |
|
|
1291 | |
|
|
1292 | struct ev_io wio; |
|
|
1293 | |
|
|
1294 | static void |
|
|
1295 | sin_cb (struct ev_io *w, int revents) |
|
|
1296 | { |
|
|
1297 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
|
|
1298 | } |
|
|
1299 | |
|
|
1300 | static void |
|
|
1301 | ocb (struct ev_timer *w, int revents) |
|
|
1302 | { |
|
|
1303 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
|
|
1304 | ev_timer_stop (w); |
|
|
1305 | ev_timer_start (w); |
|
|
1306 | } |
|
|
1307 | |
|
|
1308 | static void |
|
|
1309 | scb (struct ev_signal *w, int revents) |
|
|
1310 | { |
|
|
1311 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
1312 | ev_io_stop (&wio); |
|
|
1313 | ev_io_start (&wio); |
|
|
1314 | } |
|
|
1315 | |
|
|
1316 | static void |
|
|
1317 | gcb (struct ev_signal *w, int revents) |
|
|
1318 | { |
|
|
1319 | fprintf (stderr, "generic %x\n", revents); |
|
|
1320 | |
|
|
1321 | } |
|
|
1322 | |
|
|
1323 | int main (void) |
|
|
1324 | { |
|
|
1325 | ev_init (0); |
|
|
1326 | |
|
|
1327 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
|
|
1328 | ev_io_start (&wio); |
|
|
1329 | |
|
|
1330 | struct ev_timer t[10000]; |
|
|
1331 | |
|
|
1332 | #if 0 |
|
|
1333 | int i; |
|
|
1334 | for (i = 0; i < 10000; ++i) |
|
|
1335 | { |
1886 | } |
1336 | struct ev_timer *w = t + i; |
|
|
1337 | ev_watcher_init (w, ocb, i); |
|
|
1338 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
|
|
1339 | ev_timer_start (w); |
|
|
1340 | if (drand48 () < 0.5) |
|
|
1341 | ev_timer_stop (w); |
|
|
1342 | } |
|
|
1343 | #endif |
|
|
1344 | |
|
|
1345 | struct ev_timer t1; |
|
|
1346 | ev_timer_init (&t1, ocb, 5, 10); |
|
|
1347 | ev_timer_start (&t1); |
|
|
1348 | |
|
|
1349 | struct ev_signal sig; |
|
|
1350 | ev_signal_init (&sig, scb, SIGQUIT); |
|
|
1351 | ev_signal_start (&sig); |
|
|
1352 | |
|
|
1353 | struct ev_check cw; |
|
|
1354 | ev_check_init (&cw, gcb); |
|
|
1355 | ev_check_start (&cw); |
|
|
1356 | |
|
|
1357 | struct ev_idle iw; |
|
|
1358 | ev_idle_init (&iw, gcb); |
|
|
1359 | ev_idle_start (&iw); |
|
|
1360 | |
|
|
1361 | ev_loop (0); |
|
|
1362 | |
|
|
1363 | return 0; |
|
|
1364 | } |
1887 | } |
1365 | |
1888 | |
|
|
1889 | #ifdef __cplusplus |
|
|
1890 | } |
1366 | #endif |
1891 | #endif |
1367 | |
1892 | |
1368 | |
|
|
1369 | |
|
|
1370 | |
|
|