Commit Graph

196 Commits

Author SHA1 Message Date
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
e0bdb3dbc9 unix, windows: move uv_now() to uv-common.c 2013-05-29 16:13:34 +02:00
Ben Noordhuis
8e16f8e056 unix: add uv__is_closing() macro 2013-05-26 23:02:17 +02:00
Ben Noordhuis
f6d8ba3c9a unix, windows: run expired timers in run-once mode
Before this commit, creating an event loop, starting a timer and
calling uv_run(UV_RUN_ONCE) blocked in uv_run() until the timer
expired - but didn't actually run the timer.
2013-05-30 00:33:23 +02:00
Ben Noordhuis
8ef9592a95 Merge remote-tracking branch 'origin/v0.10'
Conflicts:
	ChangeLog
	src/unix/stream.c
	src/version.c
2013-05-29 23:32:07 +02:00
Ben Noordhuis
636a13b8c4 unix: fix stream refcounting buglet
Fix a buglet where uv_read_stop() would mark the handle as stopped even
when there are in-progress write requests.

This bug is unlikely to have affected anyone, the only case where it
has a user-visible effect is when:

  a) the handle has been stopped for reading but not writing, and
  b) it's the last active handle in the event loop's pollset

If both conditions are met, it's possible for the event loop to
terminate prematurely.

This reapplies commit 80f2f82 which was temporarily reverted in fe7b154
because it was making a lot of node.js tests fail on OS X with the
following assertion:

    Assertion failed: (!uv__is_active(handle)), function
    uv__finish_close, file ../../deps/uv/src/unix/core.c, line 165.

Expecting that the handle is inactive when the state is UV_CLOSING
turns out to be a bad assumption: it's possible that the handle is
executing (for example) a shutdown request when uv__finish_close()
is called. That's okay, uv__stream_destroy() takes care of that.

The issue wasn't specific to OS X, it was just more visible on that
platform. (Slow) debug builds on Linux exhibited the same behavior.
2013-05-25 01:30:05 +02:00
Ben Noordhuis
ebdeaed2e3 Merge remote-tracking branch 'origin/v0.10'
Conflicts:
	ChangeLog
	src/version.c
2013-05-02 09:22:39 +02:00
Ben Noordhuis
105e4dcb23 unix: silence STATIC_ASSERT compiler warnings
Fix the following two warnings:

  src/unix/core.c:74:1: warning: ISO C90 forbids array
  'static_assert_failed' whose size can't be evaluated [-Wvla]
  src/unix/core.c:76:1: warning: ISO C90 forbids array
  'static_assert_failed' whose size can't be evaluated [-Wvla]
2013-04-22 07:48:09 +02:00
Ben Noordhuis
f78bcfbd6a Merge remote-tracking branch 'origin/v0.10' 2013-04-10 14:24:18 +02:00
Timothy J Fontaine
e1ffc6c0ee unix: dtrace probes for tick-start and tick-stop 2013-04-10 14:10:05 +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
b6a50c7295 unix: add uv_buf_t static asserts to core.c
Verify that our uv_buf_t type is ABI-compatible with struct iovec.
2013-03-07 00:53:06 +01:00
Ben Noordhuis
492efb95ff unix: make uv_stop work when embedded
* Make uv_stop() work when libuv is embedded in another event loop.

* Fix a small bug where loop->stop_flag was not reset when mode ==
  UV_RUN_ONCE or UV_RUN_NOWAIT. The next call to uv_run() would return
  immediately without doing any work.
2013-02-25 17:01:02 +01:00
Saúl Ibarra Corretgé
bb3d1e24da unix, windows: add uv_stop, stop running event loop 2013-02-25 16:21:37 +01:00
Ben Noordhuis
c98083ef26 unix: short-circuit on no-op io watcher changes
Don't add the io watcher to the watcher queue if the requested change
is effectively a no-op, that is, when the event mask doesn't change.

The exception here is sunos because the event ports backend requires
that watched file descriptors are re-added on every turn of the event
loop.

