Skip to content
This repository
Browse code

uv: upgrade to 564e7c7

  • Loading branch information...
commit ed093f1314807f55a472838eb82bb532768b0e79 1 parent 63d13e8
Bert Belder authored August 22, 2012
3  deps/uv/include/uv-private/uv-darwin.h
@@ -34,4 +34,7 @@
34 34
   int fflags;                                                                 \
35 35
   int fd;                                                                     \
36 36
 
  37
+#define UV_STREAM_PRIVATE_PLATFORM_FIELDS                                     \
  38
+  void* select;                                                               \
  39
+
37 40
 #endif /* UV_DARWIN_H */
5  deps/uv/include/uv-private/uv-unix.h
@@ -79,6 +79,10 @@ struct uv__io_s {
79 79
 # define UV_PLATFORM_FS_EVENT_FIELDS /* empty */
80 80
 #endif
81 81
 
  82
+#ifndef UV_STREAM_PRIVATE_PLATFORM_FIELDS
  83
+# define UV_STREAM_PRIVATE_PLATFORM_FIELDS /* empty */
  84
+#endif
  85
+
82 86
 /* Note: May be cast to struct iovec. See writev(2). */
83 87
 typedef struct {
84 88
   char* base;
@@ -178,6 +182,7 @@ typedef struct {
178 182
   int delayed_error;                                                          \
179 183
   int accepted_fd;                                                            \
180 184
   int fd;                                                                     \
  185
+  UV_STREAM_PRIVATE_PLATFORM_FIELDS                                           \
181 186
 
182 187
 #define UV_TCP_PRIVATE_FIELDS                                                 \
183 188
   uv_idle_t* idle_handle;  /* for UV_TCP_SINGLE_ACCEPT handles */             \
1  deps/uv/include/uv-private/uv-win.h
@@ -65,6 +65,7 @@ typedef intptr_t ssize_t;
65 65
  */
66 66
 #define SIGHUP                1
67 67
 #define SIGKILL               9
  68
+#define SIGWINCH             28
68 69
 
69 70
 /*
70 71
  * Guids and typedefs for winsock extension functions
7  deps/uv/include/uv.h
@@ -1608,6 +1608,13 @@ UV_EXTERN int uv_fs_poll_stop(uv_fs_poll_t* handle);
1608 1608
  *   program is given approximately 10 seconds to perform cleanup. After that
1609 1609
  *   Windows will unconditionally terminate it.
1610 1610
  *
  1611
+ *   SIGWINCH is raised whenever libuv detects that the console has been
  1612
+ *   resized. SIGWINCH is emulated by libuv when the program uses an uv_tty_t
  1613
+ *   handle to write to the console. SIGWINCH may not always be delivered in a
  1614
+ *   timely manner; libuv will only detect size changes when the cursor is
  1615
+ *   being moved. When a readable uv_tty_handle is used in raw mode, resizing
  1616
+ *   the console buffer will also trigger a SIGWINCH signal.
  1617
+ *
1611 1618
  * Watchers for other signals can be successfully created, but these signals
1612 1619
  * are never generated. These signals are: SIGILL, SIGABRT, SIGFPE, SIGSEGV,
1613 1620
  * SIGTERM and SIGKILL.
12  deps/uv/src/unix/core.c
@@ -49,10 +49,14 @@
49 49
 
50 50
 #ifdef __APPLE__
51 51
 # include <mach-o/dyld.h> /* _NSGetExecutablePath */
  52
+# include <sys/filio.h>
  53
+# include <sys/ioctl.h>
52 54
 #endif
53 55
 
54 56
 #ifdef __FreeBSD__
55 57
 # include <sys/sysctl.h>
  58
+# include <sys/filio.h>
  59
+# include <sys/ioctl.h>
56 60
 # include <sys/wait.h>
57 61
 #endif
58 62
 
@@ -459,7 +463,7 @@ int uv__accept(int sockfd) {
459 463
 
460 464
   while (1) {
461 465
 #if __linux__
462  
-    static int no_accept4;
  466
+    static __read_mostly int no_accept4;
463 467
 
464 468
     if (no_accept4)
465 469
       goto skip;
@@ -503,7 +507,7 @@ int uv__accept(int sockfd) {
503 507
 }
504 508
 
505 509
 
506  
-#if __linux__
  510
+#if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
507 511
 
508 512
 int uv__nonblock(int fd, int set) {
509 513
   int r;
@@ -526,7 +530,7 @@ int uv__cloexec(int fd, int set) {
526 530
   return r;
527 531
 }
528 532
 
529  
-#else /* !__linux__ */
  533
+#else /* !(defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)) */
530 534
 
531 535
 int uv__nonblock(int fd, int set) {
532 536
   int flags;
@@ -575,7 +579,7 @@ int uv__cloexec(int fd, int set) {
575 579
   return r;
576 580
 }
577 581
 
578  
-#endif /* __linux__ */
  582
+#endif /* defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) */
579 583
 
580 584
 
581 585
 /* This function is not execve-safe, there is a race window
8  deps/uv/src/unix/internal.h
@@ -28,7 +28,13 @@
28 28
 #include <assert.h>
29 29
 #include <stdlib.h> /* abort */
30 30
 
31  
-#if __STRICT_ANSI__
  31
+#if defined(__GNUC__)
  32
+# define __read_mostly __attribute__((__section__(".data.read_mostly")))
  33
+#else
  34
+# define __read_mostly
  35
+#endif
  36
+
  37
+#if defined(__STRICT_ANSI__)
32 38
 # define inline __inline
33 39
 #endif
34 40
 
4  deps/uv/src/unix/linux/syscalls.c
@@ -247,9 +247,9 @@ int uv__eventfd2(unsigned int count, int flags) {
247 247
 }
248 248
 
249 249
 
250  
-int uv__epoll_create(void) {
  250
+int uv__epoll_create(int size) {
251 251
 #if __NR_epoll_create
252  
-  return syscall(__NR_epoll_create);
  252
+  return syscall(__NR_epoll_create, size);
253 253
 #else
254 254
   return errno = ENOSYS, -1;
255 255
 #endif
2  deps/uv/src/unix/linux/syscalls.h
@@ -89,7 +89,7 @@ struct uv__mmsghdr {
89 89
 
90 90
 int uv__accept4(int fd, struct sockaddr* addr, socklen_t* addrlen, int flags);
91 91
 int uv__eventfd(unsigned int count);
92  
-int uv__epoll_create(void);
  92
+int uv__epoll_create(int size);
93 93
 int uv__epoll_create1(int flags);
94 94
 int uv__epoll_ctl(int epfd, int op, int fd, struct uv__epoll_event *ev);
95 95
 int uv__epoll_wait(int epfd,
18  deps/uv/src/unix/process.c
@@ -117,6 +117,11 @@ static void uv__chld(uv_signal_t* handle, int signum) {
117 117
 
118 118
 int uv__make_socketpair(int fds[2], int flags) {
119 119
 #if __linux__
  120
+  static __read_mostly int no_cloexec;
  121
+
  122
+  if (no_cloexec)
  123
+    goto skip;
  124
+
120 125
   if (socketpair(AF_UNIX, SOCK_STREAM | UV__SOCK_CLOEXEC | flags, 0, fds) == 0)
121 126
     return 0;
122 127
 
@@ -125,6 +130,10 @@ int uv__make_socketpair(int fds[2], int flags) {
125 130
    */
126 131
   if (errno != EINVAL)
127 132
     return -1;
  133
+
  134
+  no_cloexec = 1;
  135
+
  136
+skip:
128 137
 #endif
129 138
 
130 139
   if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds))
@@ -144,11 +153,20 @@ int uv__make_socketpair(int fds[2], int flags) {
144 153
 
145 154
 int uv__make_pipe(int fds[2], int flags) {
146 155
 #if __linux__
  156
+  static __read_mostly int no_pipe2;
  157
+
  158
+  if (no_pipe2)
  159
+    goto skip;
  160
+
147 161
   if (uv__pipe2(fds, flags | UV__O_CLOEXEC) == 0)
148 162
     return 0;
149 163
 
150 164
   if (errno != ENOSYS)
151 165
     return -1;
  166
+
  167
+  no_pipe2 = 1;
  168
+
  169
+skip:
152 170
 #endif
153 171
 
154 172
   if (pipe(fds))
217  deps/uv/src/unix/stream.c
@@ -34,6 +34,27 @@
34 34
 #include <sys/un.h>
35 35
 #include <unistd.h>
36 36
 
  37
+#if defined(__APPLE__)
  38
+# include <sys/event.h>
  39
+# include <sys/time.h>
  40
+# include <sys/select.h>
  41
+
  42
+/* ev.h is overwriting EV_ERROR from sys/event.h */
  43
+#define EV_ERROR_ORIG 0x4000
  44
+
  45
+/* Forward declaration */
  46
+typedef struct uv__stream_select_s uv__stream_select_t;
  47
+
  48
+struct uv__stream_select_s {
  49
+  uv_stream_t* stream;
  50
+  uv_thread_t thread;
  51
+  uv_sem_t sem;
  52
+  uv_mutex_t mutex;
  53
+  uv_async_t async;
  54
+  int events;
  55
+  int fake_fd;
  56
+};
  57
+#endif /* defined(__APPLE__) */
37 58
 
38 59
 static void uv__stream_connect(uv_stream_t*);
39 60
 static void uv__write(uv_stream_t* stream);
@@ -69,11 +90,182 @@ void uv__stream_init(uv_loop_t* loop,
69 90
   ngx_queue_init(&stream->write_completed_queue);
70 91
   stream->write_queue_size = 0;
71 92
 
  93
+#if defined(__APPLE__)
  94
+  stream->select = NULL;
  95
+#endif /* defined(__APPLE_) */
  96
+
72 97
   uv__io_init(&stream->read_watcher, uv__stream_io, -1, 0);
73 98
   uv__io_init(&stream->write_watcher, uv__stream_io, -1, 0);
74 99
 }
75 100
 
76 101
 
  102
+#if defined(__APPLE__)
  103
+void uv__stream_osx_select(void* arg) {
  104
+  uv_stream_t* stream;
  105
+  uv__stream_select_t* s;
  106
+  fd_set read;
  107
+  fd_set write;
  108
+  fd_set error;
  109
+  struct timeval timeout;
  110
+  int events;
  111
+  int fd;
  112
+  int r;
  113
+
  114
+  stream = arg;
  115
+  s = stream->select;
  116
+  fd = stream->fd;
  117
+
  118
+  while (1) {
  119
+    /* Terminate on semaphore */
  120
+    if (uv_sem_trywait(&s->sem) == 0) break;
  121
+
  122
+    /* Watch fd using select(2) */
  123
+    FD_ZERO(&read);
  124
+    FD_ZERO(&write);
  125
+    FD_ZERO(&error);
  126
+    FD_SET(fd, &read);
  127
+    FD_SET(fd, &write);
  128
+    FD_SET(fd, &error);
  129
+
  130
+    timeout.tv_sec = 0;
  131
+    timeout.tv_usec = 250000; /* 250 ms timeout */
  132
+    r = select(fd + 1, &read, &write, &error, &timeout);
  133
+    if (r == -1) {
  134
+      if (errno == EINTR) continue;
  135
+      /* XXX: Possible?! */
  136
+      abort();
  137
+    }
  138
+
  139
+    /* Ignore timeouts */
  140
+    if (r == 0) continue;
  141
+
  142
+    /* Handle events */
  143
+    events = 0;
  144
+    if (FD_ISSET(fd, &read)) events |= UV__IO_READ;
  145
+    if (FD_ISSET(fd, &write)) events |= UV__IO_WRITE;
  146
+    if (FD_ISSET(fd, &error)) events |= UV__IO_ERROR;
  147
+
  148
+    uv_mutex_lock(&s->mutex);
  149
+    s->events |= events;
  150
+    uv_mutex_unlock(&s->mutex);
  151
+
  152
+    if (events != 0) uv_async_send(&s->async);
  153
+  }
  154
+}
  155
+
  156
+
  157
+void uv__stream_osx_select_cb(uv_async_t* handle, int status) {
  158
+  uv_stream_t* stream;
  159
+  uv__stream_select_t* s;
  160
+  int events;
  161
+
  162
+  s = container_of(handle, uv__stream_select_t, async);
  163
+  stream = s->stream;
  164
+
  165
+  /* Get and reset stream's events */
  166
+  uv_mutex_lock(&s->mutex);
  167
+  events = s->events;
  168
+  s->events = 0;
  169
+  uv_mutex_unlock(&s->mutex);
  170
+
  171
+  /* Invoke callback on event-loop */
  172
+  if ((events & UV__IO_READ) && uv__io_active(&stream->read_watcher)) {
  173
+    uv__stream_io(stream->loop, &stream->read_watcher, UV__IO_READ);
  174
+  }
  175
+  if ((events & UV__IO_WRITE) && uv__io_active(&stream->write_watcher)) {
  176
+    uv__stream_io(stream->loop, &stream->write_watcher, UV__IO_WRITE);
  177
+  }
  178
+  if (events & UV__IO_ERROR) {
  179
+    /* XXX: Handle it! */
  180
+    uv__stream_io(stream->loop, NULL, UV__IO_ERROR);
  181
+  }
  182
+}
  183
+
  184
+
  185
+void uv__stream_osx_cb_close(uv_handle_t* async) {
  186
+  /* Free container */
  187
+  free(container_of(async, uv__stream_select_t, async));
  188
+}
  189
+
  190
+
  191
+int uv__stream_try_select(uv_stream_t* stream, int fd) {
  192
+  /*
  193
+   * kqueue doesn't work with some files from /dev mount on osx.
  194
+   * select(2) in separate thread for those fds
  195
+   */
  196
+
  197
+  int kq;
  198
+  int ret;
  199
+  struct kevent filter[1];
  200
+  struct kevent events[1];
  201
+  struct timespec timeout;
  202
+  uv__stream_select_t* s;
  203
+
  204
+  kq = kqueue();
  205
+  if (kq < 0) {
  206
+    fprintf(stderr, "(libuv) Failed to create kqueue (%d)\n", errno);
  207
+    abort();
  208
+  }
  209
+
  210
+  EV_SET(&filter[0], fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
  211
+
  212
+  /* Use small timeout, because we only want to capture EINVALs */
  213
+  timeout.tv_sec = 0;
  214
+  timeout.tv_nsec = 1;
  215
+
  216
+  ret = kevent(kq, filter, 1, events, 1, &timeout);
  217
+  close(kq);
  218
+  if (ret < 1) return -1;
  219
+  if ((events[0].flags & EV_ERROR_ORIG) == 0 || events[0].data != EINVAL) {
  220
+    return -1;
  221
+  }
  222
+
  223
+  /* At this point we definitely know that this fd won't work with kqueue */
  224
+  s = malloc(sizeof(*s));
  225
+  if (s == NULL) {
  226
+    /* TODO: Return error */
  227
+    abort();
  228
+  }
  229
+
  230
+  if (uv_async_init(stream->loop,
  231
+                    &s->async,
  232
+                    uv__stream_osx_select_cb)) {
  233
+    return -1;
  234
+  }
  235
+  s->async.flags |= UV__HANDLE_INTERNAL;
  236
+  uv__handle_unref((uv_handle_t*) &s->async);
  237
+
  238
+  if (uv_sem_init(&s->sem, 0)) goto fatal1;
  239
+  if (uv_mutex_init(&s->mutex)) goto fatal2;
  240
+
  241
+  /* Create fake fd for io watcher */
  242
+  s->fake_fd = socket(AF_UNIX, SOCK_STREAM, 0);
  243
+  if (s->fake_fd == -1) goto fatal3;
  244
+
  245
+  if (uv_thread_create(&s->thread, uv__stream_osx_select, stream)) {
  246
+    goto fatal4;
  247
+  }
  248
+
  249
+  s->stream = stream;
  250
+  stream->select = s;
  251
+
  252
+  return 0;
  253
+
  254
+fatal4:
  255
+  close(s->fake_fd);
  256
+fatal3:
  257
+  uv_mutex_destroy(&s->mutex);
  258
+fatal2:
  259
+  uv_sem_destroy(&s->sem);
  260
+fatal1:
  261
+  uv_close((uv_handle_t*) &s->async, uv__stream_osx_cb_close);
  262
+
  263
+  free(s);
  264
+  return -1;
  265
+}
  266
+#endif /* defined(__APPLE__) */
  267
+
  268
+
77 269
 int uv__stream_open(uv_stream_t* stream, int fd, int flags) {
78 270
   socklen_t yes;
79 271
 
@@ -102,6 +294,13 @@ int uv__stream_open(uv_stream_t* stream, int fd, int flags) {
102 294
     }
103 295
   }
104 296
 
  297
+#if defined(__APPLE__)
  298
+  if (uv__stream_try_select(stream, fd) == 0) {
  299
+    /* Use fake fd */
  300
+    fd = ((uv__stream_select_t*) stream->select)->fake_fd;
  301
+  }
  302
+#endif /* defined(__APPLE__) */
  303
+
105 304
   /* Associate the fd with each watcher. */
106 305
   uv__io_set(&stream->read_watcher, uv__stream_io, fd, UV__IO_READ);
107 306
   uv__io_set(&stream->write_watcher, uv__stream_io, fd, UV__IO_WRITE);
@@ -980,6 +1179,24 @@ int uv_is_writable(const uv_stream_t* stream) {
980 1179
 
981 1180
 
982 1181
 void uv__stream_close(uv_stream_t* handle) {
  1182
+#if defined(__APPLE__)
  1183
+  /* Terminate select loop first */
  1184
+  if (handle->select != NULL) {
  1185
+    uv__stream_select_t* s;
  1186
+
  1187
+    s = handle->select;
  1188
+
  1189
+    uv_sem_post(&s->sem);
  1190
+    uv_thread_join(&s->thread);
  1191
+    uv_sem_destroy(&s->sem);
  1192
+    uv_mutex_destroy(&s->mutex);
  1193
+    close(s->fake_fd);
  1194
+    uv_close((uv_handle_t*) &s->async, uv__stream_osx_cb_close);
  1195
+
  1196
+    handle->select = NULL;
  1197
+  }
  1198
+#endif /* defined(__APPLE__) */
  1199
+
983 1200
   uv_read_stop(handle);
984 1201
   uv__io_stop(handle->loop, &handle->write_watcher);
985 1202
 
3  deps/uv/src/win/core.c
@@ -73,6 +73,9 @@ static void uv_loop_init(uv_loop_t* loop) {
73 73
     uv_fatal_error(GetLastError(), "CreateIoCompletionPort");
74 74
   }
75 75
 
  76
+  /* To prevent uninitialized memory access, loop->time must be intialized */
  77
+  /* to zero before calling uv_update_time for the first time. */
  78
+  loop->time = 0;
76 79
   uv_update_time(loop);
77 80
 
78 81
   ngx_queue_init(&loop->handle_queue);
8  deps/uv/src/win/signal.c
@@ -169,6 +169,10 @@ static uv_err_t uv__signal_register(int signum) {
169 169
     case SIGHUP:
170 170
       return uv__signal_register_control_handler();
171 171
 
  172
+    case SIGWINCH:
  173
+      /* SIGWINCH is generated in tty.c. No need to register anything. */
  174
+      return uv_ok_;
  175
+
172 176
     case SIGILL:
173 177
     case SIGABRT_COMPAT:
174 178
     case SIGFPE:
@@ -193,6 +197,10 @@ static void uv__signal_unregister(int signum) {
193 197
       uv__signal_unregister_control_handler();
194 198
       return;
195 199
 
  200
+    case SIGWINCH:
  201
+      /* SIGWINCH is generated in tty.c. No need to unregister anything. */
  202
+      return;
  203
+
196 204
     case SIGILL:
197 205
     case SIGABRT_COMPAT:
198 206
     case SIGFPE:
41  deps/uv/src/win/tty.c
@@ -82,6 +82,8 @@ static int uv_tty_virtual_width = -1;
82 82
 
83 83
 static CRITICAL_SECTION uv_tty_output_lock;
84 84
 
  85
+static HANDLE uv_tty_output_handle = INVALID_HANDLE_VALUE;
  86
+
85 87
 
86 88
 void uv_console_init() {
87 89
   InitializeCriticalSection(&uv_tty_output_lock);
@@ -113,10 +115,17 @@ int uv_tty_init(uv_loop_t* loop, uv_tty_t* tty, uv_file fd, int readable) {
113 115
       return -1;
114 116
     }
115 117
 
116  
-    /* Update the virtual window. We must hold the tty_output_lock because the */
117  
-    /* virtual window state is shared between all uv_tty handles. */
  118
+    /* Obtain the the tty_output_lock because the virtual window state is */
  119
+    /* shared between all uv_tty_t handles. */
118 120
     EnterCriticalSection(&uv_tty_output_lock);
  121
+
  122
+    /* Store the global tty output handle. This handle is used by TTY read */
  123
+    /* streams to update the virtual window when a CONSOLE_BUFFER_SIZE_EVENT */
  124
+    /* is received. */
  125
+    uv_tty_output_handle = handle;
  126
+
119 127
     uv_tty_update_virtual_window(&screen_buffer_info);
  128
+
120 129
     LeaveCriticalSection(&uv_tty_output_lock);
121 130
   }
122 131
 
@@ -513,7 +522,20 @@ void uv_process_tty_read_raw_req(uv_loop_t* loop, uv_tty_t* handle,
513 522
       }
514 523
       records_left--;
515 524
 
516  
-      /* Ignore events that are not keyboard events */
  525
+      /* If the window was resized, recompute the virtual window size. This */
  526
+      /* will trigger a SIGWINCH signal if the window size changed in an */
  527
+      /* way that matters to libuv. */
  528
+      if (handle->last_input_record.EventType == WINDOW_BUFFER_SIZE_EVENT) {
  529
+        CONSOLE_SCREEN_BUFFER_INFO info;
  530
+        if (uv_tty_output_handle == INVALID_HANDLE_VALUE)
  531
+          continue;
  532
+        if (!GetConsoleScreenBufferInfo(uv_tty_output_handle, &info))
  533
+          continue;
  534
+        uv_tty_update_virtual_window(&info);
  535
+        continue;
  536
+      }
  537
+
  538
+      /* Ignore other events that are not key or resize events. */
517 539
       if (handle->last_input_record.EventType != KEY_EVENT) {
518 540
         continue;
519 541
       }
@@ -835,8 +857,11 @@ int uv_tty_read_stop(uv_tty_t* handle) {
835 857
 
836 858
 
837 859
 static void uv_tty_update_virtual_window(CONSOLE_SCREEN_BUFFER_INFO* info) {
838  
-  uv_tty_virtual_height = info->srWindow.Bottom - info->srWindow.Top + 1;
  860
+  int old_virtual_width = uv_tty_virtual_width;
  861
+  int old_virtual_height = uv_tty_virtual_height;
  862
+
839 863
   uv_tty_virtual_width = info->dwSize.X;
  864
+  uv_tty_virtual_height = info->srWindow.Bottom - info->srWindow.Top + 1;
840 865
 
841 866
   /* Recompute virtual window offset row. */
842 867
   if (uv_tty_virtual_offset == -1) {
@@ -854,6 +879,14 @@ static void uv_tty_update_virtual_window(CONSOLE_SCREEN_BUFFER_INFO* info) {
854 879
   if (uv_tty_virtual_offset < 0) {
855 880
     uv_tty_virtual_offset = 0;
856 881
   }
  882
+
  883
+  /* If the virtual window size changed, emit a SIGWINCH signal. Don't emit */
  884
+  /* if this was the first time the virtual window size was computed. */
  885
+  if (old_virtual_width != -1 && old_virtual_height != -1 &&
  886
+      (uv_tty_virtual_width != old_virtual_width ||
  887
+       uv_tty_virtual_height != old_virtual_height)) {
  888
+    uv__signal_dispatch(SIGWINCH);
  889
+  }
857 890
 }
858 891
 
859 892
 

0 notes on commit ed093f1

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