1 | /* |
1 | /* |
2 | * libev event processing core, watcher management |
2 | * libev event processing core, watcher management |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009,2010,2011,2012,2013 Marc Alexander Lehmann <libev@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
10 | * 1. Redistributions of source code must retain the above copyright notice, |
10 | * 1. Redistributions of source code must retain the above copyright notice, |
11 | * this list of conditions and the following disclaimer. |
11 | * this list of conditions and the following disclaimer. |
12 | * |
12 | * |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the |
14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. |
15 | * documentation and/or other materials provided with the distribution. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
19 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
19 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
21 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
21 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
… | |
… | |
43 | # include EV_CONFIG_H |
43 | # include EV_CONFIG_H |
44 | # else |
44 | # else |
45 | # include "config.h" |
45 | # include "config.h" |
46 | # endif |
46 | # endif |
47 | |
47 | |
|
|
48 | # if HAVE_FLOOR |
|
|
49 | # ifndef EV_USE_FLOOR |
|
|
50 | # define EV_USE_FLOOR 1 |
|
|
51 | # endif |
|
|
52 | # endif |
|
|
53 | |
48 | # if HAVE_CLOCK_SYSCALL |
54 | # if HAVE_CLOCK_SYSCALL |
49 | # ifndef EV_USE_CLOCK_SYSCALL |
55 | # ifndef EV_USE_CLOCK_SYSCALL |
50 | # define EV_USE_CLOCK_SYSCALL 1 |
56 | # define EV_USE_CLOCK_SYSCALL 1 |
51 | # ifndef EV_USE_REALTIME |
57 | # ifndef EV_USE_REALTIME |
52 | # define EV_USE_REALTIME 0 |
58 | # define EV_USE_REALTIME 0 |
53 | # endif |
59 | # endif |
54 | # ifndef EV_USE_MONOTONIC |
60 | # ifndef EV_USE_MONOTONIC |
55 | # define EV_USE_MONOTONIC 1 |
61 | # define EV_USE_MONOTONIC 1 |
56 | # endif |
62 | # endif |
57 | # endif |
63 | # endif |
58 | # elif !defined(EV_USE_CLOCK_SYSCALL) |
64 | # elif !defined EV_USE_CLOCK_SYSCALL |
59 | # define EV_USE_CLOCK_SYSCALL 0 |
65 | # define EV_USE_CLOCK_SYSCALL 0 |
60 | # endif |
66 | # endif |
61 | |
67 | |
62 | # if HAVE_CLOCK_GETTIME |
68 | # if HAVE_CLOCK_GETTIME |
63 | # ifndef EV_USE_MONOTONIC |
69 | # ifndef EV_USE_MONOTONIC |
… | |
… | |
156 | # define EV_USE_EVENTFD 0 |
162 | # define EV_USE_EVENTFD 0 |
157 | # endif |
163 | # endif |
158 | |
164 | |
159 | #endif |
165 | #endif |
160 | |
166 | |
161 | #include <math.h> |
|
|
162 | #include <stdlib.h> |
167 | #include <stdlib.h> |
163 | #include <string.h> |
168 | #include <string.h> |
164 | #include <fcntl.h> |
169 | #include <fcntl.h> |
165 | #include <stddef.h> |
170 | #include <stddef.h> |
166 | |
171 | |
… | |
… | |
178 | # include EV_H |
183 | # include EV_H |
179 | #else |
184 | #else |
180 | # include "ev.h" |
185 | # include "ev.h" |
181 | #endif |
186 | #endif |
182 | |
187 | |
183 | EV_CPP(extern "C" {) |
188 | #if EV_NO_THREADS |
|
|
189 | # undef EV_NO_SMP |
|
|
190 | # define EV_NO_SMP 1 |
|
|
191 | # undef ECB_NO_THREADS |
|
|
192 | # define ECB_NO_THREADS 1 |
|
|
193 | #endif |
|
|
194 | #if EV_NO_SMP |
|
|
195 | # undef EV_NO_SMP |
|
|
196 | # define ECB_NO_SMP 1 |
|
|
197 | #endif |
184 | |
198 | |
185 | #ifndef _WIN32 |
199 | #ifndef _WIN32 |
186 | # include <sys/time.h> |
200 | # include <sys/time.h> |
187 | # include <sys/wait.h> |
201 | # include <sys/wait.h> |
188 | # include <unistd.h> |
202 | # include <unistd.h> |
189 | #else |
203 | #else |
190 | # include <io.h> |
204 | # include <io.h> |
191 | # define WIN32_LEAN_AND_MEAN |
205 | # define WIN32_LEAN_AND_MEAN |
|
|
206 | # include <winsock2.h> |
192 | # include <windows.h> |
207 | # include <windows.h> |
193 | # ifndef EV_SELECT_IS_WINSOCKET |
208 | # ifndef EV_SELECT_IS_WINSOCKET |
194 | # define EV_SELECT_IS_WINSOCKET 1 |
209 | # define EV_SELECT_IS_WINSOCKET 1 |
195 | # endif |
210 | # endif |
196 | # undef EV_AVOID_STDIO |
211 | # undef EV_AVOID_STDIO |
… | |
… | |
205 | #define _DARWIN_UNLIMITED_SELECT 1 |
220 | #define _DARWIN_UNLIMITED_SELECT 1 |
206 | |
221 | |
207 | /* this block tries to deduce configuration from header-defined symbols and defaults */ |
222 | /* this block tries to deduce configuration from header-defined symbols and defaults */ |
208 | |
223 | |
209 | /* try to deduce the maximum number of signals on this platform */ |
224 | /* try to deduce the maximum number of signals on this platform */ |
210 | #if defined (EV_NSIG) |
225 | #if defined EV_NSIG |
211 | /* use what's provided */ |
226 | /* use what's provided */ |
212 | #elif defined (NSIG) |
227 | #elif defined NSIG |
213 | # define EV_NSIG (NSIG) |
228 | # define EV_NSIG (NSIG) |
214 | #elif defined(_NSIG) |
229 | #elif defined _NSIG |
215 | # define EV_NSIG (_NSIG) |
230 | # define EV_NSIG (_NSIG) |
216 | #elif defined (SIGMAX) |
231 | #elif defined SIGMAX |
217 | # define EV_NSIG (SIGMAX+1) |
232 | # define EV_NSIG (SIGMAX+1) |
218 | #elif defined (SIG_MAX) |
233 | #elif defined SIG_MAX |
219 | # define EV_NSIG (SIG_MAX+1) |
234 | # define EV_NSIG (SIG_MAX+1) |
220 | #elif defined (_SIG_MAX) |
235 | #elif defined _SIG_MAX |
221 | # define EV_NSIG (_SIG_MAX+1) |
236 | # define EV_NSIG (_SIG_MAX+1) |
222 | #elif defined (MAXSIG) |
237 | #elif defined MAXSIG |
223 | # define EV_NSIG (MAXSIG+1) |
238 | # define EV_NSIG (MAXSIG+1) |
224 | #elif defined (MAX_SIG) |
239 | #elif defined MAX_SIG |
225 | # define EV_NSIG (MAX_SIG+1) |
240 | # define EV_NSIG (MAX_SIG+1) |
226 | #elif defined (SIGARRAYSIZE) |
241 | #elif defined SIGARRAYSIZE |
227 | # define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */ |
242 | # define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */ |
228 | #elif defined (_sys_nsig) |
243 | #elif defined _sys_nsig |
229 | # define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ |
244 | # define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ |
230 | #else |
245 | #else |
231 | # error "unable to find value for NSIG, please report" |
246 | # define EV_NSIG (8 * sizeof (sigset_t) + 1) |
232 | /* to make it compile regardless, just remove the above line, */ |
247 | #endif |
233 | /* but consider reporting it, too! :) */ |
248 | |
234 | # define EV_NSIG 65 |
249 | #ifndef EV_USE_FLOOR |
|
|
250 | # define EV_USE_FLOOR 0 |
235 | #endif |
251 | #endif |
236 | |
252 | |
237 | #ifndef EV_USE_CLOCK_SYSCALL |
253 | #ifndef EV_USE_CLOCK_SYSCALL |
238 | # if __linux && __GLIBC__ >= 2 |
254 | # if __linux && __GLIBC__ == 2 && __GLIBC_MINOR__ < 17 |
239 | # define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS |
255 | # define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS |
240 | # else |
256 | # else |
241 | # define EV_USE_CLOCK_SYSCALL 0 |
257 | # define EV_USE_CLOCK_SYSCALL 0 |
242 | # endif |
258 | # endif |
243 | #endif |
259 | #endif |
244 | |
260 | |
|
|
261 | #if !(_POSIX_TIMERS > 0) |
|
|
262 | # ifndef EV_USE_MONOTONIC |
|
|
263 | # define EV_USE_MONOTONIC 0 |
|
|
264 | # endif |
|
|
265 | # ifndef EV_USE_REALTIME |
|
|
266 | # define EV_USE_REALTIME 0 |
|
|
267 | # endif |
|
|
268 | #endif |
|
|
269 | |
245 | #ifndef EV_USE_MONOTONIC |
270 | #ifndef EV_USE_MONOTONIC |
246 | # if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 |
271 | # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0 |
247 | # define EV_USE_MONOTONIC EV_FEATURE_OS |
272 | # define EV_USE_MONOTONIC EV_FEATURE_OS |
248 | # else |
273 | # else |
249 | # define EV_USE_MONOTONIC 0 |
274 | # define EV_USE_MONOTONIC 0 |
250 | # endif |
275 | # endif |
251 | #endif |
276 | #endif |
… | |
… | |
338 | |
363 | |
339 | #ifndef EV_HEAP_CACHE_AT |
364 | #ifndef EV_HEAP_CACHE_AT |
340 | # define EV_HEAP_CACHE_AT EV_FEATURE_DATA |
365 | # define EV_HEAP_CACHE_AT EV_FEATURE_DATA |
341 | #endif |
366 | #endif |
342 | |
367 | |
|
|
368 | #ifdef ANDROID |
|
|
369 | /* supposedly, android doesn't typedef fd_mask */ |
|
|
370 | # undef EV_USE_SELECT |
|
|
371 | # define EV_USE_SELECT 0 |
|
|
372 | /* supposedly, we need to include syscall.h, not sys/syscall.h, so just disable */ |
|
|
373 | # undef EV_USE_CLOCK_SYSCALL |
|
|
374 | # define EV_USE_CLOCK_SYSCALL 0 |
|
|
375 | #endif |
|
|
376 | |
|
|
377 | /* aix's poll.h seems to cause lots of trouble */ |
|
|
378 | #ifdef _AIX |
|
|
379 | /* AIX has a completely broken poll.h header */ |
|
|
380 | # undef EV_USE_POLL |
|
|
381 | # define EV_USE_POLL 0 |
|
|
382 | #endif |
|
|
383 | |
343 | /* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ |
384 | /* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ |
344 | /* which makes programs even slower. might work on other unices, too. */ |
385 | /* which makes programs even slower. might work on other unices, too. */ |
345 | #if EV_USE_CLOCK_SYSCALL |
386 | #if EV_USE_CLOCK_SYSCALL |
346 | # include <syscall.h> |
387 | # include <sys/syscall.h> |
347 | # ifdef SYS_clock_gettime |
388 | # ifdef SYS_clock_gettime |
348 | # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
389 | # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
349 | # undef EV_USE_MONOTONIC |
390 | # undef EV_USE_MONOTONIC |
350 | # define EV_USE_MONOTONIC 1 |
391 | # define EV_USE_MONOTONIC 1 |
351 | # else |
392 | # else |
… | |
… | |
354 | # endif |
395 | # endif |
355 | #endif |
396 | #endif |
356 | |
397 | |
357 | /* this block fixes any misconfiguration where we know we run into trouble otherwise */ |
398 | /* this block fixes any misconfiguration where we know we run into trouble otherwise */ |
358 | |
399 | |
359 | #ifdef _AIX |
|
|
360 | /* AIX has a completely broken poll.h header */ |
|
|
361 | # undef EV_USE_POLL |
|
|
362 | # define EV_USE_POLL 0 |
|
|
363 | #endif |
|
|
364 | |
|
|
365 | #ifndef CLOCK_MONOTONIC |
400 | #ifndef CLOCK_MONOTONIC |
366 | # undef EV_USE_MONOTONIC |
401 | # undef EV_USE_MONOTONIC |
367 | # define EV_USE_MONOTONIC 0 |
402 | # define EV_USE_MONOTONIC 0 |
368 | #endif |
403 | #endif |
369 | |
404 | |
… | |
… | |
376 | # undef EV_USE_INOTIFY |
411 | # undef EV_USE_INOTIFY |
377 | # define EV_USE_INOTIFY 0 |
412 | # define EV_USE_INOTIFY 0 |
378 | #endif |
413 | #endif |
379 | |
414 | |
380 | #if !EV_USE_NANOSLEEP |
415 | #if !EV_USE_NANOSLEEP |
381 | # ifndef _WIN32 |
416 | /* hp-ux has it in sys/time.h, which we unconditionally include above */ |
|
|
417 | # if !defined _WIN32 && !defined __hpux |
382 | # include <sys/select.h> |
418 | # include <sys/select.h> |
383 | # endif |
419 | # endif |
384 | #endif |
420 | #endif |
385 | |
421 | |
386 | #if EV_USE_INOTIFY |
422 | #if EV_USE_INOTIFY |
… | |
… | |
389 | /* some very old inotify.h headers don't have IN_DONT_FOLLOW */ |
425 | /* some very old inotify.h headers don't have IN_DONT_FOLLOW */ |
390 | # ifndef IN_DONT_FOLLOW |
426 | # ifndef IN_DONT_FOLLOW |
391 | # undef EV_USE_INOTIFY |
427 | # undef EV_USE_INOTIFY |
392 | # define EV_USE_INOTIFY 0 |
428 | # define EV_USE_INOTIFY 0 |
393 | # endif |
429 | # endif |
394 | #endif |
|
|
395 | |
|
|
396 | #if EV_SELECT_IS_WINSOCKET |
|
|
397 | # include <winsock.h> |
|
|
398 | #endif |
430 | #endif |
399 | |
431 | |
400 | #if EV_USE_EVENTFD |
432 | #if EV_USE_EVENTFD |
401 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
433 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
402 | # include <stdint.h> |
434 | # include <stdint.h> |
… | |
… | |
442 | #else |
474 | #else |
443 | # define EV_FREQUENT_CHECK do { } while (0) |
475 | # define EV_FREQUENT_CHECK do { } while (0) |
444 | #endif |
476 | #endif |
445 | |
477 | |
446 | /* |
478 | /* |
447 | * This is used to avoid floating point rounding problems. |
479 | * This is used to work around floating point rounding problems. |
448 | * It is added to ev_rt_now when scheduling periodics |
|
|
449 | * to ensure progress, time-wise, even when rounding |
|
|
450 | * errors are against us. |
|
|
451 | * This value is good at least till the year 4000. |
480 | * This value is good at least till the year 4000. |
452 | * Better solutions welcome. |
|
|
453 | */ |
481 | */ |
454 | #define TIME_EPSILON 0.0001220703125 /* 1/8192 */ |
482 | #define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */ |
|
|
483 | /*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */ |
455 | |
484 | |
456 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
485 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
457 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
486 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
458 | |
487 | |
459 | #define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) |
488 | #define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) |
460 | #define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0) |
489 | #define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0) |
461 | |
490 | |
|
|
491 | /* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */ |
|
|
492 | /* ECB.H BEGIN */ |
|
|
493 | /* |
|
|
494 | * libecb - http://software.schmorp.de/pkg/libecb |
|
|
495 | * |
|
|
496 | * Copyright (©) 2009-2014 Marc Alexander Lehmann <libecb@schmorp.de> |
|
|
497 | * Copyright (©) 2011 Emanuele Giaquinta |
|
|
498 | * All rights reserved. |
|
|
499 | * |
|
|
500 | * Redistribution and use in source and binary forms, with or without modifica- |
|
|
501 | * tion, are permitted provided that the following conditions are met: |
|
|
502 | * |
|
|
503 | * 1. Redistributions of source code must retain the above copyright notice, |
|
|
504 | * this list of conditions and the following disclaimer. |
|
|
505 | * |
|
|
506 | * 2. Redistributions in binary form must reproduce the above copyright |
|
|
507 | * notice, this list of conditions and the following disclaimer in the |
|
|
508 | * documentation and/or other materials provided with the distribution. |
|
|
509 | * |
|
|
510 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
|
511 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
|
|
512 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
|
|
513 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
|
|
514 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
|
515 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
|
516 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
|
517 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
|
|
518 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
|
|
519 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
520 | * |
|
|
521 | * Alternatively, the contents of this file may be used under the terms of |
|
|
522 | * the GNU General Public License ("GPL") version 2 or any later version, |
|
|
523 | * in which case the provisions of the GPL are applicable instead of |
|
|
524 | * the above. If you wish to allow the use of your version of this file |
|
|
525 | * only under the terms of the GPL and not to allow others to use your |
|
|
526 | * version of this file under the BSD license, indicate your decision |
|
|
527 | * by deleting the provisions above and replace them with the notice |
|
|
528 | * and other provisions required by the GPL. If you do not delete the |
|
|
529 | * provisions above, a recipient may use your version of this file under |
|
|
530 | * either the BSD or the GPL. |
|
|
531 | */ |
|
|
532 | |
|
|
533 | #ifndef ECB_H |
|
|
534 | #define ECB_H |
|
|
535 | |
|
|
536 | /* 16 bits major, 16 bits minor */ |
|
|
537 | #define ECB_VERSION 0x00010003 |
|
|
538 | |
|
|
539 | #ifdef _WIN32 |
|
|
540 | typedef signed char int8_t; |
|
|
541 | typedef unsigned char uint8_t; |
|
|
542 | typedef signed short int16_t; |
|
|
543 | typedef unsigned short uint16_t; |
|
|
544 | typedef signed int int32_t; |
|
|
545 | typedef unsigned int uint32_t; |
462 | #if __GNUC__ >= 4 |
546 | #if __GNUC__ |
463 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
547 | typedef signed long long int64_t; |
464 | # define noinline __attribute__ ((noinline)) |
548 | typedef unsigned long long uint64_t; |
|
|
549 | #else /* _MSC_VER || __BORLANDC__ */ |
|
|
550 | typedef signed __int64 int64_t; |
|
|
551 | typedef unsigned __int64 uint64_t; |
|
|
552 | #endif |
|
|
553 | #ifdef _WIN64 |
|
|
554 | #define ECB_PTRSIZE 8 |
|
|
555 | typedef uint64_t uintptr_t; |
|
|
556 | typedef int64_t intptr_t; |
|
|
557 | #else |
|
|
558 | #define ECB_PTRSIZE 4 |
|
|
559 | typedef uint32_t uintptr_t; |
|
|
560 | typedef int32_t intptr_t; |
|
|
561 | #endif |
465 | #else |
562 | #else |
466 | # define expect(expr,value) (expr) |
563 | #include <inttypes.h> |
467 | # define noinline |
564 | #if UINTMAX_MAX > 0xffffffffU |
468 | # if __STDC_VERSION__ < 199901L && __GNUC__ < 2 |
565 | #define ECB_PTRSIZE 8 |
469 | # define inline |
566 | #else |
|
|
567 | #define ECB_PTRSIZE 4 |
|
|
568 | #endif |
470 | # endif |
569 | #endif |
|
|
570 | |
|
|
571 | /* work around x32 idiocy by defining proper macros */ |
|
|
572 | #if __amd64 || __x86_64 || _M_AMD64 || _M_X64 |
|
|
573 | #if _ILP32 |
|
|
574 | #define ECB_AMD64_X32 1 |
|
|
575 | #else |
|
|
576 | #define ECB_AMD64 1 |
471 | #endif |
577 | #endif |
|
|
578 | #endif |
472 | |
579 | |
|
|
580 | /* many compilers define _GNUC_ to some versions but then only implement |
|
|
581 | * what their idiot authors think are the "more important" extensions, |
|
|
582 | * causing enormous grief in return for some better fake benchmark numbers. |
|
|
583 | * or so. |
|
|
584 | * we try to detect these and simply assume they are not gcc - if they have |
|
|
585 | * an issue with that they should have done it right in the first place. |
|
|
586 | */ |
|
|
587 | #ifndef ECB_GCC_VERSION |
|
|
588 | #if !defined __GNUC_MINOR__ || defined __INTEL_COMPILER || defined __SUNPRO_C || defined __SUNPRO_CC || defined __llvm__ || defined __clang__ |
|
|
589 | #define ECB_GCC_VERSION(major,minor) 0 |
|
|
590 | #else |
|
|
591 | #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
|
|
592 | #endif |
|
|
593 | #endif |
|
|
594 | |
|
|
595 | #define ECB_CPP (__cplusplus+0) |
|
|
596 | #define ECB_CPP11 (__cplusplus >= 201103L) |
|
|
597 | |
|
|
598 | #if ECB_CPP |
|
|
599 | #define ECB_C 0 |
|
|
600 | #define ECB_STDC_VERSION 0 |
|
|
601 | #else |
|
|
602 | #define ECB_C 1 |
|
|
603 | #define ECB_STDC_VERSION __STDC_VERSION__ |
|
|
604 | #endif |
|
|
605 | |
|
|
606 | #define ECB_C99 (ECB_STDC_VERSION >= 199901L) |
|
|
607 | #define ECB_C11 (ECB_STDC_VERSION >= 201112L) |
|
|
608 | |
|
|
609 | #if ECB_CPP |
|
|
610 | #define ECB_EXTERN_C extern "C" |
|
|
611 | #define ECB_EXTERN_C_BEG ECB_EXTERN_C { |
|
|
612 | #define ECB_EXTERN_C_END } |
|
|
613 | #else |
|
|
614 | #define ECB_EXTERN_C extern |
|
|
615 | #define ECB_EXTERN_C_BEG |
|
|
616 | #define ECB_EXTERN_C_END |
|
|
617 | #endif |
|
|
618 | |
|
|
619 | /*****************************************************************************/ |
|
|
620 | |
|
|
621 | /* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ |
|
|
622 | /* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ |
|
|
623 | |
|
|
624 | #if ECB_NO_THREADS |
|
|
625 | #define ECB_NO_SMP 1 |
|
|
626 | #endif |
|
|
627 | |
|
|
628 | #if ECB_NO_SMP |
|
|
629 | #define ECB_MEMORY_FENCE do { } while (0) |
|
|
630 | #endif |
|
|
631 | |
|
|
632 | #ifndef ECB_MEMORY_FENCE |
|
|
633 | #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
|
|
634 | #if __i386 || __i386__ |
|
|
635 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
|
|
636 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory") |
|
|
637 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("") |
|
|
638 | #elif __amd64 || __amd64__ || __x86_64 || __x86_64__ |
|
|
639 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
|
|
640 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory") |
|
|
641 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("") |
|
|
642 | #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ |
|
|
643 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") |
|
|
644 | #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \ |
|
|
645 | || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__ |
|
|
646 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") |
|
|
647 | #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \ |
|
|
648 | || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__ |
|
|
649 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") |
|
|
650 | #elif __aarch64__ |
|
|
651 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb ish" : : : "memory") |
|
|
652 | #elif (__sparc || __sparc__) && !__sparcv8 |
|
|
653 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory") |
|
|
654 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory") |
|
|
655 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore") |
|
|
656 | #elif defined __s390__ || defined __s390x__ |
|
|
657 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory") |
|
|
658 | #elif defined __mips__ |
|
|
659 | /* GNU/Linux emulates sync on mips1 architectures, so we force its use */ |
|
|
660 | /* anybody else who still uses mips1 is supposed to send in their version, with detection code. */ |
|
|
661 | #define ECB_MEMORY_FENCE __asm__ __volatile__ (".set mips2; sync; .set mips0" : : : "memory") |
|
|
662 | #elif defined __alpha__ |
|
|
663 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory") |
|
|
664 | #elif defined __hppa__ |
|
|
665 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory") |
|
|
666 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("") |
|
|
667 | #elif defined __ia64__ |
|
|
668 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mf" : : : "memory") |
|
|
669 | #elif defined __m68k__ |
|
|
670 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory") |
|
|
671 | #elif defined __m88k__ |
|
|
672 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("tb1 0,%%r0,128" : : : "memory") |
|
|
673 | #elif defined __sh__ |
|
|
674 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory") |
|
|
675 | #endif |
|
|
676 | #endif |
|
|
677 | #endif |
|
|
678 | |
|
|
679 | #ifndef ECB_MEMORY_FENCE |
|
|
680 | #if ECB_GCC_VERSION(4,7) |
|
|
681 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
|
|
682 | #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) |
|
|
683 | #define ECB_MEMORY_FENCE_ACQUIRE __atomic_thread_fence (__ATOMIC_ACQUIRE) |
|
|
684 | #define ECB_MEMORY_FENCE_RELEASE __atomic_thread_fence (__ATOMIC_RELEASE) |
|
|
685 | |
|
|
686 | /* The __has_feature syntax from clang is so misdesigned that we cannot use it |
|
|
687 | * without risking compile time errors with other compilers. We *could* |
|
|
688 | * define our own ecb_clang_has_feature, but I just can't be bothered to work |
|
|
689 | * around this shit time and again. |
|
|
690 | * #elif defined __clang && __has_feature (cxx_atomic) |
|
|
691 | * // see comment below (stdatomic.h) about the C11 memory model. |
|
|
692 | * #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) |
|
|
693 | * #define ECB_MEMORY_FENCE_ACQUIRE __c11_atomic_thread_fence (__ATOMIC_ACQUIRE) |
|
|
694 | * #define ECB_MEMORY_FENCE_RELEASE __c11_atomic_thread_fence (__ATOMIC_RELEASE) |
|
|
695 | */ |
|
|
696 | |
|
|
697 | #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
|
|
698 | #define ECB_MEMORY_FENCE __sync_synchronize () |
|
|
699 | #elif _MSC_VER >= 1500 /* VC++ 2008 */ |
|
|
700 | /* apparently, microsoft broke all the memory barrier stuff in Visual Studio 2008... */ |
|
|
701 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
|
|
702 | #define ECB_MEMORY_FENCE _ReadWriteBarrier (); MemoryBarrier() |
|
|
703 | #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier (); MemoryBarrier() /* according to msdn, _ReadBarrier is not a load fence */ |
|
|
704 | #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier (); MemoryBarrier() |
|
|
705 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
|
|
706 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
|
|
707 | #define ECB_MEMORY_FENCE _ReadWriteBarrier () |
|
|
708 | #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ |
|
|
709 | #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () |
|
|
710 | #elif defined _WIN32 |
|
|
711 | #include <WinNT.h> |
|
|
712 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
|
|
713 | #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
|
|
714 | #include <mbarrier.h> |
|
|
715 | #define ECB_MEMORY_FENCE __machine_rw_barrier () |
|
|
716 | #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier () |
|
|
717 | #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier () |
|
|
718 | #elif __xlC__ |
|
|
719 | #define ECB_MEMORY_FENCE __sync () |
|
|
720 | #endif |
|
|
721 | #endif |
|
|
722 | |
|
|
723 | #ifndef ECB_MEMORY_FENCE |
|
|
724 | #if ECB_C11 && !defined __STDC_NO_ATOMICS__ |
|
|
725 | /* we assume that these memory fences work on all variables/all memory accesses, */ |
|
|
726 | /* not just C11 atomics and atomic accesses */ |
|
|
727 | #include <stdatomic.h> |
|
|
728 | /* Unfortunately, neither gcc 4.7 nor clang 3.1 generate any instructions for */ |
|
|
729 | /* any fence other than seq_cst, which isn't very efficient for us. */ |
|
|
730 | /* Why that is, we don't know - either the C11 memory model is quite useless */ |
|
|
731 | /* for most usages, or gcc and clang have a bug */ |
|
|
732 | /* I *currently* lean towards the latter, and inefficiently implement */ |
|
|
733 | /* all three of ecb's fences as a seq_cst fence */ |
|
|
734 | /* Update, gcc-4.8 generates mfence for all c++ fences, but nothing */ |
|
|
735 | /* for all __atomic_thread_fence's except seq_cst */ |
|
|
736 | #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst) |
|
|
737 | #endif |
|
|
738 | #endif |
|
|
739 | |
|
|
740 | #ifndef ECB_MEMORY_FENCE |
|
|
741 | #if !ECB_AVOID_PTHREADS |
|
|
742 | /* |
|
|
743 | * if you get undefined symbol references to pthread_mutex_lock, |
|
|
744 | * or failure to find pthread.h, then you should implement |
|
|
745 | * the ECB_MEMORY_FENCE operations for your cpu/compiler |
|
|
746 | * OR provide pthread.h and link against the posix thread library |
|
|
747 | * of your system. |
|
|
748 | */ |
|
|
749 | #include <pthread.h> |
|
|
750 | #define ECB_NEEDS_PTHREADS 1 |
|
|
751 | #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1 |
|
|
752 | |
|
|
753 | static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; |
|
|
754 | #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) |
|
|
755 | #endif |
|
|
756 | #endif |
|
|
757 | |
|
|
758 | #if !defined ECB_MEMORY_FENCE_ACQUIRE && defined ECB_MEMORY_FENCE |
|
|
759 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
760 | #endif |
|
|
761 | |
|
|
762 | #if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE |
|
|
763 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
764 | #endif |
|
|
765 | |
|
|
766 | /*****************************************************************************/ |
|
|
767 | |
|
|
768 | #if __cplusplus |
|
|
769 | #define ecb_inline static inline |
|
|
770 | #elif ECB_GCC_VERSION(2,5) |
|
|
771 | #define ecb_inline static __inline__ |
|
|
772 | #elif ECB_C99 |
|
|
773 | #define ecb_inline static inline |
|
|
774 | #else |
|
|
775 | #define ecb_inline static |
|
|
776 | #endif |
|
|
777 | |
|
|
778 | #if ECB_GCC_VERSION(3,3) |
|
|
779 | #define ecb_restrict __restrict__ |
|
|
780 | #elif ECB_C99 |
|
|
781 | #define ecb_restrict restrict |
|
|
782 | #else |
|
|
783 | #define ecb_restrict |
|
|
784 | #endif |
|
|
785 | |
|
|
786 | typedef int ecb_bool; |
|
|
787 | |
|
|
788 | #define ECB_CONCAT_(a, b) a ## b |
|
|
789 | #define ECB_CONCAT(a, b) ECB_CONCAT_(a, b) |
|
|
790 | #define ECB_STRINGIFY_(a) # a |
|
|
791 | #define ECB_STRINGIFY(a) ECB_STRINGIFY_(a) |
|
|
792 | |
|
|
793 | #define ecb_function_ ecb_inline |
|
|
794 | |
|
|
795 | #if ECB_GCC_VERSION(3,1) |
|
|
796 | #define ecb_attribute(attrlist) __attribute__(attrlist) |
|
|
797 | #define ecb_is_constant(expr) __builtin_constant_p (expr) |
|
|
798 | #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
799 | #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
|
|
800 | #else |
|
|
801 | #define ecb_attribute(attrlist) |
|
|
802 | |
|
|
803 | /* possible C11 impl for integral types |
|
|
804 | typedef struct ecb_is_constant_struct ecb_is_constant_struct; |
|
|
805 | #define ecb_is_constant(expr) _Generic ((1 ? (struct ecb_is_constant_struct *)0 : (void *)((expr) - (expr)), ecb_is_constant_struct *: 0, default: 1)) */ |
|
|
806 | |
|
|
807 | #define ecb_is_constant(expr) 0 |
|
|
808 | #define ecb_expect(expr,value) (expr) |
|
|
809 | #define ecb_prefetch(addr,rw,locality) |
|
|
810 | #endif |
|
|
811 | |
|
|
812 | /* no emulation for ecb_decltype */ |
|
|
813 | #if ECB_GCC_VERSION(4,5) |
|
|
814 | #define ecb_decltype(x) __decltype(x) |
|
|
815 | #elif ECB_GCC_VERSION(3,0) |
|
|
816 | #define ecb_decltype(x) __typeof(x) |
|
|
817 | #endif |
|
|
818 | |
|
|
819 | #if _MSC_VER >= 1300 |
|
|
820 | #define ecb_deprecated __declspec(deprecated) |
|
|
821 | #else |
|
|
822 | #define ecb_deprecated ecb_attribute ((__deprecated__)) |
|
|
823 | #endif |
|
|
824 | |
|
|
825 | #define ecb_noinline ecb_attribute ((__noinline__)) |
|
|
826 | #define ecb_unused ecb_attribute ((__unused__)) |
|
|
827 | #define ecb_const ecb_attribute ((__const__)) |
|
|
828 | #define ecb_pure ecb_attribute ((__pure__)) |
|
|
829 | |
|
|
830 | /* http://msdn.microsoft.com/en-us/library/k6ktzx3s.aspx __declspec(noreturn) */ |
|
|
831 | #if ECB_C11 |
|
|
832 | #define ecb_noreturn _Noreturn |
|
|
833 | #else |
|
|
834 | #define ecb_noreturn ecb_attribute ((__noreturn__)) |
|
|
835 | #endif |
|
|
836 | |
|
|
837 | #if ECB_GCC_VERSION(4,3) |
|
|
838 | #define ecb_artificial ecb_attribute ((__artificial__)) |
|
|
839 | #define ecb_hot ecb_attribute ((__hot__)) |
|
|
840 | #define ecb_cold ecb_attribute ((__cold__)) |
|
|
841 | #else |
|
|
842 | #define ecb_artificial |
|
|
843 | #define ecb_hot |
|
|
844 | #define ecb_cold |
|
|
845 | #endif |
|
|
846 | |
|
|
847 | /* put around conditional expressions if you are very sure that the */ |
|
|
848 | /* expression is mostly true or mostly false. note that these return */ |
|
|
849 | /* booleans, not the expression. */ |
473 | #define expect_false(expr) expect ((expr) != 0, 0) |
850 | #define ecb_expect_false(expr) ecb_expect (!!(expr), 0) |
474 | #define expect_true(expr) expect ((expr) != 0, 1) |
851 | #define ecb_expect_true(expr) ecb_expect (!!(expr), 1) |
|
|
852 | /* for compatibility to the rest of the world */ |
|
|
853 | #define ecb_likely(expr) ecb_expect_true (expr) |
|
|
854 | #define ecb_unlikely(expr) ecb_expect_false (expr) |
|
|
855 | |
|
|
856 | /* count trailing zero bits and count # of one bits */ |
|
|
857 | #if ECB_GCC_VERSION(3,4) |
|
|
858 | /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */ |
|
|
859 | #define ecb_ld32(x) (__builtin_clz (x) ^ 31) |
|
|
860 | #define ecb_ld64(x) (__builtin_clzll (x) ^ 63) |
|
|
861 | #define ecb_ctz32(x) __builtin_ctz (x) |
|
|
862 | #define ecb_ctz64(x) __builtin_ctzll (x) |
|
|
863 | #define ecb_popcount32(x) __builtin_popcount (x) |
|
|
864 | /* no popcountll */ |
|
|
865 | #else |
|
|
866 | ecb_function_ int ecb_ctz32 (uint32_t x) ecb_const; |
|
|
867 | ecb_function_ int |
|
|
868 | ecb_ctz32 (uint32_t x) |
|
|
869 | { |
|
|
870 | int r = 0; |
|
|
871 | |
|
|
872 | x &= ~x + 1; /* this isolates the lowest bit */ |
|
|
873 | |
|
|
874 | #if ECB_branchless_on_i386 |
|
|
875 | r += !!(x & 0xaaaaaaaa) << 0; |
|
|
876 | r += !!(x & 0xcccccccc) << 1; |
|
|
877 | r += !!(x & 0xf0f0f0f0) << 2; |
|
|
878 | r += !!(x & 0xff00ff00) << 3; |
|
|
879 | r += !!(x & 0xffff0000) << 4; |
|
|
880 | #else |
|
|
881 | if (x & 0xaaaaaaaa) r += 1; |
|
|
882 | if (x & 0xcccccccc) r += 2; |
|
|
883 | if (x & 0xf0f0f0f0) r += 4; |
|
|
884 | if (x & 0xff00ff00) r += 8; |
|
|
885 | if (x & 0xffff0000) r += 16; |
|
|
886 | #endif |
|
|
887 | |
|
|
888 | return r; |
|
|
889 | } |
|
|
890 | |
|
|
891 | ecb_function_ int ecb_ctz64 (uint64_t x) ecb_const; |
|
|
892 | ecb_function_ int |
|
|
893 | ecb_ctz64 (uint64_t x) |
|
|
894 | { |
|
|
895 | int shift = x & 0xffffffffU ? 0 : 32; |
|
|
896 | return ecb_ctz32 (x >> shift) + shift; |
|
|
897 | } |
|
|
898 | |
|
|
899 | ecb_function_ int ecb_popcount32 (uint32_t x) ecb_const; |
|
|
900 | ecb_function_ int |
|
|
901 | ecb_popcount32 (uint32_t x) |
|
|
902 | { |
|
|
903 | x -= (x >> 1) & 0x55555555; |
|
|
904 | x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
|
|
905 | x = ((x >> 4) + x) & 0x0f0f0f0f; |
|
|
906 | x *= 0x01010101; |
|
|
907 | |
|
|
908 | return x >> 24; |
|
|
909 | } |
|
|
910 | |
|
|
911 | ecb_function_ int ecb_ld32 (uint32_t x) ecb_const; |
|
|
912 | ecb_function_ int ecb_ld32 (uint32_t x) |
|
|
913 | { |
|
|
914 | int r = 0; |
|
|
915 | |
|
|
916 | if (x >> 16) { x >>= 16; r += 16; } |
|
|
917 | if (x >> 8) { x >>= 8; r += 8; } |
|
|
918 | if (x >> 4) { x >>= 4; r += 4; } |
|
|
919 | if (x >> 2) { x >>= 2; r += 2; } |
|
|
920 | if (x >> 1) { r += 1; } |
|
|
921 | |
|
|
922 | return r; |
|
|
923 | } |
|
|
924 | |
|
|
925 | ecb_function_ int ecb_ld64 (uint64_t x) ecb_const; |
|
|
926 | ecb_function_ int ecb_ld64 (uint64_t x) |
|
|
927 | { |
|
|
928 | int r = 0; |
|
|
929 | |
|
|
930 | if (x >> 32) { x >>= 32; r += 32; } |
|
|
931 | |
|
|
932 | return r + ecb_ld32 (x); |
|
|
933 | } |
|
|
934 | #endif |
|
|
935 | |
|
|
936 | ecb_function_ ecb_bool ecb_is_pot32 (uint32_t x) ecb_const; |
|
|
937 | ecb_function_ ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); } |
|
|
938 | ecb_function_ ecb_bool ecb_is_pot64 (uint64_t x) ecb_const; |
|
|
939 | ecb_function_ ecb_bool ecb_is_pot64 (uint64_t x) { return !(x & (x - 1)); } |
|
|
940 | |
|
|
941 | ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const; |
|
|
942 | ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) |
|
|
943 | { |
|
|
944 | return ( (x * 0x0802U & 0x22110U) |
|
|
945 | | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16; |
|
|
946 | } |
|
|
947 | |
|
|
948 | ecb_function_ uint16_t ecb_bitrev16 (uint16_t x) ecb_const; |
|
|
949 | ecb_function_ uint16_t ecb_bitrev16 (uint16_t x) |
|
|
950 | { |
|
|
951 | x = ((x >> 1) & 0x5555) | ((x & 0x5555) << 1); |
|
|
952 | x = ((x >> 2) & 0x3333) | ((x & 0x3333) << 2); |
|
|
953 | x = ((x >> 4) & 0x0f0f) | ((x & 0x0f0f) << 4); |
|
|
954 | x = ( x >> 8 ) | ( x << 8); |
|
|
955 | |
|
|
956 | return x; |
|
|
957 | } |
|
|
958 | |
|
|
959 | ecb_function_ uint32_t ecb_bitrev32 (uint32_t x) ecb_const; |
|
|
960 | ecb_function_ uint32_t ecb_bitrev32 (uint32_t x) |
|
|
961 | { |
|
|
962 | x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1); |
|
|
963 | x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2); |
|
|
964 | x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4); |
|
|
965 | x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8); |
|
|
966 | x = ( x >> 16 ) | ( x << 16); |
|
|
967 | |
|
|
968 | return x; |
|
|
969 | } |
|
|
970 | |
|
|
971 | /* popcount64 is only available on 64 bit cpus as gcc builtin */ |
|
|
972 | /* so for this version we are lazy */ |
|
|
973 | ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const; |
|
|
974 | ecb_function_ int |
|
|
975 | ecb_popcount64 (uint64_t x) |
|
|
976 | { |
|
|
977 | return ecb_popcount32 (x) + ecb_popcount32 (x >> 32); |
|
|
978 | } |
|
|
979 | |
|
|
980 | ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) ecb_const; |
|
|
981 | ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) ecb_const; |
|
|
982 | ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) ecb_const; |
|
|
983 | ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) ecb_const; |
|
|
984 | ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const; |
|
|
985 | ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const; |
|
|
986 | ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) ecb_const; |
|
|
987 | ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) ecb_const; |
|
|
988 | |
|
|
989 | ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); } |
|
|
990 | ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); } |
|
|
991 | ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); } |
|
|
992 | ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); } |
|
|
993 | ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } |
|
|
994 | ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } |
|
|
995 | ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } |
|
|
996 | ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } |
|
|
997 | |
|
|
998 | #if ECB_GCC_VERSION(4,3) |
|
|
999 | #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) |
|
|
1000 | #define ecb_bswap32(x) __builtin_bswap32 (x) |
|
|
1001 | #define ecb_bswap64(x) __builtin_bswap64 (x) |
|
|
1002 | #else |
|
|
1003 | ecb_function_ uint16_t ecb_bswap16 (uint16_t x) ecb_const; |
|
|
1004 | ecb_function_ uint16_t |
|
|
1005 | ecb_bswap16 (uint16_t x) |
|
|
1006 | { |
|
|
1007 | return ecb_rotl16 (x, 8); |
|
|
1008 | } |
|
|
1009 | |
|
|
1010 | ecb_function_ uint32_t ecb_bswap32 (uint32_t x) ecb_const; |
|
|
1011 | ecb_function_ uint32_t |
|
|
1012 | ecb_bswap32 (uint32_t x) |
|
|
1013 | { |
|
|
1014 | return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16); |
|
|
1015 | } |
|
|
1016 | |
|
|
1017 | ecb_function_ uint64_t ecb_bswap64 (uint64_t x) ecb_const; |
|
|
1018 | ecb_function_ uint64_t |
|
|
1019 | ecb_bswap64 (uint64_t x) |
|
|
1020 | { |
|
|
1021 | return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32); |
|
|
1022 | } |
|
|
1023 | #endif |
|
|
1024 | |
|
|
1025 | #if ECB_GCC_VERSION(4,5) |
|
|
1026 | #define ecb_unreachable() __builtin_unreachable () |
|
|
1027 | #else |
|
|
1028 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
|
|
1029 | ecb_inline void ecb_unreachable (void) ecb_noreturn; |
|
|
1030 | ecb_inline void ecb_unreachable (void) { } |
|
|
1031 | #endif |
|
|
1032 | |
|
|
1033 | /* try to tell the compiler that some condition is definitely true */ |
|
|
1034 | #define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0 |
|
|
1035 | |
|
|
1036 | ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const; |
|
|
1037 | ecb_inline unsigned char |
|
|
1038 | ecb_byteorder_helper (void) |
|
|
1039 | { |
|
|
1040 | /* the union code still generates code under pressure in gcc, */ |
|
|
1041 | /* but less than using pointers, and always seems to */ |
|
|
1042 | /* successfully return a constant. */ |
|
|
1043 | /* the reason why we have this horrible preprocessor mess */ |
|
|
1044 | /* is to avoid it in all cases, at least on common architectures */ |
|
|
1045 | /* or when using a recent enough gcc version (>= 4.6) */ |
|
|
1046 | #if __i386 || __i386__ || _M_X86 || __amd64 || __amd64__ || _M_X64 |
|
|
1047 | return 0x44; |
|
|
1048 | #elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
|
|
1049 | return 0x44; |
|
|
1050 | #elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
|
|
1051 | return 0x11; |
|
|
1052 | #else |
|
|
1053 | union |
|
|
1054 | { |
|
|
1055 | uint32_t i; |
|
|
1056 | uint8_t c; |
|
|
1057 | } u = { 0x11223344 }; |
|
|
1058 | return u.c; |
|
|
1059 | #endif |
|
|
1060 | } |
|
|
1061 | |
|
|
1062 | ecb_inline ecb_bool ecb_big_endian (void) ecb_const; |
|
|
1063 | ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } |
|
|
1064 | ecb_inline ecb_bool ecb_little_endian (void) ecb_const; |
|
|
1065 | ecb_inline ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; } |
|
|
1066 | |
|
|
1067 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
|
|
1068 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
|
|
1069 | #else |
|
|
1070 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
|
|
1071 | #endif |
|
|
1072 | |
|
|
1073 | #if __cplusplus |
|
|
1074 | template<typename T> |
|
|
1075 | static inline T ecb_div_rd (T val, T div) |
|
|
1076 | { |
|
|
1077 | return val < 0 ? - ((-val + div - 1) / div) : (val ) / div; |
|
|
1078 | } |
|
|
1079 | template<typename T> |
|
|
1080 | static inline T ecb_div_ru (T val, T div) |
|
|
1081 | { |
|
|
1082 | return val < 0 ? - ((-val ) / div) : (val + div - 1) / div; |
|
|
1083 | } |
|
|
1084 | #else |
|
|
1085 | #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div)) |
|
|
1086 | #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div)) |
|
|
1087 | #endif |
|
|
1088 | |
|
|
1089 | #if ecb_cplusplus_does_not_suck |
|
|
1090 | /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ |
|
|
1091 | template<typename T, int N> |
|
|
1092 | static inline int ecb_array_length (const T (&arr)[N]) |
|
|
1093 | { |
|
|
1094 | return N; |
|
|
1095 | } |
|
|
1096 | #else |
|
|
1097 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
|
|
1098 | #endif |
|
|
1099 | |
|
|
1100 | /*******************************************************************************/ |
|
|
1101 | /* floating point stuff, can be disabled by defining ECB_NO_LIBM */ |
|
|
1102 | |
|
|
1103 | /* basically, everything uses "ieee pure-endian" floating point numbers */ |
|
|
1104 | /* the only noteworthy exception is ancient armle, which uses order 43218765 */ |
|
|
1105 | #if 0 \ |
|
|
1106 | || __i386 || __i386__ \ |
|
|
1107 | || __amd64 || __amd64__ || __x86_64 || __x86_64__ \ |
|
|
1108 | || __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ \ |
|
|
1109 | || defined __s390__ || defined __s390x__ \ |
|
|
1110 | || defined __mips__ \ |
|
|
1111 | || defined __alpha__ \ |
|
|
1112 | || defined __hppa__ \ |
|
|
1113 | || defined __ia64__ \ |
|
|
1114 | || defined __m68k__ \ |
|
|
1115 | || defined __m88k__ \ |
|
|
1116 | || defined __sh__ \ |
|
|
1117 | || defined _M_IX86 || defined _M_AMD64 || defined _M_IA64 \ |
|
|
1118 | || (defined __arm__ && (defined __ARM_EABI__ || defined __EABI__ || defined __VFP_FP__ || defined _WIN32_WCE || defined __ANDROID__)) \ |
|
|
1119 | || defined __aarch64__ |
|
|
1120 | #define ECB_STDFP 1 |
|
|
1121 | #include <string.h> /* for memcpy */ |
|
|
1122 | #else |
|
|
1123 | #define ECB_STDFP 0 |
|
|
1124 | #endif |
|
|
1125 | |
|
|
1126 | #ifndef ECB_NO_LIBM |
|
|
1127 | |
|
|
1128 | #include <math.h> /* for frexp*, ldexp*, INFINITY, NAN */ |
|
|
1129 | |
|
|
1130 | /* only the oldest of old doesn't have this one. solaris. */ |
|
|
1131 | #ifdef INFINITY |
|
|
1132 | #define ECB_INFINITY INFINITY |
|
|
1133 | #else |
|
|
1134 | #define ECB_INFINITY HUGE_VAL |
|
|
1135 | #endif |
|
|
1136 | |
|
|
1137 | #ifdef NAN |
|
|
1138 | #define ECB_NAN NAN |
|
|
1139 | #else |
|
|
1140 | #define ECB_NAN ECB_INFINITY |
|
|
1141 | #endif |
|
|
1142 | |
|
|
1143 | /* converts an ieee half/binary16 to a float */ |
|
|
1144 | ecb_function_ float ecb_binary16_to_float (uint16_t x) ecb_const; |
|
|
1145 | ecb_function_ float |
|
|
1146 | ecb_binary16_to_float (uint16_t x) |
|
|
1147 | { |
|
|
1148 | int e = (x >> 10) & 0x1f; |
|
|
1149 | int m = x & 0x3ff; |
|
|
1150 | float r; |
|
|
1151 | |
|
|
1152 | if (!e ) r = ldexpf (m , -24); |
|
|
1153 | else if (e != 31) r = ldexpf (m + 0x400, e - 25); |
|
|
1154 | else if (m ) r = ECB_NAN; |
|
|
1155 | else r = ECB_INFINITY; |
|
|
1156 | |
|
|
1157 | return x & 0x8000 ? -r : r; |
|
|
1158 | } |
|
|
1159 | |
|
|
1160 | /* convert a float to ieee single/binary32 */ |
|
|
1161 | ecb_function_ uint32_t ecb_float_to_binary32 (float x) ecb_const; |
|
|
1162 | ecb_function_ uint32_t |
|
|
1163 | ecb_float_to_binary32 (float x) |
|
|
1164 | { |
|
|
1165 | uint32_t r; |
|
|
1166 | |
|
|
1167 | #if ECB_STDFP |
|
|
1168 | memcpy (&r, &x, 4); |
|
|
1169 | #else |
|
|
1170 | /* slow emulation, works for anything but -0 */ |
|
|
1171 | uint32_t m; |
|
|
1172 | int e; |
|
|
1173 | |
|
|
1174 | if (x == 0e0f ) return 0x00000000U; |
|
|
1175 | if (x > +3.40282346638528860e+38f) return 0x7f800000U; |
|
|
1176 | if (x < -3.40282346638528860e+38f) return 0xff800000U; |
|
|
1177 | if (x != x ) return 0x7fbfffffU; |
|
|
1178 | |
|
|
1179 | m = frexpf (x, &e) * 0x1000000U; |
|
|
1180 | |
|
|
1181 | r = m & 0x80000000U; |
|
|
1182 | |
|
|
1183 | if (r) |
|
|
1184 | m = -m; |
|
|
1185 | |
|
|
1186 | if (e <= -126) |
|
|
1187 | { |
|
|
1188 | m &= 0xffffffU; |
|
|
1189 | m >>= (-125 - e); |
|
|
1190 | e = -126; |
|
|
1191 | } |
|
|
1192 | |
|
|
1193 | r |= (e + 126) << 23; |
|
|
1194 | r |= m & 0x7fffffU; |
|
|
1195 | #endif |
|
|
1196 | |
|
|
1197 | return r; |
|
|
1198 | } |
|
|
1199 | |
|
|
1200 | /* converts an ieee single/binary32 to a float */ |
|
|
1201 | ecb_function_ float ecb_binary32_to_float (uint32_t x) ecb_const; |
|
|
1202 | ecb_function_ float |
|
|
1203 | ecb_binary32_to_float (uint32_t x) |
|
|
1204 | { |
|
|
1205 | float r; |
|
|
1206 | |
|
|
1207 | #if ECB_STDFP |
|
|
1208 | memcpy (&r, &x, 4); |
|
|
1209 | #else |
|
|
1210 | /* emulation, only works for normals and subnormals and +0 */ |
|
|
1211 | int neg = x >> 31; |
|
|
1212 | int e = (x >> 23) & 0xffU; |
|
|
1213 | |
|
|
1214 | x &= 0x7fffffU; |
|
|
1215 | |
|
|
1216 | if (e) |
|
|
1217 | x |= 0x800000U; |
|
|
1218 | else |
|
|
1219 | e = 1; |
|
|
1220 | |
|
|
1221 | /* we distrust ldexpf a bit and do the 2**-24 scaling by an extra multiply */ |
|
|
1222 | r = ldexpf (x * (0.5f / 0x800000U), e - 126); |
|
|
1223 | |
|
|
1224 | r = neg ? -r : r; |
|
|
1225 | #endif |
|
|
1226 | |
|
|
1227 | return r; |
|
|
1228 | } |
|
|
1229 | |
|
|
1230 | /* convert a double to ieee double/binary64 */ |
|
|
1231 | ecb_function_ uint64_t ecb_double_to_binary64 (double x) ecb_const; |
|
|
1232 | ecb_function_ uint64_t |
|
|
1233 | ecb_double_to_binary64 (double x) |
|
|
1234 | { |
|
|
1235 | uint64_t r; |
|
|
1236 | |
|
|
1237 | #if ECB_STDFP |
|
|
1238 | memcpy (&r, &x, 8); |
|
|
1239 | #else |
|
|
1240 | /* slow emulation, works for anything but -0 */ |
|
|
1241 | uint64_t m; |
|
|
1242 | int e; |
|
|
1243 | |
|
|
1244 | if (x == 0e0 ) return 0x0000000000000000U; |
|
|
1245 | if (x > +1.79769313486231470e+308) return 0x7ff0000000000000U; |
|
|
1246 | if (x < -1.79769313486231470e+308) return 0xfff0000000000000U; |
|
|
1247 | if (x != x ) return 0X7ff7ffffffffffffU; |
|
|
1248 | |
|
|
1249 | m = frexp (x, &e) * 0x20000000000000U; |
|
|
1250 | |
|
|
1251 | r = m & 0x8000000000000000;; |
|
|
1252 | |
|
|
1253 | if (r) |
|
|
1254 | m = -m; |
|
|
1255 | |
|
|
1256 | if (e <= -1022) |
|
|
1257 | { |
|
|
1258 | m &= 0x1fffffffffffffU; |
|
|
1259 | m >>= (-1021 - e); |
|
|
1260 | e = -1022; |
|
|
1261 | } |
|
|
1262 | |
|
|
1263 | r |= ((uint64_t)(e + 1022)) << 52; |
|
|
1264 | r |= m & 0xfffffffffffffU; |
|
|
1265 | #endif |
|
|
1266 | |
|
|
1267 | return r; |
|
|
1268 | } |
|
|
1269 | |
|
|
1270 | /* converts an ieee double/binary64 to a double */ |
|
|
1271 | ecb_function_ double ecb_binary64_to_double (uint64_t x) ecb_const; |
|
|
1272 | ecb_function_ double |
|
|
1273 | ecb_binary64_to_double (uint64_t x) |
|
|
1274 | { |
|
|
1275 | double r; |
|
|
1276 | |
|
|
1277 | #if ECB_STDFP |
|
|
1278 | memcpy (&r, &x, 8); |
|
|
1279 | #else |
|
|
1280 | /* emulation, only works for normals and subnormals and +0 */ |
|
|
1281 | int neg = x >> 63; |
|
|
1282 | int e = (x >> 52) & 0x7ffU; |
|
|
1283 | |
|
|
1284 | x &= 0xfffffffffffffU; |
|
|
1285 | |
|
|
1286 | if (e) |
|
|
1287 | x |= 0x10000000000000U; |
|
|
1288 | else |
|
|
1289 | e = 1; |
|
|
1290 | |
|
|
1291 | /* we distrust ldexp a bit and do the 2**-53 scaling by an extra multiply */ |
|
|
1292 | r = ldexp (x * (0.5 / 0x10000000000000U), e - 1022); |
|
|
1293 | |
|
|
1294 | r = neg ? -r : r; |
|
|
1295 | #endif |
|
|
1296 | |
|
|
1297 | return r; |
|
|
1298 | } |
|
|
1299 | |
|
|
1300 | #endif |
|
|
1301 | |
|
|
1302 | #endif |
|
|
1303 | |
|
|
1304 | /* ECB.H END */ |
|
|
1305 | |
|
|
1306 | #if ECB_MEMORY_FENCE_NEEDS_PTHREADS |
|
|
1307 | /* if your architecture doesn't need memory fences, e.g. because it is |
|
|
1308 | * single-cpu/core, or if you use libev in a project that doesn't use libev |
|
|
1309 | * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling |
|
|
1310 | * libev, in which cases the memory fences become nops. |
|
|
1311 | * alternatively, you can remove this #error and link against libpthread, |
|
|
1312 | * which will then provide the memory fences. |
|
|
1313 | */ |
|
|
1314 | # error "memory fences not defined for your architecture, please report" |
|
|
1315 | #endif |
|
|
1316 | |
|
|
1317 | #ifndef ECB_MEMORY_FENCE |
|
|
1318 | # define ECB_MEMORY_FENCE do { } while (0) |
|
|
1319 | # define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
1320 | # define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
1321 | #endif |
|
|
1322 | |
|
|
1323 | #define expect_false(cond) ecb_expect_false (cond) |
|
|
1324 | #define expect_true(cond) ecb_expect_true (cond) |
|
|
1325 | #define noinline ecb_noinline |
|
|
1326 | |
475 | #define inline_size static inline |
1327 | #define inline_size ecb_inline |
476 | |
1328 | |
477 | #if EV_FEATURE_CODE |
1329 | #if EV_FEATURE_CODE |
478 | # define inline_speed static inline |
1330 | # define inline_speed ecb_inline |
479 | #else |
1331 | #else |
480 | # define inline_speed static noinline |
1332 | # define inline_speed static noinline |
481 | #endif |
1333 | #endif |
482 | |
1334 | |
483 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
1335 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
… | |
… | |
522 | # include "ev_win32.c" |
1374 | # include "ev_win32.c" |
523 | #endif |
1375 | #endif |
524 | |
1376 | |
525 | /*****************************************************************************/ |
1377 | /*****************************************************************************/ |
526 | |
1378 | |
|
|
1379 | /* define a suitable floor function (only used by periodics atm) */ |
|
|
1380 | |
|
|
1381 | #if EV_USE_FLOOR |
|
|
1382 | # include <math.h> |
|
|
1383 | # define ev_floor(v) floor (v) |
|
|
1384 | #else |
|
|
1385 | |
|
|
1386 | #include <float.h> |
|
|
1387 | |
|
|
1388 | /* a floor() replacement function, should be independent of ev_tstamp type */ |
|
|
1389 | static ev_tstamp noinline |
|
|
1390 | ev_floor (ev_tstamp v) |
|
|
1391 | { |
|
|
1392 | /* the choice of shift factor is not terribly important */ |
|
|
1393 | #if FLT_RADIX != 2 /* assume FLT_RADIX == 10 */ |
|
|
1394 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.; |
|
|
1395 | #else |
|
|
1396 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.; |
|
|
1397 | #endif |
|
|
1398 | |
|
|
1399 | /* argument too large for an unsigned long? */ |
|
|
1400 | if (expect_false (v >= shift)) |
|
|
1401 | { |
|
|
1402 | ev_tstamp f; |
|
|
1403 | |
|
|
1404 | if (v == v - 1.) |
|
|
1405 | return v; /* very large number */ |
|
|
1406 | |
|
|
1407 | f = shift * ev_floor (v * (1. / shift)); |
|
|
1408 | return f + ev_floor (v - f); |
|
|
1409 | } |
|
|
1410 | |
|
|
1411 | /* special treatment for negative args? */ |
|
|
1412 | if (expect_false (v < 0.)) |
|
|
1413 | { |
|
|
1414 | ev_tstamp f = -ev_floor (-v); |
|
|
1415 | |
|
|
1416 | return f - (f == v ? 0 : 1); |
|
|
1417 | } |
|
|
1418 | |
|
|
1419 | /* fits into an unsigned long */ |
|
|
1420 | return (unsigned long)v; |
|
|
1421 | } |
|
|
1422 | |
|
|
1423 | #endif |
|
|
1424 | |
|
|
1425 | /*****************************************************************************/ |
|
|
1426 | |
527 | #ifdef __linux |
1427 | #ifdef __linux |
528 | # include <sys/utsname.h> |
1428 | # include <sys/utsname.h> |
529 | #endif |
1429 | #endif |
530 | |
1430 | |
531 | static unsigned int noinline |
1431 | static unsigned int noinline ecb_cold |
532 | ev_linux_version (void) |
1432 | ev_linux_version (void) |
533 | { |
1433 | { |
534 | #ifdef __linux |
1434 | #ifdef __linux |
535 | unsigned int v = 0; |
1435 | unsigned int v = 0; |
536 | struct utsname buf; |
1436 | struct utsname buf; |
… | |
… | |
565 | } |
1465 | } |
566 | |
1466 | |
567 | /*****************************************************************************/ |
1467 | /*****************************************************************************/ |
568 | |
1468 | |
569 | #if EV_AVOID_STDIO |
1469 | #if EV_AVOID_STDIO |
570 | static void noinline |
1470 | static void noinline ecb_cold |
571 | ev_printerr (const char *msg) |
1471 | ev_printerr (const char *msg) |
572 | { |
1472 | { |
573 | write (STDERR_FILENO, msg, strlen (msg)); |
1473 | write (STDERR_FILENO, msg, strlen (msg)); |
574 | } |
1474 | } |
575 | #endif |
1475 | #endif |
576 | |
1476 | |
577 | static void (*syserr_cb)(const char *msg); |
1477 | static void (*syserr_cb)(const char *msg) EV_THROW; |
578 | |
1478 | |
579 | void |
1479 | void ecb_cold |
580 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
1480 | ev_set_syserr_cb (void (*cb)(const char *msg) EV_THROW) EV_THROW |
581 | { |
1481 | { |
582 | syserr_cb = cb; |
1482 | syserr_cb = cb; |
583 | } |
1483 | } |
584 | |
1484 | |
585 | static void noinline |
1485 | static void noinline ecb_cold |
586 | ev_syserr (const char *msg) |
1486 | ev_syserr (const char *msg) |
587 | { |
1487 | { |
588 | if (!msg) |
1488 | if (!msg) |
589 | msg = "(libev) system error"; |
1489 | msg = "(libev) system error"; |
590 | |
1490 | |
591 | if (syserr_cb) |
1491 | if (syserr_cb) |
592 | syserr_cb (msg); |
1492 | syserr_cb (msg); |
593 | else |
1493 | else |
594 | { |
1494 | { |
595 | #if EV_AVOID_STDIO |
1495 | #if EV_AVOID_STDIO |
596 | const char *err = strerror (errno); |
|
|
597 | |
|
|
598 | ev_printerr (msg); |
1496 | ev_printerr (msg); |
599 | ev_printerr (": "); |
1497 | ev_printerr (": "); |
600 | ev_printerr (err); |
1498 | ev_printerr (strerror (errno)); |
601 | ev_printerr ("\n"); |
1499 | ev_printerr ("\n"); |
602 | #else |
1500 | #else |
603 | perror (msg); |
1501 | perror (msg); |
604 | #endif |
1502 | #endif |
605 | abort (); |
1503 | abort (); |
606 | } |
1504 | } |
607 | } |
1505 | } |
608 | |
1506 | |
609 | static void * |
1507 | static void * |
610 | ev_realloc_emul (void *ptr, long size) |
1508 | ev_realloc_emul (void *ptr, long size) EV_THROW |
611 | { |
1509 | { |
612 | #if __GLIBC__ |
|
|
613 | return realloc (ptr, size); |
|
|
614 | #else |
|
|
615 | /* some systems, notably openbsd and darwin, fail to properly |
1510 | /* some systems, notably openbsd and darwin, fail to properly |
616 | * implement realloc (x, 0) (as required by both ansi c-89 and |
1511 | * implement realloc (x, 0) (as required by both ansi c-89 and |
617 | * the single unix specification, so work around them here. |
1512 | * the single unix specification, so work around them here. |
|
|
1513 | * recently, also (at least) fedora and debian started breaking it, |
|
|
1514 | * despite documenting it otherwise. |
618 | */ |
1515 | */ |
619 | |
1516 | |
620 | if (size) |
1517 | if (size) |
621 | return realloc (ptr, size); |
1518 | return realloc (ptr, size); |
622 | |
1519 | |
623 | free (ptr); |
1520 | free (ptr); |
624 | return 0; |
1521 | return 0; |
625 | #endif |
|
|
626 | } |
1522 | } |
627 | |
1523 | |
628 | static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; |
1524 | static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul; |
629 | |
1525 | |
630 | void |
1526 | void ecb_cold |
631 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
1527 | ev_set_allocator (void *(*cb)(void *ptr, long size) EV_THROW) EV_THROW |
632 | { |
1528 | { |
633 | alloc = cb; |
1529 | alloc = cb; |
634 | } |
1530 | } |
635 | |
1531 | |
636 | inline_speed void * |
1532 | inline_speed void * |
… | |
… | |
639 | ptr = alloc (ptr, size); |
1535 | ptr = alloc (ptr, size); |
640 | |
1536 | |
641 | if (!ptr && size) |
1537 | if (!ptr && size) |
642 | { |
1538 | { |
643 | #if EV_AVOID_STDIO |
1539 | #if EV_AVOID_STDIO |
644 | ev_printerr ("libev: memory allocation failed, aborting.\n"); |
1540 | ev_printerr ("(libev) memory allocation failed, aborting.\n"); |
645 | #else |
1541 | #else |
646 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
1542 | fprintf (stderr, "(libev) cannot allocate %ld bytes, aborting.", size); |
647 | #endif |
1543 | #endif |
648 | abort (); |
1544 | abort (); |
649 | } |
1545 | } |
650 | |
1546 | |
651 | return ptr; |
1547 | return ptr; |
… | |
… | |
724 | #undef VAR |
1620 | #undef VAR |
725 | }; |
1621 | }; |
726 | #include "ev_wrap.h" |
1622 | #include "ev_wrap.h" |
727 | |
1623 | |
728 | static struct ev_loop default_loop_struct; |
1624 | static struct ev_loop default_loop_struct; |
729 | struct ev_loop *ev_default_loop_ptr; |
1625 | EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */ |
730 | |
1626 | |
731 | #else |
1627 | #else |
732 | |
1628 | |
733 | ev_tstamp ev_rt_now; |
1629 | EV_API_DECL ev_tstamp ev_rt_now = 0; /* needs to be initialised to make it a definition despite extern */ |
734 | #define VAR(name,decl) static decl; |
1630 | #define VAR(name,decl) static decl; |
735 | #include "ev_vars.h" |
1631 | #include "ev_vars.h" |
736 | #undef VAR |
1632 | #undef VAR |
737 | |
1633 | |
738 | static int ev_default_loop_ptr; |
1634 | static int ev_default_loop_ptr; |
… | |
… | |
753 | |
1649 | |
754 | /*****************************************************************************/ |
1650 | /*****************************************************************************/ |
755 | |
1651 | |
756 | #ifndef EV_HAVE_EV_TIME |
1652 | #ifndef EV_HAVE_EV_TIME |
757 | ev_tstamp |
1653 | ev_tstamp |
758 | ev_time (void) |
1654 | ev_time (void) EV_THROW |
759 | { |
1655 | { |
760 | #if EV_USE_REALTIME |
1656 | #if EV_USE_REALTIME |
761 | if (expect_true (have_realtime)) |
1657 | if (expect_true (have_realtime)) |
762 | { |
1658 | { |
763 | struct timespec ts; |
1659 | struct timespec ts; |
… | |
… | |
787 | return ev_time (); |
1683 | return ev_time (); |
788 | } |
1684 | } |
789 | |
1685 | |
790 | #if EV_MULTIPLICITY |
1686 | #if EV_MULTIPLICITY |
791 | ev_tstamp |
1687 | ev_tstamp |
792 | ev_now (EV_P) |
1688 | ev_now (EV_P) EV_THROW |
793 | { |
1689 | { |
794 | return ev_rt_now; |
1690 | return ev_rt_now; |
795 | } |
1691 | } |
796 | #endif |
1692 | #endif |
797 | |
1693 | |
798 | void |
1694 | void |
799 | ev_sleep (ev_tstamp delay) |
1695 | ev_sleep (ev_tstamp delay) EV_THROW |
800 | { |
1696 | { |
801 | if (delay > 0.) |
1697 | if (delay > 0.) |
802 | { |
1698 | { |
803 | #if EV_USE_NANOSLEEP |
1699 | #if EV_USE_NANOSLEEP |
804 | struct timespec ts; |
1700 | struct timespec ts; |
805 | |
1701 | |
806 | EV_TS_SET (ts, delay); |
1702 | EV_TS_SET (ts, delay); |
807 | nanosleep (&ts, 0); |
1703 | nanosleep (&ts, 0); |
808 | #elif defined(_WIN32) |
1704 | #elif defined _WIN32 |
809 | Sleep ((unsigned long)(delay * 1e3)); |
1705 | Sleep ((unsigned long)(delay * 1e3)); |
810 | #else |
1706 | #else |
811 | struct timeval tv; |
1707 | struct timeval tv; |
812 | |
1708 | |
813 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
1709 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
… | |
… | |
832 | |
1728 | |
833 | do |
1729 | do |
834 | ncur <<= 1; |
1730 | ncur <<= 1; |
835 | while (cnt > ncur); |
1731 | while (cnt > ncur); |
836 | |
1732 | |
837 | /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */ |
1733 | /* if size is large, round to MALLOC_ROUND - 4 * longs to accommodate malloc overhead */ |
838 | if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) |
1734 | if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) |
839 | { |
1735 | { |
840 | ncur *= elem; |
1736 | ncur *= elem; |
841 | ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1); |
1737 | ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1); |
842 | ncur = ncur - sizeof (void *) * 4; |
1738 | ncur = ncur - sizeof (void *) * 4; |
… | |
… | |
844 | } |
1740 | } |
845 | |
1741 | |
846 | return ncur; |
1742 | return ncur; |
847 | } |
1743 | } |
848 | |
1744 | |
849 | static noinline void * |
1745 | static void * noinline ecb_cold |
850 | array_realloc (int elem, void *base, int *cur, int cnt) |
1746 | array_realloc (int elem, void *base, int *cur, int cnt) |
851 | { |
1747 | { |
852 | *cur = array_nextsize (elem, *cur, cnt); |
1748 | *cur = array_nextsize (elem, *cur, cnt); |
853 | return ev_realloc (base, elem * *cur); |
1749 | return ev_realloc (base, elem * *cur); |
854 | } |
1750 | } |
… | |
… | |
857 | memset ((void *)(base), 0, sizeof (*(base)) * (count)) |
1753 | memset ((void *)(base), 0, sizeof (*(base)) * (count)) |
858 | |
1754 | |
859 | #define array_needsize(type,base,cur,cnt,init) \ |
1755 | #define array_needsize(type,base,cur,cnt,init) \ |
860 | if (expect_false ((cnt) > (cur))) \ |
1756 | if (expect_false ((cnt) > (cur))) \ |
861 | { \ |
1757 | { \ |
862 | int ocur_ = (cur); \ |
1758 | int ecb_unused ocur_ = (cur); \ |
863 | (base) = (type *)array_realloc \ |
1759 | (base) = (type *)array_realloc \ |
864 | (sizeof (type), (base), &(cur), (cnt)); \ |
1760 | (sizeof (type), (base), &(cur), (cnt)); \ |
865 | init ((base) + (ocur_), (cur) - ocur_); \ |
1761 | init ((base) + (ocur_), (cur) - ocur_); \ |
866 | } |
1762 | } |
867 | |
1763 | |
… | |
… | |
885 | pendingcb (EV_P_ ev_prepare *w, int revents) |
1781 | pendingcb (EV_P_ ev_prepare *w, int revents) |
886 | { |
1782 | { |
887 | } |
1783 | } |
888 | |
1784 | |
889 | void noinline |
1785 | void noinline |
890 | ev_feed_event (EV_P_ void *w, int revents) |
1786 | ev_feed_event (EV_P_ void *w, int revents) EV_THROW |
891 | { |
1787 | { |
892 | W w_ = (W)w; |
1788 | W w_ = (W)w; |
893 | int pri = ABSPRI (w_); |
1789 | int pri = ABSPRI (w_); |
894 | |
1790 | |
895 | if (expect_false (w_->pending)) |
1791 | if (expect_false (w_->pending)) |
… | |
… | |
899 | w_->pending = ++pendingcnt [pri]; |
1795 | w_->pending = ++pendingcnt [pri]; |
900 | array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); |
1796 | array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); |
901 | pendings [pri][w_->pending - 1].w = w_; |
1797 | pendings [pri][w_->pending - 1].w = w_; |
902 | pendings [pri][w_->pending - 1].events = revents; |
1798 | pendings [pri][w_->pending - 1].events = revents; |
903 | } |
1799 | } |
|
|
1800 | |
|
|
1801 | pendingpri = NUMPRI - 1; |
904 | } |
1802 | } |
905 | |
1803 | |
906 | inline_speed void |
1804 | inline_speed void |
907 | feed_reverse (EV_P_ W w) |
1805 | feed_reverse (EV_P_ W w) |
908 | { |
1806 | { |
… | |
… | |
954 | if (expect_true (!anfd->reify)) |
1852 | if (expect_true (!anfd->reify)) |
955 | fd_event_nocheck (EV_A_ fd, revents); |
1853 | fd_event_nocheck (EV_A_ fd, revents); |
956 | } |
1854 | } |
957 | |
1855 | |
958 | void |
1856 | void |
959 | ev_feed_fd_event (EV_P_ int fd, int revents) |
1857 | ev_feed_fd_event (EV_P_ int fd, int revents) EV_THROW |
960 | { |
1858 | { |
961 | if (fd >= 0 && fd < anfdmax) |
1859 | if (fd >= 0 && fd < anfdmax) |
962 | fd_event_nocheck (EV_A_ fd, revents); |
1860 | fd_event_nocheck (EV_A_ fd, revents); |
963 | } |
1861 | } |
964 | |
1862 | |
… | |
… | |
967 | inline_size void |
1865 | inline_size void |
968 | fd_reify (EV_P) |
1866 | fd_reify (EV_P) |
969 | { |
1867 | { |
970 | int i; |
1868 | int i; |
971 | |
1869 | |
|
|
1870 | #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP |
|
|
1871 | for (i = 0; i < fdchangecnt; ++i) |
|
|
1872 | { |
|
|
1873 | int fd = fdchanges [i]; |
|
|
1874 | ANFD *anfd = anfds + fd; |
|
|
1875 | |
|
|
1876 | if (anfd->reify & EV__IOFDSET && anfd->head) |
|
|
1877 | { |
|
|
1878 | SOCKET handle = EV_FD_TO_WIN32_HANDLE (fd); |
|
|
1879 | |
|
|
1880 | if (handle != anfd->handle) |
|
|
1881 | { |
|
|
1882 | unsigned long arg; |
|
|
1883 | |
|
|
1884 | assert (("libev: only socket fds supported in this configuration", ioctlsocket (handle, FIONREAD, &arg) == 0)); |
|
|
1885 | |
|
|
1886 | /* handle changed, but fd didn't - we need to do it in two steps */ |
|
|
1887 | backend_modify (EV_A_ fd, anfd->events, 0); |
|
|
1888 | anfd->events = 0; |
|
|
1889 | anfd->handle = handle; |
|
|
1890 | } |
|
|
1891 | } |
|
|
1892 | } |
|
|
1893 | #endif |
|
|
1894 | |
972 | for (i = 0; i < fdchangecnt; ++i) |
1895 | for (i = 0; i < fdchangecnt; ++i) |
973 | { |
1896 | { |
974 | int fd = fdchanges [i]; |
1897 | int fd = fdchanges [i]; |
975 | ANFD *anfd = anfds + fd; |
1898 | ANFD *anfd = anfds + fd; |
976 | ev_io *w; |
1899 | ev_io *w; |
… | |
… | |
978 | unsigned char o_events = anfd->events; |
1901 | unsigned char o_events = anfd->events; |
979 | unsigned char o_reify = anfd->reify; |
1902 | unsigned char o_reify = anfd->reify; |
980 | |
1903 | |
981 | anfd->reify = 0; |
1904 | anfd->reify = 0; |
982 | |
1905 | |
983 | #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP |
|
|
984 | if (o_reify & EV__IOFDSET) |
|
|
985 | { |
|
|
986 | unsigned long arg; |
|
|
987 | anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); |
|
|
988 | assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); |
|
|
989 | printf ("oi %d %x\n", fd, anfd->handle);//D |
|
|
990 | } |
|
|
991 | #endif |
|
|
992 | |
|
|
993 | /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */ |
1906 | /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */ |
994 | { |
1907 | { |
995 | anfd->events = 0; |
1908 | anfd->events = 0; |
996 | |
1909 | |
997 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
1910 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
… | |
… | |
1022 | fdchanges [fdchangecnt - 1] = fd; |
1935 | fdchanges [fdchangecnt - 1] = fd; |
1023 | } |
1936 | } |
1024 | } |
1937 | } |
1025 | |
1938 | |
1026 | /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ |
1939 | /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ |
1027 | inline_speed void |
1940 | inline_speed void ecb_cold |
1028 | fd_kill (EV_P_ int fd) |
1941 | fd_kill (EV_P_ int fd) |
1029 | { |
1942 | { |
1030 | ev_io *w; |
1943 | ev_io *w; |
1031 | |
1944 | |
1032 | while ((w = (ev_io *)anfds [fd].head)) |
1945 | while ((w = (ev_io *)anfds [fd].head)) |
… | |
… | |
1035 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
1948 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
1036 | } |
1949 | } |
1037 | } |
1950 | } |
1038 | |
1951 | |
1039 | /* check whether the given fd is actually valid, for error recovery */ |
1952 | /* check whether the given fd is actually valid, for error recovery */ |
1040 | inline_size int |
1953 | inline_size int ecb_cold |
1041 | fd_valid (int fd) |
1954 | fd_valid (int fd) |
1042 | { |
1955 | { |
1043 | #ifdef _WIN32 |
1956 | #ifdef _WIN32 |
1044 | return EV_FD_TO_WIN32_HANDLE (fd) != -1; |
1957 | return EV_FD_TO_WIN32_HANDLE (fd) != -1; |
1045 | #else |
1958 | #else |
1046 | return fcntl (fd, F_GETFD) != -1; |
1959 | return fcntl (fd, F_GETFD) != -1; |
1047 | #endif |
1960 | #endif |
1048 | } |
1961 | } |
1049 | |
1962 | |
1050 | /* called on EBADF to verify fds */ |
1963 | /* called on EBADF to verify fds */ |
1051 | static void noinline |
1964 | static void noinline ecb_cold |
1052 | fd_ebadf (EV_P) |
1965 | fd_ebadf (EV_P) |
1053 | { |
1966 | { |
1054 | int fd; |
1967 | int fd; |
1055 | |
1968 | |
1056 | for (fd = 0; fd < anfdmax; ++fd) |
1969 | for (fd = 0; fd < anfdmax; ++fd) |
… | |
… | |
1058 | if (!fd_valid (fd) && errno == EBADF) |
1971 | if (!fd_valid (fd) && errno == EBADF) |
1059 | fd_kill (EV_A_ fd); |
1972 | fd_kill (EV_A_ fd); |
1060 | } |
1973 | } |
1061 | |
1974 | |
1062 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
1975 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
1063 | static void noinline |
1976 | static void noinline ecb_cold |
1064 | fd_enomem (EV_P) |
1977 | fd_enomem (EV_P) |
1065 | { |
1978 | { |
1066 | int fd; |
1979 | int fd; |
1067 | |
1980 | |
1068 | for (fd = anfdmax; fd--; ) |
1981 | for (fd = anfdmax; fd--; ) |
… | |
… | |
1263 | |
2176 | |
1264 | /*****************************************************************************/ |
2177 | /*****************************************************************************/ |
1265 | |
2178 | |
1266 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
2179 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
1267 | |
2180 | |
1268 | static void noinline |
2181 | static void noinline ecb_cold |
1269 | evpipe_init (EV_P) |
2182 | evpipe_init (EV_P) |
1270 | { |
2183 | { |
1271 | if (!ev_is_active (&pipe_w)) |
2184 | if (!ev_is_active (&pipe_w)) |
1272 | { |
2185 | { |
|
|
2186 | int fds [2]; |
|
|
2187 | |
1273 | # if EV_USE_EVENTFD |
2188 | # if EV_USE_EVENTFD |
|
|
2189 | fds [0] = -1; |
1274 | evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); |
2190 | fds [1] = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); |
1275 | if (evfd < 0 && errno == EINVAL) |
2191 | if (fds [1] < 0 && errno == EINVAL) |
1276 | evfd = eventfd (0, 0); |
2192 | fds [1] = eventfd (0, 0); |
1277 | |
2193 | |
1278 | if (evfd >= 0) |
2194 | if (fds [1] < 0) |
|
|
2195 | # endif |
1279 | { |
2196 | { |
|
|
2197 | while (pipe (fds)) |
|
|
2198 | ev_syserr ("(libev) error creating signal/async pipe"); |
|
|
2199 | |
|
|
2200 | fd_intern (fds [0]); |
|
|
2201 | } |
|
|
2202 | |
1280 | evpipe [0] = -1; |
2203 | evpipe [0] = fds [0]; |
1281 | fd_intern (evfd); /* doing it twice doesn't hurt */ |
2204 | |
1282 | ev_io_set (&pipe_w, evfd, EV_READ); |
2205 | if (evpipe [1] < 0) |
|
|
2206 | evpipe [1] = fds [1]; /* first call, set write fd */ |
|
|
2207 | else |
|
|
2208 | { |
|
|
2209 | /* on subsequent calls, do not change evpipe [1] */ |
|
|
2210 | /* so that evpipe_write can always rely on its value. */ |
|
|
2211 | /* this branch does not do anything sensible on windows, */ |
|
|
2212 | /* so must not be executed on windows */ |
|
|
2213 | |
|
|
2214 | dup2 (fds [1], evpipe [1]); |
|
|
2215 | close (fds [1]); |
|
|
2216 | } |
|
|
2217 | |
|
|
2218 | fd_intern (evpipe [1]); |
|
|
2219 | |
|
|
2220 | ev_io_set (&pipe_w, evpipe [0] < 0 ? evpipe [1] : evpipe [0], EV_READ); |
|
|
2221 | ev_io_start (EV_A_ &pipe_w); |
|
|
2222 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
|
|
2223 | } |
|
|
2224 | } |
|
|
2225 | |
|
|
2226 | inline_speed void |
|
|
2227 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
|
|
2228 | { |
|
|
2229 | ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */ |
|
|
2230 | |
|
|
2231 | if (expect_true (*flag)) |
|
|
2232 | return; |
|
|
2233 | |
|
|
2234 | *flag = 1; |
|
|
2235 | ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */ |
|
|
2236 | |
|
|
2237 | pipe_write_skipped = 1; |
|
|
2238 | |
|
|
2239 | ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */ |
|
|
2240 | |
|
|
2241 | if (pipe_write_wanted) |
|
|
2242 | { |
|
|
2243 | int old_errno; |
|
|
2244 | |
|
|
2245 | pipe_write_skipped = 0; |
|
|
2246 | ECB_MEMORY_FENCE_RELEASE; |
|
|
2247 | |
|
|
2248 | old_errno = errno; /* save errno because write will clobber it */ |
|
|
2249 | |
|
|
2250 | #if EV_USE_EVENTFD |
|
|
2251 | if (evpipe [0] < 0) |
|
|
2252 | { |
|
|
2253 | uint64_t counter = 1; |
|
|
2254 | write (evpipe [1], &counter, sizeof (uint64_t)); |
1283 | } |
2255 | } |
1284 | else |
2256 | else |
1285 | # endif |
2257 | #endif |
1286 | { |
2258 | { |
1287 | while (pipe (evpipe)) |
2259 | #ifdef _WIN32 |
1288 | ev_syserr ("(libev) error creating signal/async pipe"); |
2260 | WSABUF buf; |
1289 | |
2261 | DWORD sent; |
1290 | fd_intern (evpipe [0]); |
2262 | buf.buf = &buf; |
1291 | fd_intern (evpipe [1]); |
2263 | buf.len = 1; |
1292 | ev_io_set (&pipe_w, evpipe [0], EV_READ); |
2264 | WSASend (EV_FD_TO_WIN32_HANDLE (evpipe [1]), &buf, 1, &sent, 0, 0, 0); |
|
|
2265 | #else |
|
|
2266 | write (evpipe [1], &(evpipe [1]), 1); |
|
|
2267 | #endif |
1293 | } |
2268 | } |
1294 | |
|
|
1295 | ev_io_start (EV_A_ &pipe_w); |
|
|
1296 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
|
|
1297 | } |
|
|
1298 | } |
|
|
1299 | |
|
|
1300 | inline_size void |
|
|
1301 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
|
|
1302 | { |
|
|
1303 | if (!*flag) |
|
|
1304 | { |
|
|
1305 | int old_errno = errno; /* save errno because write might clobber it */ |
|
|
1306 | char dummy; |
|
|
1307 | |
|
|
1308 | *flag = 1; |
|
|
1309 | |
|
|
1310 | #if EV_USE_EVENTFD |
|
|
1311 | if (evfd >= 0) |
|
|
1312 | { |
|
|
1313 | uint64_t counter = 1; |
|
|
1314 | write (evfd, &counter, sizeof (uint64_t)); |
|
|
1315 | } |
|
|
1316 | else |
|
|
1317 | #endif |
|
|
1318 | /* win32 people keep sending patches that change this write() to send() */ |
|
|
1319 | /* and then run away. but send() is wrong, it wants a socket handle on win32 */ |
|
|
1320 | /* so when you think this write should be a send instead, please find out */ |
|
|
1321 | /* where your send() is from - it's definitely not the microsoft send, and */ |
|
|
1322 | /* tell me. thank you. */ |
|
|
1323 | write (evpipe [1], &dummy, 1); |
|
|
1324 | |
2269 | |
1325 | errno = old_errno; |
2270 | errno = old_errno; |
1326 | } |
2271 | } |
1327 | } |
2272 | } |
1328 | |
2273 | |
… | |
… | |
1331 | static void |
2276 | static void |
1332 | pipecb (EV_P_ ev_io *iow, int revents) |
2277 | pipecb (EV_P_ ev_io *iow, int revents) |
1333 | { |
2278 | { |
1334 | int i; |
2279 | int i; |
1335 | |
2280 | |
|
|
2281 | if (revents & EV_READ) |
|
|
2282 | { |
1336 | #if EV_USE_EVENTFD |
2283 | #if EV_USE_EVENTFD |
1337 | if (evfd >= 0) |
2284 | if (evpipe [0] < 0) |
1338 | { |
2285 | { |
1339 | uint64_t counter; |
2286 | uint64_t counter; |
1340 | read (evfd, &counter, sizeof (uint64_t)); |
2287 | read (evpipe [1], &counter, sizeof (uint64_t)); |
1341 | } |
2288 | } |
1342 | else |
2289 | else |
1343 | #endif |
2290 | #endif |
1344 | { |
2291 | { |
1345 | char dummy; |
2292 | char dummy[4]; |
1346 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
2293 | #ifdef _WIN32 |
|
|
2294 | WSABUF buf; |
|
|
2295 | DWORD recvd; |
|
|
2296 | DWORD flags = 0; |
|
|
2297 | buf.buf = dummy; |
|
|
2298 | buf.len = sizeof (dummy); |
|
|
2299 | WSARecv (EV_FD_TO_WIN32_HANDLE (evpipe [0]), &buf, 1, &recvd, &flags, 0, 0); |
|
|
2300 | #else |
1347 | read (evpipe [0], &dummy, 1); |
2301 | read (evpipe [0], &dummy, sizeof (dummy)); |
|
|
2302 | #endif |
|
|
2303 | } |
1348 | } |
2304 | } |
1349 | |
2305 | |
|
|
2306 | pipe_write_skipped = 0; |
|
|
2307 | |
|
|
2308 | ECB_MEMORY_FENCE; /* push out skipped, acquire flags */ |
|
|
2309 | |
|
|
2310 | #if EV_SIGNAL_ENABLE |
1350 | if (sig_pending) |
2311 | if (sig_pending) |
1351 | { |
2312 | { |
1352 | sig_pending = 0; |
2313 | sig_pending = 0; |
|
|
2314 | |
|
|
2315 | ECB_MEMORY_FENCE; |
1353 | |
2316 | |
1354 | for (i = EV_NSIG - 1; i--; ) |
2317 | for (i = EV_NSIG - 1; i--; ) |
1355 | if (expect_false (signals [i].pending)) |
2318 | if (expect_false (signals [i].pending)) |
1356 | ev_feed_signal_event (EV_A_ i + 1); |
2319 | ev_feed_signal_event (EV_A_ i + 1); |
1357 | } |
2320 | } |
|
|
2321 | #endif |
1358 | |
2322 | |
1359 | #if EV_ASYNC_ENABLE |
2323 | #if EV_ASYNC_ENABLE |
1360 | if (async_pending) |
2324 | if (async_pending) |
1361 | { |
2325 | { |
1362 | async_pending = 0; |
2326 | async_pending = 0; |
|
|
2327 | |
|
|
2328 | ECB_MEMORY_FENCE; |
1363 | |
2329 | |
1364 | for (i = asynccnt; i--; ) |
2330 | for (i = asynccnt; i--; ) |
1365 | if (asyncs [i]->sent) |
2331 | if (asyncs [i]->sent) |
1366 | { |
2332 | { |
1367 | asyncs [i]->sent = 0; |
2333 | asyncs [i]->sent = 0; |
|
|
2334 | ECB_MEMORY_FENCE_RELEASE; |
1368 | ev_feed_event (EV_A_ asyncs [i], EV_ASYNC); |
2335 | ev_feed_event (EV_A_ asyncs [i], EV_ASYNC); |
1369 | } |
2336 | } |
1370 | } |
2337 | } |
1371 | #endif |
2338 | #endif |
1372 | } |
2339 | } |
1373 | |
2340 | |
1374 | /*****************************************************************************/ |
2341 | /*****************************************************************************/ |
1375 | |
2342 | |
|
|
2343 | void |
|
|
2344 | ev_feed_signal (int signum) EV_THROW |
|
|
2345 | { |
|
|
2346 | #if EV_MULTIPLICITY |
|
|
2347 | EV_P; |
|
|
2348 | ECB_MEMORY_FENCE_ACQUIRE; |
|
|
2349 | EV_A = signals [signum - 1].loop; |
|
|
2350 | |
|
|
2351 | if (!EV_A) |
|
|
2352 | return; |
|
|
2353 | #endif |
|
|
2354 | |
|
|
2355 | signals [signum - 1].pending = 1; |
|
|
2356 | evpipe_write (EV_A_ &sig_pending); |
|
|
2357 | } |
|
|
2358 | |
1376 | static void |
2359 | static void |
1377 | ev_sighandler (int signum) |
2360 | ev_sighandler (int signum) |
1378 | { |
2361 | { |
1379 | #if EV_MULTIPLICITY |
|
|
1380 | EV_P = signals [signum - 1].loop; |
|
|
1381 | #endif |
|
|
1382 | |
|
|
1383 | #ifdef _WIN32 |
2362 | #ifdef _WIN32 |
1384 | signal (signum, ev_sighandler); |
2363 | signal (signum, ev_sighandler); |
1385 | #endif |
2364 | #endif |
1386 | |
2365 | |
1387 | signals [signum - 1].pending = 1; |
2366 | ev_feed_signal (signum); |
1388 | evpipe_write (EV_A_ &sig_pending); |
|
|
1389 | } |
2367 | } |
1390 | |
2368 | |
1391 | void noinline |
2369 | void noinline |
1392 | ev_feed_signal_event (EV_P_ int signum) |
2370 | ev_feed_signal_event (EV_P_ int signum) EV_THROW |
1393 | { |
2371 | { |
1394 | WL w; |
2372 | WL w; |
1395 | |
2373 | |
1396 | if (expect_false (signum <= 0 || signum > EV_NSIG)) |
2374 | if (expect_false (signum <= 0 || signum >= EV_NSIG)) |
1397 | return; |
2375 | return; |
1398 | |
2376 | |
1399 | --signum; |
2377 | --signum; |
1400 | |
2378 | |
1401 | #if EV_MULTIPLICITY |
2379 | #if EV_MULTIPLICITY |
… | |
… | |
1405 | if (expect_false (signals [signum].loop != EV_A)) |
2383 | if (expect_false (signals [signum].loop != EV_A)) |
1406 | return; |
2384 | return; |
1407 | #endif |
2385 | #endif |
1408 | |
2386 | |
1409 | signals [signum].pending = 0; |
2387 | signals [signum].pending = 0; |
|
|
2388 | ECB_MEMORY_FENCE_RELEASE; |
1410 | |
2389 | |
1411 | for (w = signals [signum].head; w; w = w->next) |
2390 | for (w = signals [signum].head; w; w = w->next) |
1412 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
2391 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
1413 | } |
2392 | } |
1414 | |
2393 | |
… | |
… | |
1512 | #endif |
2491 | #endif |
1513 | #if EV_USE_SELECT |
2492 | #if EV_USE_SELECT |
1514 | # include "ev_select.c" |
2493 | # include "ev_select.c" |
1515 | #endif |
2494 | #endif |
1516 | |
2495 | |
1517 | int |
2496 | int ecb_cold |
1518 | ev_version_major (void) |
2497 | ev_version_major (void) EV_THROW |
1519 | { |
2498 | { |
1520 | return EV_VERSION_MAJOR; |
2499 | return EV_VERSION_MAJOR; |
1521 | } |
2500 | } |
1522 | |
2501 | |
1523 | int |
2502 | int ecb_cold |
1524 | ev_version_minor (void) |
2503 | ev_version_minor (void) EV_THROW |
1525 | { |
2504 | { |
1526 | return EV_VERSION_MINOR; |
2505 | return EV_VERSION_MINOR; |
1527 | } |
2506 | } |
1528 | |
2507 | |
1529 | /* return true if we are running with elevated privileges and should ignore env variables */ |
2508 | /* return true if we are running with elevated privileges and should ignore env variables */ |
1530 | int inline_size |
2509 | int inline_size ecb_cold |
1531 | enable_secure (void) |
2510 | enable_secure (void) |
1532 | { |
2511 | { |
1533 | #ifdef _WIN32 |
2512 | #ifdef _WIN32 |
1534 | return 0; |
2513 | return 0; |
1535 | #else |
2514 | #else |
1536 | return getuid () != geteuid () |
2515 | return getuid () != geteuid () |
1537 | || getgid () != getegid (); |
2516 | || getgid () != getegid (); |
1538 | #endif |
2517 | #endif |
1539 | } |
2518 | } |
1540 | |
2519 | |
1541 | unsigned int |
2520 | unsigned int ecb_cold |
1542 | ev_supported_backends (void) |
2521 | ev_supported_backends (void) EV_THROW |
1543 | { |
2522 | { |
1544 | unsigned int flags = 0; |
2523 | unsigned int flags = 0; |
1545 | |
2524 | |
1546 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
2525 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
1547 | if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; |
2526 | if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; |
… | |
… | |
1550 | if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; |
2529 | if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; |
1551 | |
2530 | |
1552 | return flags; |
2531 | return flags; |
1553 | } |
2532 | } |
1554 | |
2533 | |
1555 | unsigned int |
2534 | unsigned int ecb_cold |
1556 | ev_recommended_backends (void) |
2535 | ev_recommended_backends (void) EV_THROW |
1557 | { |
2536 | { |
1558 | unsigned int flags = ev_supported_backends (); |
2537 | unsigned int flags = ev_supported_backends (); |
1559 | |
2538 | |
1560 | #ifndef __NetBSD__ |
2539 | #ifndef __NetBSD__ |
1561 | /* kqueue is borked on everything but netbsd apparently */ |
2540 | /* kqueue is borked on everything but netbsd apparently */ |
… | |
… | |
1572 | #endif |
2551 | #endif |
1573 | |
2552 | |
1574 | return flags; |
2553 | return flags; |
1575 | } |
2554 | } |
1576 | |
2555 | |
1577 | unsigned int |
2556 | unsigned int ecb_cold |
1578 | ev_embeddable_backends (void) |
2557 | ev_embeddable_backends (void) EV_THROW |
1579 | { |
2558 | { |
1580 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
2559 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
1581 | |
2560 | |
1582 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
2561 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
1583 | if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */ |
2562 | if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */ |
… | |
… | |
1585 | |
2564 | |
1586 | return flags; |
2565 | return flags; |
1587 | } |
2566 | } |
1588 | |
2567 | |
1589 | unsigned int |
2568 | unsigned int |
1590 | ev_backend (EV_P) |
2569 | ev_backend (EV_P) EV_THROW |
1591 | { |
2570 | { |
1592 | return backend; |
2571 | return backend; |
1593 | } |
2572 | } |
1594 | |
2573 | |
1595 | #if EV_FEATURE_API |
2574 | #if EV_FEATURE_API |
1596 | unsigned int |
2575 | unsigned int |
1597 | ev_iteration (EV_P) |
2576 | ev_iteration (EV_P) EV_THROW |
1598 | { |
2577 | { |
1599 | return loop_count; |
2578 | return loop_count; |
1600 | } |
2579 | } |
1601 | |
2580 | |
1602 | unsigned int |
2581 | unsigned int |
1603 | ev_depth (EV_P) |
2582 | ev_depth (EV_P) EV_THROW |
1604 | { |
2583 | { |
1605 | return loop_depth; |
2584 | return loop_depth; |
1606 | } |
2585 | } |
1607 | |
2586 | |
1608 | void |
2587 | void |
1609 | ev_set_io_collect_interval (EV_P_ ev_tstamp interval) |
2588 | ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_THROW |
1610 | { |
2589 | { |
1611 | io_blocktime = interval; |
2590 | io_blocktime = interval; |
1612 | } |
2591 | } |
1613 | |
2592 | |
1614 | void |
2593 | void |
1615 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) |
2594 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW |
1616 | { |
2595 | { |
1617 | timeout_blocktime = interval; |
2596 | timeout_blocktime = interval; |
1618 | } |
2597 | } |
1619 | |
2598 | |
1620 | void |
2599 | void |
1621 | ev_set_userdata (EV_P_ void *data) |
2600 | ev_set_userdata (EV_P_ void *data) EV_THROW |
1622 | { |
2601 | { |
1623 | userdata = data; |
2602 | userdata = data; |
1624 | } |
2603 | } |
1625 | |
2604 | |
1626 | void * |
2605 | void * |
1627 | ev_userdata (EV_P) |
2606 | ev_userdata (EV_P) EV_THROW |
1628 | { |
2607 | { |
1629 | return userdata; |
2608 | return userdata; |
1630 | } |
2609 | } |
1631 | |
2610 | |
|
|
2611 | void |
1632 | void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) |
2612 | ev_set_invoke_pending_cb (EV_P_ ev_loop_callback invoke_pending_cb) EV_THROW |
1633 | { |
2613 | { |
1634 | invoke_cb = invoke_pending_cb; |
2614 | invoke_cb = invoke_pending_cb; |
1635 | } |
2615 | } |
1636 | |
2616 | |
1637 | void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) |
2617 | void |
|
|
2618 | ev_set_loop_release_cb (EV_P_ ev_loop_callback release EV_THROW, ev_loop_callback acquire EV_THROW) EV_THROW |
1638 | { |
2619 | { |
1639 | release_cb = release; |
2620 | release_cb = release; |
1640 | acquire_cb = acquire; |
2621 | acquire_cb = acquire; |
1641 | } |
2622 | } |
1642 | #endif |
2623 | #endif |
1643 | |
2624 | |
1644 | /* initialise a loop structure, must be zero-initialised */ |
2625 | /* initialise a loop structure, must be zero-initialised */ |
1645 | static void noinline |
2626 | static void noinline ecb_cold |
1646 | loop_init (EV_P_ unsigned int flags) |
2627 | loop_init (EV_P_ unsigned int flags) EV_THROW |
1647 | { |
2628 | { |
1648 | if (!backend) |
2629 | if (!backend) |
1649 | { |
2630 | { |
|
|
2631 | origflags = flags; |
|
|
2632 | |
1650 | #if EV_USE_REALTIME |
2633 | #if EV_USE_REALTIME |
1651 | if (!have_realtime) |
2634 | if (!have_realtime) |
1652 | { |
2635 | { |
1653 | struct timespec ts; |
2636 | struct timespec ts; |
1654 | |
2637 | |
… | |
… | |
1676 | if (!(flags & EVFLAG_NOENV) |
2659 | if (!(flags & EVFLAG_NOENV) |
1677 | && !enable_secure () |
2660 | && !enable_secure () |
1678 | && getenv ("LIBEV_FLAGS")) |
2661 | && getenv ("LIBEV_FLAGS")) |
1679 | flags = atoi (getenv ("LIBEV_FLAGS")); |
2662 | flags = atoi (getenv ("LIBEV_FLAGS")); |
1680 | |
2663 | |
1681 | ev_rt_now = ev_time (); |
2664 | ev_rt_now = ev_time (); |
1682 | mn_now = get_clock (); |
2665 | mn_now = get_clock (); |
1683 | now_floor = mn_now; |
2666 | now_floor = mn_now; |
1684 | rtmn_diff = ev_rt_now - mn_now; |
2667 | rtmn_diff = ev_rt_now - mn_now; |
1685 | #if EV_FEATURE_API |
2668 | #if EV_FEATURE_API |
1686 | invoke_cb = ev_invoke_pending; |
2669 | invoke_cb = ev_invoke_pending; |
1687 | #endif |
2670 | #endif |
1688 | |
2671 | |
1689 | io_blocktime = 0.; |
2672 | io_blocktime = 0.; |
1690 | timeout_blocktime = 0.; |
2673 | timeout_blocktime = 0.; |
1691 | backend = 0; |
2674 | backend = 0; |
1692 | backend_fd = -1; |
2675 | backend_fd = -1; |
1693 | sig_pending = 0; |
2676 | sig_pending = 0; |
1694 | #if EV_ASYNC_ENABLE |
2677 | #if EV_ASYNC_ENABLE |
1695 | async_pending = 0; |
2678 | async_pending = 0; |
1696 | #endif |
2679 | #endif |
|
|
2680 | pipe_write_skipped = 0; |
|
|
2681 | pipe_write_wanted = 0; |
|
|
2682 | evpipe [0] = -1; |
|
|
2683 | evpipe [1] = -1; |
1697 | #if EV_USE_INOTIFY |
2684 | #if EV_USE_INOTIFY |
1698 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
2685 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
1699 | #endif |
2686 | #endif |
1700 | #if EV_USE_SIGNALFD |
2687 | #if EV_USE_SIGNALFD |
1701 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
2688 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1702 | #endif |
2689 | #endif |
1703 | |
2690 | |
1704 | if (!(flags & 0x0000ffffU)) |
2691 | if (!(flags & EVBACKEND_MASK)) |
1705 | flags |= ev_recommended_backends (); |
2692 | flags |= ev_recommended_backends (); |
1706 | |
2693 | |
1707 | #if EV_USE_IOCP |
2694 | #if EV_USE_IOCP |
1708 | if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags); |
2695 | if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags); |
1709 | #endif |
2696 | #endif |
… | |
… | |
1731 | #endif |
2718 | #endif |
1732 | } |
2719 | } |
1733 | } |
2720 | } |
1734 | |
2721 | |
1735 | /* free up a loop structure */ |
2722 | /* free up a loop structure */ |
1736 | void |
2723 | void ecb_cold |
1737 | ev_loop_destroy (EV_P) |
2724 | ev_loop_destroy (EV_P) |
1738 | { |
2725 | { |
1739 | int i; |
2726 | int i; |
|
|
2727 | |
|
|
2728 | #if EV_MULTIPLICITY |
|
|
2729 | /* mimic free (0) */ |
|
|
2730 | if (!EV_A) |
|
|
2731 | return; |
|
|
2732 | #endif |
1740 | |
2733 | |
1741 | #if EV_CLEANUP_ENABLE |
2734 | #if EV_CLEANUP_ENABLE |
1742 | /* queue cleanup watchers (and execute them) */ |
2735 | /* queue cleanup watchers (and execute them) */ |
1743 | if (expect_false (cleanupcnt)) |
2736 | if (expect_false (cleanupcnt)) |
1744 | { |
2737 | { |
… | |
… | |
1746 | EV_INVOKE_PENDING; |
2739 | EV_INVOKE_PENDING; |
1747 | } |
2740 | } |
1748 | #endif |
2741 | #endif |
1749 | |
2742 | |
1750 | #if EV_CHILD_ENABLE |
2743 | #if EV_CHILD_ENABLE |
1751 | if (ev_is_active (&childev)) |
2744 | if (ev_is_default_loop (EV_A) && ev_is_active (&childev)) |
1752 | { |
2745 | { |
1753 | ev_ref (EV_A); /* child watcher */ |
2746 | ev_ref (EV_A); /* child watcher */ |
1754 | ev_signal_stop (EV_A_ &childev); |
2747 | ev_signal_stop (EV_A_ &childev); |
1755 | } |
2748 | } |
1756 | #endif |
2749 | #endif |
… | |
… | |
1758 | if (ev_is_active (&pipe_w)) |
2751 | if (ev_is_active (&pipe_w)) |
1759 | { |
2752 | { |
1760 | /*ev_ref (EV_A);*/ |
2753 | /*ev_ref (EV_A);*/ |
1761 | /*ev_io_stop (EV_A_ &pipe_w);*/ |
2754 | /*ev_io_stop (EV_A_ &pipe_w);*/ |
1762 | |
2755 | |
1763 | #if EV_USE_EVENTFD |
|
|
1764 | if (evfd >= 0) |
|
|
1765 | close (evfd); |
|
|
1766 | #endif |
|
|
1767 | |
|
|
1768 | if (evpipe [0] >= 0) |
|
|
1769 | { |
|
|
1770 | EV_WIN32_CLOSE_FD (evpipe [0]); |
2756 | if (evpipe [0] >= 0) EV_WIN32_CLOSE_FD (evpipe [0]); |
1771 | EV_WIN32_CLOSE_FD (evpipe [1]); |
2757 | if (evpipe [1] >= 0) EV_WIN32_CLOSE_FD (evpipe [1]); |
1772 | } |
|
|
1773 | } |
2758 | } |
1774 | |
2759 | |
1775 | #if EV_USE_SIGNALFD |
2760 | #if EV_USE_SIGNALFD |
1776 | if (ev_is_active (&sigfd_w)) |
2761 | if (ev_is_active (&sigfd_w)) |
1777 | close (sigfd); |
2762 | close (sigfd); |
… | |
… | |
1863 | #endif |
2848 | #endif |
1864 | #if EV_USE_INOTIFY |
2849 | #if EV_USE_INOTIFY |
1865 | infy_fork (EV_A); |
2850 | infy_fork (EV_A); |
1866 | #endif |
2851 | #endif |
1867 | |
2852 | |
|
|
2853 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
1868 | if (ev_is_active (&pipe_w)) |
2854 | if (ev_is_active (&pipe_w)) |
1869 | { |
2855 | { |
1870 | /* this "locks" the handlers against writing to the pipe */ |
2856 | /* pipe_write_wanted must be false now, so modifying fd vars should be safe */ |
1871 | /* while we modify the fd vars */ |
|
|
1872 | sig_pending = 1; |
|
|
1873 | #if EV_ASYNC_ENABLE |
|
|
1874 | async_pending = 1; |
|
|
1875 | #endif |
|
|
1876 | |
2857 | |
1877 | ev_ref (EV_A); |
2858 | ev_ref (EV_A); |
1878 | ev_io_stop (EV_A_ &pipe_w); |
2859 | ev_io_stop (EV_A_ &pipe_w); |
1879 | |
2860 | |
1880 | #if EV_USE_EVENTFD |
|
|
1881 | if (evfd >= 0) |
|
|
1882 | close (evfd); |
|
|
1883 | #endif |
|
|
1884 | |
|
|
1885 | if (evpipe [0] >= 0) |
2861 | if (evpipe [0] >= 0) |
1886 | { |
|
|
1887 | EV_WIN32_CLOSE_FD (evpipe [0]); |
2862 | EV_WIN32_CLOSE_FD (evpipe [0]); |
1888 | EV_WIN32_CLOSE_FD (evpipe [1]); |
|
|
1889 | } |
|
|
1890 | |
2863 | |
1891 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
|
|
1892 | evpipe_init (EV_A); |
2864 | evpipe_init (EV_A); |
1893 | /* now iterate over everything, in case we missed something */ |
2865 | /* iterate over everything, in case we missed something before */ |
1894 | pipecb (EV_A_ &pipe_w, EV_READ); |
2866 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
1895 | #endif |
|
|
1896 | } |
2867 | } |
|
|
2868 | #endif |
1897 | |
2869 | |
1898 | postfork = 0; |
2870 | postfork = 0; |
1899 | } |
2871 | } |
1900 | |
2872 | |
1901 | #if EV_MULTIPLICITY |
2873 | #if EV_MULTIPLICITY |
1902 | |
2874 | |
1903 | struct ev_loop * |
2875 | struct ev_loop * ecb_cold |
1904 | ev_loop_new (unsigned int flags) |
2876 | ev_loop_new (unsigned int flags) EV_THROW |
1905 | { |
2877 | { |
1906 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
2878 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
1907 | |
2879 | |
1908 | memset (EV_A, 0, sizeof (struct ev_loop)); |
2880 | memset (EV_A, 0, sizeof (struct ev_loop)); |
1909 | loop_init (EV_A_ flags); |
2881 | loop_init (EV_A_ flags); |
… | |
… | |
1916 | } |
2888 | } |
1917 | |
2889 | |
1918 | #endif /* multiplicity */ |
2890 | #endif /* multiplicity */ |
1919 | |
2891 | |
1920 | #if EV_VERIFY |
2892 | #if EV_VERIFY |
1921 | static void noinline |
2893 | static void noinline ecb_cold |
1922 | verify_watcher (EV_P_ W w) |
2894 | verify_watcher (EV_P_ W w) |
1923 | { |
2895 | { |
1924 | assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); |
2896 | assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); |
1925 | |
2897 | |
1926 | if (w->pending) |
2898 | if (w->pending) |
1927 | assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); |
2899 | assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); |
1928 | } |
2900 | } |
1929 | |
2901 | |
1930 | static void noinline |
2902 | static void noinline ecb_cold |
1931 | verify_heap (EV_P_ ANHE *heap, int N) |
2903 | verify_heap (EV_P_ ANHE *heap, int N) |
1932 | { |
2904 | { |
1933 | int i; |
2905 | int i; |
1934 | |
2906 | |
1935 | for (i = HEAP0; i < N + HEAP0; ++i) |
2907 | for (i = HEAP0; i < N + HEAP0; ++i) |
… | |
… | |
1940 | |
2912 | |
1941 | verify_watcher (EV_A_ (W)ANHE_w (heap [i])); |
2913 | verify_watcher (EV_A_ (W)ANHE_w (heap [i])); |
1942 | } |
2914 | } |
1943 | } |
2915 | } |
1944 | |
2916 | |
1945 | static void noinline |
2917 | static void noinline ecb_cold |
1946 | array_verify (EV_P_ W *ws, int cnt) |
2918 | array_verify (EV_P_ W *ws, int cnt) |
1947 | { |
2919 | { |
1948 | while (cnt--) |
2920 | while (cnt--) |
1949 | { |
2921 | { |
1950 | assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); |
2922 | assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); |
… | |
… | |
1952 | } |
2924 | } |
1953 | } |
2925 | } |
1954 | #endif |
2926 | #endif |
1955 | |
2927 | |
1956 | #if EV_FEATURE_API |
2928 | #if EV_FEATURE_API |
1957 | void |
2929 | void ecb_cold |
1958 | ev_verify (EV_P) |
2930 | ev_verify (EV_P) EV_THROW |
1959 | { |
2931 | { |
1960 | #if EV_VERIFY |
2932 | #if EV_VERIFY |
1961 | int i; |
2933 | int i; |
1962 | WL w; |
2934 | WL w, w2; |
1963 | |
2935 | |
1964 | assert (activecnt >= -1); |
2936 | assert (activecnt >= -1); |
1965 | |
2937 | |
1966 | assert (fdchangemax >= fdchangecnt); |
2938 | assert (fdchangemax >= fdchangecnt); |
1967 | for (i = 0; i < fdchangecnt; ++i) |
2939 | for (i = 0; i < fdchangecnt; ++i) |
1968 | assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0)); |
2940 | assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0)); |
1969 | |
2941 | |
1970 | assert (anfdmax >= 0); |
2942 | assert (anfdmax >= 0); |
1971 | for (i = 0; i < anfdmax; ++i) |
2943 | for (i = 0; i < anfdmax; ++i) |
|
|
2944 | { |
|
|
2945 | int j = 0; |
|
|
2946 | |
1972 | for (w = anfds [i].head; w; w = w->next) |
2947 | for (w = w2 = anfds [i].head; w; w = w->next) |
1973 | { |
2948 | { |
1974 | verify_watcher (EV_A_ (W)w); |
2949 | verify_watcher (EV_A_ (W)w); |
|
|
2950 | |
|
|
2951 | if (j++ & 1) |
|
|
2952 | { |
|
|
2953 | assert (("libev: io watcher list contains a loop", w != w2)); |
|
|
2954 | w2 = w2->next; |
|
|
2955 | } |
|
|
2956 | |
1975 | assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1)); |
2957 | assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1)); |
1976 | assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i)); |
2958 | assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i)); |
1977 | } |
2959 | } |
|
|
2960 | } |
1978 | |
2961 | |
1979 | assert (timermax >= timercnt); |
2962 | assert (timermax >= timercnt); |
1980 | verify_heap (EV_A_ timers, timercnt); |
2963 | verify_heap (EV_A_ timers, timercnt); |
1981 | |
2964 | |
1982 | #if EV_PERIODIC_ENABLE |
2965 | #if EV_PERIODIC_ENABLE |
… | |
… | |
2028 | #endif |
3011 | #endif |
2029 | } |
3012 | } |
2030 | #endif |
3013 | #endif |
2031 | |
3014 | |
2032 | #if EV_MULTIPLICITY |
3015 | #if EV_MULTIPLICITY |
2033 | struct ev_loop * |
3016 | struct ev_loop * ecb_cold |
2034 | #else |
3017 | #else |
2035 | int |
3018 | int |
2036 | #endif |
3019 | #endif |
2037 | ev_default_loop (unsigned int flags) |
3020 | ev_default_loop (unsigned int flags) EV_THROW |
2038 | { |
3021 | { |
2039 | if (!ev_default_loop_ptr) |
3022 | if (!ev_default_loop_ptr) |
2040 | { |
3023 | { |
2041 | #if EV_MULTIPLICITY |
3024 | #if EV_MULTIPLICITY |
2042 | EV_P = ev_default_loop_ptr = &default_loop_struct; |
3025 | EV_P = ev_default_loop_ptr = &default_loop_struct; |
… | |
… | |
2061 | |
3044 | |
2062 | return ev_default_loop_ptr; |
3045 | return ev_default_loop_ptr; |
2063 | } |
3046 | } |
2064 | |
3047 | |
2065 | void |
3048 | void |
2066 | ev_loop_fork (EV_P) |
3049 | ev_loop_fork (EV_P) EV_THROW |
2067 | { |
3050 | { |
2068 | postfork = 1; /* must be in line with ev_default_fork */ |
3051 | postfork = 1; |
2069 | } |
3052 | } |
2070 | |
3053 | |
2071 | /*****************************************************************************/ |
3054 | /*****************************************************************************/ |
2072 | |
3055 | |
2073 | void |
3056 | void |
… | |
… | |
2075 | { |
3058 | { |
2076 | EV_CB_INVOKE ((W)w, revents); |
3059 | EV_CB_INVOKE ((W)w, revents); |
2077 | } |
3060 | } |
2078 | |
3061 | |
2079 | unsigned int |
3062 | unsigned int |
2080 | ev_pending_count (EV_P) |
3063 | ev_pending_count (EV_P) EV_THROW |
2081 | { |
3064 | { |
2082 | int pri; |
3065 | int pri; |
2083 | unsigned int count = 0; |
3066 | unsigned int count = 0; |
2084 | |
3067 | |
2085 | for (pri = NUMPRI; pri--; ) |
3068 | for (pri = NUMPRI; pri--; ) |
… | |
… | |
2089 | } |
3072 | } |
2090 | |
3073 | |
2091 | void noinline |
3074 | void noinline |
2092 | ev_invoke_pending (EV_P) |
3075 | ev_invoke_pending (EV_P) |
2093 | { |
3076 | { |
2094 | int pri; |
3077 | pendingpri = NUMPRI; |
2095 | |
3078 | |
2096 | for (pri = NUMPRI; pri--; ) |
3079 | while (pendingpri) /* pendingpri possibly gets modified in the inner loop */ |
|
|
3080 | { |
|
|
3081 | --pendingpri; |
|
|
3082 | |
2097 | while (pendingcnt [pri]) |
3083 | while (pendingcnt [pendingpri]) |
2098 | { |
3084 | { |
2099 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
3085 | ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri]; |
2100 | |
3086 | |
2101 | /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ |
|
|
2102 | /* ^ this is no longer true, as pending_w could be here */ |
|
|
2103 | |
|
|
2104 | p->w->pending = 0; |
3087 | p->w->pending = 0; |
2105 | EV_CB_INVOKE (p->w, p->events); |
3088 | EV_CB_INVOKE (p->w, p->events); |
2106 | EV_FREQUENT_CHECK; |
3089 | EV_FREQUENT_CHECK; |
2107 | } |
3090 | } |
|
|
3091 | } |
2108 | } |
3092 | } |
2109 | |
3093 | |
2110 | #if EV_IDLE_ENABLE |
3094 | #if EV_IDLE_ENABLE |
2111 | /* make idle watchers pending. this handles the "call-idle */ |
3095 | /* make idle watchers pending. this handles the "call-idle */ |
2112 | /* only when higher priorities are idle" logic */ |
3096 | /* only when higher priorities are idle" logic */ |
… | |
… | |
2169 | feed_reverse_done (EV_A_ EV_TIMER); |
3153 | feed_reverse_done (EV_A_ EV_TIMER); |
2170 | } |
3154 | } |
2171 | } |
3155 | } |
2172 | |
3156 | |
2173 | #if EV_PERIODIC_ENABLE |
3157 | #if EV_PERIODIC_ENABLE |
|
|
3158 | |
|
|
3159 | static void noinline |
|
|
3160 | periodic_recalc (EV_P_ ev_periodic *w) |
|
|
3161 | { |
|
|
3162 | ev_tstamp interval = w->interval > MIN_INTERVAL ? w->interval : MIN_INTERVAL; |
|
|
3163 | ev_tstamp at = w->offset + interval * ev_floor ((ev_rt_now - w->offset) / interval); |
|
|
3164 | |
|
|
3165 | /* the above almost always errs on the low side */ |
|
|
3166 | while (at <= ev_rt_now) |
|
|
3167 | { |
|
|
3168 | ev_tstamp nat = at + w->interval; |
|
|
3169 | |
|
|
3170 | /* when resolution fails us, we use ev_rt_now */ |
|
|
3171 | if (expect_false (nat == at)) |
|
|
3172 | { |
|
|
3173 | at = ev_rt_now; |
|
|
3174 | break; |
|
|
3175 | } |
|
|
3176 | |
|
|
3177 | at = nat; |
|
|
3178 | } |
|
|
3179 | |
|
|
3180 | ev_at (w) = at; |
|
|
3181 | } |
|
|
3182 | |
2174 | /* make periodics pending */ |
3183 | /* make periodics pending */ |
2175 | inline_size void |
3184 | inline_size void |
2176 | periodics_reify (EV_P) |
3185 | periodics_reify (EV_P) |
2177 | { |
3186 | { |
2178 | EV_FREQUENT_CHECK; |
3187 | EV_FREQUENT_CHECK; |
2179 | |
3188 | |
2180 | while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) |
3189 | while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) |
2181 | { |
3190 | { |
2182 | int feed_count = 0; |
|
|
2183 | |
|
|
2184 | do |
3191 | do |
2185 | { |
3192 | { |
2186 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); |
3193 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); |
2187 | |
3194 | |
2188 | /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/ |
3195 | /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/ |
… | |
… | |
2197 | ANHE_at_cache (periodics [HEAP0]); |
3204 | ANHE_at_cache (periodics [HEAP0]); |
2198 | downheap (periodics, periodiccnt, HEAP0); |
3205 | downheap (periodics, periodiccnt, HEAP0); |
2199 | } |
3206 | } |
2200 | else if (w->interval) |
3207 | else if (w->interval) |
2201 | { |
3208 | { |
2202 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
3209 | periodic_recalc (EV_A_ w); |
2203 | /* if next trigger time is not sufficiently in the future, put it there */ |
|
|
2204 | /* this might happen because of floating point inexactness */ |
|
|
2205 | if (ev_at (w) - ev_rt_now < TIME_EPSILON) |
|
|
2206 | { |
|
|
2207 | ev_at (w) += w->interval; |
|
|
2208 | |
|
|
2209 | /* if interval is unreasonably low we might still have a time in the past */ |
|
|
2210 | /* so correct this. this will make the periodic very inexact, but the user */ |
|
|
2211 | /* has effectively asked to get triggered more often than possible */ |
|
|
2212 | if (ev_at (w) < ev_rt_now) |
|
|
2213 | ev_at (w) = ev_rt_now; |
|
|
2214 | } |
|
|
2215 | |
|
|
2216 | ANHE_at_cache (periodics [HEAP0]); |
3210 | ANHE_at_cache (periodics [HEAP0]); |
2217 | downheap (periodics, periodiccnt, HEAP0); |
3211 | downheap (periodics, periodiccnt, HEAP0); |
2218 | } |
3212 | } |
2219 | else |
3213 | else |
2220 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
3214 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
… | |
… | |
2228 | } |
3222 | } |
2229 | } |
3223 | } |
2230 | |
3224 | |
2231 | /* simply recalculate all periodics */ |
3225 | /* simply recalculate all periodics */ |
2232 | /* TODO: maybe ensure that at least one event happens when jumping forward? */ |
3226 | /* TODO: maybe ensure that at least one event happens when jumping forward? */ |
2233 | static void noinline |
3227 | static void noinline ecb_cold |
2234 | periodics_reschedule (EV_P) |
3228 | periodics_reschedule (EV_P) |
2235 | { |
3229 | { |
2236 | int i; |
3230 | int i; |
2237 | |
3231 | |
2238 | /* adjust periodics after time jump */ |
3232 | /* adjust periodics after time jump */ |
… | |
… | |
2241 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]); |
3235 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]); |
2242 | |
3236 | |
2243 | if (w->reschedule_cb) |
3237 | if (w->reschedule_cb) |
2244 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
3238 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2245 | else if (w->interval) |
3239 | else if (w->interval) |
2246 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
3240 | periodic_recalc (EV_A_ w); |
2247 | |
3241 | |
2248 | ANHE_at_cache (periodics [i]); |
3242 | ANHE_at_cache (periodics [i]); |
2249 | } |
3243 | } |
2250 | |
3244 | |
2251 | reheap (periodics, periodiccnt); |
3245 | reheap (periodics, periodiccnt); |
2252 | } |
3246 | } |
2253 | #endif |
3247 | #endif |
2254 | |
3248 | |
2255 | /* adjust all timers by a given offset */ |
3249 | /* adjust all timers by a given offset */ |
2256 | static void noinline |
3250 | static void noinline ecb_cold |
2257 | timers_reschedule (EV_P_ ev_tstamp adjust) |
3251 | timers_reschedule (EV_P_ ev_tstamp adjust) |
2258 | { |
3252 | { |
2259 | int i; |
3253 | int i; |
2260 | |
3254 | |
2261 | for (i = 0; i < timercnt; ++i) |
3255 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
2298 | * doesn't hurt either as we only do this on time-jumps or |
3292 | * doesn't hurt either as we only do this on time-jumps or |
2299 | * in the unlikely event of having been preempted here. |
3293 | * in the unlikely event of having been preempted here. |
2300 | */ |
3294 | */ |
2301 | for (i = 4; --i; ) |
3295 | for (i = 4; --i; ) |
2302 | { |
3296 | { |
|
|
3297 | ev_tstamp diff; |
2303 | rtmn_diff = ev_rt_now - mn_now; |
3298 | rtmn_diff = ev_rt_now - mn_now; |
2304 | |
3299 | |
|
|
3300 | diff = odiff - rtmn_diff; |
|
|
3301 | |
2305 | if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)) |
3302 | if (expect_true ((diff < 0. ? -diff : diff) < MIN_TIMEJUMP)) |
2306 | return; /* all is well */ |
3303 | return; /* all is well */ |
2307 | |
3304 | |
2308 | ev_rt_now = ev_time (); |
3305 | ev_rt_now = ev_time (); |
2309 | mn_now = get_clock (); |
3306 | mn_now = get_clock (); |
2310 | now_floor = mn_now; |
3307 | now_floor = mn_now; |
… | |
… | |
2332 | |
3329 | |
2333 | mn_now = ev_rt_now; |
3330 | mn_now = ev_rt_now; |
2334 | } |
3331 | } |
2335 | } |
3332 | } |
2336 | |
3333 | |
2337 | void |
3334 | int |
2338 | ev_run (EV_P_ int flags) |
3335 | ev_run (EV_P_ int flags) |
2339 | { |
3336 | { |
2340 | #if EV_FEATURE_API |
3337 | #if EV_FEATURE_API |
2341 | ++loop_depth; |
3338 | ++loop_depth; |
2342 | #endif |
3339 | #endif |
… | |
… | |
2400 | ev_tstamp prev_mn_now = mn_now; |
3397 | ev_tstamp prev_mn_now = mn_now; |
2401 | |
3398 | |
2402 | /* update time to cancel out callback processing overhead */ |
3399 | /* update time to cancel out callback processing overhead */ |
2403 | time_update (EV_A_ 1e100); |
3400 | time_update (EV_A_ 1e100); |
2404 | |
3401 | |
|
|
3402 | /* from now on, we want a pipe-wake-up */ |
|
|
3403 | pipe_write_wanted = 1; |
|
|
3404 | |
|
|
3405 | ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */ |
|
|
3406 | |
2405 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt))) |
3407 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) |
2406 | { |
3408 | { |
2407 | waittime = MAX_BLOCKTIME; |
3409 | waittime = MAX_BLOCKTIME; |
2408 | |
3410 | |
2409 | if (timercnt) |
3411 | if (timercnt) |
2410 | { |
3412 | { |
2411 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; |
3413 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; |
2412 | if (waittime > to) waittime = to; |
3414 | if (waittime > to) waittime = to; |
2413 | } |
3415 | } |
2414 | |
3416 | |
2415 | #if EV_PERIODIC_ENABLE |
3417 | #if EV_PERIODIC_ENABLE |
2416 | if (periodiccnt) |
3418 | if (periodiccnt) |
2417 | { |
3419 | { |
2418 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; |
3420 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now; |
2419 | if (waittime > to) waittime = to; |
3421 | if (waittime > to) waittime = to; |
2420 | } |
3422 | } |
2421 | #endif |
3423 | #endif |
2422 | |
3424 | |
2423 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
3425 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
2424 | if (expect_false (waittime < timeout_blocktime)) |
3426 | if (expect_false (waittime < timeout_blocktime)) |
2425 | waittime = timeout_blocktime; |
3427 | waittime = timeout_blocktime; |
|
|
3428 | |
|
|
3429 | /* at this point, we NEED to wait, so we have to ensure */ |
|
|
3430 | /* to pass a minimum nonzero value to the backend */ |
|
|
3431 | if (expect_false (waittime < backend_mintime)) |
|
|
3432 | waittime = backend_mintime; |
2426 | |
3433 | |
2427 | /* extra check because io_blocktime is commonly 0 */ |
3434 | /* extra check because io_blocktime is commonly 0 */ |
2428 | if (expect_false (io_blocktime)) |
3435 | if (expect_false (io_blocktime)) |
2429 | { |
3436 | { |
2430 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
3437 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
2431 | |
3438 | |
2432 | if (sleeptime > waittime - backend_fudge) |
3439 | if (sleeptime > waittime - backend_mintime) |
2433 | sleeptime = waittime - backend_fudge; |
3440 | sleeptime = waittime - backend_mintime; |
2434 | |
3441 | |
2435 | if (expect_true (sleeptime > 0.)) |
3442 | if (expect_true (sleeptime > 0.)) |
2436 | { |
3443 | { |
2437 | ev_sleep (sleeptime); |
3444 | ev_sleep (sleeptime); |
2438 | waittime -= sleeptime; |
3445 | waittime -= sleeptime; |
… | |
… | |
2445 | #endif |
3452 | #endif |
2446 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
3453 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2447 | backend_poll (EV_A_ waittime); |
3454 | backend_poll (EV_A_ waittime); |
2448 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
3455 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
2449 | |
3456 | |
|
|
3457 | pipe_write_wanted = 0; /* just an optimisation, no fence needed */ |
|
|
3458 | |
|
|
3459 | ECB_MEMORY_FENCE_ACQUIRE; |
|
|
3460 | if (pipe_write_skipped) |
|
|
3461 | { |
|
|
3462 | assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); |
|
|
3463 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
|
|
3464 | } |
|
|
3465 | |
|
|
3466 | |
2450 | /* update ev_rt_now, do magic */ |
3467 | /* update ev_rt_now, do magic */ |
2451 | time_update (EV_A_ waittime + sleeptime); |
3468 | time_update (EV_A_ waittime + sleeptime); |
2452 | } |
3469 | } |
2453 | |
3470 | |
2454 | /* queue pending timers and reschedule them */ |
3471 | /* queue pending timers and reschedule them */ |
… | |
… | |
2480 | loop_done = EVBREAK_CANCEL; |
3497 | loop_done = EVBREAK_CANCEL; |
2481 | |
3498 | |
2482 | #if EV_FEATURE_API |
3499 | #if EV_FEATURE_API |
2483 | --loop_depth; |
3500 | --loop_depth; |
2484 | #endif |
3501 | #endif |
|
|
3502 | |
|
|
3503 | return activecnt; |
2485 | } |
3504 | } |
2486 | |
3505 | |
2487 | void |
3506 | void |
2488 | ev_break (EV_P_ int how) |
3507 | ev_break (EV_P_ int how) EV_THROW |
2489 | { |
3508 | { |
2490 | loop_done = how; |
3509 | loop_done = how; |
2491 | } |
3510 | } |
2492 | |
3511 | |
2493 | void |
3512 | void |
2494 | ev_ref (EV_P) |
3513 | ev_ref (EV_P) EV_THROW |
2495 | { |
3514 | { |
2496 | ++activecnt; |
3515 | ++activecnt; |
2497 | } |
3516 | } |
2498 | |
3517 | |
2499 | void |
3518 | void |
2500 | ev_unref (EV_P) |
3519 | ev_unref (EV_P) EV_THROW |
2501 | { |
3520 | { |
2502 | --activecnt; |
3521 | --activecnt; |
2503 | } |
3522 | } |
2504 | |
3523 | |
2505 | void |
3524 | void |
2506 | ev_now_update (EV_P) |
3525 | ev_now_update (EV_P) EV_THROW |
2507 | { |
3526 | { |
2508 | time_update (EV_A_ 1e100); |
3527 | time_update (EV_A_ 1e100); |
2509 | } |
3528 | } |
2510 | |
3529 | |
2511 | void |
3530 | void |
2512 | ev_suspend (EV_P) |
3531 | ev_suspend (EV_P) EV_THROW |
2513 | { |
3532 | { |
2514 | ev_now_update (EV_A); |
3533 | ev_now_update (EV_A); |
2515 | } |
3534 | } |
2516 | |
3535 | |
2517 | void |
3536 | void |
2518 | ev_resume (EV_P) |
3537 | ev_resume (EV_P) EV_THROW |
2519 | { |
3538 | { |
2520 | ev_tstamp mn_prev = mn_now; |
3539 | ev_tstamp mn_prev = mn_now; |
2521 | |
3540 | |
2522 | ev_now_update (EV_A); |
3541 | ev_now_update (EV_A); |
2523 | timers_reschedule (EV_A_ mn_now - mn_prev); |
3542 | timers_reschedule (EV_A_ mn_now - mn_prev); |
… | |
… | |
2562 | w->pending = 0; |
3581 | w->pending = 0; |
2563 | } |
3582 | } |
2564 | } |
3583 | } |
2565 | |
3584 | |
2566 | int |
3585 | int |
2567 | ev_clear_pending (EV_P_ void *w) |
3586 | ev_clear_pending (EV_P_ void *w) EV_THROW |
2568 | { |
3587 | { |
2569 | W w_ = (W)w; |
3588 | W w_ = (W)w; |
2570 | int pending = w_->pending; |
3589 | int pending = w_->pending; |
2571 | |
3590 | |
2572 | if (expect_true (pending)) |
3591 | if (expect_true (pending)) |
… | |
… | |
2605 | } |
3624 | } |
2606 | |
3625 | |
2607 | /*****************************************************************************/ |
3626 | /*****************************************************************************/ |
2608 | |
3627 | |
2609 | void noinline |
3628 | void noinline |
2610 | ev_io_start (EV_P_ ev_io *w) |
3629 | ev_io_start (EV_P_ ev_io *w) EV_THROW |
2611 | { |
3630 | { |
2612 | int fd = w->fd; |
3631 | int fd = w->fd; |
2613 | |
3632 | |
2614 | if (expect_false (ev_is_active (w))) |
3633 | if (expect_false (ev_is_active (w))) |
2615 | return; |
3634 | return; |
… | |
… | |
2621 | |
3640 | |
2622 | ev_start (EV_A_ (W)w, 1); |
3641 | ev_start (EV_A_ (W)w, 1); |
2623 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
3642 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
2624 | wlist_add (&anfds[fd].head, (WL)w); |
3643 | wlist_add (&anfds[fd].head, (WL)w); |
2625 | |
3644 | |
|
|
3645 | /* common bug, apparently */ |
|
|
3646 | assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w)); |
|
|
3647 | |
2626 | fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); |
3648 | fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); |
2627 | w->events &= ~EV__IOFDSET; |
3649 | w->events &= ~EV__IOFDSET; |
2628 | |
3650 | |
2629 | EV_FREQUENT_CHECK; |
3651 | EV_FREQUENT_CHECK; |
2630 | } |
3652 | } |
2631 | |
3653 | |
2632 | void noinline |
3654 | void noinline |
2633 | ev_io_stop (EV_P_ ev_io *w) |
3655 | ev_io_stop (EV_P_ ev_io *w) EV_THROW |
2634 | { |
3656 | { |
2635 | clear_pending (EV_A_ (W)w); |
3657 | clear_pending (EV_A_ (W)w); |
2636 | if (expect_false (!ev_is_active (w))) |
3658 | if (expect_false (!ev_is_active (w))) |
2637 | return; |
3659 | return; |
2638 | |
3660 | |
… | |
… | |
2647 | |
3669 | |
2648 | EV_FREQUENT_CHECK; |
3670 | EV_FREQUENT_CHECK; |
2649 | } |
3671 | } |
2650 | |
3672 | |
2651 | void noinline |
3673 | void noinline |
2652 | ev_timer_start (EV_P_ ev_timer *w) |
3674 | ev_timer_start (EV_P_ ev_timer *w) EV_THROW |
2653 | { |
3675 | { |
2654 | if (expect_false (ev_is_active (w))) |
3676 | if (expect_false (ev_is_active (w))) |
2655 | return; |
3677 | return; |
2656 | |
3678 | |
2657 | ev_at (w) += mn_now; |
3679 | ev_at (w) += mn_now; |
… | |
… | |
2671 | |
3693 | |
2672 | /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/ |
3694 | /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/ |
2673 | } |
3695 | } |
2674 | |
3696 | |
2675 | void noinline |
3697 | void noinline |
2676 | ev_timer_stop (EV_P_ ev_timer *w) |
3698 | ev_timer_stop (EV_P_ ev_timer *w) EV_THROW |
2677 | { |
3699 | { |
2678 | clear_pending (EV_A_ (W)w); |
3700 | clear_pending (EV_A_ (W)w); |
2679 | if (expect_false (!ev_is_active (w))) |
3701 | if (expect_false (!ev_is_active (w))) |
2680 | return; |
3702 | return; |
2681 | |
3703 | |
… | |
… | |
2701 | |
3723 | |
2702 | EV_FREQUENT_CHECK; |
3724 | EV_FREQUENT_CHECK; |
2703 | } |
3725 | } |
2704 | |
3726 | |
2705 | void noinline |
3727 | void noinline |
2706 | ev_timer_again (EV_P_ ev_timer *w) |
3728 | ev_timer_again (EV_P_ ev_timer *w) EV_THROW |
2707 | { |
3729 | { |
2708 | EV_FREQUENT_CHECK; |
3730 | EV_FREQUENT_CHECK; |
|
|
3731 | |
|
|
3732 | clear_pending (EV_A_ (W)w); |
2709 | |
3733 | |
2710 | if (ev_is_active (w)) |
3734 | if (ev_is_active (w)) |
2711 | { |
3735 | { |
2712 | if (w->repeat) |
3736 | if (w->repeat) |
2713 | { |
3737 | { |
… | |
… | |
2726 | |
3750 | |
2727 | EV_FREQUENT_CHECK; |
3751 | EV_FREQUENT_CHECK; |
2728 | } |
3752 | } |
2729 | |
3753 | |
2730 | ev_tstamp |
3754 | ev_tstamp |
2731 | ev_timer_remaining (EV_P_ ev_timer *w) |
3755 | ev_timer_remaining (EV_P_ ev_timer *w) EV_THROW |
2732 | { |
3756 | { |
2733 | return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); |
3757 | return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); |
2734 | } |
3758 | } |
2735 | |
3759 | |
2736 | #if EV_PERIODIC_ENABLE |
3760 | #if EV_PERIODIC_ENABLE |
2737 | void noinline |
3761 | void noinline |
2738 | ev_periodic_start (EV_P_ ev_periodic *w) |
3762 | ev_periodic_start (EV_P_ ev_periodic *w) EV_THROW |
2739 | { |
3763 | { |
2740 | if (expect_false (ev_is_active (w))) |
3764 | if (expect_false (ev_is_active (w))) |
2741 | return; |
3765 | return; |
2742 | |
3766 | |
2743 | if (w->reschedule_cb) |
3767 | if (w->reschedule_cb) |
2744 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
3768 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2745 | else if (w->interval) |
3769 | else if (w->interval) |
2746 | { |
3770 | { |
2747 | assert (("libev: ev_periodic_start called with negative interval value", w->interval >= 0.)); |
3771 | assert (("libev: ev_periodic_start called with negative interval value", w->interval >= 0.)); |
2748 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
3772 | periodic_recalc (EV_A_ w); |
2749 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
|
|
2750 | } |
3773 | } |
2751 | else |
3774 | else |
2752 | ev_at (w) = w->offset; |
3775 | ev_at (w) = w->offset; |
2753 | |
3776 | |
2754 | EV_FREQUENT_CHECK; |
3777 | EV_FREQUENT_CHECK; |
… | |
… | |
2764 | |
3787 | |
2765 | /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/ |
3788 | /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/ |
2766 | } |
3789 | } |
2767 | |
3790 | |
2768 | void noinline |
3791 | void noinline |
2769 | ev_periodic_stop (EV_P_ ev_periodic *w) |
3792 | ev_periodic_stop (EV_P_ ev_periodic *w) EV_THROW |
2770 | { |
3793 | { |
2771 | clear_pending (EV_A_ (W)w); |
3794 | clear_pending (EV_A_ (W)w); |
2772 | if (expect_false (!ev_is_active (w))) |
3795 | if (expect_false (!ev_is_active (w))) |
2773 | return; |
3796 | return; |
2774 | |
3797 | |
… | |
… | |
2792 | |
3815 | |
2793 | EV_FREQUENT_CHECK; |
3816 | EV_FREQUENT_CHECK; |
2794 | } |
3817 | } |
2795 | |
3818 | |
2796 | void noinline |
3819 | void noinline |
2797 | ev_periodic_again (EV_P_ ev_periodic *w) |
3820 | ev_periodic_again (EV_P_ ev_periodic *w) EV_THROW |
2798 | { |
3821 | { |
2799 | /* TODO: use adjustheap and recalculation */ |
3822 | /* TODO: use adjustheap and recalculation */ |
2800 | ev_periodic_stop (EV_A_ w); |
3823 | ev_periodic_stop (EV_A_ w); |
2801 | ev_periodic_start (EV_A_ w); |
3824 | ev_periodic_start (EV_A_ w); |
2802 | } |
3825 | } |
… | |
… | |
2807 | #endif |
3830 | #endif |
2808 | |
3831 | |
2809 | #if EV_SIGNAL_ENABLE |
3832 | #if EV_SIGNAL_ENABLE |
2810 | |
3833 | |
2811 | void noinline |
3834 | void noinline |
2812 | ev_signal_start (EV_P_ ev_signal *w) |
3835 | ev_signal_start (EV_P_ ev_signal *w) EV_THROW |
2813 | { |
3836 | { |
2814 | if (expect_false (ev_is_active (w))) |
3837 | if (expect_false (ev_is_active (w))) |
2815 | return; |
3838 | return; |
2816 | |
3839 | |
2817 | assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); |
3840 | assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); |
… | |
… | |
2819 | #if EV_MULTIPLICITY |
3842 | #if EV_MULTIPLICITY |
2820 | assert (("libev: a signal must not be attached to two different loops", |
3843 | assert (("libev: a signal must not be attached to two different loops", |
2821 | !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop)); |
3844 | !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop)); |
2822 | |
3845 | |
2823 | signals [w->signum - 1].loop = EV_A; |
3846 | signals [w->signum - 1].loop = EV_A; |
|
|
3847 | ECB_MEMORY_FENCE_RELEASE; |
2824 | #endif |
3848 | #endif |
2825 | |
3849 | |
2826 | EV_FREQUENT_CHECK; |
3850 | EV_FREQUENT_CHECK; |
2827 | |
3851 | |
2828 | #if EV_USE_SIGNALFD |
3852 | #if EV_USE_SIGNALFD |
… | |
… | |
2875 | sa.sa_handler = ev_sighandler; |
3899 | sa.sa_handler = ev_sighandler; |
2876 | sigfillset (&sa.sa_mask); |
3900 | sigfillset (&sa.sa_mask); |
2877 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
3901 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
2878 | sigaction (w->signum, &sa, 0); |
3902 | sigaction (w->signum, &sa, 0); |
2879 | |
3903 | |
|
|
3904 | if (origflags & EVFLAG_NOSIGMASK) |
|
|
3905 | { |
2880 | sigemptyset (&sa.sa_mask); |
3906 | sigemptyset (&sa.sa_mask); |
2881 | sigaddset (&sa.sa_mask, w->signum); |
3907 | sigaddset (&sa.sa_mask, w->signum); |
2882 | sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0); |
3908 | sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0); |
|
|
3909 | } |
2883 | #endif |
3910 | #endif |
2884 | } |
3911 | } |
2885 | |
3912 | |
2886 | EV_FREQUENT_CHECK; |
3913 | EV_FREQUENT_CHECK; |
2887 | } |
3914 | } |
2888 | |
3915 | |
2889 | void noinline |
3916 | void noinline |
2890 | ev_signal_stop (EV_P_ ev_signal *w) |
3917 | ev_signal_stop (EV_P_ ev_signal *w) EV_THROW |
2891 | { |
3918 | { |
2892 | clear_pending (EV_A_ (W)w); |
3919 | clear_pending (EV_A_ (W)w); |
2893 | if (expect_false (!ev_is_active (w))) |
3920 | if (expect_false (!ev_is_active (w))) |
2894 | return; |
3921 | return; |
2895 | |
3922 | |
… | |
… | |
2926 | #endif |
3953 | #endif |
2927 | |
3954 | |
2928 | #if EV_CHILD_ENABLE |
3955 | #if EV_CHILD_ENABLE |
2929 | |
3956 | |
2930 | void |
3957 | void |
2931 | ev_child_start (EV_P_ ev_child *w) |
3958 | ev_child_start (EV_P_ ev_child *w) EV_THROW |
2932 | { |
3959 | { |
2933 | #if EV_MULTIPLICITY |
3960 | #if EV_MULTIPLICITY |
2934 | assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
3961 | assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
2935 | #endif |
3962 | #endif |
2936 | if (expect_false (ev_is_active (w))) |
3963 | if (expect_false (ev_is_active (w))) |
… | |
… | |
2943 | |
3970 | |
2944 | EV_FREQUENT_CHECK; |
3971 | EV_FREQUENT_CHECK; |
2945 | } |
3972 | } |
2946 | |
3973 | |
2947 | void |
3974 | void |
2948 | ev_child_stop (EV_P_ ev_child *w) |
3975 | ev_child_stop (EV_P_ ev_child *w) EV_THROW |
2949 | { |
3976 | { |
2950 | clear_pending (EV_A_ (W)w); |
3977 | clear_pending (EV_A_ (W)w); |
2951 | if (expect_false (!ev_is_active (w))) |
3978 | if (expect_false (!ev_is_active (w))) |
2952 | return; |
3979 | return; |
2953 | |
3980 | |
… | |
… | |
2980 | # define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX) |
4007 | # define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX) |
2981 | |
4008 | |
2982 | static void noinline |
4009 | static void noinline |
2983 | infy_add (EV_P_ ev_stat *w) |
4010 | infy_add (EV_P_ ev_stat *w) |
2984 | { |
4011 | { |
2985 | w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD); |
4012 | w->wd = inotify_add_watch (fs_fd, w->path, |
|
|
4013 | IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY |
|
|
4014 | | IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO |
|
|
4015 | | IN_DONT_FOLLOW | IN_MASK_ADD); |
2986 | |
4016 | |
2987 | if (w->wd >= 0) |
4017 | if (w->wd >= 0) |
2988 | { |
4018 | { |
2989 | struct statfs sfs; |
4019 | struct statfs sfs; |
2990 | |
4020 | |
… | |
… | |
2994 | |
4024 | |
2995 | if (!fs_2625) |
4025 | if (!fs_2625) |
2996 | w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; |
4026 | w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; |
2997 | else if (!statfs (w->path, &sfs) |
4027 | else if (!statfs (w->path, &sfs) |
2998 | && (sfs.f_type == 0x1373 /* devfs */ |
4028 | && (sfs.f_type == 0x1373 /* devfs */ |
|
|
4029 | || sfs.f_type == 0x4006 /* fat */ |
|
|
4030 | || sfs.f_type == 0x4d44 /* msdos */ |
2999 | || sfs.f_type == 0xEF53 /* ext2/3 */ |
4031 | || sfs.f_type == 0xEF53 /* ext2/3 */ |
|
|
4032 | || sfs.f_type == 0x72b6 /* jffs2 */ |
|
|
4033 | || sfs.f_type == 0x858458f6 /* ramfs */ |
|
|
4034 | || sfs.f_type == 0x5346544e /* ntfs */ |
3000 | || sfs.f_type == 0x3153464a /* jfs */ |
4035 | || sfs.f_type == 0x3153464a /* jfs */ |
|
|
4036 | || sfs.f_type == 0x9123683e /* btrfs */ |
3001 | || sfs.f_type == 0x52654973 /* reiser3 */ |
4037 | || sfs.f_type == 0x52654973 /* reiser3 */ |
3002 | || sfs.f_type == 0x01021994 /* tempfs */ |
4038 | || sfs.f_type == 0x01021994 /* tmpfs */ |
3003 | || sfs.f_type == 0x58465342 /* xfs */)) |
4039 | || sfs.f_type == 0x58465342 /* xfs */)) |
3004 | w->timer.repeat = 0.; /* filesystem is local, kernel new enough */ |
4040 | w->timer.repeat = 0.; /* filesystem is local, kernel new enough */ |
3005 | else |
4041 | else |
3006 | w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */ |
4042 | w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */ |
3007 | } |
4043 | } |
… | |
… | |
3028 | if (!pend || pend == path) |
4064 | if (!pend || pend == path) |
3029 | break; |
4065 | break; |
3030 | |
4066 | |
3031 | *pend = 0; |
4067 | *pend = 0; |
3032 | w->wd = inotify_add_watch (fs_fd, path, mask); |
4068 | w->wd = inotify_add_watch (fs_fd, path, mask); |
3033 | } |
4069 | } |
3034 | while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); |
4070 | while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); |
3035 | } |
4071 | } |
3036 | } |
4072 | } |
3037 | |
4073 | |
3038 | if (w->wd >= 0) |
4074 | if (w->wd >= 0) |
… | |
… | |
3105 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
4141 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
3106 | ofs += sizeof (struct inotify_event) + ev->len; |
4142 | ofs += sizeof (struct inotify_event) + ev->len; |
3107 | } |
4143 | } |
3108 | } |
4144 | } |
3109 | |
4145 | |
3110 | inline_size void |
4146 | inline_size void ecb_cold |
3111 | ev_check_2625 (EV_P) |
4147 | ev_check_2625 (EV_P) |
3112 | { |
4148 | { |
3113 | /* kernels < 2.6.25 are borked |
4149 | /* kernels < 2.6.25 are borked |
3114 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
4150 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
3115 | */ |
4151 | */ |
… | |
… | |
3120 | } |
4156 | } |
3121 | |
4157 | |
3122 | inline_size int |
4158 | inline_size int |
3123 | infy_newfd (void) |
4159 | infy_newfd (void) |
3124 | { |
4160 | { |
3125 | #if defined (IN_CLOEXEC) && defined (IN_NONBLOCK) |
4161 | #if defined IN_CLOEXEC && defined IN_NONBLOCK |
3126 | int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); |
4162 | int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); |
3127 | if (fd >= 0) |
4163 | if (fd >= 0) |
3128 | return fd; |
4164 | return fd; |
3129 | #endif |
4165 | #endif |
3130 | return inotify_init (); |
4166 | return inotify_init (); |
… | |
… | |
3205 | #else |
4241 | #else |
3206 | # define EV_LSTAT(p,b) lstat (p, b) |
4242 | # define EV_LSTAT(p,b) lstat (p, b) |
3207 | #endif |
4243 | #endif |
3208 | |
4244 | |
3209 | void |
4245 | void |
3210 | ev_stat_stat (EV_P_ ev_stat *w) |
4246 | ev_stat_stat (EV_P_ ev_stat *w) EV_THROW |
3211 | { |
4247 | { |
3212 | if (lstat (w->path, &w->attr) < 0) |
4248 | if (lstat (w->path, &w->attr) < 0) |
3213 | w->attr.st_nlink = 0; |
4249 | w->attr.st_nlink = 0; |
3214 | else if (!w->attr.st_nlink) |
4250 | else if (!w->attr.st_nlink) |
3215 | w->attr.st_nlink = 1; |
4251 | w->attr.st_nlink = 1; |
… | |
… | |
3254 | ev_feed_event (EV_A_ w, EV_STAT); |
4290 | ev_feed_event (EV_A_ w, EV_STAT); |
3255 | } |
4291 | } |
3256 | } |
4292 | } |
3257 | |
4293 | |
3258 | void |
4294 | void |
3259 | ev_stat_start (EV_P_ ev_stat *w) |
4295 | ev_stat_start (EV_P_ ev_stat *w) EV_THROW |
3260 | { |
4296 | { |
3261 | if (expect_false (ev_is_active (w))) |
4297 | if (expect_false (ev_is_active (w))) |
3262 | return; |
4298 | return; |
3263 | |
4299 | |
3264 | ev_stat_stat (EV_A_ w); |
4300 | ev_stat_stat (EV_A_ w); |
… | |
… | |
3285 | |
4321 | |
3286 | EV_FREQUENT_CHECK; |
4322 | EV_FREQUENT_CHECK; |
3287 | } |
4323 | } |
3288 | |
4324 | |
3289 | void |
4325 | void |
3290 | ev_stat_stop (EV_P_ ev_stat *w) |
4326 | ev_stat_stop (EV_P_ ev_stat *w) EV_THROW |
3291 | { |
4327 | { |
3292 | clear_pending (EV_A_ (W)w); |
4328 | clear_pending (EV_A_ (W)w); |
3293 | if (expect_false (!ev_is_active (w))) |
4329 | if (expect_false (!ev_is_active (w))) |
3294 | return; |
4330 | return; |
3295 | |
4331 | |
… | |
… | |
3311 | } |
4347 | } |
3312 | #endif |
4348 | #endif |
3313 | |
4349 | |
3314 | #if EV_IDLE_ENABLE |
4350 | #if EV_IDLE_ENABLE |
3315 | void |
4351 | void |
3316 | ev_idle_start (EV_P_ ev_idle *w) |
4352 | ev_idle_start (EV_P_ ev_idle *w) EV_THROW |
3317 | { |
4353 | { |
3318 | if (expect_false (ev_is_active (w))) |
4354 | if (expect_false (ev_is_active (w))) |
3319 | return; |
4355 | return; |
3320 | |
4356 | |
3321 | pri_adjust (EV_A_ (W)w); |
4357 | pri_adjust (EV_A_ (W)w); |
… | |
… | |
3334 | |
4370 | |
3335 | EV_FREQUENT_CHECK; |
4371 | EV_FREQUENT_CHECK; |
3336 | } |
4372 | } |
3337 | |
4373 | |
3338 | void |
4374 | void |
3339 | ev_idle_stop (EV_P_ ev_idle *w) |
4375 | ev_idle_stop (EV_P_ ev_idle *w) EV_THROW |
3340 | { |
4376 | { |
3341 | clear_pending (EV_A_ (W)w); |
4377 | clear_pending (EV_A_ (W)w); |
3342 | if (expect_false (!ev_is_active (w))) |
4378 | if (expect_false (!ev_is_active (w))) |
3343 | return; |
4379 | return; |
3344 | |
4380 | |
… | |
… | |
3358 | } |
4394 | } |
3359 | #endif |
4395 | #endif |
3360 | |
4396 | |
3361 | #if EV_PREPARE_ENABLE |
4397 | #if EV_PREPARE_ENABLE |
3362 | void |
4398 | void |
3363 | ev_prepare_start (EV_P_ ev_prepare *w) |
4399 | ev_prepare_start (EV_P_ ev_prepare *w) EV_THROW |
3364 | { |
4400 | { |
3365 | if (expect_false (ev_is_active (w))) |
4401 | if (expect_false (ev_is_active (w))) |
3366 | return; |
4402 | return; |
3367 | |
4403 | |
3368 | EV_FREQUENT_CHECK; |
4404 | EV_FREQUENT_CHECK; |
… | |
… | |
3373 | |
4409 | |
3374 | EV_FREQUENT_CHECK; |
4410 | EV_FREQUENT_CHECK; |
3375 | } |
4411 | } |
3376 | |
4412 | |
3377 | void |
4413 | void |
3378 | ev_prepare_stop (EV_P_ ev_prepare *w) |
4414 | ev_prepare_stop (EV_P_ ev_prepare *w) EV_THROW |
3379 | { |
4415 | { |
3380 | clear_pending (EV_A_ (W)w); |
4416 | clear_pending (EV_A_ (W)w); |
3381 | if (expect_false (!ev_is_active (w))) |
4417 | if (expect_false (!ev_is_active (w))) |
3382 | return; |
4418 | return; |
3383 | |
4419 | |
… | |
… | |
3396 | } |
4432 | } |
3397 | #endif |
4433 | #endif |
3398 | |
4434 | |
3399 | #if EV_CHECK_ENABLE |
4435 | #if EV_CHECK_ENABLE |
3400 | void |
4436 | void |
3401 | ev_check_start (EV_P_ ev_check *w) |
4437 | ev_check_start (EV_P_ ev_check *w) EV_THROW |
3402 | { |
4438 | { |
3403 | if (expect_false (ev_is_active (w))) |
4439 | if (expect_false (ev_is_active (w))) |
3404 | return; |
4440 | return; |
3405 | |
4441 | |
3406 | EV_FREQUENT_CHECK; |
4442 | EV_FREQUENT_CHECK; |
… | |
… | |
3411 | |
4447 | |
3412 | EV_FREQUENT_CHECK; |
4448 | EV_FREQUENT_CHECK; |
3413 | } |
4449 | } |
3414 | |
4450 | |
3415 | void |
4451 | void |
3416 | ev_check_stop (EV_P_ ev_check *w) |
4452 | ev_check_stop (EV_P_ ev_check *w) EV_THROW |
3417 | { |
4453 | { |
3418 | clear_pending (EV_A_ (W)w); |
4454 | clear_pending (EV_A_ (W)w); |
3419 | if (expect_false (!ev_is_active (w))) |
4455 | if (expect_false (!ev_is_active (w))) |
3420 | return; |
4456 | return; |
3421 | |
4457 | |
… | |
… | |
3434 | } |
4470 | } |
3435 | #endif |
4471 | #endif |
3436 | |
4472 | |
3437 | #if EV_EMBED_ENABLE |
4473 | #if EV_EMBED_ENABLE |
3438 | void noinline |
4474 | void noinline |
3439 | ev_embed_sweep (EV_P_ ev_embed *w) |
4475 | ev_embed_sweep (EV_P_ ev_embed *w) EV_THROW |
3440 | { |
4476 | { |
3441 | ev_run (w->other, EVRUN_NOWAIT); |
4477 | ev_run (w->other, EVRUN_NOWAIT); |
3442 | } |
4478 | } |
3443 | |
4479 | |
3444 | static void |
4480 | static void |
… | |
… | |
3492 | ev_idle_stop (EV_A_ idle); |
4528 | ev_idle_stop (EV_A_ idle); |
3493 | } |
4529 | } |
3494 | #endif |
4530 | #endif |
3495 | |
4531 | |
3496 | void |
4532 | void |
3497 | ev_embed_start (EV_P_ ev_embed *w) |
4533 | ev_embed_start (EV_P_ ev_embed *w) EV_THROW |
3498 | { |
4534 | { |
3499 | if (expect_false (ev_is_active (w))) |
4535 | if (expect_false (ev_is_active (w))) |
3500 | return; |
4536 | return; |
3501 | |
4537 | |
3502 | { |
4538 | { |
… | |
… | |
3523 | |
4559 | |
3524 | EV_FREQUENT_CHECK; |
4560 | EV_FREQUENT_CHECK; |
3525 | } |
4561 | } |
3526 | |
4562 | |
3527 | void |
4563 | void |
3528 | ev_embed_stop (EV_P_ ev_embed *w) |
4564 | ev_embed_stop (EV_P_ ev_embed *w) EV_THROW |
3529 | { |
4565 | { |
3530 | clear_pending (EV_A_ (W)w); |
4566 | clear_pending (EV_A_ (W)w); |
3531 | if (expect_false (!ev_is_active (w))) |
4567 | if (expect_false (!ev_is_active (w))) |
3532 | return; |
4568 | return; |
3533 | |
4569 | |
… | |
… | |
3543 | } |
4579 | } |
3544 | #endif |
4580 | #endif |
3545 | |
4581 | |
3546 | #if EV_FORK_ENABLE |
4582 | #if EV_FORK_ENABLE |
3547 | void |
4583 | void |
3548 | ev_fork_start (EV_P_ ev_fork *w) |
4584 | ev_fork_start (EV_P_ ev_fork *w) EV_THROW |
3549 | { |
4585 | { |
3550 | if (expect_false (ev_is_active (w))) |
4586 | if (expect_false (ev_is_active (w))) |
3551 | return; |
4587 | return; |
3552 | |
4588 | |
3553 | EV_FREQUENT_CHECK; |
4589 | EV_FREQUENT_CHECK; |
… | |
… | |
3558 | |
4594 | |
3559 | EV_FREQUENT_CHECK; |
4595 | EV_FREQUENT_CHECK; |
3560 | } |
4596 | } |
3561 | |
4597 | |
3562 | void |
4598 | void |
3563 | ev_fork_stop (EV_P_ ev_fork *w) |
4599 | ev_fork_stop (EV_P_ ev_fork *w) EV_THROW |
3564 | { |
4600 | { |
3565 | clear_pending (EV_A_ (W)w); |
4601 | clear_pending (EV_A_ (W)w); |
3566 | if (expect_false (!ev_is_active (w))) |
4602 | if (expect_false (!ev_is_active (w))) |
3567 | return; |
4603 | return; |
3568 | |
4604 | |
… | |
… | |
3581 | } |
4617 | } |
3582 | #endif |
4618 | #endif |
3583 | |
4619 | |
3584 | #if EV_CLEANUP_ENABLE |
4620 | #if EV_CLEANUP_ENABLE |
3585 | void |
4621 | void |
3586 | ev_cleanup_start (EV_P_ ev_cleanup *w) |
4622 | ev_cleanup_start (EV_P_ ev_cleanup *w) EV_THROW |
3587 | { |
4623 | { |
3588 | if (expect_false (ev_is_active (w))) |
4624 | if (expect_false (ev_is_active (w))) |
3589 | return; |
4625 | return; |
3590 | |
4626 | |
3591 | EV_FREQUENT_CHECK; |
4627 | EV_FREQUENT_CHECK; |
… | |
… | |
3598 | ev_unref (EV_A); |
4634 | ev_unref (EV_A); |
3599 | EV_FREQUENT_CHECK; |
4635 | EV_FREQUENT_CHECK; |
3600 | } |
4636 | } |
3601 | |
4637 | |
3602 | void |
4638 | void |
3603 | ev_cleanup_stop (EV_P_ ev_cleanup *w) |
4639 | ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_THROW |
3604 | { |
4640 | { |
3605 | clear_pending (EV_A_ (W)w); |
4641 | clear_pending (EV_A_ (W)w); |
3606 | if (expect_false (!ev_is_active (w))) |
4642 | if (expect_false (!ev_is_active (w))) |
3607 | return; |
4643 | return; |
3608 | |
4644 | |
… | |
… | |
3622 | } |
4658 | } |
3623 | #endif |
4659 | #endif |
3624 | |
4660 | |
3625 | #if EV_ASYNC_ENABLE |
4661 | #if EV_ASYNC_ENABLE |
3626 | void |
4662 | void |
3627 | ev_async_start (EV_P_ ev_async *w) |
4663 | ev_async_start (EV_P_ ev_async *w) EV_THROW |
3628 | { |
4664 | { |
3629 | if (expect_false (ev_is_active (w))) |
4665 | if (expect_false (ev_is_active (w))) |
3630 | return; |
4666 | return; |
3631 | |
4667 | |
3632 | w->sent = 0; |
4668 | w->sent = 0; |
… | |
… | |
3641 | |
4677 | |
3642 | EV_FREQUENT_CHECK; |
4678 | EV_FREQUENT_CHECK; |
3643 | } |
4679 | } |
3644 | |
4680 | |
3645 | void |
4681 | void |
3646 | ev_async_stop (EV_P_ ev_async *w) |
4682 | ev_async_stop (EV_P_ ev_async *w) EV_THROW |
3647 | { |
4683 | { |
3648 | clear_pending (EV_A_ (W)w); |
4684 | clear_pending (EV_A_ (W)w); |
3649 | if (expect_false (!ev_is_active (w))) |
4685 | if (expect_false (!ev_is_active (w))) |
3650 | return; |
4686 | return; |
3651 | |
4687 | |
… | |
… | |
3662 | |
4698 | |
3663 | EV_FREQUENT_CHECK; |
4699 | EV_FREQUENT_CHECK; |
3664 | } |
4700 | } |
3665 | |
4701 | |
3666 | void |
4702 | void |
3667 | ev_async_send (EV_P_ ev_async *w) |
4703 | ev_async_send (EV_P_ ev_async *w) EV_THROW |
3668 | { |
4704 | { |
3669 | w->sent = 1; |
4705 | w->sent = 1; |
3670 | evpipe_write (EV_A_ &async_pending); |
4706 | evpipe_write (EV_A_ &async_pending); |
3671 | } |
4707 | } |
3672 | #endif |
4708 | #endif |
… | |
… | |
3709 | |
4745 | |
3710 | once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io)); |
4746 | once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io)); |
3711 | } |
4747 | } |
3712 | |
4748 | |
3713 | void |
4749 | void |
3714 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
4750 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_THROW |
3715 | { |
4751 | { |
3716 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
4752 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
3717 | |
4753 | |
3718 | if (expect_false (!once)) |
4754 | if (expect_false (!once)) |
3719 | { |
4755 | { |
… | |
… | |
3740 | } |
4776 | } |
3741 | |
4777 | |
3742 | /*****************************************************************************/ |
4778 | /*****************************************************************************/ |
3743 | |
4779 | |
3744 | #if EV_WALK_ENABLE |
4780 | #if EV_WALK_ENABLE |
3745 | void |
4781 | void ecb_cold |
3746 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
4782 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) EV_THROW |
3747 | { |
4783 | { |
3748 | int i, j; |
4784 | int i, j; |
3749 | ev_watcher_list *wl, *wn; |
4785 | ev_watcher_list *wl, *wn; |
3750 | |
4786 | |
3751 | if (types & (EV_IO | EV_EMBED)) |
4787 | if (types & (EV_IO | EV_EMBED)) |
… | |
… | |
3794 | cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i])); |
4830 | cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i])); |
3795 | #endif |
4831 | #endif |
3796 | |
4832 | |
3797 | #if EV_IDLE_ENABLE |
4833 | #if EV_IDLE_ENABLE |
3798 | if (types & EV_IDLE) |
4834 | if (types & EV_IDLE) |
3799 | for (j = NUMPRI; i--; ) |
4835 | for (j = NUMPRI; j--; ) |
3800 | for (i = idlecnt [j]; i--; ) |
4836 | for (i = idlecnt [j]; i--; ) |
3801 | cb (EV_A_ EV_IDLE, idles [j][i]); |
4837 | cb (EV_A_ EV_IDLE, idles [j][i]); |
3802 | #endif |
4838 | #endif |
3803 | |
4839 | |
3804 | #if EV_FORK_ENABLE |
4840 | #if EV_FORK_ENABLE |
… | |
… | |
3857 | |
4893 | |
3858 | #if EV_MULTIPLICITY |
4894 | #if EV_MULTIPLICITY |
3859 | #include "ev_wrap.h" |
4895 | #include "ev_wrap.h" |
3860 | #endif |
4896 | #endif |
3861 | |
4897 | |
3862 | EV_CPP(}) |
|
|
3863 | |
|
|