This commit is a micro-optimization, it does not change the event
loop's observable behavior in any way.
2013-02-22 17:02:05 +01:00
Ben Noordhuis
d6bfedb862 unix, windows: make uv_now() return uint64_t
Using int64_t doesn't make sense here because the return value is never
negative.
2013-02-20 16:58:45 +01:00
Fedor Indutny
c15d4a7c62 stream: use kevent() information before accept()
Limit number of syscalls by using backlog length information provided by
kevent().
2013-02-09 00:54:08 +04:00
Ben Noordhuis
4ba03ddd56 unix, windows: rename uv_run2 to uv_run
This changes the prototype of uv_run() from:

  int uv_run(uv_loop_t* loop);

To:

  int uv_run(uv_loop_t* loop, uv_run_mode mode);

Where `mode` is UV_RUN_DEFAULT, UV_RUN_ONCE or UV_RUN_NOWAIT.

Fixes #683.
2013-01-16 23:35:29 +01:00
Ben Noordhuis
80f6a9c643 unix: omit second fcntl() call if possible
Omit the fcntl() syscall when the O_NONBLOCK or FD_CLOEXEC is already
set/clear because it's a no-op in that case.
2013-01-10 01:25:40 +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
Ben Noordhuis
87cbf8d0c2 unix: remove unused function uv__strlcpy() 2013-01-06 21:25:58 +01:00
Ben Noordhuis
2f55353490 unix: update loop->time after poll
Fixes a bug where timers expire prematurely when the following
conditions hold:

  a) libuv first spends some time blocked in the platform poll function
  b) a callback then calls uv_timer_start()

Cause: uv_timer_start() uses an out-of-date loop->time in its
'when should the timer callback run?' calculations.

Solution: Update loop->time before invoking any callbacks.

Fixes #678.
2013-01-06 18:30:03 +01:00
Ben Noordhuis
339033afc0 unix: use uv__hrtime() internally
This commit renames the various uv_hrtime() implementations to uv__hrtime().

Libuv uses the high-res timer internally in performance-critical code paths.
Calling the non-public version avoids going through the PLT when libuv is
compiled as a shared object.

The exported uv_hrtime() now has a single definition in src/unix/core.c that
calls uv__hrtime().

A future optimization is to lift the uv__hrtime() declarations into header
files so they can be inlined at the call sites. Then again, linking with -flto
should accomplish the same thing.
2013-01-06 17:35:34 +01:00
Saúl Ibarra Corretgé
4650a5971b unix: don't run loop when all handles are unref'd 2012-12-24 17:05:52 +01:00
Saúl Ibarra Corretgé
0820be7008 Implemented uv_run2
Allows for running the event loop in 3 modes:
  * default: loop runs until the refcount drops to zero
  * once: poll for events only once and block until one is handled
  * nowait: poll for events only once but don't block if there are
    no pending events
2012-12-18 16:11:23 +01:00
Ben Noordhuis
e079a99abd unix: fix event loop stall
Fix a rather obscure bug where the event loop stalls when an I/O watcher is
stopped while an artificial event, generated with uv__io_feed(), is pending.
2012-12-13 20:17:12 +01:00
Ben Noordhuis
4a69c4bb5f unix: change uv_backend_timeout() prototype
* change return value to signed int
* constify loop argument
2012-11-28 17:02:30 +01:00
Fedor Indutny
09a7f85b70 unix: add uv_backend_fd() and uv_backend_timeout()
This can be used in conjuction with uv_run_once() to poll in one thread and run
the event loop's event callbacks in another.

Useful for embedding libuv's event loop in another event loop.
2012-11-28 16:30:50 +01:00
Ben Noordhuis
90271e1a3c darwin, freebsd: set SO_NOSIGPIPE on sockets
Suppresses delivery of SIGPIPE signals when the remote end of the socket has
gone away.
2012-11-22 17:51:56 +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
Bert Belder
1e32cb01b5 unix: support signal handlers outside the main loop 2012-10-17 01:25:02 +02:00
Bert Belder
39d574dcff unix: make it possible to delay close callbacks 2012-10-17 01:25:00 +02:00
Ben Noordhuis
7ac23ee0c6 unix: move getaddrinfo code to getaddrinfo.c 2012-09-28 08:05:53 +02:00
Saúl Ibarra Corretgé
b7047d6701 unix: remove __read_mostly macro
It creates trouble with llvm-gcc on OS X.
2012-09-13 01:47:34 +02:00
Ben Noordhuis
b12b6498d6 darwin, freebsd: use ioctl(FIOCLEX) and ioctl(FIONBIO)
Set the non-blocking and close-on-exec flags with ioctl() instead of fcntl(),
it's about 10-25% faster.

