uv-common.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. #include <stdint.h>
  31. #include <string.h>
  32. #include "uv.h"
  33. #include "uv/tree.h"
  34. #include "queue.h"
  35. #include "strscpy.h"
  36. #ifndef _MSC_VER
  37. # include <stdatomic.h>
  38. #endif
  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 ARRAY_END(a) ((a) + ARRAY_SIZE(a))
  49. #define container_of(ptr, type, member) \
  50. ((type *) ((char *) (ptr) - offsetof(type, member)))
  51. /* C11 defines static_assert to be a macro which calls _Static_assert. */
  52. #if defined(static_assert)
  53. #define STATIC_ASSERT(expr) static_assert(expr, #expr)
  54. #else
  55. #define STATIC_ASSERT(expr) \
  56. void uv__static_assert(int static_assert_failed[1 - 2 * !(expr)])
  57. #endif
  58. #ifdef _MSC_VER
  59. #define uv__exchange_int_relaxed(p, v) \
  60. InterlockedExchangeNoFence((LONG volatile*)(p), v)
  61. #else
  62. #define uv__exchange_int_relaxed(p, v) \
  63. atomic_exchange_explicit((_Atomic int*)(p), v, memory_order_relaxed)
  64. #endif
  65. #define UV__UDP_DGRAM_MAXSIZE (64 * 1024)
  66. /* Handle flags. Some flags are specific to Windows or UNIX. */
  67. enum {
  68. /* Used by all handles. */
  69. UV_HANDLE_CLOSING = 0x00000001,
  70. UV_HANDLE_CLOSED = 0x00000002,
  71. UV_HANDLE_ACTIVE = 0x00000004,
  72. UV_HANDLE_REF = 0x00000008,
  73. UV_HANDLE_INTERNAL = 0x00000010,
  74. UV_HANDLE_ENDGAME_QUEUED = 0x00000020,
  75. /* Used by streams. */
  76. UV_HANDLE_LISTENING = 0x00000040,
  77. UV_HANDLE_CONNECTION = 0x00000080,
  78. UV_HANDLE_SHUT = 0x00000200,
  79. UV_HANDLE_READ_PARTIAL = 0x00000400,
  80. UV_HANDLE_READ_EOF = 0x00000800,
  81. /* Used by streams and UDP handles. */
  82. UV_HANDLE_READING = 0x00001000,
  83. UV_HANDLE_BOUND = 0x00002000,
  84. UV_HANDLE_READABLE = 0x00004000,
  85. UV_HANDLE_WRITABLE = 0x00008000,
  86. UV_HANDLE_READ_PENDING = 0x00010000,
  87. UV_HANDLE_SYNC_BYPASS_IOCP = 0x00020000,
  88. UV_HANDLE_ZERO_READ = 0x00040000,
  89. UV_HANDLE_EMULATE_IOCP = 0x00080000,
  90. UV_HANDLE_BLOCKING_WRITES = 0x00100000,
  91. UV_HANDLE_CANCELLATION_PENDING = 0x00200000,
  92. /* Used by uv_tcp_t and uv_udp_t handles */
  93. UV_HANDLE_IPV6 = 0x00400000,
  94. /* Only used by uv_tcp_t handles. */
  95. UV_HANDLE_TCP_NODELAY = 0x01000000,
  96. UV_HANDLE_TCP_KEEPALIVE = 0x02000000,
  97. UV_HANDLE_TCP_SINGLE_ACCEPT = 0x04000000,
  98. UV_HANDLE_TCP_ACCEPT_STATE_CHANGING = 0x08000000,
  99. UV_HANDLE_SHARED_TCP_SOCKET = 0x10000000,
  100. /* Only used by uv_udp_t handles. */
  101. UV_HANDLE_UDP_PROCESSING = 0x01000000,
  102. UV_HANDLE_UDP_CONNECTED = 0x02000000,
  103. UV_HANDLE_UDP_RECVMMSG = 0x04000000,
  104. /* Only used by uv_pipe_t handles. */
  105. UV_HANDLE_NON_OVERLAPPED_PIPE = 0x01000000,
  106. UV_HANDLE_PIPESERVER = 0x02000000,
  107. /* Only used by uv_tty_t handles. */
  108. UV_HANDLE_TTY_READABLE = 0x01000000,
  109. UV_HANDLE_UNUSED0 = 0x02000000,
  110. UV_HANDLE_TTY_SAVED_POSITION = 0x04000000,
  111. UV_HANDLE_TTY_SAVED_ATTRIBUTES = 0x08000000,
  112. /* Only used by uv_signal_t handles. */
  113. UV_SIGNAL_ONE_SHOT_DISPATCHED = 0x01000000,
  114. UV_SIGNAL_ONE_SHOT = 0x02000000,
  115. /* Only used by uv_poll_t handles. */
  116. UV_HANDLE_POLL_SLOW = 0x01000000,
  117. /* Only used by uv_process_t handles. */
  118. UV_HANDLE_REAP = 0x10000000
  119. };
  120. static inline int uv__is_raw_tty_mode(uv_tty_mode_t m) {
  121. return m == UV_TTY_MODE_RAW || m == UV_TTY_MODE_RAW_VT;
  122. }
  123. int uv__loop_configure(uv_loop_t* loop, uv_loop_option option, va_list ap);
  124. void uv__loop_close(uv_loop_t* loop);
  125. int uv__read_start(uv_stream_t* stream,
  126. uv_alloc_cb alloc_cb,
  127. uv_read_cb read_cb);
  128. int uv__tcp_bind(uv_tcp_t* tcp,
  129. const struct sockaddr* addr,
  130. unsigned int addrlen,
  131. unsigned int flags);
  132. int uv__tcp_connect(uv_connect_t* req,
  133. uv_tcp_t* handle,
  134. const struct sockaddr* addr,
  135. unsigned int addrlen,
  136. uv_connect_cb cb);
  137. int uv__udp_init_ex(uv_loop_t* loop,
  138. uv_udp_t* handle,
  139. unsigned flags,
  140. int domain);
  141. int uv__udp_bind(uv_udp_t* handle,
  142. const struct sockaddr* addr,
  143. unsigned int addrlen,
  144. unsigned int flags);
  145. int uv__udp_connect(uv_udp_t* handle,
  146. const struct sockaddr* addr,
  147. unsigned int addrlen);
  148. int uv__udp_disconnect(uv_udp_t* handle);
  149. int uv__udp_is_connected(uv_udp_t* handle);
  150. int uv__udp_send(uv_udp_send_t* req,
  151. uv_udp_t* handle,
  152. const uv_buf_t bufs[],
  153. unsigned int nbufs,
  154. const struct sockaddr* addr,
  155. unsigned int addrlen,
  156. uv_udp_send_cb send_cb);
  157. int uv__udp_try_send(uv_udp_t* handle,
  158. const uv_buf_t bufs[],
  159. unsigned int nbufs,
  160. const struct sockaddr* addr,
  161. unsigned int addrlen);
  162. int uv__udp_try_send2(uv_udp_t* handle,
  163. unsigned int count,
  164. uv_buf_t* bufs[/*count*/],
  165. unsigned int nbufs[/*count*/],
  166. struct sockaddr* addrs[/*count*/]);
  167. int uv__udp_recv_start(uv_udp_t* handle, uv_alloc_cb alloccb,
  168. uv_udp_recv_cb recv_cb);
  169. int uv__udp_recv_stop(uv_udp_t* handle);
  170. void uv__fs_poll_close(uv_fs_poll_t* handle);
  171. int uv__getaddrinfo_translate_error(int sys_err); /* EAI_* error. */
  172. enum uv__work_kind {
  173. UV__WORK_CPU,
  174. UV__WORK_FAST_IO,
  175. UV__WORK_SLOW_IO
  176. };
  177. void uv__work_submit(uv_loop_t* loop,
  178. struct uv__work *w,
  179. enum uv__work_kind kind,
  180. void (*work)(struct uv__work *w),
  181. void (*done)(struct uv__work *w, int status));
  182. void uv__work_done(uv_async_t* handle);
  183. size_t uv__count_bufs(const uv_buf_t bufs[], unsigned int nbufs);
  184. int uv__socket_sockopt(uv_handle_t* handle, int optname, int* value);
  185. void uv__fs_scandir_cleanup(uv_fs_t* req);
  186. void uv__fs_readdir_cleanup(uv_fs_t* req);
  187. uv_dirent_type_t uv__fs_get_dirent_type(uv__dirent_t* dent);
  188. int uv__next_timeout(const uv_loop_t* loop);
  189. void uv__run_timers(uv_loop_t* loop);
  190. void uv__timer_close(uv_timer_t* handle);
  191. void uv__process_title_cleanup(void);
  192. void uv__signal_cleanup(void);
  193. void uv__threadpool_cleanup(void);
  194. #define uv__has_active_reqs(loop) \
  195. ((loop)->active_reqs.count > 0)
  196. #define uv__req_register(loop) \
  197. do { \
  198. (loop)->active_reqs.count++; \
  199. } \
  200. while (0)
  201. #define uv__req_unregister(loop) \
  202. do { \
  203. assert(uv__has_active_reqs(loop)); \
  204. (loop)->active_reqs.count--; \
  205. } \
  206. while (0)
  207. #define uv__has_active_handles(loop) \
  208. ((loop)->active_handles > 0)
  209. #define uv__active_handle_add(h) \
  210. do { \
  211. (h)->loop->active_handles++; \
  212. } \
  213. while (0)
  214. #define uv__active_handle_rm(h) \
  215. do { \
  216. (h)->loop->active_handles--; \
  217. } \
  218. while (0)
  219. #define uv__is_active(h) \
  220. (((h)->flags & UV_HANDLE_ACTIVE) != 0)
  221. #define uv__is_closing(h) \
  222. (((h)->flags & (UV_HANDLE_CLOSING | UV_HANDLE_CLOSED)) != 0)
  223. #if defined(_WIN32)
  224. # define uv__is_stream_shutting(h) \
  225. (h->stream.conn.shutdown_req != NULL)
  226. #else
  227. # define uv__is_stream_shutting(h) \
  228. (h->shutdown_req != NULL)
  229. #endif
  230. #define uv__handle_start(h) \
  231. do { \
  232. if (((h)->flags & UV_HANDLE_ACTIVE) != 0) break; \
  233. (h)->flags |= UV_HANDLE_ACTIVE; \
  234. if (((h)->flags & UV_HANDLE_REF) != 0) uv__active_handle_add(h); \
  235. } \
  236. while (0)
  237. #define uv__handle_stop(h) \
  238. do { \
  239. if (((h)->flags & UV_HANDLE_ACTIVE) == 0) break; \
  240. (h)->flags &= ~UV_HANDLE_ACTIVE; \
  241. if (((h)->flags & UV_HANDLE_REF) != 0) uv__active_handle_rm(h); \
  242. } \
  243. while (0)
  244. #define uv__handle_ref(h) \
  245. do { \
  246. if (((h)->flags & UV_HANDLE_REF) != 0) break; \
  247. (h)->flags |= UV_HANDLE_REF; \
  248. if (((h)->flags & UV_HANDLE_CLOSING) != 0) break; \
  249. if (((h)->flags & UV_HANDLE_ACTIVE) != 0) uv__active_handle_add(h); \
  250. } \
  251. while (0)
  252. #define uv__handle_unref(h) \
  253. do { \
  254. if (((h)->flags & UV_HANDLE_REF) == 0) break; \
  255. (h)->flags &= ~UV_HANDLE_REF; \
  256. if (((h)->flags & UV_HANDLE_CLOSING) != 0) break; \
  257. if (((h)->flags & UV_HANDLE_ACTIVE) != 0) uv__active_handle_rm(h); \
  258. } \
  259. while (0)
  260. #define uv__has_ref(h) \
  261. (((h)->flags & UV_HANDLE_REF) != 0)
  262. #if defined(_WIN32)
  263. # define uv__handle_platform_init(h) ((h)->u.fd = -1)
  264. #else
  265. # define uv__handle_platform_init(h) ((h)->next_closing = NULL)
  266. #endif
  267. #define uv__handle_init(loop_, h, type_) \
  268. do { \
  269. (h)->loop = (loop_); \
  270. (h)->type = (type_); \
  271. (h)->flags = UV_HANDLE_REF; /* Ref the loop when active. */ \
  272. uv__queue_insert_tail(&(loop_)->handle_queue, &(h)->handle_queue); \
  273. uv__handle_platform_init(h); \
  274. } \
  275. while (0)
  276. /* Note: uses an open-coded version of SET_REQ_SUCCESS() because of
  277. * a circular dependency between src/uv-common.h and src/win/internal.h.
  278. */
  279. #if defined(_WIN32)
  280. # define UV_REQ_INIT(req, typ) \
  281. do { \
  282. (req)->type = (typ); \
  283. (req)->u.io.overlapped.Internal = 0; /* SET_REQ_SUCCESS() */ \
  284. } \
  285. while (0)
  286. #else
  287. # define UV_REQ_INIT(req, typ) \
  288. do { \
  289. (req)->type = (typ); \
  290. } \
  291. while (0)
  292. #endif
  293. #define uv__req_init(loop, req, typ) \
  294. do { \
  295. UV_REQ_INIT(req, typ); \
  296. uv__req_register(loop); \
  297. } \
  298. while (0)
  299. #define uv__get_internal_fields(loop) \
  300. ((uv__loop_internal_fields_t*) loop->internal_fields)
  301. #define uv__get_loop_metrics(loop) \
  302. (&uv__get_internal_fields(loop)->loop_metrics)
  303. #define uv__metrics_inc_loop_count(loop) \
  304. do { \
  305. uv__get_loop_metrics(loop)->metrics.loop_count++; \
  306. } while (0)
  307. #define uv__metrics_inc_events(loop, e) \
  308. do { \
  309. uv__get_loop_metrics(loop)->metrics.events += (e); \
  310. } while (0)
  311. #define uv__metrics_inc_events_waiting(loop, e) \
  312. do { \
  313. uv__get_loop_metrics(loop)->metrics.events_waiting += (e); \
  314. } while (0)
  315. /* Allocator prototypes */
  316. void *uv__calloc(size_t count, size_t size);
  317. char *uv__strdup(const char* s);
  318. char *uv__strndup(const char* s, size_t n);
  319. void* uv__malloc(size_t size);
  320. void uv__free(void* ptr);
  321. void* uv__realloc(void* ptr, size_t size);
  322. void* uv__reallocf(void* ptr, size_t size);
  323. typedef struct uv__loop_metrics_s uv__loop_metrics_t;
  324. typedef struct uv__loop_internal_fields_s uv__loop_internal_fields_t;
  325. struct uv__loop_metrics_s {
  326. uv_metrics_t metrics;
  327. uint64_t provider_entry_time;
  328. uint64_t provider_idle_time;
  329. uv_mutex_t lock;
  330. };
  331. void uv__metrics_update_idle_time(uv_loop_t* loop);
  332. void uv__metrics_set_provider_entry_time(uv_loop_t* loop);
  333. #ifdef __linux__
  334. struct uv__iou {
  335. uint32_t* sqhead;
  336. uint32_t* sqtail;
  337. uint32_t sqmask;
  338. uint32_t* sqflags;
  339. uint32_t* cqhead;
  340. uint32_t* cqtail;
  341. uint32_t cqmask;
  342. void* sq; /* pointer to munmap() on event loop teardown */
  343. void* cqe; /* pointer to array of struct uv__io_uring_cqe */
  344. void* sqe; /* pointer to array of struct uv__io_uring_sqe */
  345. size_t sqlen;
  346. size_t cqlen;
  347. size_t maxlen;
  348. size_t sqelen;
  349. int ringfd;
  350. uint32_t in_flight;
  351. };
  352. #endif /* __linux__ */
  353. struct uv__loop_internal_fields_s {
  354. unsigned int flags;
  355. uv__loop_metrics_t loop_metrics;
  356. int current_timeout;
  357. #ifdef __linux__
  358. struct uv__iou ctl;
  359. struct uv__iou iou;
  360. void* inv; /* used by uv__platform_invalidate_fd() */
  361. #endif /* __linux__ */
  362. };
  363. #if defined(_WIN32)
  364. # define UV_PTHREAD_MAX_NAMELEN_NP 32767
  365. #elif defined(__APPLE__)
  366. # define UV_PTHREAD_MAX_NAMELEN_NP 64
  367. #elif defined(__NetBSD__) || defined(__illumos__)
  368. # define UV_PTHREAD_MAX_NAMELEN_NP PTHREAD_MAX_NAMELEN_NP
  369. #elif defined (__linux__)
  370. # define UV_PTHREAD_MAX_NAMELEN_NP 16
  371. #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
  372. # define UV_PTHREAD_MAX_NAMELEN_NP (MAXCOMLEN + 1)
  373. #else
  374. # define UV_PTHREAD_MAX_NAMELEN_NP 16
  375. #endif
  376. /* Open-coded so downstream users don't have to link libm. */
  377. static inline int uv__isinf(double d) {
  378. uint64_t v;
  379. STATIC_ASSERT(sizeof(v) == sizeof(d));
  380. memcpy(&v, &d, sizeof(v));
  381. return (v << 1 >> 53) == 2047 && !(v << 12);
  382. }
  383. /* Open-coded so downstream users don't have to link libm. */
  384. static inline int uv__isnan(double d) {
  385. uint64_t v;
  386. STATIC_ASSERT(sizeof(v) == sizeof(d));
  387. memcpy(&v, &d, sizeof(v));
  388. return (v << 1 >> 53) == 2047 && !!(v << 12);
  389. }
  390. #endif /* UV_COMMON_H_ */