PageRenderTime 37ms CodeModel.GetById 17ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

/src/unix/internal.h

http://github.com/joyent/libuv
C++ Header | 237 lines | 164 code | 40 blank | 33 comment | 5 complexity | 19ac55e37e5e7814bdffd173259b641f MD5 | raw file
  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#undef HAVE_PORTS_FS
 35
 36#if __linux__
 37# include "linux/syscalls.h"
 38#endif /* __linux__ */
 39
 40#if defined(__sun)
 41# include <sys/port.h>
 42# include <port.h>
 43# ifdef PORT_SOURCE_FILE
 44#  define HAVE_PORTS_FS 1
 45# endif
 46# define futimes(fd, tv) futimesat(fd, (void*)0, tv)
 47#endif /* __sun */
 48
 49#if defined(__APPLE__) && !TARGET_OS_IPHONE
 50# include <CoreServices/CoreServices.h>
 51#endif
 52
 53#define UNREACHABLE()                                                         \
 54  do {                                                                        \
 55    assert(0 && "unreachable code");                                          \
 56    abort();                                                                  \
 57  }                                                                           \
 58  while (0)
 59
 60#define SAVE_ERRNO(block)                                                     \
 61  do {                                                                        \
 62    int _saved_errno = errno;                                                 \
 63    do { block; } while (0);                                                  \
 64    errno = _saved_errno;                                                     \
 65  }                                                                           \
 66  while (0)
 67
 68#if defined(__linux__)
 69# define UV__POLLIN   UV__EPOLLIN
 70# define UV__POLLOUT  UV__EPOLLOUT
 71# define UV__POLLERR  UV__EPOLLERR
 72# define UV__POLLHUP  UV__EPOLLHUP
 73#endif
 74
 75#if defined(__sun)
 76# define UV__POLLIN   POLLIN
 77# define UV__POLLOUT  POLLOUT
 78# define UV__POLLERR  POLLERR
 79# define UV__POLLHUP  POLLHUP
 80#endif
 81
 82#ifndef UV__POLLIN
 83# define UV__POLLIN   1
 84#endif
 85
 86#ifndef UV__POLLOUT
 87# define UV__POLLOUT  2
 88#endif
 89
 90#ifndef UV__POLLERR
 91# define UV__POLLERR  4
 92#endif
 93
 94#ifndef UV__POLLHUP
 95# define UV__POLLHUP  8
 96#endif
 97
 98/* handle flags */
 99enum {
100  UV_CLOSING          = 0x01,   /* uv_close() called but not finished. */
101  UV_CLOSED           = 0x02,   /* close(2) finished. */
102  UV_STREAM_READING   = 0x04,   /* uv_read_start() called. */
103  UV_STREAM_SHUTTING  = 0x08,   /* uv_shutdown() called but not complete. */
104  UV_STREAM_SHUT      = 0x10,   /* Write side closed. */
105  UV_STREAM_READABLE  = 0x20,   /* The stream is readable */
106  UV_STREAM_WRITABLE  = 0x40,   /* The stream is writable */
107  UV_STREAM_BLOCKING  = 0x80,   /* Synchronous writes. */
108  UV_TCP_NODELAY      = 0x100,  /* Disable Nagle. */
109  UV_TCP_KEEPALIVE    = 0x200,  /* Turn on keep-alive. */
110  UV_TCP_SINGLE_ACCEPT = 0x400  /* Only accept() when idle. */
111};
112
113__attribute__((unused))
114static void uv__req_init(uv_loop_t* loop, uv_req_t* req, uv_req_type type) {
115  req->type = type;
116  uv__req_register(loop, req);
117}
118#define uv__req_init(loop, req, type) \
119  uv__req_init((loop), (uv_req_t*)(req), (type))
120
121/* core */
122void uv__handle_init(uv_loop_t* loop, uv_handle_t* handle, uv_handle_type type);
123int uv__nonblock(int fd, int set);
124int uv__cloexec(int fd, int set);
125int uv__socket(int domain, int type, int protocol);
126int uv__dup(int fd);
127int uv_async_stop(uv_async_t* handle);
128void uv__make_close_pending(uv_handle_t* handle);
129
130void uv__io_init(uv__io_t* w, uv__io_cb cb, int fd);
131void uv__io_start(uv_loop_t* loop, uv__io_t* w, unsigned int events);
132void uv__io_stop(uv_loop_t* loop, uv__io_t* w, unsigned int events);
133void uv__io_feed(uv_loop_t* loop, uv__io_t* w);
134int uv__io_active(const uv__io_t* w, unsigned int events);
135void uv__io_poll(uv_loop_t* loop, int timeout); /* in milliseconds or -1 */
136
137/* loop */
138int uv__loop_init(uv_loop_t* loop, int default_loop);
139void uv__loop_delete(uv_loop_t* loop);
140void uv__run_idle(uv_loop_t* loop);
141void uv__run_check(uv_loop_t* loop);
142void uv__run_prepare(uv_loop_t* loop);
143
144/* error */
145uv_err_code uv_translate_sys_error(int sys_errno);
146void uv_fatal_error(const int errorno, const char* syscall);
147
148/* stream */
149void uv__stream_init(uv_loop_t* loop, uv_stream_t* stream,
150    uv_handle_type type);
151int uv__stream_open(uv_stream_t*, int fd, int flags);
152void uv__stream_destroy(uv_stream_t* stream);
153void uv__server_io(uv_loop_t* loop, uv__io_t* w, unsigned int events);
154int uv__accept(int sockfd);
155
156/* tcp */
157int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb);
158int uv__tcp_nodelay(int fd, int on);
159int uv__tcp_keepalive(int fd, int on, unsigned int delay);
160
161/* pipe */
162int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb);
163
164/* timer */
165void uv__run_timers(uv_loop_t* loop);
166unsigned int uv__next_timeout(uv_loop_t* loop);
167
168/* signal */
169void uv__signal_close(uv_signal_t* handle);
170void uv__signal_global_once_init(void);
171void uv__signal_loop_cleanup();
172
173/* thread pool */
174void uv__work_submit(uv_loop_t* loop,
175                     struct uv__work *w,
176                     void (*work)(struct uv__work *w),
177                     void (*done)(struct uv__work *w));
178void uv__work_done(uv_async_t* handle, int status);
179
180/* platform specific */
181int uv__kqueue_init(uv_loop_t* loop);
182int uv__platform_loop_init(uv_loop_t* loop, int default_loop);
183void uv__platform_loop_delete(uv_loop_t* loop);
184
185/* various */
186void uv__async_close(uv_async_t* handle);
187void uv__check_close(uv_check_t* handle);
188void uv__fs_event_close(uv_fs_event_t* handle);
189void uv__idle_close(uv_idle_t* handle);
190void uv__pipe_close(uv_pipe_t* handle);
191void uv__poll_close(uv_poll_t* handle);
192void uv__prepare_close(uv_prepare_t* handle);
193void uv__process_close(uv_process_t* handle);
194void uv__stream_close(uv_stream_t* handle);
195void uv__tcp_close(uv_tcp_t* handle);
196void uv__timer_close(uv_timer_t* handle);
197void uv__udp_close(uv_udp_t* handle);
198void uv__udp_finish_close(uv_udp_t* handle);
199
200#ifdef UV__O_NONBLOCK
201# define UV__F_NONBLOCK UV__O_NONBLOCK
202#else
203# define UV__F_NONBLOCK 1
204#endif
205
206int uv__make_socketpair(int fds[2], int flags);
207int uv__make_pipe(int fds[2], int flags);
208
209#if defined(__APPLE__)
210typedef void (*cf_loop_signal_cb)(void*);
211
212void uv__cf_loop_signal(uv_loop_t* loop, cf_loop_signal_cb cb, void* arg);
213
214int uv__fsevents_init(uv_fs_event_t* handle);
215int uv__fsevents_close(uv_fs_event_t* handle);
216
217/* OSX < 10.7 has no file events, polyfill them */
218#ifndef MAC_OS_X_VERSION_10_7
219
220static const int kFSEventStreamCreateFlagFileEvents = 0x00000010;
221static const int kFSEventStreamEventFlagItemCreated = 0x00000100;
222static const int kFSEventStreamEventFlagItemRemoved = 0x00000200;
223static const int kFSEventStreamEventFlagItemInodeMetaMod = 0x00000400;
224static const int kFSEventStreamEventFlagItemRenamed = 0x00000800;
225static const int kFSEventStreamEventFlagItemModified = 0x00001000;
226static const int kFSEventStreamEventFlagItemFinderInfoMod = 0x00002000;
227static const int kFSEventStreamEventFlagItemChangeOwner = 0x00004000;
228static const int kFSEventStreamEventFlagItemXattrMod = 0x00008000;
229static const int kFSEventStreamEventFlagItemIsFile = 0x00010000;
230static const int kFSEventStreamEventFlagItemIsDir = 0x00020000;
231static const int kFSEventStreamEventFlagItemIsSymlink = 0x00040000;
232
233#endif /* __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070 */
234
235#endif /* defined(__APPLE__) */
236
237#endif /* UV_UNIX_INTERNAL_H_ */