Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Unix: namespace stream handle flags
Browse files Browse the repository at this point in the history
  • Loading branch information
piscisaureus committed May 2, 2012
1 parent 1ebe14e commit e387554
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 38 deletions.
22 changes: 11 additions & 11 deletions src/unix/internal.h
Expand Up @@ -84,17 +84,17 @@

/* flags */
enum {
UV_CLOSING = 0x01, /* uv_close() called but not finished. */
UV_CLOSED = 0x02, /* close(2) finished. */
UV_READING = 0x04, /* uv_read_start() called. */
UV_SHUTTING = 0x08, /* uv_shutdown() called but not complete. */
UV_SHUT = 0x10, /* Write side closed. */
UV_READABLE = 0x20, /* The stream is readable */
UV_WRITABLE = 0x40, /* The stream is writable */
UV_TCP_NODELAY = 0x080, /* Disable Nagle. */
UV_TCP_KEEPALIVE = 0x100, /* Turn on keep-alive. */
UV_TIMER_ACTIVE = 0x080,
UV_TIMER_REPEAT = 0x100
UV_CLOSING = 0x01, /* uv_close() called but not finished. */
UV_CLOSED = 0x02, /* close(2) finished. */
UV_STREAM_READING = 0x04, /* uv_read_start() called. */
UV_STREAM_SHUTTING = 0x08, /* uv_shutdown() called but not complete. */
UV_STREAM_SHUT = 0x10, /* Write side closed. */
UV_STREAM_READABLE = 0x20, /* The stream is readable */
UV_STREAM_WRITABLE = 0x40, /* The stream is writable */
UV_TCP_NODELAY = 0x080, /* Disable Nagle. */
UV_TCP_KEEPALIVE = 0x100, /* Turn on keep-alive. */
UV_TIMER_ACTIVE = 0x080,
UV_TIMER_REPEAT = 0x100
};

