uv-common.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. /* Only used by uv_pipe_t handles. */
  91. UV_HANDLE_NON_OVERLAPPED_PIPE = 0x01000000,
  92. UV_HANDLE_PIPESERVER = 0x02000000,
  93. /* Only used by uv_tty_t handles. */
  94. UV_HANDLE_TTY_READABLE = 0x01000000,
  95. UV_HANDLE_TTY_RAW = 0x02000000,
  96. UV_HANDLE_TTY_SAVED_POSITION = 0x04000000,
  97. UV_HANDLE_TTY_SAVED_ATTRIBUTES = 0x08000000,
  98. /* Only used by uv_signal_t handles. */
  99. UV_SIGNAL_ONE_SHOT_DISPATCHED = 0x01000000,
  100. UV_SIGNAL_ONE_SHOT = 0x02000000,
  101. /* Only used by uv_poll_t handles. */
  102. UV_HANDLE_POLL_SLOW = 0x01000000
  103. };
  104. int uv__loop_configure(uv_loop_t* loop, uv_loop_option option, va_list ap);
  105. void uv__loop_close(uv_loop_t* loop);
  106. int uv__tcp_bind(uv_tcp_t* tcp,
  107. const struct sockaddr* addr,
  108. unsigned int addrlen,
  109. unsigned int flags);
  110. int uv__tcp_connect(uv_connect_t* req,
  111. uv_tcp_t* handle,
  112. const struct sockaddr* addr,
  113. unsigned int addrlen,
  114. uv_connect_cb cb);
  115. int uv__udp_bind(uv_udp_t* handle,
  116. const struct sockaddr* addr,
  117. unsigned int addrlen,
  118. unsigned int flags);
  119. int uv__udp_send(uv_udp_send_t* req,
  120. uv_udp_t* handle,
  121. const uv_buf_t bufs[],
  122. unsigned int nbufs,
  123. const struct sockaddr* addr,
  124. unsigned int addrlen,
  125. uv_udp_send_cb send_cb);
  126. int uv__udp_try_send(uv_udp_t* handle,
  127. const uv_buf_t bufs[],
  128. unsigned int nbufs,
  129. const struct sockaddr* addr,
  130. unsigned int addrlen);
  131. int uv__udp_recv_start(uv_udp_t* handle, uv_alloc_cb alloccb,
  132. uv_udp_recv_cb recv_cb);
  133. int uv__udp_recv_stop(uv_udp_t* handle);
  134. void uv__fs_poll_close(uv_fs_poll_t* handle);
  135. int uv__getaddrinfo_translate_error(int sys_err); /* EAI_* error. */
  136. enum uv__work_kind {
  137. UV__WORK_CPU,
  138. UV__WORK_FAST_IO,
  139. UV__WORK_SLOW_IO
  140. };
  141. void uv__work_submit(uv_loop_t* loop,
  142. struct uv__work *w,
  143. enum uv__work_kind kind,
  144. void (*work)(struct uv__work *w),
  145. void (*done)(struct uv__work *w, int status));
  146. void uv__work_done(uv_async_t* handle);
  147. size_t uv__count_bufs(const uv_buf_t bufs[], unsigned int nbufs);
  148. int uv__socket_sockopt(uv_handle_t* handle, int optname, int* value);
  149. void uv__fs_scandir_cleanup(uv_fs_t* req);
  150. int uv__next_timeout(const uv_loop_t* loop);
  151. void uv__run_timers(uv_loop_t* loop);
  152. void uv__timer_close(uv_timer_t* handle);
  153. #define uv__has_active_reqs(loop) \
  154. ((loop)->active_reqs.count > 0)
  155. #define uv__req_register(loop, req) \
  156. do { \
  157. (loop)->active_reqs.count++; \
  158. } \
  159. while (0)
  160. #define uv__req_unregister(loop, req) \
  161. do { \
  162. assert(uv__has_active_reqs(loop)); \
  163. (loop)->active_reqs.count--; \
  164. } \
  165. while (0)
  166. #define uv__has_active_handles(loop) \
  167. ((loop)->active_handles > 0)
  168. #define uv__active_handle_add(h) \
  169. do { \
  170. (h)->loop->active_handles++; \
  171. } \
  172. while (0)
  173. #define uv__active_handle_rm(h) \
  174. do { \
  175. (h)->loop->active_handles--; \
  176. } \
  177. while (0)
  178. #define uv__is_active(h) \
  179. (((h)->flags & UV_HANDLE_ACTIVE) != 0)
  180. #define uv__is_closing(h) \
  181. (((h)->flags & (UV_HANDLE_CLOSING | UV_HANDLE_CLOSED)) != 0)
  182. #define uv__handle_start(h) \
  183. do { \
  184. if (((h)->flags & UV_HANDLE_ACTIVE) != 0) break; \
  185. (h)->flags |= UV_HANDLE_ACTIVE; \
  186. if (((h)->flags & UV_HANDLE_REF) != 0) uv__active_handle_add(h); \
  187. } \
  188. while (0)
  189. #define uv__handle_stop(h) \
  190. do { \
  191. if (((h)->flags & UV_HANDLE_ACTIVE) == 0) break; \
  192. (h)->flags &= ~UV_HANDLE_ACTIVE; \
  193. if (((h)->flags & UV_HANDLE_REF) != 0) uv__active_handle_rm(h); \
  194. } \
  195. while (0)
  196. #define uv__handle_ref(h) \
  197. do { \
  198. if (((h)->flags & UV_HANDLE_REF) != 0) break; \
  199. (h)->flags |= UV_HANDLE_REF; \
  200. if (((h)->flags & UV_HANDLE_CLOSING) != 0) break; \
  201. if (((h)->flags & UV_HANDLE_ACTIVE) != 0) uv__active_handle_add(h); \
  202. } \
  203. while (0)
  204. #define uv__handle_unref(h) \
  205. do { \
  206. if (((h)->flags & UV_HANDLE_REF) == 0) break; \
  207. (h)->flags &= ~UV_HANDLE_REF; \
  208. if (((h)->flags & UV_HANDLE_CLOSING) != 0) break; \
  209. if (((h)->flags & UV_HANDLE_ACTIVE) != 0) uv__active_handle_rm(h); \
  210. } \
  211. while (0)
  212. #define uv__has_ref(h) \
  213. (((h)->flags & UV_HANDLE_REF) != 0)
  214. #if defined(_WIN32)
  215. # define uv__handle_platform_init(h) ((h)->u.fd = -1)
  216. #else
  217. # define uv__handle_platform_init(h) ((h)->next_closing = NULL)
  218. #endif
  219. #define uv__handle_init(loop_, h, type_) \
  220. do { \
  221. (h)->loop = (loop_); \
  222. (h)->type = (type_); \
  223. (h)->flags = UV_HANDLE_REF; /* Ref the loop when active. */ \
  224. QUEUE_INSERT_TAIL(&(loop_)->handle_queue, &(h)->handle_queue); \
  225. uv__handle_platform_init(h); \
  226. } \
  227. while (0)
  228. /* Note: uses an open-coded version of SET_REQ_SUCCESS() because of
  229. * a circular dependency between src/uv-common.h and src/win/internal.h.
  230. */
  231. #if defined(_WIN32)
  232. # define UV_REQ_INIT(req, typ) \
  233. do { \
  234. (req)->type = (typ); \
  235. (req)->u.io.overlapped.Internal = 0; /* SET_REQ_SUCCESS() */ \
  236. } \
  237. while (0)
  238. #else
  239. # define UV_REQ_INIT(req, typ) \
  240. do { \
  241. (req)->type = (typ); \
  242. } \
  243. while (0)
  244. #endif
  245. #define uv__req_init(loop, req, typ) \
  246. do { \
  247. UV_REQ_INIT(req, typ); \
  248. uv__req_register(loop, req); \
  249. } \
  250. while (0)
  251. /* Allocator prototypes */
  252. void *uv__calloc(size_t count, size_t size);
  253. char *uv__strdup(const char* s);
  254. char *uv__strndup(const char* s, size_t n);
  255. void* uv__malloc(size_t size);
  256. void uv__free(void* ptr);
  257. void* uv__realloc(void* ptr, size_t size);
  258. #endif /* UV_COMMON_H_ */