uv-unix.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. #ifndef UV_UNIX_H
  22. #define UV_UNIX_H
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <sys/socket.h>
  27. #include <netinet/in.h>
  28. #include <netinet/tcp.h>
  29. #include <arpa/inet.h>
  30. #include <netdb.h>
  31. #include <termios.h>
  32. #include <pwd.h>
  33. #include <semaphore.h>
  34. #include <pthread.h>
  35. #ifdef __ANDROID__
  36. #include "pthread-fixes.h"
  37. #endif
  38. #include <signal.h>
  39. #if defined(__linux__)
  40. # include "uv-linux.h"
  41. #elif defined(__sun)
  42. # include "uv-sunos.h"
  43. #elif defined(__APPLE__)
  44. # include "uv-darwin.h"
  45. #elif defined(__DragonFly__) || \
  46. defined(__FreeBSD__) || \
  47. defined(__OpenBSD__) || \
  48. defined(__NetBSD__)
  49. # include "uv-bsd.h"
  50. #endif
  51. #ifndef UV_IO_PRIVATE_PLATFORM_FIELDS
  52. # define UV_IO_PRIVATE_PLATFORM_FIELDS /* empty */
  53. #endif
  54. struct uv__io_s;
  55. struct uv__async;
  56. struct uv_loop_s;
  57. typedef void (*uv__io_cb)(struct uv_loop_s* loop,
  58. struct uv__io_s* w,
  59. unsigned int events);
  60. typedef struct uv__io_s uv__io_t;
  61. struct uv__io_s {
  62. uv__io_cb cb;
  63. void* pending_queue[2];
  64. void* watcher_queue[2];
  65. unsigned int pevents; /* Pending event mask i.e. mask at next tick. */
  66. unsigned int events; /* Current event mask. */
  67. int fd;
  68. UV_IO_PRIVATE_PLATFORM_FIELDS
  69. };
  70. typedef void (*uv__async_cb)(struct uv_loop_s* loop,
  71. struct uv__async* w,
  72. unsigned int nevents);
  73. struct uv__async {
  74. uv__async_cb cb;
  75. uv__io_t io_watcher;
  76. int wfd;
  77. };
  78. struct uv__work {
  79. void (*work)(struct uv__work *w);
  80. void (*done)(struct uv__work *w, int status);
  81. struct uv_loop_s* loop;
  82. void* wq[2];
  83. };
  84. #ifndef UV_PLATFORM_SEM_T
  85. # define UV_PLATFORM_SEM_T sem_t
  86. #endif
  87. #ifndef UV_PLATFORM_LOOP_FIELDS
  88. # define UV_PLATFORM_LOOP_FIELDS /* empty */
  89. #endif
  90. #ifndef UV_PLATFORM_FS_EVENT_FIELDS
  91. # define UV_PLATFORM_FS_EVENT_FIELDS /* empty */
  92. #endif
  93. #ifndef UV_STREAM_PRIVATE_PLATFORM_FIELDS
  94. # define UV_STREAM_PRIVATE_PLATFORM_FIELDS /* empty */
  95. #endif
  96. /* Note: May be cast to struct iovec. See writev(2). */
  97. typedef struct {
  98. char* base;
  99. size_t len;
  100. } uv_buf_t;
  101. typedef int uv_file;
  102. typedef int uv_os_sock_t;
  103. typedef int uv_os_file_t;
  104. #define UV_ONCE_INIT PTHREAD_ONCE_INIT
  105. typedef pthread_once_t uv_once_t;
  106. typedef pthread_t uv_thread_t;
  107. typedef pthread_mutex_t uv_mutex_t;
  108. // typedef pthread_rwlock_t uv_rwlock_t;
  109. typedef UV_PLATFORM_SEM_T uv_sem_t;
  110. typedef pthread_cond_t uv_cond_t;
  111. typedef pthread_key_t uv_key_t;
  112. // #if defined(__APPLE__) && defined(__MACH__)
  113. // typedef struct {
  114. // unsigned int n;
  115. // unsigned int count;
  116. // uv_mutex_t mutex;
  117. // uv_sem_t turnstile1;
  118. // uv_sem_t turnstile2;
  119. // } uv_barrier_t;
  120. // #else /* defined(__APPLE__) && defined(__MACH__) */
  121. // typedef pthread_barrier_t uv_barrier_t;
  122. // #endif /* defined(__APPLE__) && defined(__MACH__) */
  123. /* Platform-specific definitions for uv_spawn support. */
  124. typedef gid_t uv_gid_t;
  125. typedef uid_t uv_uid_t;
  126. /* Platform-specific definitions for uv_dlopen support. */
  127. #define UV_DYNAMIC /* empty */
  128. typedef struct {
  129. void* handle;
  130. char* errmsg;
  131. } uv_lib_t;
  132. #define UV_LOOP_PRIVATE_FIELDS \
  133. unsigned long flags; \
  134. int backend_fd; \
  135. void* pending_queue[2]; \
  136. void* watcher_queue[2]; \
  137. uv__io_t** watchers; \
  138. unsigned int nwatchers; \
  139. unsigned int nfds; \
  140. void* wq[2]; \
  141. uv_mutex_t wq_mutex; \
  142. uv_async_t wq_async; \
  143. uv_handle_t* closing_handles; \
  144. void* process_handles[1][2]; \
  145. void* prepare_handles[2]; \
  146. void* check_handles[2]; \
  147. void* idle_handles[2]; \
  148. void* async_handles[2]; \
  149. struct uv__async async_watcher; \
  150. /* RB_HEAD(uv__timers, uv_timer_s) */ \
  151. struct uv__timers { \
  152. struct uv_timer_s* rbh_root; \
  153. } timer_handles; \
  154. uint64_t time; \
  155. int signal_pipefd[2]; \
  156. uv__io_t signal_io_watcher; \
  157. uv_signal_t child_watcher; \
  158. int emfile_fd; \
  159. uint64_t timer_counter; \
  160. UV_PLATFORM_LOOP_FIELDS \
  161. #define UV_REQ_TYPE_PRIVATE /* empty */
  162. #define UV_REQ_PRIVATE_FIELDS /* empty */
  163. #define UV_PRIVATE_REQ_TYPES /* empty */
  164. #define UV_WRITE_PRIVATE_FIELDS \
  165. void* queue[2]; \
  166. unsigned int write_index; \
  167. uv_buf_t* bufs; \
  168. unsigned int nbufs; \
  169. int error; \
  170. uv_buf_t bufsml[4]; \
  171. #define UV_CONNECT_PRIVATE_FIELDS \
  172. void* queue[2]; \
  173. #define UV_SHUTDOWN_PRIVATE_FIELDS /* empty */
  174. #define UV_UDP_SEND_PRIVATE_FIELDS \
  175. void* queue[2]; \
  176. struct sockaddr_in6 addr; \
  177. unsigned int nbufs; \
  178. uv_buf_t* bufs; \
  179. ssize_t status; \
  180. uv_udp_send_cb send_cb; \
  181. uv_buf_t bufsml[4]; \
  182. #define UV_HANDLE_PRIVATE_FIELDS \
  183. int flags; \
  184. uv_handle_t* next_closing; \
  185. #define UV_STREAM_PRIVATE_FIELDS \
  186. uv_connect_t *connect_req; \
  187. uv_shutdown_t *shutdown_req; \
  188. uv__io_t io_watcher; \
  189. void* write_queue[2]; \
  190. void* write_completed_queue[2]; \
  191. uv_connection_cb connection_cb; \
  192. int delayed_error; \
  193. int accepted_fd; \
  194. UV_STREAM_PRIVATE_PLATFORM_FIELDS \
  195. #define UV_TCP_PRIVATE_FIELDS /* empty */
  196. #define UV_UDP_PRIVATE_FIELDS \
  197. uv_alloc_cb alloc_cb; \
  198. uv_udp_recv_cb recv_cb; \
  199. uv__io_t io_watcher; \
  200. void* write_queue[2]; \
  201. void* write_completed_queue[2]; \
  202. #define UV_PIPE_PRIVATE_FIELDS \
  203. const char* pipe_fname; /* strdup'ed */
  204. #define UV_IOCP_PRIVATE_FIELDS /* empty */
  205. #define UV_POLL_PRIVATE_FIELDS \
  206. uv__io_t io_watcher;
  207. #define UV_PREPARE_PRIVATE_FIELDS \
  208. uv_prepare_cb prepare_cb; \
  209. void* queue[2]; \
  210. #define UV_CHECK_PRIVATE_FIELDS \
  211. uv_check_cb check_cb; \
  212. void* queue[2]; \
  213. #define UV_IDLE_PRIVATE_FIELDS \
  214. uv_idle_cb idle_cb; \
  215. void* queue[2]; \
  216. #define UV_ASYNC_PRIVATE_FIELDS \
  217. uv_async_cb async_cb; \
  218. void* queue[2]; \
  219. int pending; \
  220. #define UV_TIMER_PRIVATE_FIELDS \
  221. /* RB_ENTRY(uv_timer_s) tree_entry; */ \
  222. struct { \
  223. struct uv_timer_s* rbe_left; \
  224. struct uv_timer_s* rbe_right; \
  225. struct uv_timer_s* rbe_parent; \
  226. int rbe_color; \
  227. } tree_entry; \
  228. uv_timer_cb timer_cb; \
  229. uint64_t timeout; \
  230. uint64_t repeat; \
  231. uint64_t start_id;
  232. #define UV_GETADDRINFO_PRIVATE_FIELDS \
  233. struct uv__work work_req; \
  234. uv_getaddrinfo_cb cb; \
  235. struct addrinfo* hints; \
  236. char* hostname; \
  237. char* service; \
  238. struct addrinfo* res; \
  239. int retcode;
  240. #define UV_PROCESS_PRIVATE_FIELDS \
  241. void* queue[2]; \
  242. int status; \
  243. #define UV_FS_PRIVATE_FIELDS \
  244. const char *new_path; \
  245. uv_file file; \
  246. int flags; \
  247. mode_t mode; \
  248. void* buf; \
  249. size_t len; \
  250. off_t off; \
  251. uv_uid_t uid; \
  252. uv_gid_t gid; \
  253. double atime; \
  254. double mtime; \
  255. struct uv__work work_req; \
  256. #define UV_WORK_PRIVATE_FIELDS \
  257. struct uv__work work_req;
  258. #define UV_TTY_PRIVATE_FIELDS \
  259. struct termios orig_termios; \
  260. int mode;
  261. #define UV_SIGNAL_PRIVATE_FIELDS \
  262. /* RB_ENTRY(uv_signal_s) tree_entry; */ \
  263. struct { \
  264. struct uv_signal_s* rbe_left; \
  265. struct uv_signal_s* rbe_right; \
  266. struct uv_signal_s* rbe_parent; \
  267. int rbe_color; \
  268. } tree_entry; \
  269. /* Use two counters here so we don have to fiddle with atomics. */ \
  270. unsigned int caught_signals; \
  271. unsigned int dispatched_signals;
  272. #define UV_FS_EVENT_PRIVATE_FIELDS \
  273. uv_fs_event_cb cb; \
  274. UV_PLATFORM_FS_EVENT_FIELDS \
  275. #endif /* UV_UNIX_H */