Commit Graph

32 Commits

Author SHA1 Message Date
Ben Noordhuis
375ba2d76d unix: use POLL{IN,OUT,etc} constants directly
Remove the UV__POLL defines and use POLL{IN,OUT,etc} directly.
On Linux, we lean on the fact that the POLL constants correspond
one-to-one to their EPOLL counterparts.

Fixes: https://github.com/libuv/libuv/issues/816
PR-URL: https://github.com/libuv/libuv/pull/817
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
2016-04-11 10:51:13 +02:00
Ben Noordhuis
442b8a5a84 unix: use QUEUE_MOVE when iterating over lists
Replace uses of QUEUE_FOREACH when the list can get modified while
iterating over it, in particular when a callback is made into the
user's code.  This should fix a number of spurious failures that
people have been reporting.

PR-URL: https://github.com/libuv/libuv/pull/565
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
2015-10-08 21:47:43 +02:00
Michael Penick
4ed237278c unix: fix for uv_async data race
There's a data race in the consuming side of uv_async. The "pending"
flag could be trampled by producing thread causing an async send
event to be missed.

PR-URL: https://github.com/libuv/libuv/pull/189
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
2015-02-18 21:44:37 +01:00
Saúl Ibarra Corretgé
4ce5470f3a unix: fix uv__open_cloexec usage
It returns the fd or the negated errno.
2014-05-12 11:23:13 +02:00
Ben Noordhuis
86831fe99c linux: reduce file descriptor count of async pipe
Reopen one of the pipe descriptors as read/write through the procfs.
Allows us to close the original pipe file descriptors, saving a file
descriptor on kernels that don't support eventfd(2).
2014-04-09 20:27:42 +04:00
Saúl Ibarra Corretgé
db2a9072bc unix, windows: removed unused status parameter
async, timer, prepare, idle and check handles don't need the status
parameter.
2014-03-17 21:42:36 +01:00
Ben Noordhuis
359d667893 unix: sanity-check fds before closing
Ensure that close() system calls don't close stdio file descriptors
because that is almost never the intention.

This is also a partial workaround for a kernel bug that seems to affect
all Linux kernels when stdin is a pipe that gets closed: fd 0 keeps
signalling EPOLLHUP but a subsequent call to epoll_ctl(EPOLL_CTL_DEL)
fails with EBADF.  See joyent/node#6271 for details and a test case.
2013-10-01 03:55:54 +02:00
Ben Noordhuis
1510ce81fd unix, windows: allow NULL async callback
Allow a NULL callback so the user doesn't have to provide a dummy when
the actual event is processed by e.g. a check handle callback.
2013-08-12 08:55:51 +02:00
Ben Noordhuis
3ee4d3f183 unix, windows: return error codes directly
This commit changes the libuv API to return error codes directly rather
than storing them in a loop-global field.

A code snippet like this one:

    if (uv_foo(loop) < 0) {
      uv_err_t err = uv_last_error(loop);
      fprintf(stderr, "%s\n", uv_strerror(err));
    }

Should be rewritten like this:

    int err = uv_foo(loop);
    if (err < 0)
      fprintf(stderr, "%s\n", uv_strerror(err));

The rationale for this change is that it should make creating bindings
for other languages a lot easier: dealing with struct return values is
painful with most FFIs and often downright buggy.
2013-07-07 09:51:00 +02:00
Ben Noordhuis
0635e29714 unix, windows: remove ngx-queue.h
Avoids an extra #include in public headers and stops the ngx_queue_*
types and macros from leaking into user code.
2013-03-27 00:09:36 +01:00
Ben Noordhuis
b04fc33ef7 linux: use eventfds for async handles
Use eventfds to drive async handles, fall back to regular pipes on older
kernels (pre-2.6.22).

