uv-common.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. * This file is private to libuv. It provides common functionality to both
  23. * Windows and Unix backends.
  24. */
  25. #ifndef UV_COMMON_H_
  26. #define UV_COMMON_H_
  27. #include <assert.h>
  28. #include <stdarg.h>
  29. #include <stddef.h>
  30. #if defined(_MSC_VER) && _MSC_VER < 1600
  31. # include "uv/stdint-msvc2008.h"
  32. #else
  33. # include <stdint.h>
  34. #endif
  35. #include "uv.h"
  36. #include "uv/tree.h"
  37. #include "queue.h"
  38. #include "strscpy.h"
  39. #if EDOM > 0
  40. # define UV__ERR(x) (-(x))
  41. #else
  42. # define UV__ERR(x) (x)
  43. #endif
  44. #if !defined(snprintf) && defined(_MSC_VER) && _MSC_VER < 1900
  45. extern int snprintf(char*, size_t, const char*, ...);
  46. #endif
  47. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  48. #define container_of(ptr, type, member) \
  49. ((type *) ((char *) (ptr) - offsetof(type, member)))
  50. #define STATIC_ASSERT(expr) \
  51. void uv__static_assert(int static_assert_failed[1 - 2 * !(expr)])
  52. /* Handle flags. Some flags are specific to Windows or UNIX. */
  53. enum {
  54. /* Used by all handles. */
  55. UV_HANDLE_CLOSING = 0x00000001,
  56. UV_HANDLE_CLOSED = 0x00000002,
  57. UV_HANDLE_ACTIVE = 0x00000004,
  58. UV_HANDLE_REF = 0x00000008,
  59. UV_HANDLE_INTERNAL = 0x00000010,
  60. UV_HANDLE_ENDGAME_QUEUED = 0x00000020,
  61. /* Used by streams. */
  62. UV_HANDLE_LISTENING = 0x00000040,
  63. UV_HANDLE_CONNECTION = 0x00000080,
  64. UV_HANDLE_SHUTTING = 0x00000100,
  65. UV_HANDLE_SHUT = 0x00000200,
  66. UV_HANDLE_READ_PARTIAL = 0x00000400,
  67. UV_HANDLE_READ_EOF = 0x00000800,
  68. /* Used by streams and UDP handles. */
  69. UV_HANDLE_READING = 0x00001000,
  70. UV_HANDLE_BOUND = 0x00002000,
  71. UV_HANDLE_READABLE = 0x00004000,
  72. UV_HANDLE_WRITABLE = 0x00008000,
  73. UV_HANDLE_READ_PENDING = 0x00010000,
  74. UV_HANDLE_SYNC_BYPASS_IOCP = 0x00020000,
  75. UV_HANDLE_ZERO_READ = 0x00040000,
  76. UV_HANDLE_EMULATE_IOCP = 0x00080000,
  77. UV_HANDLE_BLOCKING_WRITES = 0x00100000,
  78. UV_HANDLE_CANCELLATION_PENDING = 0x00200000,
  79. /* Used by uv_tcp_t and uv_udp_t handles */
  80. UV_HANDLE_IPV6 = 0x00400000,
  81. /* Only used by uv_tcp_t handles. */
  82. UV_HANDLE_TCP_NODELAY = 0x01000000,
  83. UV_HANDLE_TCP_KEEPALIVE = 0x02000000,
  84. UV_HANDLE_TCP_SINGLE_ACCEPT = 0x04000000,
  85. UV_HANDLE_TCP_ACCEPT_STATE_CHANGING = 0x08000000,
  86. UV_HANDLE_TCP_SOCKET_CLOSED = 0x10000000,
  87. UV_HANDLE_SHARED_TCP_SOCKET = 0x20000000,
  88. /* Only used by uv_udp_t handles. */
  89. UV_HANDLE_UDP_PROCESSING = 0x01000000,
  90. UV_HANDLE_UDP_CONNECTED = 0x02000000,
  91. UV_HANDLE_UDP_RECVMMSG = 0x04000000,
  92. /* Only used by uv_pipe_t handles. */
  93. UV_HANDLE_NON_OVERLAPPED_PIPE = 0x01000000,
  94. UV_HANDLE_PIPESERVER = 0x02000000,
  95. /* Only used by uv_tty_t handles. */
  96. UV_HANDLE_TTY_READABLE = 0x01000000,
  97. UV_HANDLE_TTY_RAW = 0x02000000,
  98. UV_HANDLE_TTY_SAVED_POSITION = 0x04000000,
  99. UV_HANDLE_TTY_SAVED_ATTRIBUTES = 0x08000000,
  100. /* Only used by uv_signal_t handles. */
  101. UV_SIGNAL_ONE_SHOT_DISPATCHED = 0x01000000,
  102. UV_SIGNAL_ONE_SHOT = 0x02000000,
  103. /* Only used by uv_poll_t handles. */
  104. UV_HANDLE_POLL_SLOW = 0x01000000
  105. };
  106. int uv__loop_configure(uv_loop_t* loop, uv_loop_option option, va_list ap);
  107. void uv__loop_close(uv_loop_t* loop);
  108. int uv__tcp_bind(uv_tcp_t* tcp,
  109. const struct sockaddr* addr,
  110. unsigned int addrlen,
  111. unsigned int flags);
  112. int uv__tcp_connect(uv_connect_t* req,
  113. uv_tcp_t* handle,
  114. const struct sockaddr* addr,
  115. unsigned int addrlen,
  116. uv_connect_cb cb);
  117. int uv__udp_bind(uv_udp_t* handle,
  118. const struct sockaddr* addr,
  119. unsigned int addrlen,
  120. unsigned int flags);
  121. int uv__udp_connect(uv_udp_t* handle,
  122. const struct sockaddr* addr,
  123. unsigned int addrlen);
  124. int uv__udp_disconnect(uv_udp_t* handle);
  125. int uv__udp_is_connected(uv_udp_t* handle);
  126. int uv__udp_send(uv_udp_send_t* req,
  127. uv_udp_t* handle,
  128. const uv_buf_t bufs[],
  129. unsigned int nbufs,
  130. const struct sockaddr* addr,
  131. unsigned int addrlen,
  132. uv_udp_send_cb send_cb);
  133. int uv__udp_try_send(uv_udp_t* handle,
  134. const uv_buf_t bufs[],
  135. unsigned int nbufs,
  136. const struct sockaddr* addr,
  137. unsigned int addrlen);
  138. int uv__udp_recv_start(uv_udp_t* handle, uv_alloc_cb alloccb,
  139. uv_udp_recv_cb recv_cb);
  140. int uv__udp_recv_stop(uv_udp_t* handle);
  141. void uv__fs_poll_close(uv_fs_poll_t* handle);
  142. int uv__getaddrinfo_translate_error(int sys_err); /* EAI_* error. */
  143. enum uv__work_kind {
  144. UV__WORK_CPU,
  145. UV__WORK_FAST_IO,
  146. UV__WORK_SLOW_IO
  147. };
  148. void uv__work_submit(uv_loop_t* loop,
  149. struct uv__work *w,
  150. enum uv__work_kind kind,
  151. void (*work)(struct uv__work *w),
  152. void (*done)(struct uv__work *w, int status));
  153. void uv__work_done(uv_async_t* handle);
  154. size_t uv__count_bufs(const uv_buf_t bufs[], unsigned int nbufs);
  155. int uv__socket_sockopt(uv_handle_t* handle, int optname, int* value);
  156. void uv__fs_scandir_cleanup(uv_fs_t* req);
  157. void uv__fs_readdir_cleanup(uv_fs_t* req);
  158. uv_dirent_type_t uv__fs_get_dirent_type(uv__dirent_t* dent);
  159. int uv__next_timeout(const uv_loop_t* loop);
  160. void uv__run_timers(uv_loop_t* loop);
  161. void uv__timer_close(uv_timer_t* handle);
  162. #define uv__has_active_reqs(loop) \
  163. ((loop)->active_reqs.count > 0)
  164. #define uv__req_register(loop, req) \
  165. do { \
  166. (loop)->active_reqs.count++; \
  167. } \
  168. while (0)
  169. #define uv__req_unregister(loop, req) \
  170. do { \
  171. assert(uv__has_active_reqs(loop)); \
  172. (loop)->active_reqs.count--; \
  173. } \
  174. while (0)
  175. #define uv__has_active_handles(loop) \
  176. ((loop)->active_handles > 0)
  177. #define uv__active_handle_add(h) \
  178. do { \
  179. (h)->loop->active_handles++; \
  180. } \
  181. while (0)
  182. #define uv__active_handle_rm(h) \
  183. do { \
  184. (h)->loop->active_handles--; \
  185. } \
  186. while (0)
  187. #define uv__is_active(h) \
  188. (((h)->flags & UV_HANDLE_ACTIVE) != 0)
  189. #define uv__is_closing(h) \
  190. (((h)->flags & (UV_HANDLE_CLOSING | UV_HANDLE_CLOSED)) != 0)
  191. #define uv__handle_start(h) \
  192. do { \
  193. if (((h)->flags & UV_HANDLE_ACTIVE) != 0) break; \
  194. (h)->flags |= UV_HANDLE_ACTIVE; \
  195. if (((h)->flags & UV_HANDLE_REF) != 0) uv__active_handle_add(h); \
  196. } \
  197. while (0)
  198. #define uv__handle_stop(h) \
  199. do { \
  200. if (((h)->flags & UV_HANDLE_ACTIVE) == 0) break; \
  201. (h)->flags &= ~UV_HANDLE_ACTIVE; \
  202. if (((h)->flags & UV_HANDLE_REF) != 0) uv__active_handle_rm(h); \
  203. } \
  204. while (0)
  205. #define uv__handle_ref(h) \
  206. do { \
  207. if (((h)->flags & UV_HANDLE_REF) != 0) break; \
  208. (h)->flags |= UV_HANDLE_REF; \
  209. if (((h)->flags & UV_HANDLE_CLOSING) != 0) break; \
  210. if (((h)->flags & UV_HANDLE_ACTIVE) != 0) uv__active_handle_add(h); \
  211. } \
  212. while (0)
  213. #define uv__handle_unref(h) \
  214. do { \
  215. if (((h)->flags & UV_HANDLE_REF) == 0) break; \
  216. (h)->flags &= ~UV_HANDLE_REF; \
  217. if (((h)->flags & UV_HANDLE_CLOSING) != 0) break; \
  218. if (((h)->flags & UV_HANDLE_ACTIVE) != 0) uv__active_handle_rm(h); \
  219. } \
  220. while (0)
  221. #define uv__has_ref(h) \
  222. (((h)->flags & UV_HANDLE_REF) != 0)
  223. #if defined(_WIN32)
  224. # define uv__handle_platform_init(h) ((h)->u.fd = -1)
  225. #else
  226. # define uv__handle_platform_init(h) ((h)->next_closing = NULL)
  227. #endif
  228. #define uv__handle_init(loop_, h, type_) \
  229. do { \
  230. (h)->loop = (loop_); \
  231. (h)->type = (type_); \
  232. (h)->flags = UV_HANDLE_REF; /* Ref the loop when active. */ \
  233. QUEUE_INSERT_TAIL(&(loop_)->handle_queue, &(h)->handle_queue); \
  234. uv__handle_platform_init(h); \
  235. } \
  236. while (0)
  237. /* Note: uses an open-coded version of SET_REQ_SUCCESS() because of
  238. * a circular dependency between src/uv-common.h and src/win/internal.h.
  239. */
  240. #if defined(_WIN32)
  241. # define UV_REQ_INIT(req, typ) \
  242. do { \
  243. (req)->type = (typ); \
  244. (req)->u.io.overlapped.Internal = 0; /* SET_REQ_SUCCESS() */ \
  245. } \
  246. while (0)
  247. #else
  248. # define UV_REQ_INIT(req, typ) \
  249. do { \
  250. (req)->type = (typ); \
  251. } \
  252. while (0)
  253. #endif
  254. #define uv__req_init(loop, req, typ) \
  255. do { \
  256. UV_REQ_INIT(req, typ); \
  257. uv__req_register(loop, req); \
  258. } \
  259. while (0)
  260. /* Allocator prototypes */
  261. void *uv__calloc(size_t count, size_t size);
  262. char *uv__strdup(const char* s);
  263. char *uv__strndup(const char* s, size_t n);
  264. void* uv__malloc(size_t size);
  265. void uv__free(void* ptr);
  266. void* uv__realloc(void* ptr, size_t size);
  267. void* uv__reallocf(void* ptr, size_t size);
  268. #endif /* UV_COMMON_H_ */