Stick with fcntl() on Solaris. ioctl(FIONBIO) works but is twice as slow as
fcntl(O_NONBLOCK). ioctl(FIOCLEX) doesn't raise an error but doesn't actually
work either.
2012-08-21 23:21:37 +02:00
Ben Noordhuis
6545e9bda4 linux: tag no_accept4 __read_mostly 2012-08-21 01:03:59 +02:00
Ben Noordhuis
ee50db6e36 unix, windows: preliminary signal handler support
* a no-op on Windows for now
* only supports the main loop on UNIX (again, for now)
2012-08-10 02:00:07 +02:00
Ben Noordhuis
9f7cdb20aa unix: add relaxed accept() setting
Mitigates unfair scheduling in multi-process setups that share a single listen
socket across multiple processes.
2012-08-02 15:58:55 +02:00
Ben Noordhuis
be1032431d Merge branch 'v0.8' 2012-07-29 03:13:21 +02:00
Ben Noordhuis
889ab216ae unix: fix 'zero handles, one request' busy loop
Fixes #484.
2012-07-01 23:59:30 +02:00
Ben Noordhuis
b779a0db74 Merge branch 'v0.8' 2012-06-29 02:28:57 +02:00
Ben Noordhuis
f6a02fbe76 linux: don't use accept4() syscall after ENOSYS
Repeatedly calling the syscall when it's not supported has a small but
measurable performance impact.

Besides, it's a silly thing to do.
2012-06-29 02:23:47 +02:00
Ben Noordhuis
0fde10825e linux: set close-on-exec flag with ioctl(FIOCLEX)
ioctl(FIOCLEX) is 25% faster than fcntl(F_SETFD) on a stock 2.6.32 kernel.
2012-06-24 06:06:33 +02:00
Ben Noordhuis
e21cdf1e0d unix: malloc only once in uv_getaddrinfo() 2012-06-23 04:09:22 +02:00
Ben Noordhuis
f01e9d708a unix: clarify that uv_getaddrinfo_t is a req 2012-06-22 16:40:45 +02:00
Ben Noordhuis
cc7c8542a5 unix, windows: add stat() based file watcher
Monitors a file path for changes. Supersedes ev_stat.
2012-05-31 20:32:24 +02:00
Ben Noordhuis
4d7f1e1864 unix: implement uv_disable_stdio_inheritance() 2012-06-14 01:20:49 +02:00
Bert Belder
ade6930241 windows: implement uv_disable_stdio_inheritance 2012-06-14 01:19:52 +02:00
Ben Noordhuis
94cb06fecf unix: make uv__nonblock() EINTR resilient
It's underspecified if and when ioctl(FIONBIO) or fcntl() can return EINTR.
Let's take the safe route.
2012-06-14 01:19:34 +02:00
Ben Noordhuis
b3a97f8981 unix: make uv__cloexec() EINTR resilient
It's somewhat underspecified if and when fcntl() can return EINTR. It never
does on Linux for F_GETFD or F_SETFD but let's not make any assumptions.
2012-06-14 01:19:34 +02:00
Ben Noordhuis
95e89c6a0e unix, windows: share uv__handle_init() 2012-06-13 01:28:58 +02:00
Ben Noordhuis
dea45940cd unix: fix req cb / close cb invoke delay
Finalization of closed handles and associated requests was sometimes delayed
until an external event (network I/O, timeout, etc.) happened. This commit
addresses that.
2012-06-08 01:37:39 +02:00
Ben Noordhuis
649ad50c32 unix: fix event loop stall
Undoes most of the changes made to libev in 7d2ea31 and c9396dd.
2012-06-06 05:12:28 +02:00
Ben Noordhuis
7d2ea31618 unix: fix event loop stall
Stop libev from entering the epoll_wait/kevent/port_getn/etc. syscall when
there are no active handles left, that will block indefinitely.
2012-06-04 23:36:50 +02:00
Ben Noordhuis
c9396dd57e unix: implement timers in libuv
* replace libev backed timers with a pure libuv implementation
* gut ev_run() and make it take a timeout instead of flags