Gives a nice boost on the async handle benchmarks. Before:

  12,516,113 async events in 5.0 seconds (2,503,222/s, 1,048,576 unique
  async1: 11.95 sec (83,701/sec)
  async2: 11.65 sec (85,862/sec)
  async4: 5.20 sec (192,154/sec)
  async8: 9.97 sec (100,315/sec)
  async_pummel_1: 1,000,000 callbacks in 2.56 seconds (389,919/sec)
  async_pummel_2: 1,000,000 callbacks in 2.65 seconds (377,205/sec)
  async_pummel_4: 1,000,000 callbacks in 2.18 seconds (457,704/sec)
  async_pummel_8: 1,000,000 callbacks in 4.19 seconds (238,632/sec)

After:

  16,168,081 async events in 5.0 seconds (3,233,616/s, 1,048,576 unique
  async1: 11.08 sec (90,213/sec)
  async2: 10.17 sec (98,297/sec)
  async4: 4.81 sec (207,789/sec)
  async8: 8.98 sec (111,419/sec)
  async_pummel_1: 1,000,000 callbacks in 1.16 seconds (863,296/sec)
  async_pummel_2: 1,000,000 callbacks in 1.45 seconds (691,459/sec)
  async_pummel_4: 1,000,000 callbacks in 0.66 seconds (1,514,770/sec)
  async_pummel_8: 1,000,000 callbacks in 1.42 seconds (704,549/sec)

That's a speedup from anywhere between 10% to 330%.
2013-02-25 02:55:24 +01:00
Ben Noordhuis
92151658eb unix: abstract away async pipe infrastructure
This commit lays the groundwork for the switch to eventfds on Linux.
2013-02-25 02:36:52 +01:00
Ben Noordhuis
cb3c448d8f unix: fix GNU-ism introduced in edd1007
Don't use features.h, it's only available on GNU/glibc systems.
2013-01-07 15:43:58 +01:00
Ben Noordhuis
edd10071eb unix: fix up #if defined checks
`#if FOO` (where FOO is undefined) is a legal construct in C89 and C99
but gcc, clang and sparse complain loudly about it at higher warning
levels.

Squelch those warnings. Makes the code more consistent as well.
2013-01-06 22:31:47 +01:00
Fedor Indutny
731adacad2 unix: use select() for specific fds on OS X
kqueue(2) on osx doesn't work (emits EINVAL error) with specific fds
(i.e. /dev/tty, /dev/null, etc). When given such descriptors - start
select(2) watcher thread that will emit io events.
2012-12-09 15:43:08 +01:00
Ben Noordhuis
65bb6f068e unix: rename UV__IO_* constants 2012-11-16 17:33:29 +01:00
Ben Noordhuis
1282d64868 unix: remove dependency on libev 2012-11-16 17:33:25 +01:00
Ben Noordhuis
b7f38b1e53 Revert "unix: avoid iterating over all async handles"
This reverts commit 209abbab27.

Fixes the following SIGSEGV:

  (gdb) f 1
  #1  0x00007fc084683aec in uv__async_io (loop=0x7fc0848e0b40,
  handle=0x7fc0848e0c78, events=1) at src/unix/async.c:175
  175             ASYNC_CB(h)
  (gdb) list
  170
  171         /* If we need to sweep all handles anyway - skip this loop */
  172         if (!loop->async_sweep_needed) {
  173           for (i = 0; i < end; i += sizeof(h)) {
  174             h = *((uv_async_t**) (buf + i));
  175             ASYNC_CB(h)
  176           }
  177         }
  178
  179         bytes -= end;
  (gdb) print *h
  $1 = {close_cb = 0x184e1b0, data = 0x18d9520, loop = 0x7fc0848e0b40,
  type = 49, handle_queue = {prev = 0x18dae10, next = 0x7860c0}, flags = 32,
  next_closing = 0x1863b40, pending = 0, async_cb = 0x31,
  queue = {prev = 0x18dae50, next = 0x7860c0}}
  (gdb)

It looks like the async handle gets closed or otherwise becomes invalid before
the sweep is executed.

Fixes #603.
2012-10-24 14:58:49 +02:00
Ben Noordhuis
1bb1ba27dd unix: fix compiler warning in async.c
Include missing <string.h> header. Fixes the following compiler warning:

  src/unix/async.c:182:7: warning: implicit declaration of
  function ‘memmove’ [-Wimplicit-function-declaration]
2012-10-20 23:36:00 +02:00
Fedor Indutny
209abbab27 unix: avoid iterating over all async handles 2012-10-19 17:22:30 +02:00
Ben Noordhuis
fd136da04a unix: remove always_inline attribute
Fixes the following gcc 4.7+ warning:

  ../src/unix/internal.h:105:13: warning: always_inline function might not be
  inlinable [-Wattributes]

gcc wants the always_inline function to be annotated with the 'inline' keyword
which we can't do because we compile in C89 mode.

Using __inline is not an option because that makes clang generate warnings when
-Wlanguage-extension-token is enabled.

Therefore, remove the always_inline attribute altogether and hope that the
compiler is smart enough to inline the functions.
2012-10-15 01:08:47 +02:00
Ben Noordhuis
ff0a93a04f unix: fix clang -Wlanguage-extension-token warnings 2012-09-01 00:27:57 +02:00
Bert Belder
a787a16ac3 unix: fix uv_async_send not working with Sun Studio
uv_async_send would always return 1 when non-gcc compilers were used.
When uv_async_send returns 1 no attempt is made to make port_getn
return, so in this situation uv_async_send didn't wake up the event
loop.
2012-08-22 20:40:14 +02:00
Ben Noordhuis
837edf4c0f unix, windows: remove handle init counters
Remove the handle init counters, no one uses them.
2012-08-10 02:00:11 +02:00
Ben Noordhuis
c5761f72b3 unix: speed up uv_async_send() some more still
__sync_val_compare_and_swap() emits a CMPXCHG instruction on i386 and x86_64.
Use XCHG instead, it's about four times faster.
2012-07-09 02:46:19 +02:00
Ben Noordhuis
3d9c1ebfeb unix: speed up uv_async_send() some more
Use atomic compare-and-swap to detect if we've been preempted by another thread
and therefore can avoid making the expensive write() syscall.

Speeds up the heavily contended case by about 1-2% and has little if any impact
on the non-contended case. I wasn't able to measure the difference at any rate.
2012-07-02 04:30:48 +02:00
Ben Noordhuis
4c87666a93 unix: speed up uv_async_send()
Don't make a syscall when the handle is already pending.

Speeds up the async_pummel benchmark by about 13%.
2012-06-29 03:18:09 +02:00
Ben Noordhuis
78bc0d6134 unix: implement async handles in libuv
Replace libev backed async handles with a pure libuv implementation.
2012-06-11 04:19:31 +02:00
Bert Belder
5c30443555 unix: uv_async handles should not be unref'ed automatically 2012-06-10 02:25:20 +02:00
Ben Noordhuis
9efa8b3571 unix, windows: rework reference counting scheme
This commit changes how the event loop determines if it needs to stay alive.

Previously, an internal counter was increased whenever a handle got created
and decreased again when the handle was closed.

While conceptually simple, it turned out hard to work with: you often want
to keep the event loop alive only if the handle is actually doing something.
Stopped or inactive handles were a frequent source of hanging event loops.

That's why this commit changes the reference counting scheme to a model where
a handle only references the event loop when it's active. 'Active' means
different things for different handle types, e.g.:

 * timers: ticking
 * sockets: reading, writing or listening
 * processes: always active (for now, subject to change)
 * idle, check, prepare: only active when started

This commit also changes how the uv_ref() and uv_unref() functions work: they
now operate on the level of individual handles, not the whole event loop.

The Windows implementation was done by Bert Belder.
2012-05-17 07:07:53 +02:00
Ben Noordhuis
5a8446c309 unix: move handle specific close logic out of core.c 2012-04-04 05:30:15 -07:00
Ben Noordhuis
f7359a335c unix: move async code from core.c to async.c 2012-04-04 05:25:34 +02:00