Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Unix: implement uv_poll

  • Loading branch information...
commit d60d94e0c36cc45ab32773e3a7c47173a784e1f9 1 parent d7a7176
Bert Belder piscisaureus authored
1  config-unix.mk
@@ -37,6 +37,7 @@ OBJS += src/unix/fs.o
37 37 OBJS += src/unix/idle.o
38 38 OBJS += src/unix/loop.o
39 39 OBJS += src/unix/pipe.o
  40 +OBJS += src/unix/poll.o
40 41 OBJS += src/unix/prepare.o
41 42 OBJS += src/unix/process.o
42 43 OBJS += src/unix/stream.o
7 include/uv-private/uv-unix.h
@@ -45,6 +45,8 @@ typedef struct {
45 45
46 46 typedef int uv_file;
47 47
  48 +typedef int uv_platform_socket_t;
  49 +
48 50 #define UV_ONCE_INIT PTHREAD_ONCE_INIT
49 51
50 52 typedef pthread_once_t uv_once_t;
@@ -162,6 +164,11 @@ typedef void* uv_lib_t;
162 164 const char* pipe_fname; /* strdup'ed */
163 165
164 166
  167 +/* UV_POLL */
  168 +#define UV_POLL_PRIVATE_FIELDS \
  169 + ev_io io_watcher;
  170 +
  171 +
165 172 /* UV_PREPARE */ \
166 173 #define UV_PREPARE_PRIVATE_FIELDS \
167 174 ev_prepare prepare_watcher; \
9 src/unix/core.c
@@ -107,6 +107,10 @@ void uv_close(uv_handle_t* handle, uv_close_cb close_cb) {
107 107 uv__fs_event_close((uv_fs_event_t*)handle);
108 108 break;
109 109
  110 + case UV_POLL:
  111 + uv__poll_close((uv_poll_t*)handle);
  112 + break;
  113 +
110 114 default:
111 115 assert(0);
112 116 }
@@ -249,6 +253,9 @@ void uv__finish_close(uv_handle_t* handle) {
249 253 case UV_FS_EVENT:
250 254 break;
251 255
  256 + case UV_POLL:
  257 + break;
  258 +
252 259 default:
253 260 assert(0);
254 261 break;
@@ -285,6 +292,8 @@ int64_t uv_now(uv_loop_t* loop) {
285 292
286 293 int uv_is_active(const uv_handle_t* handle) {
287 294 switch (handle->type) {
  295 + case UV_POLL:
  296 + return uv__poll_active((const uv_poll_t*)handle);
288 297 case UV_CHECK:
289 298 return uv__check_active((const uv_check_t*)handle);
290 299 case UV_IDLE:
4 src/unix/internal.h
@@ -141,6 +141,10 @@ int uv__tcp_keepalive(uv_tcp_t* handle, int enable, unsigned int delay);
141 141 int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb);
142 142 void uv__pipe_accept(EV_P_ ev_io* watcher, int revents);
143 143
  144 +/* poll */
  145 +void uv__poll_close(uv_poll_t* handle);
  146 +int uv__poll_active(const uv_poll_t* handle);
  147 +
144 148 /* various */
145 149 int uv__check_active(const uv_check_t* handle);
146 150 int uv__idle_active(const uv_idle_t* handle);
130 src/unix/poll.c
... ... @@ -0,0 +1,130 @@
  1 +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2 + *
  3 + * Permission is hereby granted, free of charge, to any person obtaining a copy
  4 + * of this software and associated documentation files (the "Software"), to
  5 + * deal in the Software without restriction, including without limitation the
  6 + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7 + * sell copies of the Software, and to permit persons to whom the Software is
  8 + * furnished to do so, subject to the following conditions:
  9 + *
  10 + * The above copyright notice and this permission notice shall be included in
  11 + * all copies or substantial portions of the Software.
  12 + *
  13 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19 + * IN THE SOFTWARE.
  20 + */
  21 +
  22 +#include "uv.h"
  23 +#include "internal.h"
  24 +
  25 +#include <unistd.h>
  26 +#include <assert.h>
  27 +#include <errno.h>
  28 +
  29 +
  30 +static void uv__poll_io(EV_P_ ev_io* watcher, int ev_events) {
  31 + uv_poll_t* handle = watcher->data;
  32 + int events;
  33 +
  34 + if (ev_events & EV_ERROR) {
  35 + /* An error happened. Libev has implicitly stopped the watcher, but we */
  36 + /* need to fix the refcount. */
  37 + uv_ref(handle->loop);
  38 + uv__set_sys_error(handle->loop, EBADF);
  39 + handle->poll_cb(handle, -1, 0);
  40 + return;
  41 + }
  42 +
  43 + assert(ev_events & (EV_READ | EV_WRITE));
  44 + assert((ev_events & ~(EV_READ | EV_WRITE)) == 0);
  45 +
  46 + events = 0;
  47 + if (ev_events & EV_READ)
  48 + events |= UV_READABLE;
  49 + if (ev_events & EV_WRITE)
  50 + events |= UV_WRITABLE;
  51 +
  52 + handle->poll_cb(handle, 0, events);
  53 +}
  54 +
  55 +
  56 +int uv_poll_init(uv_loop_t* loop, uv_poll_t* handle, int fd) {
  57 + uv__handle_init(loop, (uv_handle_t*) handle, UV_POLL);
  58 + loop->counters.poll_init++;
  59 +
  60 + handle->fd = fd;
  61 + handle->poll_cb = NULL;
  62 +
  63 + ev_init(&handle->io_watcher, uv__poll_io);
  64 + handle->io_watcher.data = handle;
  65 +
  66 + return 0;
  67 +}
  68 +
  69 +
  70 +int uv_poll_init_socket(uv_loop_t* loop, uv_poll_t* handle,
  71 + uv_platform_socket_t socket) {
  72 + return uv_poll_init(loop, handle, socket);
  73 +}
  74 +
  75 +
  76 +static void uv__poll_stop(uv_poll_t* handle) {
  77 + if (ev_is_active(&handle->io_watcher)) {
  78 + ev_io_stop(handle->loop->ev, &handle->io_watcher);
  79 + uv_ref(handle->loop);
  80 + }
  81 +}
  82 +
  83 +
  84 +int uv_poll_stop(uv_poll_t* handle) {
  85 + assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
  86 + uv__poll_stop(handle);
  87 + return 0;
  88 +}
  89 +
  90 +
  91 +int uv_poll_start(uv_poll_t* handle, int events, uv_poll_cb poll_cb) {
  92 + int ev_events;
  93 + int was_active;
  94 +
  95 + assert((events & ~(UV_READABLE | UV_WRITABLE)) == 0);
  96 + assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
  97 +
  98 + if (events == 0) {
  99 + uv__poll_stop(handle);
  100 + return 0;
  101 + }
  102 +
  103 + ev_events = 0;
  104 + if (events & UV_READABLE)
  105 + ev_events |= EV_READ;
  106 + if (events & UV_WRITABLE)
  107 + ev_events |= EV_WRITE;
  108 +
  109 + was_active = ev_is_active(&handle->io_watcher);
  110 +
  111 + ev_io_set(&handle->io_watcher, handle->fd, ev_events);
  112 + ev_io_start(handle->loop->ev, &handle->io_watcher);
  113 +
  114 + if (!was_active)
  115 + uv_unref(handle->loop);
  116 +
  117 + handle->poll_cb = poll_cb;
  118 +
  119 + return 0;
  120 +}
  121 +
  122 +
  123 +void uv__poll_close(uv_poll_t* handle) {
  124 + uv__poll_stop(handle);
  125 +}
  126 +
  127 +
  128 +int uv__poll_active(const uv_poll_t* handle) {
  129 + return ev_is_active(&handle->io_watcher);
  130 +}
1  uv.gyp
@@ -199,6 +199,7 @@
199 199 'src/unix/internal.h',
200 200 'src/unix/loop.c',
201 201 'src/unix/pipe.c',
  202 + 'src/unix/poll.c',
202 203 'src/unix/prepare.c',
203 204 'src/unix/process.c',
204 205 'src/unix/stream.c',

0 comments on commit d60d94e

Please sign in to comment.
Something went wrong with that request. Please try again.