From 5a59e4924aa727a0cf07ed58a57075d42825b32a Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 4 Apr 2012 05:25:07 +0200 Subject: [PATCH] unix: move idle code from core.c to idle.c --- config-unix.mk | 1 + src/unix/core.c | 47 --------------------------------- src/unix/idle.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ uv.gyp | 1 + 4 files changed, 71 insertions(+), 47 deletions(-) create mode 100644 src/unix/idle.c diff --git a/config-unix.mk b/config-unix.mk index 2dbbea58..e633d8d8 100644 --- a/config-unix.mk +++ b/config-unix.mk @@ -34,6 +34,7 @@ OBJS += src/unix/core.o OBJS += src/unix/dl.o OBJS += src/unix/error.o OBJS += src/unix/fs.o +OBJS += src/unix/idle.o OBJS += src/unix/pipe.o OBJS += src/unix/prepare.o OBJS += src/unix/process.o diff --git a/src/unix/core.c b/src/unix/core.c index 2e9e5336..a83079ea 100644 --- a/src/unix/core.c +++ b/src/unix/core.c @@ -338,53 +338,6 @@ void uv__req_init(uv_loop_t* loop, uv_req_t* req) { } -static void uv__idle(EV_P_ ev_idle* w, int revents) { - uv_idle_t* idle = container_of(w, uv_idle_t, idle_watcher); - - if (idle->idle_cb) { - idle->idle_cb(idle, 0); - } -} - - -int uv_idle_init(uv_loop_t* loop, uv_idle_t* idle) { - uv__handle_init(loop, (uv_handle_t*)idle, UV_IDLE); - loop->counters.idle_init++; - - ev_idle_init(&idle->idle_watcher, uv__idle); - idle->idle_cb = NULL; - - return 0; -} - - -int uv_idle_start(uv_idle_t* idle, uv_idle_cb cb) { - int was_active = ev_is_active(&idle->idle_watcher); - - idle->idle_cb = cb; - ev_idle_start(idle->loop->ev, &idle->idle_watcher); - - if (!was_active) { - ev_unref(idle->loop->ev); - } - - return 0; -} - - -int uv_idle_stop(uv_idle_t* idle) { - int was_active = ev_is_active(&idle->idle_watcher); - - ev_idle_stop(idle->loop->ev, &idle->idle_watcher); - - if (was_active) { - ev_ref(idle->loop->ev); - } - - return 0; -} - - int uv_is_active(uv_handle_t* handle) { switch (handle->type) { case UV_TIMER: diff --git a/src/unix/idle.c b/src/unix/idle.c new file mode 100644 index 00000000..0fab4db2 --- /dev/null +++ b/src/unix/idle.c @@ -0,0 +1,69 @@ +/* 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 "internal.h" + + +static void uv__idle(EV_P_ ev_idle* w, int revents) { + uv_idle_t* idle = container_of(w, uv_idle_t, idle_watcher); + + if (idle->idle_cb) { + idle->idle_cb(idle, 0); + } +} + + +int uv_idle_init(uv_loop_t* loop, uv_idle_t* idle) { + uv__handle_init(loop, (uv_handle_t*)idle, UV_IDLE); + loop->counters.idle_init++; + + ev_idle_init(&idle->idle_watcher, uv__idle); + idle->idle_cb = NULL; + + return 0; +} + + +int uv_idle_start(uv_idle_t* idle, uv_idle_cb cb) { + int was_active = ev_is_active(&idle->idle_watcher); + + idle->idle_cb = cb; + ev_idle_start(idle->loop->ev, &idle->idle_watcher); + + if (!was_active) { + ev_unref(idle->loop->ev); + } + + return 0; +} + + +int uv_idle_stop(uv_idle_t* idle) { + int was_active = ev_is_active(&idle->idle_watcher); + + ev_idle_stop(idle->loop->ev, &idle->idle_watcher); + + if (was_active) { + ev_ref(idle->loop->ev); + } + + return 0; +} diff --git a/uv.gyp b/uv.gyp index b2e5694c..cc5f1beb 100644 --- a/uv.gyp +++ b/uv.gyp @@ -194,6 +194,7 @@ 'src/unix/ev/ev_wrap.h', 'src/unix/ev/event.h', 'src/unix/fs.c', + 'src/unix/idle.c', 'src/unix/internal.h', 'src/unix/pipe.c', 'src/unix/prepare.c',