Showing with 181 additions and 7 deletions.
  1. +22 −0 include/uv.h
  2. +7 −2 src/unix/core.c
  3. +1 −1 src/unix/internal.h
  4. +5 −4 src/unix/timer.c
  5. +10 −0 src/win/core.c
  6. +132 −0 test/test-embed.c
  7. +3 −0 test/test-list.h
  8. +1 −0 uv.gyp
@@ -261,6 +261,28 @@ UV_EXTERN void uv_unref(uv_handle_t*);
UV_EXTERN void uv_update_time(uv_loop_t*);
UV_EXTERN int64_t uv_now(uv_loop_t*);

/*
* Get backend file descriptor. Only kqueue, epoll and event ports are
* supported.
*
* 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.
* See test/test-embed.c for an example.
*
* Note that embedding a kqueue fd in another kqueue pollset doesn't work on
* all platforms. It's not an error to add the fd but it never generates
* events.
*/
UV_EXTERN int uv_backend_fd(const uv_loop_t*);

/*
* Get the poll timeout. The return value is in milliseconds, or -1 for no
* timeout.
*/
UV_EXTERN int uv_backend_timeout(const uv_loop_t*);


/*
* Should return a buffer that libuv can use to read data into.
@@ -248,7 +248,12 @@ void uv_loop_delete(uv_loop_t* loop) {
}


static unsigned int uv__poll_timeout(uv_loop_t* loop) {
int uv_backend_fd(const uv_loop_t* loop) {
return loop->backend_fd;
}


int uv_backend_timeout(const uv_loop_t* loop) {
if (!uv__has_active_handles(loop) && !uv__has_active_reqs(loop))
return 0;

@@ -268,7 +273,7 @@ static int uv__run(uv_loop_t* loop) {
uv__run_idle(loop);
uv__run_prepare(loop);
uv__run_pending(loop);
uv__io_poll(loop, uv__poll_timeout(loop));
uv__io_poll(loop, uv_backend_timeout(loop));
uv__run_check(loop);
uv__run_closing_handles(loop);
return uv__has_active_handles(loop) || uv__has_active_reqs(loop);
@@ -163,7 +163,7 @@ int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb);

/* timer */
void uv__run_timers(uv_loop_t* loop);
unsigned int uv__next_timeout(uv_loop_t* loop);
int uv__next_timeout(const uv_loop_t* loop);

/* signal */
void uv__signal_close(uv_signal_t* handle);
@@ -102,13 +102,14 @@ int64_t uv_timer_get_repeat(uv_timer_t* handle) {
}


unsigned int uv__next_timeout(uv_loop_t* loop) {
uv_timer_t* handle;
int uv__next_timeout(const uv_loop_t* loop) {
const uv_timer_t* handle;

handle = RB_MIN(uv__timers, &loop->timer_handles);
/* RB_MIN expects a non-const tree root. That's okay, it doesn't modify it. */
handle = RB_MIN(uv__timers, (struct uv__timers*) &loop->timer_handles);

if (handle == NULL)
return (unsigned int) -1; /* block indefinitely */
return -1; /* block indefinitely */

if (handle->timeout <= loop->time)
return 0;
@@ -171,6 +171,16 @@ void uv_loop_delete(uv_loop_t* loop) {
}


int uv_backend_fd(const uv_loop_t* loop) {
return uv__new_artificial_error(UV_ENOSYS);
}


int uv_backend_timeout(const uv_loop_t* loop) {
return 0;
}


static void uv_poll(uv_loop_t* loop, int block) {
BOOL success;
DWORD bytes, timeout;
@@ -0,0 +1,132 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

#include "uv.h"
#include "task.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#ifndef HAVE_KQUEUE
# if __APPLE__ || __DragonFly__ || __FreeBSD__ || __OpenBSD__ || __NetBSD__
# define HAVE_KQUEUE 1
# endif
#endif

#ifndef HAVE_EPOLL
# if defined(__linux__)
# define HAVE_EPOLL 1
# endif
#endif

#if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL)

#if defined(HAVE_KQUEUE)
# include <sys/types.h>
# include <sys/event.h>
# include <sys/time.h>
#endif

#if defined(HAVE_EPOLL)
# include <sys/epoll.h>
#endif

static uv_thread_t embed_thread;
static uv_sem_t embed_sem;
static uv_timer_t embed_timer;
static uv_async_t embed_async;
static volatile int embed_closed;

static int embed_timer_called;


static void embed_thread_runner(void* arg) {
int r;
int fd;
int timeout;

while (!embed_closed) {
fd = uv_backend_fd(uv_default_loop());
timeout = uv_backend_timeout(uv_default_loop());

do {
#if defined(HAVE_KQUEUE)
struct timespec ts;
ts.tv_sec = timeout / 1000;
ts.tv_nsec = (timeout % 1000) * 1000000;
r = kevent(fd, NULL, 0, NULL, 0, &ts);
#elif defined(HAVE_EPOLL)
r = epoll_wait(fd, NULL, 0, timeout);
#endif
} while (r == -1 && errno == EINTR);
uv_async_send(&embed_async);
uv_sem_wait(&embed_sem);
}
}


static void embed_cb(uv_async_t* async, int status) {
uv_run_once(uv_default_loop());

uv_sem_post(&embed_sem);
}


static void embed_timer_cb(uv_timer_t* timer, int status) {
embed_timer_called++;
embed_closed = 1;

uv_close((uv_handle_t*) &embed_async, NULL);
}
#endif


TEST_IMPL(embed) {
#if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL)
uv_loop_t* external;

external = uv_loop_new();
ASSERT(external != NULL);

embed_timer_called = 0;
embed_closed = 0;

uv_async_init(external, &embed_async, embed_cb);

/* Start timer in default loop */
uv_timer_init(uv_default_loop(), &embed_timer);
uv_timer_start(&embed_timer, embed_timer_cb, 250, 0);

/* Start worker that will interrupt external loop */
uv_sem_init(&embed_sem, 0);
uv_thread_create(&embed_thread, embed_thread_runner, NULL);

/* But run external loop */
uv_run(external);

uv_thread_join(&embed_thread);
uv_loop_delete(external);

ASSERT(embed_timer_called == 1);
#endif

return 0;
}
@@ -124,6 +124,7 @@ TEST_DECLARE (pipe_ref3)
TEST_DECLARE (pipe_ref4)
TEST_DECLARE (process_ref)
TEST_DECLARE (active)
TEST_DECLARE (embed)
TEST_DECLARE (async)
TEST_DECLARE (get_currentexe)
TEST_DECLARE (process_title)
@@ -363,6 +364,8 @@ TASK_LIST_START

TEST_ENTRY (active)

TEST_ENTRY (embed)

TEST_ENTRY (async)

TEST_ENTRY (get_currentexe)
1 uv.gyp
@@ -250,6 +250,7 @@
'test/test-cwd-and-chdir.c',
'test/test-delayed-accept.c',
'test/test-error.c',
'test/test-embed.c',
'test/test-fail-always.c',
'test/test-fs.c',
'test/test-fs-event.c',