ext/libuv/src/unix/core.c in uvrb-0.1.4 vs ext/libuv/src/unix/core.c in uvrb-0.2.0

- old
+ new

@@ -60,13 +60,10 @@ # include <sys/wait.h> #endif static void uv__run_pending(uv_loop_t* loop); -static uv_loop_t default_loop_struct; -static uv_loop_t* default_loop_ptr; - /* Verify that uv_buf_t is ABI-compatible with struct iovec. */ STATIC_ASSERT(sizeof(uv_buf_t) == sizeof(struct iovec)); STATIC_ASSERT(sizeof(&((uv_buf_t*) 0)->base) == sizeof(((struct iovec*) 0)->iov_base)); STATIC_ASSERT(sizeof(&((uv_buf_t*) 0)->len) == @@ -199,11 +196,11 @@ assert(0); break; } uv__handle_unref(handle); - ngx_queue_remove(&handle->handle_queue); + QUEUE_REMOVE(&handle->handle_queue); if (handle->close_cb) { handle->close_cb(handle); } } @@ -227,48 +224,10 @@ int uv_is_closing(const uv_handle_t* handle) { return uv__is_closing(handle); } -uv_loop_t* uv_default_loop(void) { - if (default_loop_ptr) - return default_loop_ptr; - - if (uv__loop_init(&default_loop_struct, /* default_loop? */ 1)) - return NULL; - - return (default_loop_ptr = &default_loop_struct); -} - - -uv_loop_t* uv_loop_new(void) { - uv_loop_t* loop; - - if ((loop = malloc(sizeof(*loop))) == NULL) - return NULL; - - if (uv__loop_init(loop, /* default_loop? */ 0)) { - free(loop); - return NULL; - } - - return loop; -} - - -void uv_loop_delete(uv_loop_t* loop) { - uv__loop_delete(loop); -#ifndef NDEBUG - memset(loop, -1, sizeof *loop); -#endif - if (loop == default_loop_ptr) - default_loop_ptr = NULL; - else - free(loop); -} - - int uv_backend_fd(const uv_loop_t* loop) { return loop->backend_fd; } @@ -277,11 +236,11 @@ return 0; if (!uv__has_active_handles(loop) && !uv__has_active_reqs(loop)) return 0; - if (!ngx_queue_empty(&loop->idle_handles)) + if (!QUEUE_EMPTY(&loop->idle_handles)) return 0; if (loop->closing_handles) return 0; @@ -315,12 +274,25 @@ timeout = uv_backend_timeout(loop); uv__io_poll(loop, timeout); uv__run_check(loop); uv__run_closing_handles(loop); - r = uv__loop_alive(loop); + if (mode == UV_RUN_ONCE) { + /* UV_RUN_ONCE implies forward progess: at least one callback must have + * been invoked when it returns. uv__io_poll() can return without doing + * I/O (meaning: no callbacks) when its timeout expires - which means we + * have pending timers that satisfy the forward progress constraint. + * + * UV_RUN_NOWAIT makes no guarantees about progress so it's omitted from + * the check. + */ + uv__update_time(loop); + uv__run_timers(loop); + } + + r = uv__loop_alive(loop); UV_TICK_STOP(loop, mode); if (mode & (UV_RUN_ONCE | UV_RUN_NOWAIT)) break; } @@ -346,45 +318,48 @@ /* Open a socket in non-blocking close-on-exec mode, atomically if possible. */ int uv__socket(int domain, int type, int protocol) { int sockfd; + int err; #if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC) sockfd = socket(domain, type | SOCK_NONBLOCK | SOCK_CLOEXEC, protocol); - if (sockfd != -1) - goto out; + return sockfd; if (errno != EINVAL) - goto out; + return -errno; #endif sockfd = socket(domain, type, protocol); - if (sockfd == -1) - goto out; + return -errno; - if (uv__nonblock(sockfd, 1) || uv__cloexec(sockfd, 1)) { + err = uv__nonblock(sockfd, 1); + if (err == 0) + err = uv__cloexec(sockfd, 1); + + if (err) { close(sockfd); - sockfd = -1; + return err; } #if defined(SO_NOSIGPIPE) { int on = 1; setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof(on)); } #endif -out: return sockfd; } int uv__accept(int sockfd) { int peerfd; + int err; assert(sockfd >= 0); while (1) { #if defined(__linux__) @@ -395,42 +370,41 @@ peerfd = uv__accept4(sockfd, NULL, NULL, UV__SOCK_NONBLOCK|UV__SOCK_CLOEXEC); - if (peerfd != -1) - break; + return peerfd; if (errno == EINTR) continue; if (errno != ENOSYS) - break; + return -errno; no_accept4 = 1; skip: #endif peerfd = accept(sockfd, NULL, NULL); - if (peerfd == -1) { if (errno == EINTR) continue; - else - break; + return -errno; } - if (uv__cloexec(peerfd, 1) || uv__nonblock(peerfd, 1)) { + err = uv__cloexec(peerfd, 1); + if (err == 0) + err = uv__nonblock(peerfd, 1); + + if (err) { close(peerfd); - peerfd = -1; + return err; } - break; + return peerfd; } - - return peerfd; } #if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) @@ -439,22 +413,28 @@ do r = ioctl(fd, FIONBIO, &set); while (r == -1 && errno == EINTR); - return r; + if (r) + return -errno; + + return 0; } int uv__cloexec(int fd, int set) { int r; do r = ioctl(fd, set ? FIOCLEX : FIONCLEX); while (r == -1 && errno == EINTR); - return r; + if (r) + return -errno; + + return 0; } #else /* !(defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)) */ int uv__nonblock(int fd, int set) { @@ -464,11 +444,11 @@ do r = fcntl(fd, F_GETFL); while (r == -1 && errno == EINTR); if (r == -1) - return -1; + return -errno; /* Bail out now if already set/clear. */ if (!!(r & O_NONBLOCK) == !!set) return 0; @@ -479,11 +459,14 @@ do r = fcntl(fd, F_SETFL, flags); while (r == -1 && errno == EINTR); - return r; + if (r) + return -errno; + + return 0; } int uv__cloexec(int fd, int set) { int flags; @@ -492,11 +475,11 @@ do r = fcntl(fd, F_GETFD); while (r == -1 && errno == EINTR); if (r == -1) - return -1; + return -errno; /* Bail out now if already set/clear. */ if (!!(r & FD_CLOEXEC) == !!set) return 0; @@ -507,53 +490,59 @@ do r = fcntl(fd, F_SETFD, flags); while (r == -1 && errno == EINTR); - return r; + if (r) + return -errno; + + return 0; } #endif /* defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) */ /* This function is not execve-safe, there is a race window * between the call to dup() and fcntl(FD_CLOEXEC). */ int uv__dup(int fd) { + int err; + fd = dup(fd); if (fd == -1) - return -1; + return -errno; - if (uv__cloexec(fd, 1)) { - SAVE_ERRNO(close(fd)); - return -1; + err = uv__cloexec(fd, 1); + if (err) { + close(fd); + return err; } return fd; } -uv_err_t uv_cwd(char* buffer, size_t size) { - if (!buffer || !size) { - return uv__new_artificial_error(UV_EINVAL); - } +int uv_cwd(char* buffer, size_t size) { + if (buffer == NULL) + return -EINVAL; - if (getcwd(buffer, size)) { - return uv_ok_; - } else { - return uv__new_sys_error(errno); - } + if (size == 0) + return -EINVAL; + + if (getcwd(buffer, size) == NULL) + return -errno; + + return 0; } -uv_err_t uv_chdir(const char* dir) { - if (chdir(dir) == 0) { - return uv_ok_; - } else { - return uv__new_sys_error(errno); - } +int uv_chdir(const char* dir) { + if (chdir(dir)) + return -errno; + + return 0; } void uv_disable_stdio_inheritance(void) { int fd; @@ -566,19 +555,19 @@ break; } static void uv__run_pending(uv_loop_t* loop) { - ngx_queue_t* q; + QUEUE* q; uv__io_t* w; - while (!ngx_queue_empty(&loop->pending_queue)) { - q = ngx_queue_head(&loop->pending_queue); - ngx_queue_remove(q); - ngx_queue_init(q); + while (!QUEUE_EMPTY(&loop->pending_queue)) { + q = QUEUE_HEAD(&loop->pending_queue); + QUEUE_REMOVE(q); + QUEUE_INIT(q); - w = ngx_queue_data(q, uv__io_t, pending_queue); + w = QUEUE_DATA(q, uv__io_t, pending_queue); w->cb(loop, w, UV__POLLOUT); } } @@ -616,12 +605,12 @@ void uv__io_init(uv__io_t* w, uv__io_cb cb, int fd) { assert(cb != NULL); assert(fd >= -1); - ngx_queue_init(&w->pending_queue); - ngx_queue_init(&w->watcher_queue); + QUEUE_INIT(&w->pending_queue); + QUEUE_INIT(&w->watcher_queue); w->cb = cb; w->fd = fd; w->events = 0; w->pevents = 0; @@ -645,20 +634,20 @@ /* The event ports backend needs to rearm all file descriptors on each and * every tick of the event loop but the other backends allow us to * short-circuit here if the event mask is unchanged. */ if (w->events == w->pevents) { - if (w->events == 0 && !ngx_queue_empty(&w->watcher_queue)) { - ngx_queue_remove(&w->watcher_queue); - ngx_queue_init(&w->watcher_queue); + if (w->events == 0 && !QUEUE_EMPTY(&w->watcher_queue)) { + QUEUE_REMOVE(&w->watcher_queue); + QUEUE_INIT(&w->watcher_queue); } return; } #endif - if (ngx_queue_empty(&w->watcher_queue)) - ngx_queue_insert_tail(&loop->watcher_queue, &w->watcher_queue); + if (QUEUE_EMPTY(&w->watcher_queue)) + QUEUE_INSERT_TAIL(&loop->watcher_queue, &w->watcher_queue); if (loop->watchers[w->fd] == NULL) { loop->watchers[w->fd] = w; loop->nfds++; } @@ -679,34 +668,34 @@ return; w->pevents &= ~events; if (w->pevents == 0) { - ngx_queue_remove(&w->watcher_queue); - ngx_queue_init(&w->watcher_queue); + QUEUE_REMOVE(&w->watcher_queue); + QUEUE_INIT(&w->watcher_queue); if (loop->watchers[w->fd] != NULL) { assert(loop->watchers[w->fd] == w); assert(loop->nfds > 0); loop->watchers[w->fd] = NULL; loop->nfds--; w->events = 0; } } - else if (ngx_queue_empty(&w->watcher_queue)) - ngx_queue_insert_tail(&loop->watcher_queue, &w->watcher_queue); + else if (QUEUE_EMPTY(&w->watcher_queue)) + QUEUE_INSERT_TAIL(&loop->watcher_queue, &w->watcher_queue); } void uv__io_close(uv_loop_t* loop, uv__io_t* w) { uv__io_stop(loop, w, UV__POLLIN | UV__POLLOUT); - ngx_queue_remove(&w->pending_queue); + QUEUE_REMOVE(&w->pending_queue); } void uv__io_feed(uv_loop_t* loop, uv__io_t* w) { - if (ngx_queue_empty(&w->pending_queue)) - ngx_queue_insert_tail(&loop->pending_queue, &w->pending_queue); + if (QUEUE_EMPTY(&w->pending_queue)) + QUEUE_INSERT_TAIL(&loop->pending_queue, &w->pending_queue); } int uv__io_active(const uv__io_t* w, unsigned int events) { assert(0 == (events & ~(UV__POLLIN | UV__POLLOUT)));