uv-common.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. #if defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 7)
  53. #define uv__load_relaxed(p) __atomic_load_n(p, __ATOMIC_RELAXED)
  54. #define uv__store_relaxed(p, v) __atomic_store_n(p, v, __ATOMIC_RELAXED)
  55. #else
  56. #define uv__load_relaxed(p) (*p)
  57. #define uv__store_relaxed(p, v) do *p = v; while (0)
  58. #endif
  59. /* Handle flags. Some flags are specific to Windows or UNIX. */
  60. enum {
  61. /* Used by all handles. */
  62. UV_HANDLE_CLOSING = 0x00000001,
  63. UV_HANDLE_CLOSED = 0x00000002,
  64. UV_HANDLE_ACTIVE = 0x00000004,
  65. UV_HANDLE_REF = 0x00000008,
  66. UV_HANDLE_INTERNAL = 0x00000010,
  67. UV_HANDLE_ENDGAME_QUEUED = 0x00000020,
  68. /* Used by streams. */
  69. UV_HANDLE_LISTENING = 0x00000040,
  70. UV_HANDLE_CONNECTION = 0x00000080,
  71. UV_HANDLE_SHUTTING = 0x00000100,
  72. UV_HANDLE_SHUT = 0x00000200,
  73. UV_HANDLE_READ_PARTIAL = 0x00000400,
  74. UV_HANDLE_READ_EOF = 0x00000800,
  75. /* Used by streams and UDP handles. */
  76. UV_HANDLE_READING = 0x00001000,
  77. UV_HANDLE_BOUND = 0x00002000,
  78. UV_HANDLE_READABLE = 0x00004000,
  79. UV_HANDLE_WRITABLE = 0x00008000,
  80. UV_HANDLE_READ_PENDING = 0x00010000,
  81. UV_HANDLE_SYNC_BYPASS_IOCP = 0x00020000,
  82. UV_HANDLE_ZERO_READ = 0x00040000,
  83. UV_HANDLE_EMULATE_IOCP = 0x00080000,
  84. UV_HANDLE_BLOCKING_WRITES = 0x00100000,
  85. UV_HANDLE_CANCELLATION_PENDING = 0x00200000,
  86. /* Used by uv_tcp_t and uv_udp_t handles */
  87. UV_HANDLE_IPV6 = 0x00400000,
  88. /* Only used by uv_tcp_t handles. */
  89. UV_HANDLE_TCP_NODELAY = 0x01000000,
  90. UV_HANDLE_TCP_KEEPALIVE = 0x02000000,
  91. UV_HANDLE_TCP_SINGLE_ACCEPT = 0x04000000,
  92. UV_HANDLE_TCP_ACCEPT_STATE_CHANGING = 0x08000000,
  93. UV_HANDLE_TCP_SOCKET_CLOSED = 0x10000000,
  94. UV_HANDLE_SHARED_TCP_SOCKET = 0x20000000,
  95. /* Only used by uv_udp_t handles. */
  96. UV_HANDLE_UDP_PROCESSING = 0x01000000,
  97. UV_HANDLE_UDP_CONNECTED = 0x02000000,
  98. UV_HANDLE_UDP_RECVMMSG = 0x04000000,
  99. /* Only used by uv_pipe_t handles. */
  100. UV_HANDLE_NON_OVERLAPPED_PIPE = 0x01000000,
  101. UV_HANDLE_PIPESERVER = 0x02000000,
  102. /* Only used by uv_tty_t handles. */
  103. UV_HANDLE_TTY_READABLE = 0x01000000,
  104. UV_HANDLE_TTY_RAW = 0x02000000,
  105. UV_HANDLE_TTY_SAVED_POSITION = 0x04000000,
  106. UV_HANDLE_TTY_SAVED_ATTRIBUTES = 0x08000000,
  107. /* Only used by uv_signal_t handles. */
  108. UV_SIGNAL_ONE_SHOT_DISPATCHED = 0x01000000,
  109. UV_SIGNAL_ONE_SHOT = 0x02000000,
  110. /* Only used by uv_poll_t handles. */
  111. UV_HANDLE_POLL_SLOW = 0x01000000
  112. };
  113. int uv__loop_configure(uv_loop_t* loop, uv_loop_option option, va_list ap);
  114. void uv__loop_close(uv_loop_t* loop);
  115. int uv__tcp_bind(uv_tcp_t* tcp,
  116. const struct sockaddr* addr,
  117. unsigned int addrlen,
  118. unsigned int flags);
  119. int uv__tcp_connect(uv_connect_t* req,
  120. uv_tcp_t* handle,
  121. const struct sockaddr* addr,
  122. unsigned int addrlen,
  123. uv_connect_cb cb);
  124. int uv__udp_init_ex(uv_loop_t* loop,
  125. uv_udp_t* handle,
  126. unsigned flags,
  127. int domain);
  128. int uv__udp_bind(uv_udp_t* handle,
  129. const struct sockaddr* addr,
  130. unsigned int addrlen,
  131. unsigned int flags);
  132. int uv__udp_connect(uv_udp_t* handle,
  133. const struct sockaddr* addr,
  134. unsigned int addrlen);
  135. int uv__udp_disconnect(uv_udp_t* handle);
  136. int uv__udp_is_connected(uv_udp_t* handle);
  137. int uv__udp_send(uv_udp_send_t* req,
  138. uv_udp_t* handle,
  139. const uv_buf_t bufs[],
  140. unsigned int nbufs,
  141. const struct sockaddr* addr,
  142. unsigned int addrlen,
  143. uv_udp_send_cb send_cb);
  144. int uv__udp_try_send(uv_udp_t* handle,
  145. const uv_buf_t bufs[],
  146. unsigned int nbufs,
  147. const struct sockaddr* addr,
  148. unsigned int addrlen);
  149. int uv__udp_recv_start(uv_udp_t* handle, uv_alloc_cb alloccb,
  150. uv_udp_recv_cb recv_cb);
  151. int uv__udp_recv_stop(uv_udp_t* handle);
  152. void uv__fs_poll_close(uv_fs_poll_t* handle);
  153. int uv__getaddrinfo_translate_error(int sys_err); /* EAI_* error. */
  154. enum uv__work_kind {
  155. UV__WORK_CPU,
  156. UV__WORK_FAST_IO,
  157. UV__WORK_SLOW_IO
  158. };
  159. void uv__work_submit(uv_loop_t* loop,
  160. struct uv__work *w,
  161. enum uv__work_kind kind,
  162. void (*work)(struct uv__work *w),
  163. void (*done)(struct uv__work *w, int status));
  164. void uv__work_done(uv_async_t* handle);
  165. size_t uv__count_bufs(const uv_buf_t bufs[], unsigned int nbufs);
  166. int uv__socket_sockopt(uv_handle_t* handle, int optname, int* value);
  167. void uv__fs_scandir_cleanup(uv_fs_t* req);
  168. void uv__fs_readdir_cleanup(uv_fs_t* req);
  169. uv_dirent_type_t uv__fs_get_dirent_type(uv__dirent_t* dent);
  170. int uv__next_timeout(const uv_loop_t* loop);
  171. void uv__run_timers(uv_loop_t* loop);
  172. void uv__timer_close(uv_timer_t* handle);
  173. void uv__process_title_cleanup(void);
  174. void uv__signal_cleanup(void);
  175. void uv__threadpool_cleanup(void);
  176. #define uv__has_active_reqs(loop) \
  177. ((loop)->active_reqs.count > 0)
  178. #define uv__req_register(loop, req) \
  179. do { \
  180. (loop)->active_reqs.count++; \
  181. } \
  182. while (0)
  183. #define uv__req_unregister(loop, req) \
  184. do { \
  185. assert(uv__has_active_reqs(loop)); \
  186. (loop)->active_reqs.count--; \
  187. } \
  188. while (0)
  189. #define uv__has_active_handles(loop) \
  190. ((loop)->active_handles > 0)
  191. #define uv__active_handle_add(h) \
  192. do { \
  193. (h)->loop->active_handles++; \
  194. } \
  195. while (0)
  196. #define uv__active_handle_rm(h) \
  197. do { \
  198. (h)->loop->active_handles--; \
  199. } \
  200. while (0)
  201. #define uv__is_active(h) \
  202. (((h)->flags & UV_HANDLE_ACTIVE) != 0)
  203. #define uv__is_closing(h) \
  204. (((h)->flags & (UV_HANDLE_CLOSING | UV_HANDLE_CLOSED)) != 0)
  205. #define uv__handle_start(h) \
  206. do { \
  207. if (((h)->flags & UV_HANDLE_ACTIVE) != 0) break; \
  208. (h)->flags |= UV_HANDLE_ACTIVE; \
  209. if (((h)->flags & UV_HANDLE_REF) != 0) uv__active_handle_add(h); \
  210. } \
  211. while (0)
  212. #define uv__handle_stop(h) \
  213. do { \
  214. if (((h)->flags & UV_HANDLE_ACTIVE) == 0) break; \
  215. (h)->flags &= ~UV_HANDLE_ACTIVE; \
  216. if (((h)->flags & UV_HANDLE_REF) != 0) uv__active_handle_rm(h); \
  217. } \
  218. while (0)
  219. #define uv__handle_ref(h) \
  220. do { \
  221. if (((h)->flags & UV_HANDLE_REF) != 0) break; \
  222. (h)->flags |= UV_HANDLE_REF; \
  223. if (((h)->flags & UV_HANDLE_CLOSING) != 0) break; \
  224. if (((h)->flags & UV_HANDLE_ACTIVE) != 0) uv__active_handle_add(h); \
  225. } \
  226. while (0)
  227. #define uv__handle_unref(h) \
  228. do { \
  229. if (((h)->flags & UV_HANDLE_REF) == 0) break; \
  230. (h)->flags &= ~UV_HANDLE_REF; \
  231. if (((h)->flags & UV_HANDLE_CLOSING) != 0) break; \
  232. if (((h)->flags & UV_HANDLE_ACTIVE) != 0) uv__active_handle_rm(h); \
  233. } \
  234. while (0)
  235. #define uv__has_ref(h) \
  236. (((h)->flags & UV_HANDLE_REF) != 0)
  237. #if defined(_WIN32)
  238. # define uv__handle_platform_init(h) ((h)->u.fd = -1)
  239. #else
  240. # define uv__handle_platform_init(h) ((h)->next_closing = NULL)
  241. #endif
  242. #define uv__handle_init(loop_, h, type_) \
  243. do { \
  244. (h)->loop = (loop_); \
  245. (h)->type = (type_); \
  246. (h)->flags = UV_HANDLE_REF; /* Ref the loop when active. */ \
  247. QUEUE_INSERT_TAIL(&(loop_)->handle_queue, &(h)->handle_queue); \
  248. uv__handle_platform_init(h); \
  249. } \
  250. while (0)
  251. /* Note: uses an open-coded version of SET_REQ_SUCCESS() because of
  252. * a circular dependency between src/uv-common.h and src/win/internal.h.
  253. */
  254. #if defined(_WIN32)
  255. # define UV_REQ_INIT(req, typ) \
  256. do { \
  257. (req)->type = (typ); \
  258. (req)->u.io.overlapped.Internal = 0; /* SET_REQ_SUCCESS() */ \
  259. } \
  260. while (0)
  261. #else
  262. # define UV_REQ_INIT(req, typ) \
  263. do { \
  264. (req)->type = (typ); \
  265. } \
  266. while (0)
  267. #endif
  268. #define uv__req_init(loop, req, typ) \
  269. do { \
  270. UV_REQ_INIT(req, typ); \
  271. uv__req_register(loop, req); \
  272. } \
  273. while (0)
  274. #define uv__get_internal_fields(loop) \
  275. ((uv__loop_internal_fields_t*) loop->internal_fields)
  276. #define uv__get_loop_metrics(loop) \
  277. (&uv__get_internal_fields(loop)->loop_metrics)
  278. /* Allocator prototypes */
  279. void *uv__calloc(size_t count, size_t size);
  280. char *uv__strdup(const char* s);
  281. char *uv__strndup(const char* s, size_t n);
  282. void* uv__malloc(size_t size);
  283. void uv__free(void* ptr);
  284. void* uv__realloc(void* ptr, size_t size);
  285. void* uv__reallocf(void* ptr, size_t size);
  286. typedef struct uv__loop_metrics_s uv__loop_metrics_t;
  287. typedef struct uv__loop_internal_fields_s uv__loop_internal_fields_t;
  288. struct uv__loop_metrics_s {
  289. uint64_t provider_entry_time;
  290. uint64_t provider_idle_time;
  291. uv_mutex_t lock;
  292. };
  293. void uv__metrics_update_idle_time(uv_loop_t* loop);
  294. void uv__metrics_set_provider_entry_time(uv_loop_t* loop);
  295. struct uv__loop_internal_fields_s {
  296. unsigned int flags;
  297. uv__loop_metrics_t loop_metrics;
  298. };
  299. #endif /* UV_COMMON_H_ */