1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
22 #ifndef UV_UNIX_INTERNAL_H_
23 #define UV_UNIX_INTERNAL_H_
25 #include "uv-common.h"
28 #include <stdlib.h> /* abort */
30 #if defined(__STRICT_ANSI__)
31 # define inline __inline
34 #if defined(__linux__)
35 # include "linux-syscalls.h"
36 #endif /* __linux__ */
39 # include <sys/port.h>
41 # define futimes(fd, tv) futimesat(fd, (void*)0, tv)
44 #if defined(__APPLE__) && !TARGET_OS_IPHONE
45 # include <CoreServices/CoreServices.h>
48 #define STATIC_ASSERT(expr) \
49 void uv__static_assert(int static_assert_failed[1 - 2 * !(expr)])
51 #define ACCESS_ONCE(type, var) \
52 (*(volatile type*) &(var))
54 #define UNREACHABLE() \
56 assert(0 && "unreachable code"); \
61 #define SAVE_ERRNO(block) \
63 int _saved_errno = errno; \
64 do { block; } while (0); \
65 errno = _saved_errno; \
69 #if defined(__linux__)
70 # define UV__POLLIN UV__EPOLLIN
71 # define UV__POLLOUT UV__EPOLLOUT
72 # define UV__POLLERR UV__EPOLLERR
73 # define UV__POLLHUP UV__EPOLLHUP
77 # define UV__POLLIN POLLIN
78 # define UV__POLLOUT POLLOUT
79 # define UV__POLLERR POLLERR
80 # define UV__POLLHUP POLLHUP
88 # define UV__POLLOUT 2
92 # define UV__POLLERR 4
96 # define UV__POLLHUP 8
101 UV_CLOSING = 0x01, /* uv_close() called but not finished. */
102 UV_CLOSED = 0x02, /* close(2) finished. */
103 UV_STREAM_READING = 0x04, /* uv_read_start() called. */
104 UV_STREAM_SHUTTING = 0x08, /* uv_shutdown() called but not complete. */
105 UV_STREAM_SHUT = 0x10, /* Write side closed. */
106 UV_STREAM_READABLE = 0x20, /* The stream is readable */
107 UV_STREAM_WRITABLE = 0x40, /* The stream is writable */
108 UV_STREAM_BLOCKING = 0x80, /* Synchronous writes. */
109 UV_TCP_NODELAY = 0x100, /* Disable Nagle. */
110 UV_TCP_KEEPALIVE = 0x200, /* Turn on keep-alive. */
111 UV_TCP_SINGLE_ACCEPT = 0x400 /* Only accept() when idle. */
115 int uv__nonblock(int fd, int set);
116 int uv__cloexec(int fd, int set);
117 int uv__socket(int domain, int type, int protocol);
119 void uv__make_close_pending(uv_handle_t* handle);
121 void uv__io_init(uv__io_t* w, uv__io_cb cb, int fd);
122 void uv__io_start(uv_loop_t* loop, uv__io_t* w, unsigned int events);
123 void uv__io_stop(uv_loop_t* loop, uv__io_t* w, unsigned int events);
124 void uv__io_close(uv_loop_t* loop, uv__io_t* w);
125 void uv__io_feed(uv_loop_t* loop, uv__io_t* w);
126 int uv__io_active(const uv__io_t* w, unsigned int events);
127 void uv__io_poll(uv_loop_t* loop, int timeout); /* in milliseconds or -1 */
130 void uv__async_send(struct uv__async* wa);
131 void uv__async_init(struct uv__async* wa);
132 int uv__async_start(uv_loop_t* loop, struct uv__async* wa, uv__async_cb cb);
133 void uv__async_stop(uv_loop_t* loop, struct uv__async* wa);
136 int uv__loop_init(uv_loop_t* loop, int default_loop);
137 void uv__loop_delete(uv_loop_t* loop);
138 void uv__run_idle(uv_loop_t* loop);
139 void uv__run_check(uv_loop_t* loop);
140 void uv__run_prepare(uv_loop_t* loop);
143 uv_err_code uv_translate_sys_error(int sys_errno);
144 void uv_fatal_error(const int errorno, const char* syscall);
147 void uv__stream_init(uv_loop_t* loop, uv_stream_t* stream,
148 uv_handle_type type);
149 int uv__stream_open(uv_stream_t*, int fd, int flags);
150 void uv__stream_destroy(uv_stream_t* stream);
151 #if defined(__APPLE__)
152 int uv__stream_try_select(uv_stream_t* stream, int* fd);
153 #endif /* defined(__APPLE__) */
154 void uv__server_io(uv_loop_t* loop, uv__io_t* w, unsigned int events);
155 int uv__accept(int sockfd);
158 int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb);
159 int uv__tcp_nodelay(int fd, int on);
160 int uv__tcp_keepalive(int fd, int on, unsigned int delay);
163 int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb);
166 void uv__run_timers(uv_loop_t* loop);
167 int uv__next_timeout(const uv_loop_t* loop);
170 void uv__signal_close(uv_signal_t* handle);
171 void uv__signal_global_once_init(void);
172 void uv__signal_loop_cleanup(uv_loop_t* loop);
175 void uv__work_submit(uv_loop_t* loop,
177 void (*work)(struct uv__work *w),
178 void (*done)(struct uv__work *w, int status));
179 void uv__work_done(uv_async_t* handle, int status);
181 /* platform specific */
182 uint64_t uv__hrtime(void);
183 int uv__kqueue_init(uv_loop_t* loop);
184 int uv__platform_loop_init(uv_loop_t* loop, int default_loop);
185 void uv__platform_loop_delete(uv_loop_t* loop);
188 void uv__async_close(uv_async_t* handle);
189 void uv__check_close(uv_check_t* handle);
190 void uv__fs_event_close(uv_fs_event_t* handle);
191 void uv__idle_close(uv_idle_t* handle);
192 void uv__pipe_close(uv_pipe_t* handle);
193 void uv__poll_close(uv_poll_t* handle);
194 void uv__prepare_close(uv_prepare_t* handle);
195 void uv__process_close(uv_process_t* handle);
196 void uv__stream_close(uv_stream_t* handle);
197 void uv__tcp_close(uv_tcp_t* handle);
198 void uv__timer_close(uv_timer_t* handle);
199 void uv__udp_close(uv_udp_t* handle);
200 void uv__udp_finish_close(uv_udp_t* handle);
202 #if defined(__APPLE__)
203 int uv___stream_fd(uv_stream_t* handle);
204 #define uv__stream_fd(handle) (uv___stream_fd((uv_stream_t*) (handle)))
206 #define uv__stream_fd(handle) ((handle)->io_watcher.fd)
207 #endif /* defined(__APPLE__) */
209 #ifdef UV__O_NONBLOCK
210 # define UV__F_NONBLOCK UV__O_NONBLOCK
212 # define UV__F_NONBLOCK 1
215 int uv__make_socketpair(int fds[2], int flags);
216 int uv__make_pipe(int fds[2], int flags);
218 #if defined(__APPLE__)
219 typedef void (*cf_loop_signal_cb)(void*);
221 void uv__cf_loop_signal(uv_loop_t* loop, cf_loop_signal_cb cb, void* arg);
223 int uv__fsevents_init(uv_fs_event_t* handle);
224 int uv__fsevents_close(uv_fs_event_t* handle);
226 /* OSX < 10.7 has no file events, polyfill them */
227 #ifndef MAC_OS_X_VERSION_10_7
229 static const int kFSEventStreamCreateFlagFileEvents = 0x00000010;
230 static const int kFSEventStreamEventFlagItemCreated = 0x00000100;
231 static const int kFSEventStreamEventFlagItemRemoved = 0x00000200;
232 static const int kFSEventStreamEventFlagItemInodeMetaMod = 0x00000400;
233 static const int kFSEventStreamEventFlagItemRenamed = 0x00000800;
234 static const int kFSEventStreamEventFlagItemModified = 0x00001000;
235 static const int kFSEventStreamEventFlagItemFinderInfoMod = 0x00002000;
236 static const int kFSEventStreamEventFlagItemChangeOwner = 0x00004000;
237 static const int kFSEventStreamEventFlagItemXattrMod = 0x00008000;
238 static const int kFSEventStreamEventFlagItemIsFile = 0x00010000;
239 static const int kFSEventStreamEventFlagItemIsDir = 0x00020000;
240 static const int kFSEventStreamEventFlagItemIsSymlink = 0x00040000;
242 #endif /* __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070 */
244 #endif /* defined(__APPLE__) */
246 __attribute__((unused))
247 static void uv__req_init(uv_loop_t* loop, uv_req_t* req, uv_req_type type) {
249 uv__req_register(loop, req);
251 #define uv__req_init(loop, req, type) \
252 uv__req_init((loop), (uv_req_t*)(req), (type))
254 __attribute__((unused))
255 static void uv__update_time(uv_loop_t* loop) {
256 loop->time = uv__hrtime() / 1000000;
260 #include "uv-dtrace.h"
262 #define UV_TICK_START(arg0, arg1)
263 #define UV_TICK_STOP(arg0, arg1)
266 #endif /* UV_UNIX_INTERNAL_H_ */