Incidentally speeds up the loop_count_timed benchmark by about 100%.
2012-05-31 03:08:34 +02:00
Ben Noordhuis
171ad8567d unix, windows: add uv_walk()
Lets the libuv user iterate over the open handles. Mostly intended as a
debugging tool or a post-hoc cleanup mechanism.
2012-05-30 02:33:39 +02:00
Ben Noordhuis
9d26f49725 unix, windows: rename flags UV__ACTIVE, UV__REF
Rename UV__ACTIVE and UV__REF to UV__HANDLE_ACTIVE and UV__HANDLE_REF to make
it clear that they apply to handles, not requests or loops.
2012-05-30 01:30:24 +02:00
Ben Noordhuis
58a272e556 unix: rework pending handle/req logic 2012-05-30 00:08:22 +02:00
Ben Noordhuis
4690204682 unix: fix getaddrinfo bad pointer deref 2012-05-29 00:06:11 +02:00
Bert Belder
e4f23aacec Get rid of UV_LEAN_AND_MEAN 2012-05-28 01:53:22 +02:00
isaacs
604802ad47 Fix sunos build: undeclared variable 'handle' 2012-05-24 23:00:31 +02:00
Ben Noordhuis
752ac30ec8 unix: don't pass sockaddr to accept()
Shaves a few nanoseconds off the accept() syscall.
2012-05-24 14:31:53 +02:00
Ben Noordhuis
5fd2c406f1 unix: fix up asserts in core.c and stream.c 2012-05-23 22:19:12 +02:00
Ben Noordhuis
7c8313bd0f unix, windows: make uv_run_once() return a bool
The return value of uv_run_once() now signals if it needs to be called again.

Fixes #427.
2012-05-23 20:49:03 +02:00
Ben Noordhuis
5b9c45120a unix: fold uv__io_cb into ev_io struct
Makes the uv__io code a little more obscure but has the advantage that
sizeof(uv__io_t) == sizeof(ev_io), i.e. the sizes of embedding handles
don't change.
2012-05-23 03:57:08 +02:00
Ben Noordhuis
3bc9707054 unix: replace ev_io with uv__io_t
Replace ev_io usage with wrapper constructs.

This is preliminary work for the transition to a libev-less linux backend.
2012-05-23 03:42:32 +02:00
Ben Noordhuis
80b554129b unix: reactive new idle watcher implementation
The new idle watcher was temporarily disabled in 073a48d due to some semantic
incompatibilities with the previous implementation. This commit resolves those
issues and reactivates the new implementation.

One outstanding bug is that idle watchers can run in a different order
(relative to other handle types) than the old implementation, e.g. (timer, idle)
instead of the expected (idle, timer). This will be fixed in an upcoming commit.
2012-05-22 15:13:52 +02:00
Ben Noordhuis
6fd9bfb7b0 unix: fix event loop block, prepare/check invoke
Don't block in epoll_wait() / kevent() / etc. syscall unless there are active
handles. If there are only active requests, do a non-blocking poll.

Likewise, don't run the prepare and check watchers unless there are active
handles *or* requests.
2012-05-21 17:41:52 +02:00
Ben Noordhuis
073a48d6bf unix: back out new idle watcher for now
Its semantics don't quite match what node.js expects. This breaks the
stdio_over_pipes and shutdown_close_pipe tests but that can't be helped.
2012-05-18 01:19:56 +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
Frank Denis
07622e767d unix: change #if FIONBIO to #ifdef FIONBIO
Fixes compilation on Debian/ppc 6.0.4.
2012-05-16 16:59:42 +02:00
Bert Belder
d60d94e0c3 Unix: implement uv_poll 2012-05-03 15:52:56 +02:00
Ben Noordhuis
ab3b307df3 unix: clean up uv__req_init() 2012-04-18 22:30:20 +02:00
Ben Noordhuis
edb39b2499 const-ify handle argument to uv_is_closing() 2012-04-18 03:11:25 +02:00
Ben Noordhuis
fb6c9eef2a const-ify handle argument to uv_is_active() 2012-04-18 03:09:53 +02:00
Ben Noordhuis
68bed698fc unix: move loop init logic out of core.c 2012-04-04 05:54:39 -07:00
Ben Noordhuis
5a8446c309 unix: move handle specific close logic out of core.c 2012-04-04 05:30:15 -07:00
Ben Noordhuis
ed395e0619 unix: replace handle->next_watcher
Remove the next_watcher and replace it with a linked list. Said list is named
endgame_handles (because the uv-win calls it that) and contains all the handles
that are in the UV_CLOSING state.

