unix: move handle specific close logic out of core.c

This commit is contained in:
Ben Noordhuis 2012-04-04 05:30:13 -07:00
parent ed395e0619
commit 5a8446c309
15 changed files with 102 additions and 81 deletions

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;

View File

@ -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");
}

View File

@ -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);
}

View File

@ -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);

View File

@ -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();
}

View File

@ -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;

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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));
}

View File

@ -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();
}

View File

@ -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);
}

View File

@ -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);