From 3118e5a4e47c7376dfb191b928d23ec1a2a2b8a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Thu, 4 Aug 2016 23:24:46 +0100 Subject: [PATCH] unix,win: share loop watchers implementation PR-URL: https://github.com/libuv/libuv/pull/974 Reviewed-By: Ben Noordhuis --- Makefile.am | 3 +- Makefile.mingw | 2 +- checksparse.sh | 2 +- src/{unix => }/loop-watcher.c | 2 +- src/unix/internal.h | 3 -- src/uv-common.h | 5 +++ src/win/handle-inl.h | 5 ++- src/win/handle.c | 6 +-- src/win/loop-watcher.c | 77 ----------------------------------- uv.gyp | 3 +- 10 files changed, 17 insertions(+), 91 deletions(-) rename src/{unix => }/loop-watcher.c (99%) delete mode 100644 src/win/loop-watcher.c diff --git a/Makefile.am b/Makefile.am index 882bbef5..11019a2c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -27,6 +27,7 @@ libuv_la_LDFLAGS = -no-undefined -version-info 1:0:0 libuv_la_SOURCES = src/fs-poll.c \ src/heap-inl.h \ src/inet.c \ + src/loop-watcher.c \ src/queue.h \ src/threadpool.c \ src/uv-common.c \ @@ -57,7 +58,6 @@ libuv_la_SOURCES += src/win/async.c \ src/win/handle.c \ src/win/handle-inl.h \ src/win/internal.h \ - src/win/loop-watcher.c \ src/win/pipe.c \ src/win/poll.c \ src/win/process-stdio.c \ @@ -90,7 +90,6 @@ libuv_la_SOURCES += src/unix/async.c \ src/unix/getaddrinfo.c \ src/unix/getnameinfo.c \ src/unix/internal.h \ - src/unix/loop-watcher.c \ src/unix/loop.c \ src/unix/pipe.c \ src/unix/poll.c \ diff --git a/Makefile.mingw b/Makefile.mingw index 721e22f4..ffa705f3 100644 --- a/Makefile.mingw +++ b/Makefile.mingw @@ -42,6 +42,7 @@ INCLUDES = include/tree.h \ OBJS = src/fs-poll.o \ src/inet.o \ + src/loop-watcher.o \ src/threadpool.o \ src/uv-common.o \ src/version.o \ @@ -54,7 +55,6 @@ OBJS = src/fs-poll.o \ src/win/getaddrinfo.o \ src/win/getnameinfo.o \ src/win/handle.o \ - src/win/loop-watcher.o \ src/win/pipe.o \ src/win/poll.o \ src/win/process-stdio.o \ diff --git a/checksparse.sh b/checksparse.sh index 68e3bde3..d17ffe10 100755 --- a/checksparse.sh +++ b/checksparse.sh @@ -31,6 +31,7 @@ include/uv-unix.h include/uv.h src/fs-poll.c src/inet.c +src/loop-watcher.c src/queue.h src/unix/async.c src/unix/core.c @@ -38,7 +39,6 @@ src/unix/dl.c src/unix/fs.c src/unix/getaddrinfo.c src/unix/internal.h -src/unix/loop-watcher.c src/unix/loop.c src/unix/pipe.c src/unix/poll.c diff --git a/src/unix/loop-watcher.c b/src/loop-watcher.c similarity index 99% rename from src/unix/loop-watcher.c rename to src/loop-watcher.c index 340bb0df..17dbe7ea 100644 --- a/src/unix/loop-watcher.c +++ b/src/loop-watcher.c @@ -20,7 +20,7 @@ */ #include "uv.h" -#include "internal.h" +#include "uv-common.h" #define UV_LOOP_WATCHER_DEFINE(name, type) \ int uv_##name##_init(uv_loop_t* loop, uv_##name##_t* handle) { \ diff --git a/src/unix/internal.h b/src/unix/internal.h index 38dfe392..6ad29a9f 100644 --- a/src/unix/internal.h +++ b/src/unix/internal.h @@ -236,12 +236,9 @@ void uv__platform_invalidate_fd(uv_loop_t* loop, int fd); /* various */ void uv__async_close(uv_async_t* handle); -void uv__check_close(uv_check_t* handle); void uv__fs_event_close(uv_fs_event_t* handle); -void uv__idle_close(uv_idle_t* handle); void uv__pipe_close(uv_pipe_t* handle); void uv__poll_close(uv_poll_t* handle); -void uv__prepare_close(uv_prepare_t* handle); void uv__process_close(uv_process_t* handle); void uv__stream_close(uv_stream_t* handle); void uv__tcp_close(uv_tcp_t* handle); diff --git a/src/uv-common.h b/src/uv-common.h index 1b8cd972..041e6bbd 100644 --- a/src/uv-common.h +++ b/src/uv-common.h @@ -216,4 +216,9 @@ void* uv__malloc(size_t size); void uv__free(void* ptr); void* uv__realloc(void* ptr, size_t size); +/* Loop watcher prototypes */ +void uv__idle_close(uv_idle_t* handle); +void uv__prepare_close(uv_prepare_t* handle); +void uv__check_close(uv_check_t* handle); + #endif /* UV_COMMON_H_ */ diff --git a/src/win/handle-inl.h b/src/win/handle-inl.h index e5e1e0fe..c168b575 100644 --- a/src/win/handle-inl.h +++ b/src/win/handle-inl.h @@ -132,7 +132,10 @@ INLINE static void uv_process_endgames(uv_loop_t* loop) { case UV_PREPARE: case UV_CHECK: case UV_IDLE: - uv__loop_watcher_endgame(loop, handle); + assert(handle->flags & UV__HANDLE_CLOSING); + assert(!(handle->flags & UV_HANDLE_CLOSED)); + handle->flags |= UV_HANDLE_CLOSED; + uv__handle_close(handle); break; case UV_ASYNC: diff --git a/src/win/handle.c b/src/win/handle.c index 72b49d97..a322781b 100644 --- a/src/win/handle.c +++ b/src/win/handle.c @@ -103,19 +103,19 @@ void uv_close(uv_handle_t* handle, uv_close_cb cb) { return; case UV_PREPARE: - uv_prepare_stop((uv_prepare_t*)handle); + uv__prepare_close((uv_prepare_t*) handle); uv__handle_closing(handle); uv_want_endgame(loop, handle); return; case UV_CHECK: - uv_check_stop((uv_check_t*)handle); + uv__check_close((uv_check_t*) handle); uv__handle_closing(handle); uv_want_endgame(loop, handle); return; case UV_IDLE: - uv_idle_stop((uv_idle_t*)handle); + uv__idle_close((uv_idle_t*) handle); uv__handle_closing(handle); uv_want_endgame(loop, handle); return; diff --git a/src/win/loop-watcher.c b/src/win/loop-watcher.c deleted file mode 100644 index 2ec88f90..00000000 --- a/src/win/loop-watcher.c +++ /dev/null @@ -1,77 +0,0 @@ -/* 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 - -#include "uv.h" -#include "internal.h" -#include "handle-inl.h" - - -void uv__loop_watcher_endgame(uv_loop_t* loop, uv_handle_t* handle) { - if (handle->flags & UV__HANDLE_CLOSING) { - assert(!(handle->flags & UV_HANDLE_CLOSED)); - handle->flags |= UV_HANDLE_CLOSED; - uv__handle_close(handle); - } -} - - -#define UV_LOOP_WATCHER_DEFINE(name, type) \ - int uv_##name##_init(uv_loop_t* loop, uv_##name##_t* handle) { \ - uv__handle_init(loop, (uv_handle_t*)handle, UV_##type); \ - handle->name##_cb = NULL; \ - return 0; \ - } \ - \ - int uv_##name##_start(uv_##name##_t* handle, uv_##name##_cb cb) { \ - if (uv__is_active(handle)) return 0; \ - if (cb == NULL) return UV_EINVAL; \ - QUEUE_INSERT_HEAD(&handle->loop->name##_handles, &handle->queue); \ - handle->name##_cb = cb; \ - uv__handle_start(handle); \ - return 0; \ - } \ - \ - int uv_##name##_stop(uv_##name##_t* handle) { \ - if (!uv__is_active(handle)) return 0; \ - QUEUE_REMOVE(&handle->queue); \ - uv__handle_stop(handle); \ - return 0; \ - } \ - \ - void uv__run_##name(uv_loop_t* loop) { \ - uv_##name##_t* h; \ - QUEUE queue; \ - QUEUE* q; \ - QUEUE_MOVE(&loop->name##_handles, &queue); \ - while (!QUEUE_EMPTY(&queue)) { \ - q = QUEUE_HEAD(&queue); \ - h = QUEUE_DATA(q, uv_##name##_t, queue); \ - QUEUE_REMOVE(q); \ - QUEUE_INSERT_TAIL(&loop->name##_handles, q); \ - h->name##_cb(h); \ - } \ - } \ - -UV_LOOP_WATCHER_DEFINE(prepare, PREPARE) -UV_LOOP_WATCHER_DEFINE(check, CHECK) -UV_LOOP_WATCHER_DEFINE(idle, IDLE) diff --git a/uv.gyp b/uv.gyp index e2c7835e..280d9687 100644 --- a/uv.gyp +++ b/uv.gyp @@ -58,6 +58,7 @@ 'src/fs-poll.c', 'src/heap-inl.h', 'src/inet.c', + 'src/loop-watcher.c', 'src/queue.h', 'src/threadpool.c', 'src/uv-common.c', @@ -84,7 +85,6 @@ 'src/win/handle.c', 'src/win/handle-inl.h', 'src/win/internal.h', - 'src/win/loop-watcher.c', 'src/win/pipe.c', 'src/win/thread.c', 'src/win/poll.c', @@ -142,7 +142,6 @@ 'src/unix/getnameinfo.c', 'src/unix/internal.h', 'src/unix/loop.c', - 'src/unix/loop-watcher.c', 'src/unix/pipe.c', 'src/unix/poll.c', 'src/unix/process.c',