From 5a8446c3099fd10353de206825a6a42ac1ce8d63 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 4 Apr 2012 05:30:13 -0700 Subject: [PATCH] unix: move handle specific close logic out of core.c --- src/unix/async.c | 6 +++ src/unix/check.c | 5 +++ src/unix/core.c | 87 +++++++++++++++------------------------- src/unix/cygwin.c | 2 +- src/unix/idle.c | 5 +++ src/unix/internal.h | 20 +++++---- src/unix/kqueue.c | 4 +- src/unix/linux/inotify.c | 2 +- src/unix/pipe.c | 14 +------ src/unix/prepare.c | 5 +++ src/unix/process.c | 5 +++ src/unix/stream.c | 17 ++++++++ src/unix/sunos.c | 4 +- src/unix/timer.c | 5 +++ src/unix/udp.c | 2 +- 15 files changed, 102 insertions(+), 81 deletions(-) diff --git a/src/unix/async.c b/src/unix/async.c index f5d005a2..638774c6 100644 --- a/src/unix/async.c +++ b/src/unix/async.c @@ -50,3 +50,9 @@ int uv_async_send(uv_async_t* async) { ev_async_send(async->loop->ev, &async->async_watcher); return 0; } + + +void uv__async_close(uv_async_t* handle) { + ev_async_stop(handle->loop->ev, &handle->async_watcher); + ev_ref(handle->loop->ev); +} diff --git a/src/unix/check.c b/src/unix/check.c index cc5d3c7e..a9752100 100644 --- a/src/unix/check.c +++ b/src/unix/check.c @@ -73,3 +73,8 @@ int uv_check_stop(uv_check_t* check) { int uv__check_active(const uv_check_t* handle) { return ev_is_active(&handle->check_watcher); } + + +void uv__check_close(uv_check_t* handle) { + uv_check_stop(handle); +} diff --git a/src/unix/core.c b/src/unix/core.c index 5dafa32e..56eb9b95 100644 --- a/src/unix/core.c +++ b/src/unix/core.c @@ -63,73 +63,52 @@ static void uv__finish_close(uv_handle_t* handle); void uv_close(uv_handle_t* handle, uv_close_cb close_cb) { - uv_async_t* async; - uv_stream_t* stream; - uv_process_t* process; - handle->close_cb = close_cb; switch (handle->type) { - case UV_NAMED_PIPE: - uv_pipe_cleanup((uv_pipe_t*)handle); - /* Fall through. */ + case UV_NAMED_PIPE: + uv__pipe_close((uv_pipe_t*)handle); + break; - case UV_TTY: - case UV_TCP: - stream = (uv_stream_t*)handle; + case UV_TTY: + case UV_TCP: + uv__stream_close((uv_stream_t*)handle); + break; - uv_read_stop(stream); - ev_io_stop(stream->loop->ev, &stream->write_watcher); + case UV_UDP: + uv__udp_close((uv_udp_t*)handle); + break; - close(stream->fd); - stream->fd = -1; + case UV_PREPARE: + uv__prepare_close((uv_prepare_t*)handle); + break; - if (stream->accepted_fd >= 0) { - close(stream->accepted_fd); - stream->accepted_fd = -1; - } + case UV_CHECK: + uv__check_close((uv_check_t*)handle); + break; - assert(!ev_is_active(&stream->read_watcher)); - assert(!ev_is_active(&stream->write_watcher)); - break; + case UV_IDLE: + uv__idle_close((uv_idle_t*)handle); + break; - case UV_UDP: - uv__udp_start_close((uv_udp_t*)handle); - break; + case UV_ASYNC: + uv__async_close((uv_async_t*)handle); + break; - case UV_PREPARE: - uv_prepare_stop((uv_prepare_t*) handle); - break; + case UV_TIMER: + uv__timer_close((uv_timer_t*)handle); + break; - case UV_CHECK: - uv_check_stop((uv_check_t*) handle); - break; + case UV_PROCESS: + uv__process_close((uv_process_t*)handle); + break; - case UV_IDLE: - uv_idle_stop((uv_idle_t*) handle); - break; + case UV_FS_EVENT: + uv__fs_event_close((uv_fs_event_t*)handle); + break; - case UV_ASYNC: - async = (uv_async_t*)handle; - ev_async_stop(async->loop->ev, &async->async_watcher); - ev_ref(async->loop->ev); - break; - - case UV_TIMER: - uv_timer_stop((uv_timer_t*)handle); - break; - - case UV_PROCESS: - process = (uv_process_t*)handle; - ev_child_stop(process->loop->ev, &process->child_watcher); - break; - - case UV_FS_EVENT: - uv__fs_event_destroy((uv_fs_event_t*)handle); - break; - - default: - assert(0); + default: + assert(0); } handle->flags |= UV_CLOSING; diff --git a/src/unix/cygwin.c b/src/unix/cygwin.c index aef8d767..31b069f9 100644 --- a/src/unix/cygwin.c +++ b/src/unix/cygwin.c @@ -79,6 +79,6 @@ int uv_fs_event_init(uv_loop_t* loop, } -void uv__fs_event_destroy(uv_fs_event_t* handle) { +void uv__fs_event_close(uv_fs_event_t* handle) { assert(0 && "implement me"); } diff --git a/src/unix/idle.c b/src/unix/idle.c index bcefd6a1..5b4cf577 100644 --- a/src/unix/idle.c +++ b/src/unix/idle.c @@ -72,3 +72,8 @@ int uv_idle_stop(uv_idle_t* idle) { int uv__idle_active(const uv_idle_t* handle) { return ev_is_active(&handle->idle_watcher); } + + +void uv__idle_close(uv_idle_t* handle) { + uv_idle_stop(handle); +} diff --git a/src/unix/internal.h b/src/unix/internal.h index 2f5f54cb..5123d0ee 100644 --- a/src/unix/internal.h +++ b/src/unix/internal.h @@ -130,20 +130,24 @@ int uv__tcp_keepalive(uv_tcp_t* handle, int enable, unsigned int delay); /* pipe */ int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb); void uv__pipe_accept(EV_P_ ev_io* watcher, int revents); -int uv_pipe_cleanup(uv_pipe_t* handle); - -/* udp */ -void uv__udp_start_close(uv_udp_t* handle); -void uv__udp_finish_close(uv_udp_t* handle); - -/* fs */ -void uv__fs_event_destroy(uv_fs_event_t* handle); +/* various */ int uv__check_active(const uv_check_t* handle); int uv__idle_active(const uv_idle_t* handle); int uv__prepare_active(const uv_prepare_t* handle); int uv__timer_active(const uv_timer_t* handle); +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__prepare_close(uv_prepare_t* handle); +void uv__process_close(uv_process_t* handle); +void uv__stream_close(uv_stream_t* handle); +void uv__timer_close(uv_timer_t* handle); +void uv__udp_close(uv_udp_t* handle); + #define UV__F_IPC (1 << 0) #define UV__F_NONBLOCK (1 << 1) int uv__make_socketpair(int fds[2], int flags); diff --git a/src/unix/kqueue.c b/src/unix/kqueue.c index 58988f9d..1b1a1b68 100644 --- a/src/unix/kqueue.c +++ b/src/unix/kqueue.c @@ -121,7 +121,7 @@ int uv_fs_event_init(uv_loop_t* loop, } -void uv__fs_event_destroy(uv_fs_event_t* handle) { +void uv__fs_event_close(uv_fs_event_t* handle) { uv__fs_event_stop(handle); free(handle->filename); close(handle->fd); @@ -141,7 +141,7 @@ int uv_fs_event_init(uv_loop_t* loop, } -void uv__fs_event_destroy(uv_fs_event_t* handle) { +void uv__fs_event_close(uv_fs_event_t* handle) { UNREACHABLE(); } diff --git a/src/unix/linux/inotify.c b/src/unix/linux/inotify.c index 5e1508f7..c96fbd7b 100644 --- a/src/unix/linux/inotify.c +++ b/src/unix/linux/inotify.c @@ -215,7 +215,7 @@ int uv_fs_event_init(uv_loop_t* loop, } -void uv__fs_event_destroy(uv_fs_event_t* handle) { +void uv__fs_event_close(uv_fs_event_t* handle) { uv__inotify_rm_watch(handle->loop->inotify_fd, handle->fd); remove_watcher(handle); handle->fd = -1; diff --git a/src/unix/pipe.c b/src/unix/pipe.c index 3573a571..d085304a 100644 --- a/src/unix/pipe.c +++ b/src/unix/pipe.c @@ -146,29 +146,19 @@ out: } -int uv_pipe_cleanup(uv_pipe_t* handle) { - int saved_errno; - int status; - - saved_errno = errno; - status = -1; - +void uv__pipe_close(uv_pipe_t* handle) { if (handle->pipe_fname) { /* * Unlink the file system entity before closing the file descriptor. * Doing it the other way around introduces a race where our process * unlinks a socket with the same name that's just been created by * another thread or process. - * - * This is less of an issue now that we attach a file lock - * to the socket but it's still a best practice. */ unlink(handle->pipe_fname); free((void*)handle->pipe_fname); } - errno = saved_errno; - return status; + uv__stream_close((uv_stream_t*)handle); } diff --git a/src/unix/prepare.c b/src/unix/prepare.c index 59ddf8c2..6c18fbd7 100644 --- a/src/unix/prepare.c +++ b/src/unix/prepare.c @@ -72,3 +72,8 @@ int uv_prepare_stop(uv_prepare_t* prepare) { int uv__prepare_active(const uv_prepare_t* handle) { return ev_is_active(&handle->prepare_watcher); } + + +void uv__prepare_close(uv_prepare_t* handle) { + uv_prepare_stop(handle); +} diff --git a/src/unix/process.c b/src/unix/process.c index 9a2af8b0..9a5c76c7 100644 --- a/src/unix/process.c +++ b/src/unix/process.c @@ -362,3 +362,8 @@ uv_err_t uv_kill(int pid, int signum) { return uv_ok_; } } + + +void uv__process_close(uv_process_t* handle) { + ev_child_stop(handle->loop->ev, &handle->child_watcher); +} diff --git a/src/unix/stream.c b/src/unix/stream.c index fd24caaa..ef71fc72 100644 --- a/src/unix/stream.c +++ b/src/unix/stream.c @@ -1005,3 +1005,20 @@ int uv_is_readable(uv_stream_t* stream) { int uv_is_writable(uv_stream_t* stream) { return stream->flags & UV_WRITABLE; } + + +void uv__stream_close(uv_stream_t* handle) { + uv_read_stop(handle); + ev_io_stop(handle->loop->ev, &handle->write_watcher); + + close(handle->fd); + handle->fd = -1; + + if (handle->accepted_fd >= 0) { + close(handle->accepted_fd); + handle->accepted_fd = -1; + } + + assert(!ev_is_active(&handle->read_watcher)); + assert(!ev_is_active(&handle->write_watcher)); +} diff --git a/src/unix/sunos.c b/src/unix/sunos.c index 4c2dae09..0057e686 100644 --- a/src/unix/sunos.c +++ b/src/unix/sunos.c @@ -191,7 +191,7 @@ int uv_fs_event_init(uv_loop_t* loop, } -void uv__fs_event_destroy(uv_fs_event_t* handle) { +void uv__fs_event_close(uv_fs_event_t* handle) { ev_ref(handle->loop->ev); ev_io_stop(handle->loop->ev, &handle->event_watcher); close(handle->fd); @@ -214,7 +214,7 @@ int uv_fs_event_init(uv_loop_t* loop, } -void uv__fs_event_destroy(uv_fs_event_t* handle) { +void uv__fs_event_close(uv_fs_event_t* handle) { UNREACHABLE(); } diff --git a/src/unix/timer.c b/src/unix/timer.c index e719c20e..6a002294 100644 --- a/src/unix/timer.c +++ b/src/unix/timer.c @@ -120,3 +120,8 @@ int64_t uv_timer_get_repeat(uv_timer_t* timer) { int uv__timer_active(const uv_timer_t* timer) { return timer->flags & UV_TIMER_ACTIVE; } + + +void uv__timer_close(uv_timer_t* handle) { + uv_timer_stop(handle); +} diff --git a/src/unix/udp.c b/src/unix/udp.c index e1c7621c..9c84cef3 100644 --- a/src/unix/udp.c +++ b/src/unix/udp.c @@ -85,7 +85,7 @@ static void uv__udp_stop_write_watcher(uv_udp_t* handle) { } -void uv__udp_start_close(uv_udp_t* handle) { +void uv__udp_close(uv_udp_t* handle) { uv__udp_stop_write_watcher(handle); uv__udp_stop_read_watcher(handle); close(handle->fd);