From 9da5fd443ebc2c56a7f5946f0a1e04801b370a33 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 27 Nov 2014 08:43:48 +0100 Subject: [PATCH] unix, windows: add uv_loop_configure() function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The only supported option right now is UV_LOOP_BLOCK_SIGNAL, which only supports the SIGPROF signal and only on UNIX platforms. So yes, it is kind of limited right now. But everything has to start somewhere. Refs strongloop/strong-agent#3 and strongloop-internal/scrum-cs#37. PR-URL: https://github.com/libuv/libuv/pull/15 Reviewed-By: Saúl Ibarra Corretgé --- include/uv.h | 19 +++++++++++++++++++ src/unix/loop.c | 12 ++++++++++++ src/uv-common.c | 14 ++++++++++++++ src/uv-common.h | 2 ++ src/win/core.c | 5 +++++ 5 files changed, 52 insertions(+) diff --git a/include/uv.h b/include/uv.h index 7022bc66..be789987 100644 --- a/include/uv.h +++ b/include/uv.h @@ -226,6 +226,16 @@ typedef struct uv_work_s uv_work_t; typedef struct uv_cpu_info_s uv_cpu_info_t; typedef struct uv_interface_address_s uv_interface_address_t; +typedef enum { + /* Block a signal when polling for new events. The second argument to + * uv_loop_configure() is the signal number. + * + * This operation is currently only implemented for SIGPROF signals, + * to suppress unnecessary wakeups when using a sampling profiler. + * Requesting other signals will fail with UV_EINVAL. + */ + UV_LOOP_BLOCK_SIGNAL +} uv_loop_option; typedef enum { UV_RUN_DEFAULT = 0, @@ -264,6 +274,15 @@ UV_EXTERN void uv_loop_delete(uv_loop_t*); */ UV_EXTERN uv_loop_t* uv_default_loop(void); +/* + * Set additional loop options. You should normally call this before the + * first call to uv_run() unless mentioned otherwise. + * + * Returns 0 on success or a UV_E* error code on failure. Be prepared to + * handle UV_ENOSYS; it means the loop option is not supported by the platform. + */ +UV_EXTERN int uv_loop_configure(uv_loop_t* loop, uv_loop_option option, ...); + /* * This function runs the event loop. It will act differently depending on the * specified mode: diff --git a/src/unix/loop.c b/src/unix/loop.c index 75b0702a..a75c9345 100644 --- a/src/unix/loop.c +++ b/src/unix/loop.c @@ -112,3 +112,15 @@ void uv__loop_delete(uv_loop_t* loop) { loop->watchers = NULL; loop->nwatchers = 0; } + + +int uv__loop_configure(uv_loop_t* loop, uv_loop_option option, va_list ap) { + if (option != UV_LOOP_BLOCK_SIGNAL) + return UV_ENOSYS; + + if (va_arg(ap, int) != SIGPROF) + return UV_EINVAL; + + loop->flags |= UV_LOOP_BLOCK_SIGPROF; + return 0; +} diff --git a/src/uv-common.c b/src/uv-common.c index f0792f11..69da523e 100644 --- a/src/uv-common.c +++ b/src/uv-common.c @@ -24,6 +24,7 @@ #include #include +#include #include /* NULL */ #include /* malloc */ #include /* memset */ @@ -434,3 +435,16 @@ void uv_stop(uv_loop_t* loop) { uint64_t uv_now(uv_loop_t* loop) { return loop->time; } + + +int uv_loop_configure(uv_loop_t* loop, uv_loop_option option, ...) { + va_list ap; + int err; + + va_start(ap, option); + /* Any platform-agnostic options should be handled here. */ + err = uv__loop_configure(loop, option, ap); + va_end(ap); + + return err; +} diff --git a/src/uv-common.h b/src/uv-common.h index bbf24859..ad45f190 100644 --- a/src/uv-common.h +++ b/src/uv-common.h @@ -28,6 +28,7 @@ #define UV_COMMON_H_ #include +#include #include #if defined(_MSC_VER) && _MSC_VER < 1600 @@ -77,6 +78,7 @@ int uv__set_artificial_error(uv_loop_t* loop, uv_err_code code); uv_err_t uv__new_sys_error(int sys_error); uv_err_t uv__new_artificial_error(uv_err_code code); +int uv__loop_configure(uv_loop_t* loop, uv_loop_option option, va_list ap); int uv__tcp_bind(uv_tcp_t* handle, struct sockaddr_in addr); int uv__tcp_bind6(uv_tcp_t* handle, struct sockaddr_in6 addr); diff --git a/src/win/core.c b/src/win/core.c index 62d6bf8f..74c602fb 100644 --- a/src/win/core.c +++ b/src/win/core.c @@ -174,6 +174,11 @@ void uv_loop_delete(uv_loop_t* loop) { } +int uv__loop_configure(uv_loop_t* loop, uv_loop_option option, va_list ap) { + return UV_ENOSYS; +} + + int uv_backend_fd(const uv_loop_t* loop) { return -1; }