inline static void uv__req_init(uv_loop_t* loop,
Expand Down
8 changes: 6 additions & 2 deletions src/unix/pipe.c
Expand Up @@ -163,7 +163,9 @@ void uv__pipe_close(uv_pipe_t* handle) {


void uv_pipe_open(uv_pipe_t* handle, uv_file fd) {
uv__stream_open((uv_stream_t*)handle, fd, UV_READABLE | UV_WRITABLE);
uv__stream_open((uv_stream_t*)handle,
fd,
UV_STREAM_READABLE | UV_STREAM_WRITABLE);
}


Expand Down Expand Up @@ -204,7 +206,9 @@ void uv_pipe_connect(uv_connect_t* req,
goto out;
}

uv__stream_open((uv_stream_t*)handle, sockfd, UV_READABLE | UV_WRITABLE);
uv__stream_open((uv_stream_t*)handle,
sockfd,
UV_STREAM_READABLE | UV_STREAM_WRITABLE);

ev_io_start(handle->loop->ev, &handle->read_watcher);
ev_io_start(handle->loop->ev, &handle->write_watcher);
Expand Down
9 changes: 6 additions & 3 deletions src/unix/process.c
Expand Up @@ -317,7 +317,8 @@ int uv_spawn(uv_loop_t* loop, uv_process_t* process,
assert(stdin_pipe[0] >= 0);
close(stdin_pipe[0]);
uv__nonblock(stdin_pipe[1], 1);
flags = UV_WRITABLE | (options.stdin_stream->ipc ? UV_READABLE : 0);
flags = UV_STREAM_WRITABLE |
(options.stdin_stream->ipc ? UV_STREAM_READABLE : 0);
uv__stream_open((uv_stream_t*)options.stdin_stream, stdin_pipe[1],
flags);
}
Expand All @@ -327,7 +328,8 @@ int uv_spawn(uv_loop_t* loop, uv_process_t* process,
assert(stdout_pipe[1] >= 0);
close(stdout_pipe[1]);
uv__nonblock(stdout_pipe[0], 1);
flags = UV_READABLE | (options.stdout_stream->ipc ? UV_WRITABLE : 0);
flags = UV_STREAM_READABLE |
(options.stdout_stream->ipc ? UV_STREAM_WRITABLE : 0);
uv__stream_open((uv_stream_t*)options.stdout_stream, stdout_pipe[0],
flags);
}
Expand All @@ -337,7 +339,8 @@ int uv_spawn(uv_loop_t* loop, uv_process_t* process,
assert(stderr_pipe[1] >= 0);
close(stderr_pipe[1]);
uv__nonblock(stderr_pipe[0], 1);
flags = UV_READABLE | (options.stderr_stream->ipc ? UV_WRITABLE : 0);
flags = UV_STREAM_READABLE |
(options.stderr_stream->ipc ? UV_STREAM_WRITABLE : 0);
uv__stream_open((uv_stream_t*)options.stderr_stream, stderr_pipe[0],
flags);
}
Expand Down
38 changes: 20 additions & 18 deletions src/unix/stream.c
Expand Up @@ -153,7 +153,7 @@ void uv__stream_destroy(uv_stream_t* stream) {
}
}

if (stream->flags & UV_SHUTTING) {
if (stream->flags & UV_STREAM_SHUTTING) {
uv_shutdown_t* req = stream->shutdown_req;
if (req && req->cb) {
uv__set_artificial_error(stream->loop, UV_EINTR);
Expand Down Expand Up @@ -234,7 +234,7 @@ int uv_accept(uv_stream_t* server, uv_stream_t* client) {
}

if (uv__stream_open(streamClient, streamServer->accepted_fd,
UV_READABLE | UV_WRITABLE)) {
UV_STREAM_READABLE | UV_STREAM_WRITABLE)) {
/* TODO handle error */
close(streamServer->accepted_fd);
streamServer->accepted_fd = -1;
Expand Down Expand Up @@ -293,9 +293,9 @@ static void uv__drain(uv_stream_t* stream) {
ev_io_stop(stream->loop->ev, &stream->write_watcher);

/* Shutdown? */
if ((stream->flags & UV_SHUTTING) &&
if ((stream->flags & UV_STREAM_SHUTTING) &&
!(stream->flags & UV_CLOSING) &&
!(stream->flags & UV_SHUT)) {
!(stream->flags & UV_STREAM_SHUT)) {
assert(stream->shutdown_req);

req = stream->shutdown_req;
Expand All @@ -309,7 +309,7 @@ static void uv__drain(uv_stream_t* stream) {
}
} else {
uv__set_sys_error(stream->loop, 0);
((uv_handle_t*) stream)->flags |= UV_SHUT;
((uv_handle_t*) stream)->flags |= UV_STREAM_SHUT;
if (req->cb) {
req->cb(req, 0);
}
Expand Down Expand Up @@ -558,11 +558,11 @@ static void uv__read(uv_stream_t* stream) {
char cmsg_space[64];
struct ev_loop* ev = stream->loop->ev;

/* XXX: Maybe instead of having UV_READING we just test if
/* XXX: Maybe instead of having UV_STREAM_READING we just test if
* tcp->read_cb is NULL or not?
*/
while ((stream->read_cb || stream->read2_cb) &&
stream->flags & UV_READING) {
stream->flags & UV_STREAM_READING) {
assert(stream->alloc_cb);
buf = stream->alloc_cb((uv_handle_t*)stream, 64 * 1024);

Expand Down Expand Up @@ -598,7 +598,7 @@ static void uv__read(uv_stream_t* stream) {
/* Error */
if (errno == EAGAIN || errno == EWOULDBLOCK) {
/* Wait for the next one. */
if (stream->flags & UV_READING) {
if (stream->flags & UV_STREAM_READING) {
ev_io_start(ev, &stream->read_watcher);
}
uv__set_sys_error(stream->loop, EAGAIN);
Expand Down Expand Up @@ -691,8 +691,8 @@ int uv_shutdown(uv_shutdown_t* req, uv_stream_t* stream, uv_shutdown_cb cb) {
"uv_shutdown (unix) only supports uv_handle_t right now");
assert(stream->fd >= 0);

if (!(stream->flags & UV_WRITABLE) ||
stream->flags & UV_SHUT ||
if (!(stream->flags & UV_STREAM_WRITABLE) ||
stream->flags & UV_STREAM_SHUT ||
stream->flags & UV_CLOSED ||
stream->flags & UV_CLOSING) {
uv__set_sys_error(stream->loop, EINVAL);
Expand All @@ -705,7 +705,7 @@ int uv_shutdown(uv_shutdown_t* req, uv_stream_t* stream, uv_shutdown_cb cb) {
req->cb = cb;
stream->shutdown_req = req;

((uv_handle_t*)stream)->flags |= UV_SHUTTING;
((uv_handle_t*)stream)->flags |= UV_STREAM_SHUTTING;


ev_io_start(stream->loop->ev, &stream->write_watcher);
Expand Down Expand Up @@ -792,7 +792,7 @@ static void uv__stream_connect(uv_stream_t* stream) {


int uv__connect(uv_connect_t* req, uv_stream_t* stream, struct sockaddr* addr,
socklen_t addrlen, uv_connect_cb cb) {
socklen_t addrlen, uv_connect_cb cb) {
int sockfd;
int r;

Expand All @@ -802,7 +802,9 @@ int uv__connect(uv_connect_t* req, uv_stream_t* stream, struct sockaddr* addr,
return -1;
}

if (uv__stream_open(stream, sockfd, UV_READABLE | UV_WRITABLE)) {
if (uv__stream_open(stream,
sockfd,
UV_STREAM_READABLE | UV_STREAM_WRITABLE)) {
close(sockfd);
return -2;
}
Expand Down Expand Up @@ -956,10 +958,10 @@ int uv__read_start_common(uv_stream_t* stream, uv_alloc_cb alloc_cb,
return -1;
}

/* The UV_READING flag is irrelevant of the state of the tcp - it just
/* The UV_STREAM_READING flag is irrelevant of the state of the tcp - it just
* expresses the desired state of the user.
*/
((uv_handle_t*)stream)->flags |= UV_READING;
((uv_handle_t*)stream)->flags |= UV_STREAM_READING;

/* TODO: try to do the read inline? */
/* TODO: keep track of tcp state. If we've gotten a EOF then we should
Expand Down Expand Up @@ -994,7 +996,7 @@ int uv_read2_start(uv_stream_t* stream, uv_alloc_cb alloc_cb,

int uv_read_stop(uv_stream_t* stream) {
ev_io_stop(stream->loop->ev, &stream->read_watcher);
stream->flags &= ~UV_READING;
stream->flags &= ~UV_STREAM_READING;
stream->read_cb = NULL;
stream->read2_cb = NULL;
stream->alloc_cb = NULL;
Expand All @@ -1003,12 +1005,12 @@ int uv_read_stop(uv_stream_t* stream) {


int uv_is_readable(const uv_stream_t* stream) {
return stream->flags & UV_READABLE;
return stream->flags & UV_STREAM_READABLE;
}


int uv_is_writable(const uv_stream_t* stream) {
return stream->flags & UV_WRITABLE;
return stream->flags & UV_STREAM_WRITABLE;
}


Expand Down
6 changes: 4 additions & 2 deletions src/unix/tcp.c
Expand Up @@ -50,7 +50,9 @@ static int uv__bind(uv_tcp_t* tcp,
goto out;
}

if (uv__stream_open((uv_stream_t*)tcp, tcp->fd, UV_READABLE | UV_WRITABLE)) {
if (uv__stream_open((uv_stream_t*)tcp,
tcp->fd,
UV_STREAM_READABLE | UV_STREAM_WRITABLE)) {
close(tcp->fd);
tcp->fd = -1;
status = -2;
Expand Down Expand Up @@ -181,7 +183,7 @@ int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb) {
return -1;
}

if (uv__stream_open((uv_stream_t*)tcp, tcp->fd, UV_READABLE)) {
if (uv__stream_open((uv_stream_t*)tcp, tcp->fd, UV_STREAM_READABLE)) {
close(tcp->fd);
tcp->fd = -1;
return -1;
Expand Down
4 changes: 2 additions & 2 deletions src/unix/tty.c
Expand Up @@ -38,10 +38,10 @@ int uv_tty_init(uv_loop_t* loop, uv_tty_t* tty, int fd, int readable) {

if (readable) {
uv__nonblock(fd, 1);
uv__stream_open((uv_stream_t*)tty, fd, UV_READABLE);
uv__stream_open((uv_stream_t*)tty, fd, UV_STREAM_READABLE);
} else {
/* Note: writable tty we set to blocking mode. */
uv__stream_open((uv_stream_t*)tty, fd, UV_WRITABLE);
uv__stream_open((uv_stream_t*)tty, fd, UV_STREAM_WRITABLE);
tty->blocking = 1;
}

Expand Down

2 comments on commit e387554

@bnoordhuis
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@piscisaureus
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Landed as-is.

Please sign in to comment.