uv: Upgrade to v0.10.18
[platform/upstream/nodejs.git] / deps / uv / src / unix / internal.h
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 #ifndef UV_UNIX_INTERNAL_H_
23 #define UV_UNIX_INTERNAL_H_
24
25 #include "uv-common.h"
26
27 #include <assert.h>
28 #include <stdlib.h> /* abort */
29
30 #if defined(__STRICT_ANSI__)
31 # define inline __inline
32 #endif
33
34 #if defined(__linux__)
35 # include "linux-syscalls.h"
36 #endif /* __linux__ */
37
38 #if defined(__sun)
39 # include <sys/port.h>
40 # include <port.h>
41 # define futimes(fd, tv) futimesat(fd, (void*)0, tv)
42 #endif /* __sun */
43
44 #if defined(__APPLE__) && !TARGET_OS_IPHONE
45 # include <CoreServices/CoreServices.h>
46 #endif
47
48 #define STATIC_ASSERT(expr)                                                   \
49   void uv__static_assert(int static_assert_failed[1 - 2 * !(expr)])
50
51 #define ACCESS_ONCE(type, var)                                                \
52   (*(volatile type*) &(var))
53
54 #define UNREACHABLE()                                                         \
55   do {                                                                        \
56     assert(0 && "unreachable code");                                          \
57     abort();                                                                  \
58   }                                                                           \
59   while (0)
60
61 #define SAVE_ERRNO(block)                                                     \
62   do {                                                                        \
63     int _saved_errno = errno;                                                 \
64     do { block; } while (0);                                                  \
65     errno = _saved_errno;                                                     \
66   }                                                                           \
67   while (0)
68
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
74 #endif
75
76 #if defined(__sun)
77 # define UV__POLLIN   POLLIN
78 # define UV__POLLOUT  POLLOUT
79 # define UV__POLLERR  POLLERR
80 # define UV__POLLHUP  POLLHUP
81 #endif
82
83 #ifndef UV__POLLIN
84 # define UV__POLLIN   1
85 #endif
86
87 #ifndef UV__POLLOUT
88 # define UV__POLLOUT  2
89 #endif
90
91 #ifndef UV__POLLERR
92 # define UV__POLLERR  4
93 #endif
94
95 #ifndef UV__POLLHUP
96 # define UV__POLLHUP  8
97 #endif
98
99 /* handle flags */
100 enum {
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. */
112 };
113
114 /* core */
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);
118 int uv__dup(int fd);
119 void uv__make_close_pending(uv_handle_t* handle);
120
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 */
128
129 /* async */
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);
134
135 /* loop */
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);
141
142 /* error */
143 uv_err_code uv_translate_sys_error(int sys_errno);
144 void uv_fatal_error(const int errorno, const char* syscall);
145
146 /* stream */
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);
156
157 /* tcp */
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);
161
162 /* pipe */
163 int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb);
164
165 /* timer */
166 void uv__run_timers(uv_loop_t* loop);
167 int uv__next_timeout(const uv_loop_t* loop);
168
169 /* signal */
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);
173
174 /* thread pool */
175 void uv__work_submit(uv_loop_t* loop,
176                      struct uv__work *w,
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);
180
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);
186
187 /* various */
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);
201
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)))
205 #else
206 #define uv__stream_fd(handle) ((handle)->io_watcher.fd)
207 #endif /* defined(__APPLE__) */
208
209 #ifdef UV__O_NONBLOCK
210 # define UV__F_NONBLOCK UV__O_NONBLOCK
211 #else
212 # define UV__F_NONBLOCK 1
213 #endif
214
215 int uv__make_socketpair(int fds[2], int flags);
216 int uv__make_pipe(int fds[2], int flags);
217
218 #if defined(__APPLE__)
219 typedef void (*cf_loop_signal_cb)(void*);
220
221 void uv__cf_loop_signal(uv_loop_t* loop, cf_loop_signal_cb cb, void* arg);
222
223 int uv__fsevents_init(uv_fs_event_t* handle);
224 int uv__fsevents_close(uv_fs_event_t* handle);
225
226 /* OSX < 10.7 has no file events, polyfill them */
227 #ifndef MAC_OS_X_VERSION_10_7
228
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;
241
242 #endif /* __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070 */
243
244 #endif /* defined(__APPLE__) */
245
246 __attribute__((unused))
247 static void uv__req_init(uv_loop_t* loop, uv_req_t* req, uv_req_type type) {
248   req->type = type;
249   uv__req_register(loop, req);
250 }
251 #define uv__req_init(loop, req, type) \
252   uv__req_init((loop), (uv_req_t*)(req), (type))
253
254 __attribute__((unused))
255 static void uv__update_time(uv_loop_t* loop) {
256   loop->time = uv__hrtime() / 1000000;
257 }
258
259 #ifdef HAVE_DTRACE
260 #include "uv-dtrace.h"
261 #else
262 #define UV_TICK_START(arg0, arg1)
263 #define UV_TICK_STOP(arg0, arg1)
264 #endif
265
266 #endif /* UV_UNIX_INTERNAL_H_ */