1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
23 # define _WIN32_WINNT 0x0600
26 #if !defined(_SSIZE_T_) && !defined(_SSIZE_T_DEFINED)
27 typedef intptr_t ssize_t;
28 # define SSIZE_MAX INTPTR_MAX
30 # define _SSIZE_T_DEFINED
35 #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
36 typedef struct pollfd {
40 } WSAPOLLFD, *PWSAPOLLFD, *LPWSAPOLLFD;
43 #ifndef LOCALE_INVARIANT
44 # define LOCALE_INVARIANT 0x007f
48 // Disable the typedef in mstcpip.h of MinGW.
49 #define _TCP_INITIAL_RTO_PARAMETERS _TCP_INITIAL_RTO_PARAMETERS__AVOID
50 #define TCP_INITIAL_RTO_PARAMETERS TCP_INITIAL_RTO_PARAMETERS__AVOID
51 #define PTCP_INITIAL_RTO_PARAMETERS PTCP_INITIAL_RTO_PARAMETERS__AVOID
53 #undef _TCP_INITIAL_RTO_PARAMETERS
54 #undef TCP_INITIAL_RTO_PARAMETERS
55 #undef PTCP_INITIAL_RTO_PARAMETERS
63 #if defined(_MSC_VER) && _MSC_VER < 1600
64 # include "stdint-msvc2008.h"
70 #include "threadpool.h"
72 #define MAX_PIPENAME_LEN 256
75 # define S_IFLNK 0xA000
78 /* Additional signals supported by uv_signal and or uv_kill. The CRT defines
79 * the following signals already:
83 * #define SIGABRT_COMPAT 6
90 * The additional signals have values that are common on other Unix
91 * variants (Linux and Darwin)
97 /* Redefine NSIG to take SIGWINCH into consideration */
98 #if defined(NSIG) && NSIG <= SIGWINCH
102 # define NSIG SIGWINCH + 1
105 /* The CRT defines SIGABRT_COMPAT as 6, which equals SIGABRT on many unix-like
106 * platforms. However MinGW doesn't define it, so we do. */
107 #ifndef SIGABRT_COMPAT
108 # define SIGABRT_COMPAT 6
112 * Guids and typedefs for winsock extension functions
113 * Mingw32 doesn't have these :-(
115 #ifndef WSAID_ACCEPTEX
116 # define WSAID_ACCEPTEX \
117 {0xb5367df1, 0xcbac, 0x11cf, \
118 {0x95, 0xca, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92}}
121 #ifndef WSAID_CONNECTEX
122 # define WSAID_CONNECTEX \
123 {0x25a207b9, 0xddf3, 0x4660, \
124 {0x8e, 0xe9, 0x76, 0xe5, 0x8c, 0x74, 0x06, 0x3e}}
127 #ifndef WSAID_GETACCEPTEXSOCKADDRS
128 # define WSAID_GETACCEPTEXSOCKADDRS \
129 {0xb5367df2, 0xcbac, 0x11cf, \
130 {0x95, 0xca, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92}}
133 #ifndef WSAID_DISCONNECTEX
134 # define WSAID_DISCONNECTEX \
135 {0x7fda2e11, 0x8630, 0x436f, \
136 {0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
139 #ifndef WSAID_TRANSMITFILE
140 # define WSAID_TRANSMITFILE \
141 {0xb5367df0, 0xcbac, 0x11cf, \
142 {0x95, 0xca, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92}}
145 #if (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) \
146 || (defined(_MSC_VER) && _MSC_VER < 1500)
147 typedef BOOL (PASCAL *LPFN_ACCEPTEX)
148 (SOCKET sListenSocket,
149 SOCKET sAcceptSocket,
150 PVOID lpOutputBuffer,
151 DWORD dwReceiveDataLength,
152 DWORD dwLocalAddressLength,
153 DWORD dwRemoteAddressLength,
154 LPDWORD lpdwBytesReceived,
155 LPOVERLAPPED lpOverlapped);
157 typedef BOOL (PASCAL *LPFN_CONNECTEX)
159 const struct sockaddr* name,
162 DWORD dwSendDataLength,
163 LPDWORD lpdwBytesSent,
164 LPOVERLAPPED lpOverlapped);
166 typedef void (PASCAL *LPFN_GETACCEPTEXSOCKADDRS)
167 (PVOID lpOutputBuffer,
168 DWORD dwReceiveDataLength,
169 DWORD dwLocalAddressLength,
170 DWORD dwRemoteAddressLength,
171 LPSOCKADDR* LocalSockaddr,
172 LPINT LocalSockaddrLength,
173 LPSOCKADDR* RemoteSockaddr,
174 LPINT RemoteSockaddrLength);
176 typedef BOOL (PASCAL *LPFN_DISCONNECTEX)
178 LPOVERLAPPED lpOverlapped,
182 typedef BOOL (PASCAL *LPFN_TRANSMITFILE)
185 DWORD nNumberOfBytesToWrite,
186 DWORD nNumberOfBytesPerSend,
187 LPOVERLAPPED lpOverlapped,
188 LPTRANSMIT_FILE_BUFFERS lpTransmitBuffers,
191 typedef PVOID RTL_SRWLOCK;
192 typedef RTL_SRWLOCK SRWLOCK, *PSRWLOCK;
195 typedef int (WSAAPI* LPFN_WSARECV)
201 LPWSAOVERLAPPED overlapped,
202 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_routine);
204 typedef int (WSAAPI* LPFN_WSARECVFROM)
210 struct sockaddr* addr,
212 LPWSAOVERLAPPED overlapped,
213 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_routine);
216 typedef LONG NTSTATUS;
217 typedef NTSTATUS *PNTSTATUS;
220 #ifndef RTL_CONDITION_VARIABLE_INIT
221 typedef PVOID CONDITION_VARIABLE, *PCONDITION_VARIABLE;
224 typedef struct _AFD_POLL_HANDLE_INFO {
228 } AFD_POLL_HANDLE_INFO, *PAFD_POLL_HANDLE_INFO;
230 typedef struct _AFD_POLL_INFO {
231 LARGE_INTEGER Timeout;
232 ULONG NumberOfHandles;
234 AFD_POLL_HANDLE_INFO Handles[1];
235 } AFD_POLL_INFO, *PAFD_POLL_INFO;
237 #define UV_MSAFD_PROVIDER_COUNT 3
241 * It should be possible to cast uv_buf_t[] to WSABUF[]
242 * see http://msdn.microsoft.com/en-us/library/ms741542(v=vs.85).aspx
244 typedef struct uv_buf_t {
250 typedef SOCKET uv_os_sock_t;
251 typedef HANDLE uv_os_fd_t;
252 typedef int uv_pid_t;
254 typedef HANDLE uv_thread_t;
256 typedef HANDLE uv_sem_t;
258 typedef CRITICAL_SECTION uv_mutex_t;
260 /* This condition variable implementation is based on the SetEvent solution
261 * (section 3.2) at http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
262 * We could not use the SignalObjectAndWait solution (section 3.4) because
263 * it want the 2nd argument (type uv_mutex_t) of uv_cond_wait() and
264 * uv_cond_timedwait() to be HANDLEs, but we use CRITICAL_SECTIONs.
268 CONDITION_VARIABLE cond_var;
270 unsigned int waiters_count;
271 CRITICAL_SECTION waiters_count_lock;
273 HANDLE broadcast_event;
274 } unused_; /* TODO: retained for ABI compatibility; remove me in v2.x. */
278 SRWLOCK read_write_lock_;
279 /* TODO: retained for ABI compatibility; remove me in v2.x */
281 unsigned char padding_[72];
283 unsigned char padding_[44];
299 #define UV_ONCE_INIT { 0, NULL }
301 typedef struct uv_once_s {
306 /* Platform-specific definitions for uv_spawn support. */
307 typedef unsigned char uv_uid_t;
308 typedef unsigned char uv_gid_t;
310 typedef struct uv__dirent_s {
315 #define UV_DIR_PRIVATE_FIELDS \
317 WIN32_FIND_DATAW find_data; \
320 #define HAVE_DIRENT_TYPES
321 #define UV__DT_DIR UV_DIRENT_DIR
322 #define UV__DT_FILE UV_DIRENT_FILE
323 #define UV__DT_LINK UV_DIRENT_LINK
324 #define UV__DT_FIFO UV_DIRENT_FIFO
325 #define UV__DT_SOCKET UV_DIRENT_SOCKET
326 #define UV__DT_CHAR UV_DIRENT_CHAR
327 #define UV__DT_BLOCK UV_DIRENT_BLOCK
329 /* Platform-specific definitions for uv_dlopen support. */
330 #define UV_DYNAMIC FAR WINAPI
336 #define UV_LOOP_PRIVATE_FIELDS \
337 /* The loop's I/O completion port */ \
339 /* The current time according to the event loop. in msecs. */ \
341 /* Tail of a single-linked circular queue of pending reqs. If the queue */ \
342 /* is empty, tail_ is NULL. If there is only one item, */ \
343 /* tail_->next_req == tail_ */ \
344 uv_req_t* pending_reqs_tail; \
345 /* Head of a single-linked list of closed handles */ \
346 uv_handle_t* endgame_handles; \
347 /* TODO(bnoordhuis) Stop heap-allocating |timer_heap| in libuv v2.x. */ \
349 /* Lists of active loop (prepare / check / idle) watchers */ \
350 uv_prepare_t* prepare_handles; \
351 uv_check_t* check_handles; \
352 uv_idle_t* idle_handles; \
353 /* This pointer will refer to the prepare/check/idle handle whose */ \
354 /* callback is scheduled to be called next. This is needed to allow */ \
355 /* safe removal from one of the lists above while that list being */ \
356 /* iterated over. */ \
357 uv_prepare_t* next_prepare_handle; \
358 uv_check_t* next_check_handle; \
359 uv_idle_t* next_idle_handle; \
360 /* This handle holds the peer sockets for the fast variant of uv_poll_t */ \
361 SOCKET poll_peer_sockets[UV_MSAFD_PROVIDER_COUNT]; \
362 /* Counter to keep track of active tcp streams */ \
363 unsigned int active_tcp_streams; \
364 /* Counter to keep track of active udp streams */ \
365 unsigned int active_udp_streams; \
366 /* Counter to started timer */ \
367 uint64_t timer_counter; \
370 uv_mutex_t wq_mutex; \
373 #define UV_REQ_TYPE_PRIVATE \
374 /* TODO: remove the req suffix */ \
384 #define UV_REQ_PRIVATE_FIELDS \
386 /* Used by I/O operations */ \
388 OVERLAPPED overlapped; \
389 size_t queued_bytes; \
392 struct uv_req_s* next_req;
394 #define UV_WRITE_PRIVATE_FIELDS \
396 uv_buf_t write_buffer; \
397 HANDLE event_handle; \
400 #define UV_CONNECT_PRIVATE_FIELDS \
403 #define UV_SHUTDOWN_PRIVATE_FIELDS \
406 #define UV_UDP_SEND_PRIVATE_FIELDS \
409 #define UV_PRIVATE_REQ_TYPES \
410 typedef struct uv_pipe_accept_s { \
413 struct uv_pipe_accept_s* next_pending; \
414 } uv_pipe_accept_t; \
416 typedef struct uv_tcp_accept_s { \
418 SOCKET accept_socket; \
419 char accept_buffer[sizeof(struct sockaddr_storage) * 2 + 32]; \
420 HANDLE event_handle; \
421 HANDLE wait_handle; \
422 struct uv_tcp_accept_s* next_pending; \
425 typedef struct uv_read_s { \
427 HANDLE event_handle; \
428 HANDLE wait_handle; \
431 #define uv_stream_connection_fields \
432 unsigned int write_reqs_pending; \
433 uv_shutdown_t* shutdown_req;
435 #define uv_stream_server_fields \
436 uv_connection_cb connection_cb;
438 #define UV_STREAM_PRIVATE_FIELDS \
439 unsigned int reqs_pending; \
441 uv_read_t read_req; \
443 struct { uv_stream_connection_fields } conn; \
444 struct { uv_stream_server_fields } serv; \
447 #define uv_tcp_server_fields \
448 uv_tcp_accept_t* accept_reqs; \
449 unsigned int processed_accepts; \
450 uv_tcp_accept_t* pending_accepts; \
451 LPFN_ACCEPTEX func_acceptex;
453 #define uv_tcp_connection_fields \
454 uv_buf_t read_buffer; \
455 LPFN_CONNECTEX func_connectex;
457 #define UV_TCP_PRIVATE_FIELDS \
461 struct { uv_tcp_server_fields } serv; \
462 struct { uv_tcp_connection_fields } conn; \
465 #define UV_UDP_PRIVATE_FIELDS \
467 unsigned int reqs_pending; \
470 uv_buf_t recv_buffer; \
471 struct sockaddr_storage recv_from; \
473 uv_udp_recv_cb recv_cb; \
474 uv_alloc_cb alloc_cb; \
475 LPFN_WSARECV func_wsarecv; \
476 LPFN_WSARECVFROM func_wsarecvfrom;
478 #define uv_pipe_server_fields \
479 int pending_instances; \
480 uv_pipe_accept_t* accept_reqs; \
481 uv_pipe_accept_t* pending_accepts;
483 #define uv_pipe_connection_fields \
484 uv_timer_t* eof_timer; \
485 uv_write_t dummy; /* TODO: retained for ABI compat; remove this in v2.x. */ \
486 DWORD ipc_remote_pid; \
488 uint32_t payload_remaining; \
489 uint64_t dummy; /* TODO: retained for ABI compat; remove this in v2.x. */ \
491 void* ipc_xfer_queue[2]; \
492 int ipc_xfer_queue_length; \
493 uv_write_t* non_overlapped_writes_tail; \
494 CRITICAL_SECTION readfile_thread_lock; \
495 volatile HANDLE readfile_thread_handle;
497 #define UV_PIPE_PRIVATE_FIELDS \
501 struct { uv_pipe_server_fields } serv; \
502 struct { uv_pipe_connection_fields } conn; \
505 /* TODO: put the parser states in an union - TTY handles are always half-duplex
506 * so read-state can safely overlap write-state. */
507 #define UV_TTY_PRIVATE_FIELDS \
511 /* Used for readable TTY handles */ \
512 /* TODO: remove me in v2.x. */ \
514 uv_buf_t read_line_buffer; \
515 HANDLE read_raw_wait; \
516 /* Fields used for translating win keystrokes into vt100 characters */ \
518 unsigned char last_key_offset; \
519 unsigned char last_key_len; \
520 WCHAR last_utf16_high_surrogate; \
521 INPUT_RECORD last_input_record; \
524 /* Used for writable TTY handles */ \
525 /* utf8-to-utf16 conversion state */ \
526 unsigned int utf8_codepoint; \
527 unsigned char utf8_bytes_left; \
528 /* eol conversion state */ \
529 unsigned char previous_eol; \
530 /* ansi parser state */ \
531 unsigned short ansi_parser_state; \
532 unsigned char ansi_csi_argc; \
533 unsigned short ansi_csi_argv[4]; \
534 COORD saved_position; \
535 WORD saved_attributes; \
539 #define UV_POLL_PRIVATE_FIELDS \
541 /* Used in fast mode */ \
542 SOCKET peer_socket; \
543 AFD_POLL_INFO afd_poll_info_1; \
544 AFD_POLL_INFO afd_poll_info_2; \
545 /* Used in fast and slow mode. */ \
546 uv_req_t poll_req_1; \
547 uv_req_t poll_req_2; \
548 unsigned char submitted_events_1; \
549 unsigned char submitted_events_2; \
550 unsigned char mask_events_1; \
551 unsigned char mask_events_2; \
552 unsigned char events;
554 #define UV_TIMER_PRIVATE_FIELDS \
555 void* heap_node[3]; \
560 uv_timer_cb timer_cb;
562 #define UV_ASYNC_PRIVATE_FIELDS \
563 struct uv_req_s async_req; \
564 uv_async_cb async_cb; \
565 /* char to avoid alignment issues */ \
566 char volatile async_sent;
568 #define UV_PREPARE_PRIVATE_FIELDS \
569 uv_prepare_t* prepare_prev; \
570 uv_prepare_t* prepare_next; \
571 uv_prepare_cb prepare_cb;
573 #define UV_CHECK_PRIVATE_FIELDS \
574 uv_check_t* check_prev; \
575 uv_check_t* check_next; \
576 uv_check_cb check_cb;
578 #define UV_IDLE_PRIVATE_FIELDS \
579 uv_idle_t* idle_prev; \
580 uv_idle_t* idle_next; \
583 #define UV_HANDLE_PRIVATE_FIELDS \
584 uv_handle_t* endgame_next; \
587 #define UV_GETADDRINFO_PRIVATE_FIELDS \
588 struct uv__work work_req; \
589 uv_getaddrinfo_cb getaddrinfo_cb; \
593 /* The addrinfoW field is used to store a pointer to the hints, and */ \
594 /* later on to store the result of GetAddrInfoW. The final result will */ \
595 /* be converted to struct addrinfo* and stored in the addrinfo field. */ \
596 struct addrinfoW* addrinfow; \
597 struct addrinfo* addrinfo; \
600 #define UV_GETNAMEINFO_PRIVATE_FIELDS \
601 struct uv__work work_req; \
602 uv_getnameinfo_cb getnameinfo_cb; \
603 struct sockaddr_storage storage; \
605 char host[NI_MAXHOST]; \
606 char service[NI_MAXSERV]; \
609 #define UV_PROCESS_PRIVATE_FIELDS \
610 struct uv_process_exit_s { \
613 BYTE* child_stdio_buffer; \
615 HANDLE wait_handle; \
616 HANDLE process_handle; \
617 volatile char exit_cb_pending;
619 #define UV_FS_PRIVATE_FIELDS \
620 struct uv__work work_req; \
624 /* TODO: remove me in 0.9. */ \
634 unsigned int nbufs; \
637 uv_buf_t bufsml[4]; \
645 #define UV_WORK_PRIVATE_FIELDS \
646 struct uv__work work_req;
648 #define UV_FS_EVENT_PRIVATE_FIELDS \
649 struct uv_fs_event_req_s { \
656 WCHAR* short_filew; \
660 #define UV_SIGNAL_PRIVATE_FIELDS \
661 RB_ENTRY(uv_signal_s) tree_entry; \
662 struct uv_req_s signal_req; \
663 unsigned long pending_signum;
678 /* fs open() flags supported on this platform: */
679 #define UV_FS_O_APPEND _O_APPEND
680 #define UV_FS_O_CREAT _O_CREAT
681 #define UV_FS_O_EXCL _O_EXCL
682 #define UV_FS_O_FILEMAP 0x20000000
683 #define UV_FS_O_RANDOM _O_RANDOM
684 #define UV_FS_O_RDONLY _O_RDONLY
685 #define UV_FS_O_RDWR _O_RDWR
686 #define UV_FS_O_SEQUENTIAL _O_SEQUENTIAL
687 #define UV_FS_O_SHORT_LIVED _O_SHORT_LIVED
688 #define UV_FS_O_TEMPORARY _O_TEMPORARY
689 #define UV_FS_O_TRUNC _O_TRUNC
690 #define UV_FS_O_WRONLY _O_WRONLY
692 /* fs open() flags supported on other platforms (or mapped on this platform): */
693 #define UV_FS_O_DIRECT 0x02000000 /* FILE_FLAG_NO_BUFFERING */
694 #define UV_FS_O_DIRECTORY 0
695 #define UV_FS_O_DSYNC 0x04000000 /* FILE_FLAG_WRITE_THROUGH */
696 #define UV_FS_O_EXLOCK 0x10000000 /* EXCLUSIVE SHARING MODE */
697 #define UV_FS_O_NOATIME 0
698 #define UV_FS_O_NOCTTY 0
699 #define UV_FS_O_NOFOLLOW 0
700 #define UV_FS_O_NONBLOCK 0
701 #define UV_FS_O_SYMLINK 0
702 #define UV_FS_O_SYNC 0x08000000 /* FILE_FLAG_WRITE_THROUGH */