Skip to content

Commit 5656e3c

Browse files
committed
Prepare for writable TTY to be blocking
1 parent 41e8574 commit 5656e3c

6 files changed

Lines changed: 30 additions & 8 deletions

File tree

include/uv-private/uv-unix.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ typedef int uv_file;
9999
ngx_queue_t write_completed_queue; \
100100
int delayed_error; \
101101
uv_connection_cb connection_cb; \
102-
int accepted_fd;
102+
int accepted_fd; \
103+
int blocking;
103104

104105

105106
/* UV_TCP */

include/uv.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,18 @@ struct uv_tty_s {
626626
UV_TTY_PRIVATE_FIELDS
627627
};
628628

629-
int uv_tty_init(uv_loop_t*, uv_tty_t*, uv_file fd);
629+
/*
630+
* Initialize a new TTY stream with the given file descriptor. Usually the
631+
* file descriptor will be
632+
* 0 = stdin
633+
* 1 = stdout
634+
* 2 = stderr
635+
* The last argument, readable, specifies if you plan on calling
636+
* uv_read_start with this stream. stdin is readable, stdout is not.
637+
*
638+
* TTY streams which are not readable have blocking writes.
639+
*/
640+
int uv_tty_init(uv_loop_t*, uv_tty_t*, uv_file fd, int readable);
630641

631642
/*
632643
* Set mode. 0 for normal, 1 for raw.

src/unix/stream.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ void uv__stream_init(uv_loop_t* loop,
5353
uv_stream_t* stream,
5454
uv_handle_type type) {
5555
uv__handle_init(loop, (uv_handle_t*)stream, type);
56+
loop->counters.stream_init++;
5657

5758
stream->alloc_cb = NULL;
5859
stream->close_cb = NULL;
@@ -83,7 +84,7 @@ int uv__stream_open(uv_stream_t* stream, int fd, int flags) {
8384
assert(fd >= 0);
8485
stream->fd = fd;
8586

86-
((uv_handle_t*)stream)->flags |= flags;
87+
stream->flags |= flags;
8788

8889
/* Reuse the port address if applicable. */
8990
yes = 1;

src/unix/tty.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,19 @@ static int orig_termios_fd = -1;
3333
static struct termios orig_termios;
3434

3535

36-
int uv_tty_init(uv_loop_t* loop, uv_tty_t* tty, int fd) {
37-
uv__nonblock(fd, 1);
36+
int uv_tty_init(uv_loop_t* loop, uv_tty_t* tty, int fd, int readable) {
3837
uv__stream_init(loop, (uv_stream_t*)tty, UV_TTY);
39-
uv__stream_open((uv_stream_t*)tty, fd, UV_READABLE | UV_WRITABLE);
38+
39+
if (readable) {
40+
uv__nonblock(fd, 1);
41+
uv__stream_open((uv_stream_t*)tty, fd, UV_READABLE);
42+
} else {
43+
/* Note: writable tty we set to blocking mode. */
44+
uv__nonblock(fd, 0);
45+
uv__stream_open((uv_stream_t*)tty, fd, UV_WRITABLE);
46+
tty->blocking = 1;
47+
}
48+
4049
loop->counters.tty_init++;
4150
tty->mode = 0;
4251
return 0;

src/win/tty.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void uv_console_init() {
8686
}
8787

8888

89-
int uv_tty_init(uv_loop_t* loop, uv_tty_t* tty, uv_file fd) {
89+
int uv_tty_init(uv_loop_t* loop, uv_tty_t* tty, uv_file fd, int readable) {
9090
HANDLE win_handle;
9191
CONSOLE_SCREEN_BUFFER_INFO info;
9292

test/test-tty.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ TEST_IMPL(tty) {
3333
*/
3434
ASSERT(UV_TTY == uv_guess_handle(0));
3535

36-
r = uv_tty_init(uv_default_loop(), &tty, 0);
36+
r = uv_tty_init(uv_default_loop(), &tty, 0, 1);
3737
ASSERT(r == 0);
3838

3939
r = uv_tty_get_winsize(&tty, &width, &height);

0 commit comments

Comments
 (0)