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