The goal of this commit is two-fold:

a) remove a dependency on libev, and
b) share more code with uv-win in the future

A nice side effect is that it shaves a few bytes off a uv_handle_t.
2012-04-04 05:22:20 -07:00
Ben Noordhuis
5fbe0aab33 unix: move active checks out of core.c
Move active checks out of core.c and into their respective compilation units:
check, idle, prepare, timer.
2012-04-04 06:52:29 +02:00
Ben Noordhuis
5a59e4924a unix: move idle code from core.c to idle.c 2012-04-04 05:25:34 +02:00
Ben Noordhuis
dd31265751 unix: move check code from core.c to check.c 2012-04-04 05:25:34 +02:00
Ben Noordhuis
f1a19e6d34 unix: move prepare code from core.c to prepare.c 2012-04-04 05:25:34 +02:00
Ben Noordhuis
f7359a335c unix: move async code from core.c to async.c 2012-04-04 05:25:34 +02:00
Ben Noordhuis
fd987a26fe unix: move timer code from core.c to timer.c 2012-04-04 05:25:27 +02:00
Vladimir Dronnikov
b309f2e2e6 Add uv_is_closing()
Closes #367.
2012-04-01 21:05:50 +02:00
Ben Noordhuis
685b36ba66 linux: tidy up syscall code 2012-03-31 00:19:01 +00:00
Ben Noordhuis
4ff0898c5f unix: replace uv__close() with close()
uv__close() was deprecated a while ago. It's been an alias for close() ever
since. Remove it.
2012-03-21 02:11:18 +01:00
Ben Noordhuis
dfda5009c2 unix, win: store ares handles in a binary tree
Store the uv_ares_task_t handles in a red-black tree, not a linked list.

Fixes #72.
2012-03-16 00:49:29 +01:00
Ben Noordhuis
2f886c892e unix: don't rely on libev to track timer state
An obscure libev bug sometimes makes it miss state changes. Keep track of the
state ourselves.

Fixes joyent/node#2515.
2012-02-28 17:24:50 +01:00
Ben Noordhuis
ec0eff955e Revert b3e0ad4, 149d32c, e99fdf0 and ea9baef.
Detaching doesn't work yet, the setsid() call fails and leaves the child process
attached to the parent's session.

Revert "test: Add test case for spawning detached child processes."
Revert "win: Implement options.detached for uv_spawn() for Windows."
Revert "unix: Implement options.detached for uv_spawn() for unix."
Revert "Add "detached" member to uv_process_options_t to denote whether a child
        process should spawn detached from its parent."

This reverts commit ea9baef95c.
This reverts commit e99fdf0df6.
This reverts commit 149d32cb96.
This reverts commit b3e0ad4db8.
2012-02-28 15:29:05 +01:00
Charlie McConnell
149d32cb96 unix: Implement options.detached for uv_spawn() for unix. 2012-02-24 15:15:00 +01:00
Ben Noordhuis
d3efefb043 linux: share inotify fd across event loop
Previously, a new inotify fd was created for each watcher, making it quite easy
to run into the system-wide fs.inotify.max_user_instances limit (usually 128).

Fixes #300.
2012-02-23 09:21:30 -08:00
Ben Noordhuis
abdc3efffe unix: add uv__dup() 2012-01-30 21:44:27 +01:00
Ben Noordhuis
28b0867f03 unix: clean up udp shutdown sequence 2012-01-18 20:18:57 +01:00
Ben Noordhuis
ee10cb77bd unix: use container_of() instead of w->data 2012-01-18 19:34:22 +01:00
Ben Noordhuis
dee86dd5b0 unix: don't retry close() on EINTR
Linux 2.6 always closes the file descriptor, even on EINTR. Retrying the close()
call isn't merely useless, it's actively harmful - the file descriptor may have
been acquired by another thread.
2012-01-18 15:06:30 +01:00
Ben Noordhuis
71f6c0edb8 Merge remote-tracking branch 'origin/v0.6'
Conflicts:
	src/win/util.c
2012-01-16 18:07:49 +01:00
Ben Noordhuis
6d9c9a533f Add uv_loop_refcount() function, debug tool. 2012-01-14 00:33:08 +01:00
Ben Noordhuis
a13584bec7 unix: remove unused #define 2012-01-13 15:59:53 +01:00