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 "ngx-queue.h"
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <fcntl.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <netinet/tcp.h>
  30. #include <arpa/inet.h>
  31. #include <netdb.h>
  32. #include <termios.h>
  33. #include <pwd.h>
  34. #include <semaphore.h>
  35. #include <pthread.h>
  36. #include <signal.h>
  37. #if defined(__linux__)
  38. # include "uv-linux.h"
  39. #elif defined(__sun)
  40. # include "uv-sunos.h"
  41. #elif defined(__APPLE__)
  42. # include "uv-darwin.h"
  43. #elif defined(__DragonFly__) || \
  44. defined(__FreeBSD__) || \
  45. defined(__OpenBSD__) || \
  46. defined(__NetBSD__)
  47. # include "uv-bsd.h"
  48. #endif
  49. #ifndef UV_IO_PRIVATE_PLATFORM_FIELDS
  50. # define UV_IO_PRIVATE_PLATFORM_FIELDS /* empty */
  51. #endif
  52. #define UV_IO_PRIVATE_FIELDS \
  53. UV_IO_PRIVATE_PLATFORM_FIELDS \
  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. ngx_queue_t pending_queue;
  64. ngx_queue_t watcher_queue;
  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_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. ngx_queue_t wq;
  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 struct stat uv_statbuf_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. #if defined(__APPLE__) && defined(__MACH__)
  112. typedef struct {
  113. unsigned int n;
  114. unsigned int count;
  115. uv_mutex_t mutex;
  116. uv_sem_t turnstile1;
  117. uv_sem_t turnstile2;
  118. } uv_barrier_t;
  119. #else /* defined(__APPLE__) && defined(__MACH__) */
  120. typedef pthread_barrier_t uv_barrier_t;
  121. #endif /* defined(__APPLE__) && defined(__MACH__) */
  122. /* Platform-specific definitions for uv_spawn support. */
  123. typedef gid_t uv_gid_t;
  124. typedef uid_t uv_uid_t;
  125. /* Platform-specific definitions for uv_dlopen support. */
  126. #define UV_DYNAMIC /* empty */
  127. typedef struct {
  128. void* handle;
  129. char* errmsg;
  130. } uv_lib_t;
  131. #define UV_LOOP_PRIVATE_FIELDS \
  132. unsigned long flags; \
  133. int backend_fd; \
  134. ngx_queue_t pending_queue; \
  135. ngx_queue_t watcher_queue; \
  136. uv__io_t** watchers; \
  137. unsigned int nwatchers; \
  138. unsigned int nfds; \
  139. ngx_queue_t wq; \
  140. uv_mutex_t wq_mutex; \
  141. uv_async_t wq_async; \
  142. uv_handle_t* closing_handles; \
  143. ngx_queue_t process_handles[1]; \
  144. ngx_queue_t prepare_handles; \
  145. ngx_queue_t check_handles; \
  146. ngx_queue_t idle_handles; \
  147. ngx_queue_t async_handles; \
  148. struct uv__async async_watcher; \
  149. /* RB_HEAD(uv__timers, uv_timer_s) */ \
  150. struct uv__timers { \
  151. struct uv_timer_s* rbh_root; \
  152. } timer_handles; \
  153. uint64_t time; \
  154. int signal_pipefd[2]; \
  155. uv__io_t signal_io_watcher; \
  156. uv_signal_t child_watcher; \
  157. int emfile_fd; \
  158. uint64_t timer_counter; \
  159. UV_PLATFORM_LOOP_FIELDS \
  160. #define UV_REQ_TYPE_PRIVATE /* empty */
  161. #define UV_REQ_PRIVATE_FIELDS /* empty */
  162. #define UV_PRIVATE_REQ_TYPES /* empty */
  163. #define UV_WRITE_PRIVATE_FIELDS \
  164. ngx_queue_t queue; \
  165. int write_index; \
  166. uv_buf_t* bufs; \
  167. int bufcnt; \
  168. int error; \
  169. uv_buf_t bufsml[4]; \
  170. #define UV_CONNECT_PRIVATE_FIELDS \
  171. ngx_queue_t queue; \
  172. #define UV_SHUTDOWN_PRIVATE_FIELDS /* empty */
  173. #define UV_UDP_SEND_PRIVATE_FIELDS \
  174. ngx_queue_t queue; \
  175. struct sockaddr_in6 addr; \
  176. int bufcnt; \
  177. uv_buf_t* bufs; \
  178. ssize_t status; \
  179. uv_udp_send_cb send_cb; \
  180. uv_buf_t bufsml[4]; \
  181. #define UV_HANDLE_PRIVATE_FIELDS \
  182. int flags; \
  183. uv_handle_t* next_closing; \
  184. #define UV_STREAM_PRIVATE_FIELDS \
  185. uv_connect_t *connect_req; \
  186. uv_shutdown_t *shutdown_req; \
  187. uv__io_t io_watcher; \
  188. ngx_queue_t write_queue; \
  189. ngx_queue_t write_completed_queue; \
  190. uv_connection_cb connection_cb; \
  191. int delayed_error; \
  192. int accepted_fd; \
  193. UV_STREAM_PRIVATE_PLATFORM_FIELDS \
  194. #define UV_TCP_PRIVATE_FIELDS /* empty */
  195. #define UV_UDP_PRIVATE_FIELDS \
  196. uv_alloc_cb alloc_cb; \
  197. uv_udp_recv_cb recv_cb; \
  198. uv__io_t io_watcher; \
  199. ngx_queue_t write_queue; \
  200. ngx_queue_t write_completed_queue; \
  201. #define UV_PIPE_PRIVATE_FIELDS \
  202. const char* pipe_fname; /* strdup'ed */
  203. #define UV_POLL_PRIVATE_FIELDS \
  204. uv__io_t io_watcher;
  205. #define UV_PREPARE_PRIVATE_FIELDS \
  206. uv_prepare_cb prepare_cb; \
  207. ngx_queue_t queue;
  208. #define UV_CHECK_PRIVATE_FIELDS \
  209. uv_check_cb check_cb; \
  210. ngx_queue_t queue;
  211. #define UV_IDLE_PRIVATE_FIELDS \
  212. uv_idle_cb idle_cb; \
  213. ngx_queue_t queue;
  214. #define UV_ASYNC_PRIVATE_FIELDS \
  215. uv_async_cb async_cb; \
  216. ngx_queue_t queue; \
  217. int pending; \
  218. #define UV_TIMER_PRIVATE_FIELDS \
  219. /* RB_ENTRY(uv_timer_s) tree_entry; */ \
  220. struct { \
  221. struct uv_timer_s* rbe_left; \
  222. struct uv_timer_s* rbe_right; \
  223. struct uv_timer_s* rbe_parent; \
  224. int rbe_color; \
  225. } tree_entry; \
  226. uv_timer_cb timer_cb; \
  227. uint64_t timeout; \
  228. uint64_t repeat; \
  229. uint64_t start_id;
  230. #define UV_GETADDRINFO_PRIVATE_FIELDS \
  231. struct uv__work work_req; \
  232. uv_getaddrinfo_cb cb; \
  233. struct addrinfo* hints; \
  234. char* hostname; \
  235. char* service; \
  236. struct addrinfo* res; \
  237. int retcode;
  238. #define UV_PROCESS_PRIVATE_FIELDS \
  239. ngx_queue_t queue; \
  240. int errorno; \
  241. #define UV_FS_PRIVATE_FIELDS \
  242. const char *new_path; \
  243. uv_file file; \
  244. int flags; \
  245. mode_t mode; \
  246. void* buf; \
  247. size_t len; \
  248. off_t off; \
  249. uid_t uid; \
  250. gid_t gid; \
  251. double atime; \
  252. double mtime; \
  253. struct uv__work work_req; \
  254. #define UV_WORK_PRIVATE_FIELDS \
  255. struct uv__work work_req;
  256. #define UV_TTY_PRIVATE_FIELDS \
  257. struct termios orig_termios; \
  258. int mode;
  259. #define UV_SIGNAL_PRIVATE_FIELDS \
  260. /* RB_ENTRY(uv_signal_s) tree_entry; */ \
  261. struct { \
  262. struct uv_signal_s* rbe_left; \
  263. struct uv_signal_s* rbe_right; \
  264. struct uv_signal_s* rbe_parent; \
  265. int rbe_color; \
  266. } tree_entry; \
  267. /* Use two counters here so we don have to fiddle with atomics. */ \
  268. unsigned int caught_signals; \
  269. unsigned int dispatched_signals;
  270. #define UV_FS_EVENT_PRIVATE_FIELDS \
  271. uv_fs_event_cb cb; \
  272. UV_PLATFORM_FS_EVENT_FIELDS \
  273. #endif /* UV_UNIX_H */