1 |
root |
1.17 |
/* |
2 |
root |
1.36 |
* libev event processing core, watcher management |
3 |
|
|
* |
4 |
root |
1.17 |
* Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
5 |
|
|
* All rights reserved. |
6 |
|
|
* |
7 |
|
|
* Redistribution and use in source and binary forms, with or without |
8 |
|
|
* modification, are permitted provided that the following conditions are |
9 |
|
|
* met: |
10 |
|
|
* |
11 |
|
|
* * Redistributions of source code must retain the above copyright |
12 |
|
|
* notice, this list of conditions and the following disclaimer. |
13 |
|
|
* |
14 |
|
|
* * Redistributions in binary form must reproduce the above |
15 |
|
|
* copyright notice, this list of conditions and the following |
16 |
|
|
* disclaimer in the documentation and/or other materials provided |
17 |
|
|
* with the distribution. |
18 |
|
|
* |
19 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 |
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 |
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 |
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 |
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 |
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 |
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 |
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 |
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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. |
30 |
|
|
*/ |
31 |
root |
1.87 |
|
32 |
|
|
#ifdef __cplusplus |
33 |
|
|
extern "C" { |
34 |
|
|
#endif |
35 |
|
|
|
36 |
root |
1.59 |
#ifndef EV_STANDALONE |
37 |
root |
1.29 |
# include "config.h" |
38 |
root |
1.60 |
|
39 |
|
|
# if HAVE_CLOCK_GETTIME |
40 |
root |
1.97 |
# ifndef EV_USE_MONOTONIC |
41 |
|
|
# define EV_USE_MONOTONIC 1 |
42 |
|
|
# endif |
43 |
|
|
# ifndef EV_USE_REALTIME |
44 |
|
|
# define EV_USE_REALTIME 1 |
45 |
|
|
# endif |
46 |
root |
1.60 |
# endif |
47 |
|
|
|
48 |
root |
1.96 |
# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT) |
49 |
root |
1.60 |
# define EV_USE_SELECT 1 |
50 |
|
|
# endif |
51 |
|
|
|
52 |
root |
1.96 |
# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) |
53 |
root |
1.60 |
# define EV_USE_POLL 1 |
54 |
|
|
# endif |
55 |
|
|
|
56 |
root |
1.96 |
# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) |
57 |
root |
1.60 |
# define EV_USE_EPOLL 1 |
58 |
|
|
# endif |
59 |
|
|
|
60 |
root |
1.96 |
# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) |
61 |
root |
1.60 |
# define EV_USE_KQUEUE 1 |
62 |
|
|
# endif |
63 |
|
|
|
64 |
root |
1.29 |
#endif |
65 |
root |
1.17 |
|
66 |
root |
1.1 |
#include <math.h> |
67 |
|
|
#include <stdlib.h> |
68 |
root |
1.7 |
#include <fcntl.h> |
69 |
root |
1.16 |
#include <stddef.h> |
70 |
root |
1.1 |
|
71 |
|
|
#include <stdio.h> |
72 |
|
|
|
73 |
root |
1.4 |
#include <assert.h> |
74 |
root |
1.1 |
#include <errno.h> |
75 |
root |
1.22 |
#include <sys/types.h> |
76 |
root |
1.71 |
#include <time.h> |
77 |
|
|
|
78 |
root |
1.72 |
#include <signal.h> |
79 |
root |
1.71 |
|
80 |
root |
1.45 |
#ifndef WIN32 |
81 |
root |
1.71 |
# include <unistd.h> |
82 |
|
|
# include <sys/time.h> |
83 |
root |
1.45 |
# include <sys/wait.h> |
84 |
|
|
#endif |
85 |
root |
1.40 |
/**/ |
86 |
|
|
|
87 |
root |
1.29 |
#ifndef EV_USE_MONOTONIC |
88 |
root |
1.37 |
# define EV_USE_MONOTONIC 1 |
89 |
|
|
#endif |
90 |
|
|
|
91 |
root |
1.29 |
#ifndef EV_USE_SELECT |
92 |
|
|
# define EV_USE_SELECT 1 |
93 |
root |
1.10 |
#endif |
94 |
|
|
|
95 |
root |
1.59 |
#ifndef EV_USE_POLL |
96 |
|
|
# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
97 |
root |
1.41 |
#endif |
98 |
|
|
|
99 |
root |
1.29 |
#ifndef EV_USE_EPOLL |
100 |
|
|
# define EV_USE_EPOLL 0 |
101 |
root |
1.10 |
#endif |
102 |
|
|
|
103 |
root |
1.44 |
#ifndef EV_USE_KQUEUE |
104 |
|
|
# define EV_USE_KQUEUE 0 |
105 |
|
|
#endif |
106 |
|
|
|
107 |
root |
1.62 |
#ifndef EV_USE_WIN32 |
108 |
|
|
# ifdef WIN32 |
109 |
root |
1.71 |
# define EV_USE_WIN32 0 /* it does not exist, use select */ |
110 |
|
|
# undef EV_USE_SELECT |
111 |
|
|
# define EV_USE_SELECT 1 |
112 |
root |
1.62 |
# else |
113 |
|
|
# define EV_USE_WIN32 0 |
114 |
|
|
# endif |
115 |
|
|
#endif |
116 |
|
|
|
117 |
root |
1.40 |
#ifndef EV_USE_REALTIME |
118 |
|
|
# define EV_USE_REALTIME 1 |
119 |
|
|
#endif |
120 |
|
|
|
121 |
|
|
/**/ |
122 |
|
|
|
123 |
|
|
#ifndef CLOCK_MONOTONIC |
124 |
|
|
# undef EV_USE_MONOTONIC |
125 |
|
|
# define EV_USE_MONOTONIC 0 |
126 |
|
|
#endif |
127 |
|
|
|
128 |
root |
1.31 |
#ifndef CLOCK_REALTIME |
129 |
root |
1.40 |
# undef EV_USE_REALTIME |
130 |
root |
1.31 |
# define EV_USE_REALTIME 0 |
131 |
|
|
#endif |
132 |
root |
1.40 |
|
133 |
|
|
/**/ |
134 |
root |
1.1 |
|
135 |
root |
1.4 |
#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
136 |
root |
1.40 |
#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
137 |
root |
1.31 |
#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
138 |
root |
1.40 |
/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
139 |
root |
1.1 |
|
140 |
root |
1.81 |
#ifdef EV_H |
141 |
|
|
# include EV_H |
142 |
|
|
#else |
143 |
|
|
# include "ev.h" |
144 |
|
|
#endif |
145 |
root |
1.1 |
|
146 |
root |
1.40 |
#if __GNUC__ >= 3 |
147 |
|
|
# define expect(expr,value) __builtin_expect ((expr),(value)) |
148 |
|
|
# define inline inline |
149 |
|
|
#else |
150 |
|
|
# define expect(expr,value) (expr) |
151 |
|
|
# define inline static |
152 |
|
|
#endif |
153 |
|
|
|
154 |
|
|
#define expect_false(expr) expect ((expr) != 0, 0) |
155 |
|
|
#define expect_true(expr) expect ((expr) != 0, 1) |
156 |
|
|
|
157 |
root |
1.42 |
#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
158 |
|
|
#define ABSPRI(w) ((w)->priority - EV_MINPRI) |
159 |
|
|
|
160 |
root |
1.10 |
typedef struct ev_watcher *W; |
161 |
|
|
typedef struct ev_watcher_list *WL; |
162 |
root |
1.12 |
typedef struct ev_watcher_time *WT; |
163 |
root |
1.10 |
|
164 |
root |
1.54 |
static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
165 |
|
|
|
166 |
root |
1.98 |
#ifdef WIN32 |
167 |
|
|
# include "ev_win32.c" |
168 |
|
|
#endif |
169 |
root |
1.67 |
|
170 |
root |
1.53 |
/*****************************************************************************/ |
171 |
root |
1.1 |
|
172 |
root |
1.70 |
static void (*syserr_cb)(const char *msg); |
173 |
root |
1.69 |
|
174 |
root |
1.70 |
void ev_set_syserr_cb (void (*cb)(const char *msg)) |
175 |
root |
1.69 |
{ |
176 |
|
|
syserr_cb = cb; |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
static void |
180 |
root |
1.70 |
syserr (const char *msg) |
181 |
root |
1.69 |
{ |
182 |
root |
1.70 |
if (!msg) |
183 |
|
|
msg = "(libev) system error"; |
184 |
|
|
|
185 |
root |
1.69 |
if (syserr_cb) |
186 |
root |
1.70 |
syserr_cb (msg); |
187 |
root |
1.69 |
else |
188 |
|
|
{ |
189 |
root |
1.70 |
perror (msg); |
190 |
root |
1.69 |
abort (); |
191 |
|
|
} |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
static void *(*alloc)(void *ptr, long size); |
195 |
|
|
|
196 |
|
|
void ev_set_allocator (void *(*cb)(void *ptr, long size)) |
197 |
|
|
{ |
198 |
|
|
alloc = cb; |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
static void * |
202 |
|
|
ev_realloc (void *ptr, long size) |
203 |
|
|
{ |
204 |
|
|
ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); |
205 |
|
|
|
206 |
|
|
if (!ptr && size) |
207 |
|
|
{ |
208 |
|
|
fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
209 |
|
|
abort (); |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
return ptr; |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
#define ev_malloc(size) ev_realloc (0, (size)) |
216 |
|
|
#define ev_free(ptr) ev_realloc ((ptr), 0) |
217 |
|
|
|
218 |
|
|
/*****************************************************************************/ |
219 |
|
|
|
220 |
root |
1.53 |
typedef struct |
221 |
|
|
{ |
222 |
root |
1.68 |
WL head; |
223 |
root |
1.53 |
unsigned char events; |
224 |
|
|
unsigned char reify; |
225 |
|
|
} ANFD; |
226 |
root |
1.1 |
|
227 |
root |
1.53 |
typedef struct |
228 |
|
|
{ |
229 |
|
|
W w; |
230 |
|
|
int events; |
231 |
|
|
} ANPENDING; |
232 |
root |
1.51 |
|
233 |
root |
1.55 |
#if EV_MULTIPLICITY |
234 |
root |
1.54 |
|
235 |
root |
1.80 |
struct ev_loop |
236 |
|
|
{ |
237 |
root |
1.86 |
ev_tstamp ev_rt_now; |
238 |
root |
1.99 |
#define ev_rt_now ((loop)->ev_rt_now) |
239 |
root |
1.80 |
#define VAR(name,decl) decl; |
240 |
|
|
#include "ev_vars.h" |
241 |
|
|
#undef VAR |
242 |
|
|
}; |
243 |
|
|
#include "ev_wrap.h" |
244 |
|
|
|
245 |
|
|
struct ev_loop default_loop_struct; |
246 |
|
|
static struct ev_loop *default_loop; |
247 |
root |
1.54 |
|
248 |
root |
1.53 |
#else |
249 |
root |
1.54 |
|
250 |
root |
1.86 |
ev_tstamp ev_rt_now; |
251 |
root |
1.80 |
#define VAR(name,decl) static decl; |
252 |
|
|
#include "ev_vars.h" |
253 |
|
|
#undef VAR |
254 |
|
|
|
255 |
|
|
static int default_loop; |
256 |
root |
1.54 |
|
257 |
root |
1.51 |
#endif |
258 |
root |
1.1 |
|
259 |
root |
1.8 |
/*****************************************************************************/ |
260 |
|
|
|
261 |
root |
1.92 |
ev_tstamp |
262 |
root |
1.1 |
ev_time (void) |
263 |
|
|
{ |
264 |
root |
1.29 |
#if EV_USE_REALTIME |
265 |
root |
1.1 |
struct timespec ts; |
266 |
|
|
clock_gettime (CLOCK_REALTIME, &ts); |
267 |
|
|
return ts.tv_sec + ts.tv_nsec * 1e-9; |
268 |
|
|
#else |
269 |
|
|
struct timeval tv; |
270 |
|
|
gettimeofday (&tv, 0); |
271 |
|
|
return tv.tv_sec + tv.tv_usec * 1e-6; |
272 |
|
|
#endif |
273 |
|
|
} |
274 |
|
|
|
275 |
root |
1.51 |
inline ev_tstamp |
276 |
root |
1.1 |
get_clock (void) |
277 |
|
|
{ |
278 |
root |
1.29 |
#if EV_USE_MONOTONIC |
279 |
root |
1.40 |
if (expect_true (have_monotonic)) |
280 |
root |
1.1 |
{ |
281 |
|
|
struct timespec ts; |
282 |
|
|
clock_gettime (CLOCK_MONOTONIC, &ts); |
283 |
|
|
return ts.tv_sec + ts.tv_nsec * 1e-9; |
284 |
|
|
} |
285 |
|
|
#endif |
286 |
|
|
|
287 |
|
|
return ev_time (); |
288 |
|
|
} |
289 |
|
|
|
290 |
root |
1.85 |
#if EV_MULTIPLICITY |
291 |
root |
1.51 |
ev_tstamp |
292 |
|
|
ev_now (EV_P) |
293 |
|
|
{ |
294 |
root |
1.85 |
return ev_rt_now; |
295 |
root |
1.51 |
} |
296 |
root |
1.85 |
#endif |
297 |
root |
1.51 |
|
298 |
root |
1.74 |
#define array_roundsize(type,n) ((n) | 4 & ~3) |
299 |
root |
1.29 |
|
300 |
root |
1.74 |
#define array_needsize(type,base,cur,cnt,init) \ |
301 |
root |
1.69 |
if (expect_false ((cnt) > cur)) \ |
302 |
|
|
{ \ |
303 |
|
|
int newcnt = cur; \ |
304 |
|
|
do \ |
305 |
|
|
{ \ |
306 |
root |
1.74 |
newcnt = array_roundsize (type, newcnt << 1); \ |
307 |
root |
1.69 |
} \ |
308 |
|
|
while ((cnt) > newcnt); \ |
309 |
|
|
\ |
310 |
root |
1.74 |
base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
311 |
root |
1.69 |
init (base + cur, newcnt - cur); \ |
312 |
|
|
cur = newcnt; \ |
313 |
root |
1.1 |
} |
314 |
|
|
|
315 |
root |
1.74 |
#define array_slim(type,stem) \ |
316 |
root |
1.67 |
if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
317 |
|
|
{ \ |
318 |
|
|
stem ## max = array_roundsize (stem ## cnt >> 1); \ |
319 |
root |
1.74 |
base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
320 |
root |
1.67 |
fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
321 |
|
|
} |
322 |
|
|
|
323 |
root |
1.71 |
/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
324 |
|
|
/* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
325 |
|
|
#define array_free_microshit(stem) \ |
326 |
|
|
ev_free (stem ## s); stem ## cnt = stem ## max = 0; |
327 |
|
|
|
328 |
root |
1.65 |
#define array_free(stem, idx) \ |
329 |
root |
1.69 |
ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
330 |
root |
1.65 |
|
331 |
root |
1.8 |
/*****************************************************************************/ |
332 |
|
|
|
333 |
root |
1.1 |
static void |
334 |
|
|
anfds_init (ANFD *base, int count) |
335 |
|
|
{ |
336 |
|
|
while (count--) |
337 |
|
|
{ |
338 |
root |
1.27 |
base->head = 0; |
339 |
|
|
base->events = EV_NONE; |
340 |
root |
1.33 |
base->reify = 0; |
341 |
|
|
|
342 |
root |
1.1 |
++base; |
343 |
|
|
} |
344 |
|
|
} |
345 |
|
|
|
346 |
root |
1.78 |
void |
347 |
|
|
ev_feed_event (EV_P_ void *w, int revents) |
348 |
root |
1.1 |
{ |
349 |
root |
1.78 |
W w_ = (W)w; |
350 |
|
|
|
351 |
|
|
if (w_->pending) |
352 |
root |
1.32 |
{ |
353 |
root |
1.78 |
pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
354 |
root |
1.32 |
return; |
355 |
|
|
} |
356 |
|
|
|
357 |
root |
1.78 |
w_->pending = ++pendingcnt [ABSPRI (w_)]; |
358 |
|
|
array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
359 |
|
|
pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
360 |
|
|
pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
361 |
root |
1.1 |
} |
362 |
|
|
|
363 |
|
|
static void |
364 |
root |
1.51 |
queue_events (EV_P_ W *events, int eventcnt, int type) |
365 |
root |
1.27 |
{ |
366 |
|
|
int i; |
367 |
|
|
|
368 |
|
|
for (i = 0; i < eventcnt; ++i) |
369 |
root |
1.78 |
ev_feed_event (EV_A_ events [i], type); |
370 |
root |
1.27 |
} |
371 |
|
|
|
372 |
root |
1.79 |
inline void |
373 |
|
|
fd_event (EV_P_ int fd, int revents) |
374 |
root |
1.1 |
{ |
375 |
|
|
ANFD *anfd = anfds + fd; |
376 |
|
|
struct ev_io *w; |
377 |
|
|
|
378 |
root |
1.50 |
for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
379 |
root |
1.1 |
{ |
380 |
root |
1.79 |
int ev = w->events & revents; |
381 |
root |
1.1 |
|
382 |
|
|
if (ev) |
383 |
root |
1.78 |
ev_feed_event (EV_A_ (W)w, ev); |
384 |
root |
1.1 |
} |
385 |
|
|
} |
386 |
|
|
|
387 |
root |
1.79 |
void |
388 |
|
|
ev_feed_fd_event (EV_P_ int fd, int revents) |
389 |
|
|
{ |
390 |
|
|
fd_event (EV_A_ fd, revents); |
391 |
|
|
} |
392 |
|
|
|
393 |
root |
1.27 |
/*****************************************************************************/ |
394 |
|
|
|
395 |
root |
1.9 |
static void |
396 |
root |
1.51 |
fd_reify (EV_P) |
397 |
root |
1.9 |
{ |
398 |
|
|
int i; |
399 |
|
|
|
400 |
root |
1.27 |
for (i = 0; i < fdchangecnt; ++i) |
401 |
|
|
{ |
402 |
|
|
int fd = fdchanges [i]; |
403 |
|
|
ANFD *anfd = anfds + fd; |
404 |
|
|
struct ev_io *w; |
405 |
|
|
|
406 |
|
|
int events = 0; |
407 |
|
|
|
408 |
root |
1.50 |
for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
409 |
root |
1.27 |
events |= w->events; |
410 |
|
|
|
411 |
root |
1.33 |
anfd->reify = 0; |
412 |
root |
1.27 |
|
413 |
root |
1.64 |
method_modify (EV_A_ fd, anfd->events, events); |
414 |
|
|
anfd->events = events; |
415 |
root |
1.27 |
} |
416 |
|
|
|
417 |
|
|
fdchangecnt = 0; |
418 |
|
|
} |
419 |
|
|
|
420 |
|
|
static void |
421 |
root |
1.51 |
fd_change (EV_P_ int fd) |
422 |
root |
1.27 |
{ |
423 |
root |
1.70 |
if (anfds [fd].reify) |
424 |
root |
1.27 |
return; |
425 |
|
|
|
426 |
root |
1.33 |
anfds [fd].reify = 1; |
427 |
root |
1.27 |
|
428 |
|
|
++fdchangecnt; |
429 |
root |
1.74 |
array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
430 |
root |
1.27 |
fdchanges [fdchangecnt - 1] = fd; |
431 |
root |
1.9 |
} |
432 |
|
|
|
433 |
root |
1.41 |
static void |
434 |
root |
1.51 |
fd_kill (EV_P_ int fd) |
435 |
root |
1.41 |
{ |
436 |
|
|
struct ev_io *w; |
437 |
|
|
|
438 |
root |
1.50 |
while ((w = (struct ev_io *)anfds [fd].head)) |
439 |
root |
1.41 |
{ |
440 |
root |
1.51 |
ev_io_stop (EV_A_ w); |
441 |
root |
1.78 |
ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
442 |
root |
1.41 |
} |
443 |
|
|
} |
444 |
|
|
|
445 |
root |
1.71 |
static int |
446 |
|
|
fd_valid (int fd) |
447 |
|
|
{ |
448 |
|
|
#ifdef WIN32 |
449 |
|
|
return !!win32_get_osfhandle (fd); |
450 |
|
|
#else |
451 |
|
|
return fcntl (fd, F_GETFD) != -1; |
452 |
|
|
#endif |
453 |
|
|
} |
454 |
|
|
|
455 |
root |
1.19 |
/* called on EBADF to verify fds */ |
456 |
|
|
static void |
457 |
root |
1.51 |
fd_ebadf (EV_P) |
458 |
root |
1.19 |
{ |
459 |
|
|
int fd; |
460 |
|
|
|
461 |
|
|
for (fd = 0; fd < anfdmax; ++fd) |
462 |
root |
1.27 |
if (anfds [fd].events) |
463 |
root |
1.71 |
if (!fd_valid (fd) == -1 && errno == EBADF) |
464 |
root |
1.51 |
fd_kill (EV_A_ fd); |
465 |
root |
1.41 |
} |
466 |
|
|
|
467 |
|
|
/* called on ENOMEM in select/poll to kill some fds and retry */ |
468 |
|
|
static void |
469 |
root |
1.51 |
fd_enomem (EV_P) |
470 |
root |
1.41 |
{ |
471 |
root |
1.62 |
int fd; |
472 |
root |
1.41 |
|
473 |
root |
1.62 |
for (fd = anfdmax; fd--; ) |
474 |
root |
1.41 |
if (anfds [fd].events) |
475 |
|
|
{ |
476 |
root |
1.51 |
fd_kill (EV_A_ fd); |
477 |
root |
1.41 |
return; |
478 |
|
|
} |
479 |
root |
1.19 |
} |
480 |
|
|
|
481 |
root |
1.70 |
/* usually called after fork if method needs to re-arm all fds from scratch */ |
482 |
root |
1.56 |
static void |
483 |
|
|
fd_rearm_all (EV_P) |
484 |
|
|
{ |
485 |
|
|
int fd; |
486 |
|
|
|
487 |
|
|
/* this should be highly optimised to not do anything but set a flag */ |
488 |
|
|
for (fd = 0; fd < anfdmax; ++fd) |
489 |
|
|
if (anfds [fd].events) |
490 |
|
|
{ |
491 |
|
|
anfds [fd].events = 0; |
492 |
root |
1.60 |
fd_change (EV_A_ fd); |
493 |
root |
1.56 |
} |
494 |
|
|
} |
495 |
|
|
|
496 |
root |
1.8 |
/*****************************************************************************/ |
497 |
|
|
|
498 |
root |
1.1 |
static void |
499 |
root |
1.54 |
upheap (WT *heap, int k) |
500 |
root |
1.1 |
{ |
501 |
root |
1.54 |
WT w = heap [k]; |
502 |
root |
1.1 |
|
503 |
root |
1.54 |
while (k && heap [k >> 1]->at > w->at) |
504 |
root |
1.1 |
{ |
505 |
root |
1.54 |
heap [k] = heap [k >> 1]; |
506 |
root |
1.62 |
((W)heap [k])->active = k + 1; |
507 |
root |
1.1 |
k >>= 1; |
508 |
|
|
} |
509 |
|
|
|
510 |
root |
1.54 |
heap [k] = w; |
511 |
root |
1.62 |
((W)heap [k])->active = k + 1; |
512 |
root |
1.1 |
|
513 |
|
|
} |
514 |
|
|
|
515 |
|
|
static void |
516 |
root |
1.54 |
downheap (WT *heap, int N, int k) |
517 |
root |
1.1 |
{ |
518 |
root |
1.54 |
WT w = heap [k]; |
519 |
root |
1.1 |
|
520 |
root |
1.4 |
while (k < (N >> 1)) |
521 |
root |
1.1 |
{ |
522 |
|
|
int j = k << 1; |
523 |
|
|
|
524 |
root |
1.54 |
if (j + 1 < N && heap [j]->at > heap [j + 1]->at) |
525 |
root |
1.1 |
++j; |
526 |
|
|
|
527 |
root |
1.54 |
if (w->at <= heap [j]->at) |
528 |
root |
1.1 |
break; |
529 |
|
|
|
530 |
root |
1.54 |
heap [k] = heap [j]; |
531 |
root |
1.62 |
((W)heap [k])->active = k + 1; |
532 |
root |
1.1 |
k = j; |
533 |
|
|
} |
534 |
|
|
|
535 |
root |
1.54 |
heap [k] = w; |
536 |
root |
1.62 |
((W)heap [k])->active = k + 1; |
537 |
root |
1.1 |
} |
538 |
|
|
|
539 |
root |
1.84 |
inline void |
540 |
root |
1.99 |
adjustheap (WT *heap, int N, int k) |
541 |
root |
1.84 |
{ |
542 |
root |
1.99 |
upheap (heap, k); |
543 |
|
|
downheap (heap, N, k); |
544 |
root |
1.84 |
} |
545 |
|
|
|
546 |
root |
1.8 |
/*****************************************************************************/ |
547 |
|
|
|
548 |
root |
1.7 |
typedef struct |
549 |
|
|
{ |
550 |
root |
1.68 |
WL head; |
551 |
root |
1.34 |
sig_atomic_t volatile gotsig; |
552 |
root |
1.7 |
} ANSIG; |
553 |
|
|
|
554 |
|
|
static ANSIG *signals; |
555 |
root |
1.4 |
static int signalmax; |
556 |
root |
1.1 |
|
557 |
root |
1.7 |
static int sigpipe [2]; |
558 |
root |
1.34 |
static sig_atomic_t volatile gotsig; |
559 |
root |
1.59 |
static struct ev_io sigev; |
560 |
root |
1.7 |
|
561 |
root |
1.1 |
static void |
562 |
root |
1.7 |
signals_init (ANSIG *base, int count) |
563 |
root |
1.1 |
{ |
564 |
|
|
while (count--) |
565 |
root |
1.7 |
{ |
566 |
|
|
base->head = 0; |
567 |
|
|
base->gotsig = 0; |
568 |
root |
1.33 |
|
569 |
root |
1.7 |
++base; |
570 |
|
|
} |
571 |
|
|
} |
572 |
|
|
|
573 |
|
|
static void |
574 |
|
|
sighandler (int signum) |
575 |
|
|
{ |
576 |
root |
1.67 |
#if WIN32 |
577 |
|
|
signal (signum, sighandler); |
578 |
|
|
#endif |
579 |
|
|
|
580 |
root |
1.7 |
signals [signum - 1].gotsig = 1; |
581 |
|
|
|
582 |
|
|
if (!gotsig) |
583 |
|
|
{ |
584 |
root |
1.48 |
int old_errno = errno; |
585 |
root |
1.7 |
gotsig = 1; |
586 |
root |
1.75 |
#ifdef WIN32 |
587 |
|
|
send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
588 |
|
|
#else |
589 |
root |
1.34 |
write (sigpipe [1], &signum, 1); |
590 |
root |
1.75 |
#endif |
591 |
root |
1.48 |
errno = old_errno; |
592 |
root |
1.7 |
} |
593 |
|
|
} |
594 |
|
|
|
595 |
root |
1.79 |
void |
596 |
|
|
ev_feed_signal_event (EV_P_ int signum) |
597 |
|
|
{ |
598 |
root |
1.80 |
WL w; |
599 |
|
|
|
600 |
root |
1.79 |
#if EV_MULTIPLICITY |
601 |
|
|
assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
602 |
|
|
#endif |
603 |
|
|
|
604 |
|
|
--signum; |
605 |
|
|
|
606 |
|
|
if (signum < 0 || signum >= signalmax) |
607 |
|
|
return; |
608 |
|
|
|
609 |
|
|
signals [signum].gotsig = 0; |
610 |
|
|
|
611 |
|
|
for (w = signals [signum].head; w; w = w->next) |
612 |
|
|
ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
613 |
|
|
} |
614 |
|
|
|
615 |
root |
1.7 |
static void |
616 |
root |
1.51 |
sigcb (EV_P_ struct ev_io *iow, int revents) |
617 |
root |
1.7 |
{ |
618 |
root |
1.38 |
int signum; |
619 |
root |
1.7 |
|
620 |
root |
1.75 |
#ifdef WIN32 |
621 |
|
|
recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
622 |
|
|
#else |
623 |
root |
1.34 |
read (sigpipe [0], &revents, 1); |
624 |
root |
1.75 |
#endif |
625 |
root |
1.7 |
gotsig = 0; |
626 |
|
|
|
627 |
root |
1.38 |
for (signum = signalmax; signum--; ) |
628 |
|
|
if (signals [signum].gotsig) |
629 |
root |
1.80 |
ev_feed_signal_event (EV_A_ signum + 1); |
630 |
root |
1.7 |
} |
631 |
|
|
|
632 |
|
|
static void |
633 |
root |
1.51 |
siginit (EV_P) |
634 |
root |
1.7 |
{ |
635 |
root |
1.45 |
#ifndef WIN32 |
636 |
root |
1.7 |
fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
637 |
|
|
fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
638 |
|
|
|
639 |
|
|
/* rather than sort out wether we really need nb, set it */ |
640 |
|
|
fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
641 |
|
|
fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
642 |
root |
1.45 |
#endif |
643 |
root |
1.7 |
|
644 |
root |
1.28 |
ev_io_set (&sigev, sigpipe [0], EV_READ); |
645 |
root |
1.54 |
ev_io_start (EV_A_ &sigev); |
646 |
root |
1.52 |
ev_unref (EV_A); /* child watcher should not keep loop alive */ |
647 |
root |
1.1 |
} |
648 |
|
|
|
649 |
root |
1.8 |
/*****************************************************************************/ |
650 |
|
|
|
651 |
root |
1.71 |
static struct ev_child *childs [PID_HASHSIZE]; |
652 |
|
|
|
653 |
root |
1.45 |
#ifndef WIN32 |
654 |
|
|
|
655 |
root |
1.59 |
static struct ev_signal childev; |
656 |
|
|
|
657 |
root |
1.22 |
#ifndef WCONTINUED |
658 |
|
|
# define WCONTINUED 0 |
659 |
|
|
#endif |
660 |
|
|
|
661 |
|
|
static void |
662 |
root |
1.51 |
child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) |
663 |
root |
1.47 |
{ |
664 |
|
|
struct ev_child *w; |
665 |
|
|
|
666 |
root |
1.50 |
for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
667 |
root |
1.47 |
if (w->pid == pid || !w->pid) |
668 |
|
|
{ |
669 |
root |
1.63 |
ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
670 |
|
|
w->rpid = pid; |
671 |
|
|
w->rstatus = status; |
672 |
root |
1.78 |
ev_feed_event (EV_A_ (W)w, EV_CHILD); |
673 |
root |
1.47 |
} |
674 |
|
|
} |
675 |
|
|
|
676 |
|
|
static void |
677 |
root |
1.51 |
childcb (EV_P_ struct ev_signal *sw, int revents) |
678 |
root |
1.22 |
{ |
679 |
|
|
int pid, status; |
680 |
|
|
|
681 |
root |
1.47 |
if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
682 |
|
|
{ |
683 |
|
|
/* make sure we are called again until all childs have been reaped */ |
684 |
root |
1.78 |
ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
685 |
root |
1.47 |
|
686 |
root |
1.51 |
child_reap (EV_A_ sw, pid, pid, status); |
687 |
|
|
child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
688 |
root |
1.47 |
} |
689 |
root |
1.22 |
} |
690 |
|
|
|
691 |
root |
1.45 |
#endif |
692 |
|
|
|
693 |
root |
1.22 |
/*****************************************************************************/ |
694 |
|
|
|
695 |
root |
1.44 |
#if EV_USE_KQUEUE |
696 |
|
|
# include "ev_kqueue.c" |
697 |
|
|
#endif |
698 |
root |
1.29 |
#if EV_USE_EPOLL |
699 |
root |
1.1 |
# include "ev_epoll.c" |
700 |
|
|
#endif |
701 |
root |
1.59 |
#if EV_USE_POLL |
702 |
root |
1.41 |
# include "ev_poll.c" |
703 |
|
|
#endif |
704 |
root |
1.29 |
#if EV_USE_SELECT |
705 |
root |
1.1 |
# include "ev_select.c" |
706 |
|
|
#endif |
707 |
|
|
|
708 |
root |
1.24 |
int |
709 |
|
|
ev_version_major (void) |
710 |
|
|
{ |
711 |
|
|
return EV_VERSION_MAJOR; |
712 |
|
|
} |
713 |
|
|
|
714 |
|
|
int |
715 |
|
|
ev_version_minor (void) |
716 |
|
|
{ |
717 |
|
|
return EV_VERSION_MINOR; |
718 |
|
|
} |
719 |
|
|
|
720 |
root |
1.49 |
/* return true if we are running with elevated privileges and should ignore env variables */ |
721 |
root |
1.41 |
static int |
722 |
root |
1.51 |
enable_secure (void) |
723 |
root |
1.41 |
{ |
724 |
root |
1.49 |
#ifdef WIN32 |
725 |
|
|
return 0; |
726 |
|
|
#else |
727 |
root |
1.41 |
return getuid () != geteuid () |
728 |
|
|
|| getgid () != getegid (); |
729 |
root |
1.49 |
#endif |
730 |
root |
1.41 |
} |
731 |
|
|
|
732 |
root |
1.51 |
int |
733 |
|
|
ev_method (EV_P) |
734 |
root |
1.1 |
{ |
735 |
root |
1.51 |
return method; |
736 |
|
|
} |
737 |
|
|
|
738 |
root |
1.56 |
static void |
739 |
root |
1.54 |
loop_init (EV_P_ int methods) |
740 |
root |
1.51 |
{ |
741 |
|
|
if (!method) |
742 |
root |
1.23 |
{ |
743 |
root |
1.29 |
#if EV_USE_MONOTONIC |
744 |
root |
1.23 |
{ |
745 |
|
|
struct timespec ts; |
746 |
|
|
if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
747 |
|
|
have_monotonic = 1; |
748 |
|
|
} |
749 |
root |
1.1 |
#endif |
750 |
|
|
|
751 |
root |
1.85 |
ev_rt_now = ev_time (); |
752 |
root |
1.51 |
mn_now = get_clock (); |
753 |
|
|
now_floor = mn_now; |
754 |
root |
1.85 |
rtmn_diff = ev_rt_now - mn_now; |
755 |
root |
1.1 |
|
756 |
root |
1.41 |
if (methods == EVMETHOD_AUTO) |
757 |
root |
1.56 |
if (!enable_secure () && getenv ("LIBEV_METHODS")) |
758 |
|
|
methods = atoi (getenv ("LIBEV_METHODS")); |
759 |
root |
1.50 |
else |
760 |
|
|
methods = EVMETHOD_ANY; |
761 |
root |
1.41 |
|
762 |
root |
1.51 |
method = 0; |
763 |
root |
1.62 |
#if EV_USE_WIN32 |
764 |
|
|
if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
765 |
|
|
#endif |
766 |
root |
1.44 |
#if EV_USE_KQUEUE |
767 |
root |
1.51 |
if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
768 |
root |
1.44 |
#endif |
769 |
root |
1.29 |
#if EV_USE_EPOLL |
770 |
root |
1.51 |
if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
771 |
root |
1.41 |
#endif |
772 |
root |
1.59 |
#if EV_USE_POLL |
773 |
root |
1.51 |
if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
774 |
root |
1.1 |
#endif |
775 |
root |
1.29 |
#if EV_USE_SELECT |
776 |
root |
1.51 |
if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
777 |
root |
1.1 |
#endif |
778 |
root |
1.70 |
|
779 |
root |
1.83 |
ev_init (&sigev, sigcb); |
780 |
root |
1.70 |
ev_set_priority (&sigev, EV_MAXPRI); |
781 |
root |
1.56 |
} |
782 |
|
|
} |
783 |
|
|
|
784 |
|
|
void |
785 |
|
|
loop_destroy (EV_P) |
786 |
|
|
{ |
787 |
root |
1.65 |
int i; |
788 |
|
|
|
789 |
root |
1.62 |
#if EV_USE_WIN32 |
790 |
|
|
if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
791 |
|
|
#endif |
792 |
root |
1.56 |
#if EV_USE_KQUEUE |
793 |
|
|
if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
794 |
|
|
#endif |
795 |
|
|
#if EV_USE_EPOLL |
796 |
|
|
if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
797 |
|
|
#endif |
798 |
root |
1.59 |
#if EV_USE_POLL |
799 |
root |
1.56 |
if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
800 |
|
|
#endif |
801 |
|
|
#if EV_USE_SELECT |
802 |
|
|
if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
803 |
|
|
#endif |
804 |
root |
1.1 |
|
805 |
root |
1.65 |
for (i = NUMPRI; i--; ) |
806 |
|
|
array_free (pending, [i]); |
807 |
|
|
|
808 |
root |
1.71 |
/* have to use the microsoft-never-gets-it-right macro */ |
809 |
|
|
array_free_microshit (fdchange); |
810 |
|
|
array_free_microshit (timer); |
811 |
root |
1.93 |
#if EV_PERIODICS |
812 |
root |
1.71 |
array_free_microshit (periodic); |
813 |
root |
1.93 |
#endif |
814 |
root |
1.71 |
array_free_microshit (idle); |
815 |
|
|
array_free_microshit (prepare); |
816 |
|
|
array_free_microshit (check); |
817 |
root |
1.65 |
|
818 |
root |
1.56 |
method = 0; |
819 |
|
|
} |
820 |
root |
1.22 |
|
821 |
root |
1.70 |
static void |
822 |
root |
1.56 |
loop_fork (EV_P) |
823 |
|
|
{ |
824 |
|
|
#if EV_USE_EPOLL |
825 |
|
|
if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
826 |
|
|
#endif |
827 |
|
|
#if EV_USE_KQUEUE |
828 |
|
|
if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
829 |
root |
1.45 |
#endif |
830 |
root |
1.70 |
|
831 |
|
|
if (ev_is_active (&sigev)) |
832 |
|
|
{ |
833 |
|
|
/* default loop */ |
834 |
|
|
|
835 |
|
|
ev_ref (EV_A); |
836 |
|
|
ev_io_stop (EV_A_ &sigev); |
837 |
|
|
close (sigpipe [0]); |
838 |
|
|
close (sigpipe [1]); |
839 |
|
|
|
840 |
root |
1.73 |
while (pipe (sigpipe)) |
841 |
root |
1.70 |
syserr ("(libev) error creating pipe"); |
842 |
|
|
|
843 |
|
|
siginit (EV_A); |
844 |
|
|
} |
845 |
|
|
|
846 |
|
|
postfork = 0; |
847 |
root |
1.1 |
} |
848 |
|
|
|
849 |
root |
1.55 |
#if EV_MULTIPLICITY |
850 |
root |
1.54 |
struct ev_loop * |
851 |
|
|
ev_loop_new (int methods) |
852 |
|
|
{ |
853 |
root |
1.69 |
struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
854 |
|
|
|
855 |
|
|
memset (loop, 0, sizeof (struct ev_loop)); |
856 |
root |
1.54 |
|
857 |
root |
1.56 |
loop_init (EV_A_ methods); |
858 |
|
|
|
859 |
root |
1.60 |
if (ev_method (EV_A)) |
860 |
root |
1.55 |
return loop; |
861 |
root |
1.54 |
|
862 |
root |
1.55 |
return 0; |
863 |
root |
1.54 |
} |
864 |
|
|
|
865 |
|
|
void |
866 |
root |
1.56 |
ev_loop_destroy (EV_P) |
867 |
root |
1.54 |
{ |
868 |
root |
1.56 |
loop_destroy (EV_A); |
869 |
root |
1.69 |
ev_free (loop); |
870 |
root |
1.54 |
} |
871 |
|
|
|
872 |
root |
1.56 |
void |
873 |
|
|
ev_loop_fork (EV_P) |
874 |
|
|
{ |
875 |
root |
1.70 |
postfork = 1; |
876 |
root |
1.56 |
} |
877 |
|
|
|
878 |
|
|
#endif |
879 |
|
|
|
880 |
|
|
#if EV_MULTIPLICITY |
881 |
|
|
struct ev_loop * |
882 |
root |
1.54 |
#else |
883 |
|
|
int |
884 |
root |
1.56 |
#endif |
885 |
|
|
ev_default_loop (int methods) |
886 |
root |
1.54 |
{ |
887 |
root |
1.56 |
if (sigpipe [0] == sigpipe [1]) |
888 |
root |
1.73 |
if (pipe (sigpipe)) |
889 |
root |
1.56 |
return 0; |
890 |
root |
1.54 |
|
891 |
root |
1.56 |
if (!default_loop) |
892 |
|
|
{ |
893 |
|
|
#if EV_MULTIPLICITY |
894 |
|
|
struct ev_loop *loop = default_loop = &default_loop_struct; |
895 |
|
|
#else |
896 |
|
|
default_loop = 1; |
897 |
root |
1.54 |
#endif |
898 |
|
|
|
899 |
root |
1.56 |
loop_init (EV_A_ methods); |
900 |
|
|
|
901 |
|
|
if (ev_method (EV_A)) |
902 |
|
|
{ |
903 |
|
|
siginit (EV_A); |
904 |
|
|
|
905 |
|
|
#ifndef WIN32 |
906 |
|
|
ev_signal_init (&childev, childcb, SIGCHLD); |
907 |
|
|
ev_set_priority (&childev, EV_MAXPRI); |
908 |
|
|
ev_signal_start (EV_A_ &childev); |
909 |
|
|
ev_unref (EV_A); /* child watcher should not keep loop alive */ |
910 |
|
|
#endif |
911 |
|
|
} |
912 |
|
|
else |
913 |
|
|
default_loop = 0; |
914 |
|
|
} |
915 |
root |
1.8 |
|
916 |
root |
1.56 |
return default_loop; |
917 |
root |
1.1 |
} |
918 |
|
|
|
919 |
root |
1.24 |
void |
920 |
root |
1.56 |
ev_default_destroy (void) |
921 |
root |
1.1 |
{ |
922 |
root |
1.57 |
#if EV_MULTIPLICITY |
923 |
root |
1.56 |
struct ev_loop *loop = default_loop; |
924 |
root |
1.57 |
#endif |
925 |
root |
1.56 |
|
926 |
root |
1.71 |
#ifndef WIN32 |
927 |
root |
1.56 |
ev_ref (EV_A); /* child watcher */ |
928 |
|
|
ev_signal_stop (EV_A_ &childev); |
929 |
root |
1.71 |
#endif |
930 |
root |
1.56 |
|
931 |
|
|
ev_ref (EV_A); /* signal watcher */ |
932 |
|
|
ev_io_stop (EV_A_ &sigev); |
933 |
|
|
|
934 |
|
|
close (sigpipe [0]); sigpipe [0] = 0; |
935 |
|
|
close (sigpipe [1]); sigpipe [1] = 0; |
936 |
|
|
|
937 |
|
|
loop_destroy (EV_A); |
938 |
root |
1.1 |
} |
939 |
|
|
|
940 |
root |
1.24 |
void |
941 |
root |
1.60 |
ev_default_fork (void) |
942 |
root |
1.1 |
{ |
943 |
root |
1.60 |
#if EV_MULTIPLICITY |
944 |
|
|
struct ev_loop *loop = default_loop; |
945 |
|
|
#endif |
946 |
|
|
|
947 |
root |
1.70 |
if (method) |
948 |
|
|
postfork = 1; |
949 |
root |
1.1 |
} |
950 |
|
|
|
951 |
root |
1.8 |
/*****************************************************************************/ |
952 |
|
|
|
953 |
root |
1.76 |
static int |
954 |
|
|
any_pending (EV_P) |
955 |
|
|
{ |
956 |
|
|
int pri; |
957 |
|
|
|
958 |
|
|
for (pri = NUMPRI; pri--; ) |
959 |
|
|
if (pendingcnt [pri]) |
960 |
|
|
return 1; |
961 |
|
|
|
962 |
|
|
return 0; |
963 |
|
|
} |
964 |
|
|
|
965 |
root |
1.1 |
static void |
966 |
root |
1.51 |
call_pending (EV_P) |
967 |
root |
1.1 |
{ |
968 |
root |
1.42 |
int pri; |
969 |
|
|
|
970 |
|
|
for (pri = NUMPRI; pri--; ) |
971 |
|
|
while (pendingcnt [pri]) |
972 |
|
|
{ |
973 |
|
|
ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
974 |
root |
1.1 |
|
975 |
root |
1.42 |
if (p->w) |
976 |
|
|
{ |
977 |
|
|
p->w->pending = 0; |
978 |
root |
1.82 |
EV_CB_INVOKE (p->w, p->events); |
979 |
root |
1.42 |
} |
980 |
|
|
} |
981 |
root |
1.1 |
} |
982 |
|
|
|
983 |
|
|
static void |
984 |
root |
1.51 |
timers_reify (EV_P) |
985 |
root |
1.1 |
{ |
986 |
root |
1.63 |
while (timercnt && ((WT)timers [0])->at <= mn_now) |
987 |
root |
1.1 |
{ |
988 |
|
|
struct ev_timer *w = timers [0]; |
989 |
|
|
|
990 |
root |
1.61 |
assert (("inactive timer on timer heap detected", ev_is_active (w))); |
991 |
|
|
|
992 |
root |
1.4 |
/* first reschedule or stop timer */ |
993 |
root |
1.1 |
if (w->repeat) |
994 |
|
|
{ |
995 |
root |
1.33 |
assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
996 |
root |
1.90 |
|
997 |
|
|
((WT)w)->at += w->repeat; |
998 |
|
|
if (((WT)w)->at < mn_now) |
999 |
|
|
((WT)w)->at = mn_now; |
1000 |
|
|
|
1001 |
root |
1.12 |
downheap ((WT *)timers, timercnt, 0); |
1002 |
|
|
} |
1003 |
|
|
else |
1004 |
root |
1.51 |
ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1005 |
root |
1.30 |
|
1006 |
root |
1.78 |
ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1007 |
root |
1.12 |
} |
1008 |
|
|
} |
1009 |
root |
1.4 |
|
1010 |
root |
1.93 |
#if EV_PERIODICS |
1011 |
root |
1.12 |
static void |
1012 |
root |
1.51 |
periodics_reify (EV_P) |
1013 |
root |
1.12 |
{ |
1014 |
root |
1.85 |
while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1015 |
root |
1.12 |
{ |
1016 |
|
|
struct ev_periodic *w = periodics [0]; |
1017 |
root |
1.1 |
|
1018 |
root |
1.61 |
assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1019 |
|
|
|
1020 |
root |
1.12 |
/* first reschedule or stop timer */ |
1021 |
root |
1.77 |
if (w->reschedule_cb) |
1022 |
|
|
{ |
1023 |
root |
1.85 |
ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
1024 |
root |
1.77 |
|
1025 |
root |
1.85 |
assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
1026 |
root |
1.77 |
downheap ((WT *)periodics, periodiccnt, 0); |
1027 |
|
|
} |
1028 |
|
|
else if (w->interval) |
1029 |
root |
1.12 |
{ |
1030 |
root |
1.85 |
((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1031 |
|
|
assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
1032 |
root |
1.12 |
downheap ((WT *)periodics, periodiccnt, 0); |
1033 |
root |
1.1 |
} |
1034 |
|
|
else |
1035 |
root |
1.51 |
ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1036 |
root |
1.12 |
|
1037 |
root |
1.78 |
ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1038 |
root |
1.12 |
} |
1039 |
|
|
} |
1040 |
|
|
|
1041 |
|
|
static void |
1042 |
root |
1.54 |
periodics_reschedule (EV_P) |
1043 |
root |
1.12 |
{ |
1044 |
|
|
int i; |
1045 |
|
|
|
1046 |
root |
1.13 |
/* adjust periodics after time jump */ |
1047 |
root |
1.12 |
for (i = 0; i < periodiccnt; ++i) |
1048 |
|
|
{ |
1049 |
|
|
struct ev_periodic *w = periodics [i]; |
1050 |
|
|
|
1051 |
root |
1.77 |
if (w->reschedule_cb) |
1052 |
root |
1.85 |
((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1053 |
root |
1.77 |
else if (w->interval) |
1054 |
root |
1.85 |
((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1055 |
root |
1.77 |
} |
1056 |
root |
1.12 |
|
1057 |
root |
1.77 |
/* now rebuild the heap */ |
1058 |
|
|
for (i = periodiccnt >> 1; i--; ) |
1059 |
|
|
downheap ((WT *)periodics, periodiccnt, i); |
1060 |
root |
1.1 |
} |
1061 |
root |
1.93 |
#endif |
1062 |
root |
1.1 |
|
1063 |
root |
1.51 |
inline int |
1064 |
|
|
time_update_monotonic (EV_P) |
1065 |
root |
1.40 |
{ |
1066 |
root |
1.51 |
mn_now = get_clock (); |
1067 |
root |
1.40 |
|
1068 |
root |
1.51 |
if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1069 |
root |
1.40 |
{ |
1070 |
root |
1.85 |
ev_rt_now = rtmn_diff + mn_now; |
1071 |
root |
1.40 |
return 0; |
1072 |
|
|
} |
1073 |
|
|
else |
1074 |
|
|
{ |
1075 |
root |
1.51 |
now_floor = mn_now; |
1076 |
root |
1.85 |
ev_rt_now = ev_time (); |
1077 |
root |
1.40 |
return 1; |
1078 |
|
|
} |
1079 |
|
|
} |
1080 |
|
|
|
1081 |
root |
1.4 |
static void |
1082 |
root |
1.51 |
time_update (EV_P) |
1083 |
root |
1.4 |
{ |
1084 |
|
|
int i; |
1085 |
root |
1.12 |
|
1086 |
root |
1.40 |
#if EV_USE_MONOTONIC |
1087 |
|
|
if (expect_true (have_monotonic)) |
1088 |
|
|
{ |
1089 |
root |
1.51 |
if (time_update_monotonic (EV_A)) |
1090 |
root |
1.40 |
{ |
1091 |
root |
1.54 |
ev_tstamp odiff = rtmn_diff; |
1092 |
root |
1.4 |
|
1093 |
root |
1.40 |
for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1094 |
|
|
{ |
1095 |
root |
1.85 |
rtmn_diff = ev_rt_now - mn_now; |
1096 |
root |
1.4 |
|
1097 |
root |
1.54 |
if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1098 |
root |
1.40 |
return; /* all is well */ |
1099 |
root |
1.4 |
|
1100 |
root |
1.85 |
ev_rt_now = ev_time (); |
1101 |
root |
1.51 |
mn_now = get_clock (); |
1102 |
|
|
now_floor = mn_now; |
1103 |
root |
1.40 |
} |
1104 |
root |
1.4 |
|
1105 |
root |
1.93 |
# if EV_PERIODICS |
1106 |
root |
1.54 |
periodics_reschedule (EV_A); |
1107 |
root |
1.93 |
# endif |
1108 |
root |
1.40 |
/* no timer adjustment, as the monotonic clock doesn't jump */ |
1109 |
root |
1.54 |
/* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1110 |
root |
1.4 |
} |
1111 |
|
|
} |
1112 |
|
|
else |
1113 |
root |
1.40 |
#endif |
1114 |
root |
1.4 |
{ |
1115 |
root |
1.85 |
ev_rt_now = ev_time (); |
1116 |
root |
1.40 |
|
1117 |
root |
1.85 |
if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1118 |
root |
1.13 |
{ |
1119 |
root |
1.93 |
#if EV_PERIODICS |
1120 |
root |
1.54 |
periodics_reschedule (EV_A); |
1121 |
root |
1.93 |
#endif |
1122 |
root |
1.13 |
|
1123 |
|
|
/* adjust timers. this is easy, as the offset is the same for all */ |
1124 |
|
|
for (i = 0; i < timercnt; ++i) |
1125 |
root |
1.85 |
((WT)timers [i])->at += ev_rt_now - mn_now; |
1126 |
root |
1.13 |
} |
1127 |
root |
1.4 |
|
1128 |
root |
1.85 |
mn_now = ev_rt_now; |
1129 |
root |
1.4 |
} |
1130 |
|
|
} |
1131 |
|
|
|
1132 |
root |
1.51 |
void |
1133 |
|
|
ev_ref (EV_P) |
1134 |
|
|
{ |
1135 |
|
|
++activecnt; |
1136 |
|
|
} |
1137 |
root |
1.1 |
|
1138 |
root |
1.51 |
void |
1139 |
|
|
ev_unref (EV_P) |
1140 |
|
|
{ |
1141 |
|
|
--activecnt; |
1142 |
|
|
} |
1143 |
|
|
|
1144 |
|
|
static int loop_done; |
1145 |
|
|
|
1146 |
|
|
void |
1147 |
|
|
ev_loop (EV_P_ int flags) |
1148 |
root |
1.1 |
{ |
1149 |
|
|
double block; |
1150 |
root |
1.51 |
loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
1151 |
root |
1.1 |
|
1152 |
root |
1.20 |
do |
1153 |
root |
1.9 |
{ |
1154 |
root |
1.20 |
/* queue check watchers (and execute them) */ |
1155 |
root |
1.40 |
if (expect_false (preparecnt)) |
1156 |
root |
1.20 |
{ |
1157 |
root |
1.51 |
queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1158 |
|
|
call_pending (EV_A); |
1159 |
root |
1.20 |
} |
1160 |
root |
1.9 |
|
1161 |
root |
1.70 |
/* we might have forked, so reify kernel state if necessary */ |
1162 |
|
|
if (expect_false (postfork)) |
1163 |
|
|
loop_fork (EV_A); |
1164 |
|
|
|
1165 |
root |
1.1 |
/* update fd-related kernel structures */ |
1166 |
root |
1.51 |
fd_reify (EV_A); |
1167 |
root |
1.1 |
|
1168 |
|
|
/* calculate blocking time */ |
1169 |
root |
1.12 |
|
1170 |
root |
1.76 |
/* we only need this for !monotonic clock or timers, but as we basically |
1171 |
root |
1.21 |
always have timers, we just calculate it always */ |
1172 |
root |
1.40 |
#if EV_USE_MONOTONIC |
1173 |
|
|
if (expect_true (have_monotonic)) |
1174 |
root |
1.51 |
time_update_monotonic (EV_A); |
1175 |
root |
1.40 |
else |
1176 |
|
|
#endif |
1177 |
|
|
{ |
1178 |
root |
1.85 |
ev_rt_now = ev_time (); |
1179 |
|
|
mn_now = ev_rt_now; |
1180 |
root |
1.40 |
} |
1181 |
root |
1.12 |
|
1182 |
root |
1.9 |
if (flags & EVLOOP_NONBLOCK || idlecnt) |
1183 |
root |
1.1 |
block = 0.; |
1184 |
|
|
else |
1185 |
|
|
{ |
1186 |
root |
1.4 |
block = MAX_BLOCKTIME; |
1187 |
|
|
|
1188 |
root |
1.12 |
if (timercnt) |
1189 |
root |
1.4 |
{ |
1190 |
root |
1.63 |
ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1191 |
root |
1.4 |
if (block > to) block = to; |
1192 |
|
|
} |
1193 |
|
|
|
1194 |
root |
1.93 |
#if EV_PERIODICS |
1195 |
root |
1.12 |
if (periodiccnt) |
1196 |
root |
1.4 |
{ |
1197 |
root |
1.85 |
ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1198 |
root |
1.4 |
if (block > to) block = to; |
1199 |
|
|
} |
1200 |
root |
1.93 |
#endif |
1201 |
root |
1.4 |
|
1202 |
root |
1.1 |
if (block < 0.) block = 0.; |
1203 |
|
|
} |
1204 |
|
|
|
1205 |
root |
1.51 |
method_poll (EV_A_ block); |
1206 |
root |
1.1 |
|
1207 |
root |
1.85 |
/* update ev_rt_now, do magic */ |
1208 |
root |
1.51 |
time_update (EV_A); |
1209 |
root |
1.4 |
|
1210 |
root |
1.9 |
/* queue pending timers and reschedule them */ |
1211 |
root |
1.51 |
timers_reify (EV_A); /* relative timers called last */ |
1212 |
root |
1.93 |
#if EV_PERIODICS |
1213 |
root |
1.51 |
periodics_reify (EV_A); /* absolute timers called first */ |
1214 |
root |
1.93 |
#endif |
1215 |
root |
1.1 |
|
1216 |
root |
1.9 |
/* queue idle watchers unless io or timers are pending */ |
1217 |
root |
1.76 |
if (idlecnt && !any_pending (EV_A)) |
1218 |
root |
1.51 |
queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1219 |
root |
1.9 |
|
1220 |
root |
1.20 |
/* queue check watchers, to be executed first */ |
1221 |
|
|
if (checkcnt) |
1222 |
root |
1.51 |
queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1223 |
root |
1.9 |
|
1224 |
root |
1.51 |
call_pending (EV_A); |
1225 |
root |
1.1 |
} |
1226 |
root |
1.51 |
while (activecnt && !loop_done); |
1227 |
root |
1.13 |
|
1228 |
root |
1.51 |
if (loop_done != 2) |
1229 |
|
|
loop_done = 0; |
1230 |
|
|
} |
1231 |
|
|
|
1232 |
|
|
void |
1233 |
|
|
ev_unloop (EV_P_ int how) |
1234 |
|
|
{ |
1235 |
|
|
loop_done = how; |
1236 |
root |
1.1 |
} |
1237 |
|
|
|
1238 |
root |
1.8 |
/*****************************************************************************/ |
1239 |
|
|
|
1240 |
root |
1.51 |
inline void |
1241 |
root |
1.10 |
wlist_add (WL *head, WL elem) |
1242 |
root |
1.1 |
{ |
1243 |
|
|
elem->next = *head; |
1244 |
|
|
*head = elem; |
1245 |
|
|
} |
1246 |
|
|
|
1247 |
root |
1.51 |
inline void |
1248 |
root |
1.10 |
wlist_del (WL *head, WL elem) |
1249 |
root |
1.1 |
{ |
1250 |
|
|
while (*head) |
1251 |
|
|
{ |
1252 |
|
|
if (*head == elem) |
1253 |
|
|
{ |
1254 |
|
|
*head = elem->next; |
1255 |
|
|
return; |
1256 |
|
|
} |
1257 |
|
|
|
1258 |
|
|
head = &(*head)->next; |
1259 |
|
|
} |
1260 |
|
|
} |
1261 |
|
|
|
1262 |
root |
1.51 |
inline void |
1263 |
|
|
ev_clear_pending (EV_P_ W w) |
1264 |
root |
1.16 |
{ |
1265 |
|
|
if (w->pending) |
1266 |
|
|
{ |
1267 |
root |
1.42 |
pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1268 |
root |
1.16 |
w->pending = 0; |
1269 |
|
|
} |
1270 |
|
|
} |
1271 |
|
|
|
1272 |
root |
1.51 |
inline void |
1273 |
|
|
ev_start (EV_P_ W w, int active) |
1274 |
root |
1.1 |
{ |
1275 |
root |
1.43 |
if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1276 |
|
|
if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
1277 |
|
|
|
1278 |
root |
1.1 |
w->active = active; |
1279 |
root |
1.51 |
ev_ref (EV_A); |
1280 |
root |
1.1 |
} |
1281 |
|
|
|
1282 |
root |
1.51 |
inline void |
1283 |
|
|
ev_stop (EV_P_ W w) |
1284 |
root |
1.1 |
{ |
1285 |
root |
1.51 |
ev_unref (EV_A); |
1286 |
root |
1.1 |
w->active = 0; |
1287 |
|
|
} |
1288 |
|
|
|
1289 |
root |
1.8 |
/*****************************************************************************/ |
1290 |
|
|
|
1291 |
root |
1.1 |
void |
1292 |
root |
1.51 |
ev_io_start (EV_P_ struct ev_io *w) |
1293 |
root |
1.1 |
{ |
1294 |
root |
1.37 |
int fd = w->fd; |
1295 |
|
|
|
1296 |
root |
1.1 |
if (ev_is_active (w)) |
1297 |
|
|
return; |
1298 |
|
|
|
1299 |
root |
1.33 |
assert (("ev_io_start called with negative fd", fd >= 0)); |
1300 |
|
|
|
1301 |
root |
1.51 |
ev_start (EV_A_ (W)w, 1); |
1302 |
root |
1.74 |
array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
1303 |
root |
1.10 |
wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1304 |
root |
1.1 |
|
1305 |
root |
1.51 |
fd_change (EV_A_ fd); |
1306 |
root |
1.1 |
} |
1307 |
|
|
|
1308 |
|
|
void |
1309 |
root |
1.51 |
ev_io_stop (EV_P_ struct ev_io *w) |
1310 |
root |
1.1 |
{ |
1311 |
root |
1.51 |
ev_clear_pending (EV_A_ (W)w); |
1312 |
root |
1.1 |
if (!ev_is_active (w)) |
1313 |
|
|
return; |
1314 |
|
|
|
1315 |
root |
1.89 |
assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
1316 |
|
|
|
1317 |
root |
1.10 |
wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1318 |
root |
1.51 |
ev_stop (EV_A_ (W)w); |
1319 |
root |
1.1 |
|
1320 |
root |
1.51 |
fd_change (EV_A_ w->fd); |
1321 |
root |
1.1 |
} |
1322 |
|
|
|
1323 |
|
|
void |
1324 |
root |
1.51 |
ev_timer_start (EV_P_ struct ev_timer *w) |
1325 |
root |
1.1 |
{ |
1326 |
|
|
if (ev_is_active (w)) |
1327 |
|
|
return; |
1328 |
|
|
|
1329 |
root |
1.63 |
((WT)w)->at += mn_now; |
1330 |
root |
1.12 |
|
1331 |
root |
1.33 |
assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1332 |
root |
1.13 |
|
1333 |
root |
1.51 |
ev_start (EV_A_ (W)w, ++timercnt); |
1334 |
root |
1.74 |
array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1335 |
root |
1.12 |
timers [timercnt - 1] = w; |
1336 |
|
|
upheap ((WT *)timers, timercnt - 1); |
1337 |
root |
1.62 |
|
1338 |
|
|
assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1339 |
root |
1.12 |
} |
1340 |
|
|
|
1341 |
|
|
void |
1342 |
root |
1.51 |
ev_timer_stop (EV_P_ struct ev_timer *w) |
1343 |
root |
1.12 |
{ |
1344 |
root |
1.51 |
ev_clear_pending (EV_A_ (W)w); |
1345 |
root |
1.12 |
if (!ev_is_active (w)) |
1346 |
|
|
return; |
1347 |
|
|
|
1348 |
root |
1.62 |
assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1349 |
|
|
|
1350 |
|
|
if (((W)w)->active < timercnt--) |
1351 |
root |
1.1 |
{ |
1352 |
root |
1.62 |
timers [((W)w)->active - 1] = timers [timercnt]; |
1353 |
root |
1.99 |
adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1354 |
root |
1.12 |
} |
1355 |
root |
1.4 |
|
1356 |
root |
1.91 |
((WT)w)->at -= mn_now; |
1357 |
root |
1.14 |
|
1358 |
root |
1.51 |
ev_stop (EV_A_ (W)w); |
1359 |
root |
1.12 |
} |
1360 |
root |
1.4 |
|
1361 |
root |
1.12 |
void |
1362 |
root |
1.51 |
ev_timer_again (EV_P_ struct ev_timer *w) |
1363 |
root |
1.14 |
{ |
1364 |
|
|
if (ev_is_active (w)) |
1365 |
|
|
{ |
1366 |
|
|
if (w->repeat) |
1367 |
root |
1.99 |
{ |
1368 |
|
|
((WT)w)->at = mn_now + w->repeat; |
1369 |
|
|
adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1370 |
|
|
} |
1371 |
root |
1.14 |
else |
1372 |
root |
1.51 |
ev_timer_stop (EV_A_ w); |
1373 |
root |
1.14 |
} |
1374 |
|
|
else if (w->repeat) |
1375 |
root |
1.51 |
ev_timer_start (EV_A_ w); |
1376 |
root |
1.14 |
} |
1377 |
|
|
|
1378 |
root |
1.93 |
#if EV_PERIODICS |
1379 |
root |
1.14 |
void |
1380 |
root |
1.51 |
ev_periodic_start (EV_P_ struct ev_periodic *w) |
1381 |
root |
1.12 |
{ |
1382 |
|
|
if (ev_is_active (w)) |
1383 |
|
|
return; |
1384 |
root |
1.1 |
|
1385 |
root |
1.77 |
if (w->reschedule_cb) |
1386 |
root |
1.85 |
((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1387 |
root |
1.77 |
else if (w->interval) |
1388 |
|
|
{ |
1389 |
|
|
assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1390 |
|
|
/* this formula differs from the one in periodic_reify because we do not always round up */ |
1391 |
root |
1.85 |
((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1392 |
root |
1.77 |
} |
1393 |
root |
1.12 |
|
1394 |
root |
1.51 |
ev_start (EV_A_ (W)w, ++periodiccnt); |
1395 |
root |
1.74 |
array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1396 |
root |
1.12 |
periodics [periodiccnt - 1] = w; |
1397 |
|
|
upheap ((WT *)periodics, periodiccnt - 1); |
1398 |
root |
1.62 |
|
1399 |
|
|
assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1400 |
root |
1.1 |
} |
1401 |
|
|
|
1402 |
|
|
void |
1403 |
root |
1.51 |
ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1404 |
root |
1.1 |
{ |
1405 |
root |
1.51 |
ev_clear_pending (EV_A_ (W)w); |
1406 |
root |
1.1 |
if (!ev_is_active (w)) |
1407 |
|
|
return; |
1408 |
|
|
|
1409 |
root |
1.62 |
assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1410 |
|
|
|
1411 |
|
|
if (((W)w)->active < periodiccnt--) |
1412 |
root |
1.2 |
{ |
1413 |
root |
1.62 |
periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1414 |
root |
1.99 |
adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1415 |
root |
1.2 |
} |
1416 |
|
|
|
1417 |
root |
1.51 |
ev_stop (EV_A_ (W)w); |
1418 |
root |
1.1 |
} |
1419 |
|
|
|
1420 |
root |
1.28 |
void |
1421 |
root |
1.77 |
ev_periodic_again (EV_P_ struct ev_periodic *w) |
1422 |
|
|
{ |
1423 |
root |
1.84 |
/* TODO: use adjustheap and recalculation */ |
1424 |
root |
1.77 |
ev_periodic_stop (EV_A_ w); |
1425 |
|
|
ev_periodic_start (EV_A_ w); |
1426 |
|
|
} |
1427 |
root |
1.93 |
#endif |
1428 |
root |
1.77 |
|
1429 |
|
|
void |
1430 |
root |
1.51 |
ev_idle_start (EV_P_ struct ev_idle *w) |
1431 |
root |
1.9 |
{ |
1432 |
|
|
if (ev_is_active (w)) |
1433 |
|
|
return; |
1434 |
|
|
|
1435 |
root |
1.51 |
ev_start (EV_A_ (W)w, ++idlecnt); |
1436 |
root |
1.74 |
array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1437 |
root |
1.9 |
idles [idlecnt - 1] = w; |
1438 |
|
|
} |
1439 |
|
|
|
1440 |
root |
1.28 |
void |
1441 |
root |
1.51 |
ev_idle_stop (EV_P_ struct ev_idle *w) |
1442 |
root |
1.9 |
{ |
1443 |
root |
1.51 |
ev_clear_pending (EV_A_ (W)w); |
1444 |
root |
1.95 |
if (ev_is_active (w)) |
1445 |
root |
1.16 |
return; |
1446 |
|
|
|
1447 |
root |
1.62 |
idles [((W)w)->active - 1] = idles [--idlecnt]; |
1448 |
root |
1.51 |
ev_stop (EV_A_ (W)w); |
1449 |
root |
1.9 |
} |
1450 |
|
|
|
1451 |
root |
1.28 |
void |
1452 |
root |
1.51 |
ev_prepare_start (EV_P_ struct ev_prepare *w) |
1453 |
root |
1.20 |
{ |
1454 |
|
|
if (ev_is_active (w)) |
1455 |
|
|
return; |
1456 |
|
|
|
1457 |
root |
1.51 |
ev_start (EV_A_ (W)w, ++preparecnt); |
1458 |
root |
1.74 |
array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1459 |
root |
1.20 |
prepares [preparecnt - 1] = w; |
1460 |
|
|
} |
1461 |
|
|
|
1462 |
root |
1.28 |
void |
1463 |
root |
1.51 |
ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1464 |
root |
1.20 |
{ |
1465 |
root |
1.51 |
ev_clear_pending (EV_A_ (W)w); |
1466 |
root |
1.95 |
if (ev_is_active (w)) |
1467 |
root |
1.20 |
return; |
1468 |
|
|
|
1469 |
root |
1.62 |
prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1470 |
root |
1.51 |
ev_stop (EV_A_ (W)w); |
1471 |
root |
1.20 |
} |
1472 |
|
|
|
1473 |
root |
1.28 |
void |
1474 |
root |
1.51 |
ev_check_start (EV_P_ struct ev_check *w) |
1475 |
root |
1.9 |
{ |
1476 |
|
|
if (ev_is_active (w)) |
1477 |
|
|
return; |
1478 |
|
|
|
1479 |
root |
1.51 |
ev_start (EV_A_ (W)w, ++checkcnt); |
1480 |
root |
1.74 |
array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1481 |
root |
1.9 |
checks [checkcnt - 1] = w; |
1482 |
|
|
} |
1483 |
|
|
|
1484 |
root |
1.28 |
void |
1485 |
root |
1.51 |
ev_check_stop (EV_P_ struct ev_check *w) |
1486 |
root |
1.9 |
{ |
1487 |
root |
1.51 |
ev_clear_pending (EV_A_ (W)w); |
1488 |
root |
1.94 |
if (!ev_is_active (w)) |
1489 |
root |
1.16 |
return; |
1490 |
|
|
|
1491 |
root |
1.62 |
checks [((W)w)->active - 1] = checks [--checkcnt]; |
1492 |
root |
1.51 |
ev_stop (EV_A_ (W)w); |
1493 |
root |
1.9 |
} |
1494 |
|
|
|
1495 |
root |
1.56 |
#ifndef SA_RESTART |
1496 |
|
|
# define SA_RESTART 0 |
1497 |
|
|
#endif |
1498 |
|
|
|
1499 |
|
|
void |
1500 |
|
|
ev_signal_start (EV_P_ struct ev_signal *w) |
1501 |
|
|
{ |
1502 |
|
|
#if EV_MULTIPLICITY |
1503 |
|
|
assert (("signal watchers are only supported in the default loop", loop == default_loop)); |
1504 |
|
|
#endif |
1505 |
|
|
if (ev_is_active (w)) |
1506 |
|
|
return; |
1507 |
|
|
|
1508 |
|
|
assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1509 |
|
|
|
1510 |
|
|
ev_start (EV_A_ (W)w, 1); |
1511 |
root |
1.74 |
array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1512 |
root |
1.56 |
wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1513 |
|
|
|
1514 |
root |
1.63 |
if (!((WL)w)->next) |
1515 |
root |
1.56 |
{ |
1516 |
root |
1.67 |
#if WIN32 |
1517 |
|
|
signal (w->signum, sighandler); |
1518 |
|
|
#else |
1519 |
root |
1.56 |
struct sigaction sa; |
1520 |
|
|
sa.sa_handler = sighandler; |
1521 |
|
|
sigfillset (&sa.sa_mask); |
1522 |
|
|
sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1523 |
|
|
sigaction (w->signum, &sa, 0); |
1524 |
root |
1.67 |
#endif |
1525 |
root |
1.56 |
} |
1526 |
|
|
} |
1527 |
|
|
|
1528 |
|
|
void |
1529 |
|
|
ev_signal_stop (EV_P_ struct ev_signal *w) |
1530 |
|
|
{ |
1531 |
|
|
ev_clear_pending (EV_A_ (W)w); |
1532 |
|
|
if (!ev_is_active (w)) |
1533 |
|
|
return; |
1534 |
|
|
|
1535 |
|
|
wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1536 |
|
|
ev_stop (EV_A_ (W)w); |
1537 |
|
|
|
1538 |
|
|
if (!signals [w->signum - 1].head) |
1539 |
|
|
signal (w->signum, SIG_DFL); |
1540 |
|
|
} |
1541 |
|
|
|
1542 |
root |
1.28 |
void |
1543 |
root |
1.51 |
ev_child_start (EV_P_ struct ev_child *w) |
1544 |
root |
1.22 |
{ |
1545 |
root |
1.56 |
#if EV_MULTIPLICITY |
1546 |
|
|
assert (("child watchers are only supported in the default loop", loop == default_loop)); |
1547 |
|
|
#endif |
1548 |
root |
1.22 |
if (ev_is_active (w)) |
1549 |
|
|
return; |
1550 |
|
|
|
1551 |
root |
1.51 |
ev_start (EV_A_ (W)w, 1); |
1552 |
root |
1.22 |
wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1553 |
|
|
} |
1554 |
|
|
|
1555 |
root |
1.28 |
void |
1556 |
root |
1.51 |
ev_child_stop (EV_P_ struct ev_child *w) |
1557 |
root |
1.22 |
{ |
1558 |
root |
1.51 |
ev_clear_pending (EV_A_ (W)w); |
1559 |
root |
1.95 |
if (!ev_is_active (w)) |
1560 |
root |
1.22 |
return; |
1561 |
|
|
|
1562 |
|
|
wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1563 |
root |
1.51 |
ev_stop (EV_A_ (W)w); |
1564 |
root |
1.22 |
} |
1565 |
|
|
|
1566 |
root |
1.1 |
/*****************************************************************************/ |
1567 |
root |
1.10 |
|
1568 |
root |
1.16 |
struct ev_once |
1569 |
|
|
{ |
1570 |
|
|
struct ev_io io; |
1571 |
|
|
struct ev_timer to; |
1572 |
|
|
void (*cb)(int revents, void *arg); |
1573 |
|
|
void *arg; |
1574 |
|
|
}; |
1575 |
|
|
|
1576 |
|
|
static void |
1577 |
root |
1.51 |
once_cb (EV_P_ struct ev_once *once, int revents) |
1578 |
root |
1.16 |
{ |
1579 |
|
|
void (*cb)(int revents, void *arg) = once->cb; |
1580 |
|
|
void *arg = once->arg; |
1581 |
|
|
|
1582 |
root |
1.51 |
ev_io_stop (EV_A_ &once->io); |
1583 |
|
|
ev_timer_stop (EV_A_ &once->to); |
1584 |
root |
1.69 |
ev_free (once); |
1585 |
root |
1.16 |
|
1586 |
|
|
cb (revents, arg); |
1587 |
|
|
} |
1588 |
|
|
|
1589 |
|
|
static void |
1590 |
root |
1.51 |
once_cb_io (EV_P_ struct ev_io *w, int revents) |
1591 |
root |
1.16 |
{ |
1592 |
root |
1.51 |
once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1593 |
root |
1.16 |
} |
1594 |
|
|
|
1595 |
|
|
static void |
1596 |
root |
1.51 |
once_cb_to (EV_P_ struct ev_timer *w, int revents) |
1597 |
root |
1.16 |
{ |
1598 |
root |
1.51 |
once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1599 |
root |
1.16 |
} |
1600 |
|
|
|
1601 |
|
|
void |
1602 |
root |
1.51 |
ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1603 |
root |
1.16 |
{ |
1604 |
root |
1.74 |
struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1605 |
root |
1.16 |
|
1606 |
|
|
if (!once) |
1607 |
root |
1.29 |
cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1608 |
root |
1.16 |
else |
1609 |
|
|
{ |
1610 |
|
|
once->cb = cb; |
1611 |
|
|
once->arg = arg; |
1612 |
|
|
|
1613 |
root |
1.83 |
ev_init (&once->io, once_cb_io); |
1614 |
root |
1.16 |
if (fd >= 0) |
1615 |
|
|
{ |
1616 |
root |
1.28 |
ev_io_set (&once->io, fd, events); |
1617 |
root |
1.51 |
ev_io_start (EV_A_ &once->io); |
1618 |
root |
1.16 |
} |
1619 |
|
|
|
1620 |
root |
1.83 |
ev_init (&once->to, once_cb_to); |
1621 |
root |
1.16 |
if (timeout >= 0.) |
1622 |
|
|
{ |
1623 |
root |
1.28 |
ev_timer_set (&once->to, timeout, 0.); |
1624 |
root |
1.51 |
ev_timer_start (EV_A_ &once->to); |
1625 |
root |
1.16 |
} |
1626 |
|
|
} |
1627 |
|
|
} |
1628 |
|
|
|
1629 |
root |
1.87 |
#ifdef __cplusplus |
1630 |
|
|
} |
1631 |
|
|
#endif |
1632 |
|
|
|