The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / uv / src / unix / internal.h @ 962f96d3

History | View | Annotate | Download (11 KB)

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
#include <string.h> /* strrchr */
30
#include <fcntl.h>  /* O_CLOEXEC, may be */
31

    
32
#if defined(__STRICT_ANSI__)
33
# define inline __inline
34
#endif
35

    
36
#if defined(__linux__)
37
# include "linux-syscalls.h"
38
#endif /* __linux__ */
39

    
40
#if defined(__sun)
41
# include <sys/port.h>
42
# include <port.h>
43
#endif /* __sun */
44

    
45
#if defined(__APPLE__) && !TARGET_OS_IPHONE
46
# include <CoreServices/CoreServices.h>
47
#endif
48

    
49
#define STATIC_ASSERT(expr)                                                   \
50
  void uv__static_assert(int static_assert_failed[1 - 2 * !(expr)])
51

    
52
#define ACCESS_ONCE(type, var)                                                \
53
  (*(volatile type*) &(var))
54

    
55
#define UNREACHABLE()                                                         \
56
  do {                                                                        \
57
    assert(0 && "unreachable code");                                          \
58
    abort();                                                                  \
59
  }                                                                           \
60
  while (0)
61

    
62
#define SAVE_ERRNO(block)                                                     \
63
  do {                                                                        \
64
    int _saved_errno = errno;                                                 \
65
    do { block; } while (0);                                                  \
66
    errno = _saved_errno;                                                     \
67
  }                                                                           \
68
  while (0)
69

    
70
/* The __clang__ and __INTEL_COMPILER checks are superfluous because they
71
 * define __GNUC__. They are here to convey to you, dear reader, that these
72
 * macros are enabled when compiling with clang or icc.
73
 */
74
#if defined(__clang__) ||                                                     \
75
    defined(__GNUC__) ||                                                      \
76
    defined(__INTEL_COMPILER) ||                                              \
77
    defined(__SUNPRO_C)
78
# define UV_DESTRUCTOR(declaration) __attribute__((destructor)) declaration
79
# define UV_UNUSED(declaration)     __attribute__((unused)) declaration
80
#else
81
# define UV_DESTRUCTOR(declaration) declaration
82
# define UV_UNUSED(declaration)     declaration
83
#endif
84

    
85
#if defined(__linux__)
86
# define UV__POLLIN   UV__EPOLLIN
87
# define UV__POLLOUT  UV__EPOLLOUT
88
# define UV__POLLERR  UV__EPOLLERR
89
# define UV__POLLHUP  UV__EPOLLHUP
90
#endif
91

    
92
#if defined(__sun)
93
# define UV__POLLIN   POLLIN
94
# define UV__POLLOUT  POLLOUT
95
# define UV__POLLERR  POLLERR
96
# define UV__POLLHUP  POLLHUP
97
#endif
98

    
99
#ifndef UV__POLLIN
100
# define UV__POLLIN   1
101
#endif
102

    
103
#ifndef UV__POLLOUT
104
# define UV__POLLOUT  2
105
#endif
106

    
107
#ifndef UV__POLLERR
108
# define UV__POLLERR  4
109
#endif
110

    
111
#ifndef UV__POLLHUP
112
# define UV__POLLHUP  8
113
#endif
114

    
115
#if !defined(O_CLOEXEC) && defined(__FreeBSD__)
116
/*
117
 * It may be that we are just missing `__POSIX_VISIBLE >= 200809`.
118
 * Try using fixed value const and give up, if it doesn't work
119
 */
120
# define O_CLOEXEC 0x00100000
121
#endif
122

    
123
typedef struct uv__stream_queued_fds_s uv__stream_queued_fds_t;
124

    
125
/* handle flags */
126
enum {
127
  UV_CLOSING              = 0x01,   /* uv_close() called but not finished. */
128
  UV_CLOSED               = 0x02,   /* close(2) finished. */
129
  UV_STREAM_READING       = 0x04,   /* uv_read_start() called. */
130
  UV_STREAM_SHUTTING      = 0x08,   /* uv_shutdown() called but not complete. */
131
  UV_STREAM_SHUT          = 0x10,   /* Write side closed. */
132
  UV_STREAM_READABLE      = 0x20,   /* The stream is readable */
133
  UV_STREAM_WRITABLE      = 0x40,   /* The stream is writable */
134
  UV_STREAM_BLOCKING      = 0x80,   /* Synchronous writes. */
135
  UV_STREAM_READ_PARTIAL  = 0x100,  /* read(2) read less than requested. */
136
  UV_STREAM_READ_EOF      = 0x200,  /* read(2) read EOF. */
137
  UV_TCP_NODELAY          = 0x400,  /* Disable Nagle. */
138
  UV_TCP_KEEPALIVE        = 0x800,  /* Turn on keep-alive. */
139
  UV_TCP_SINGLE_ACCEPT    = 0x1000, /* Only accept() when idle. */
140
  UV_HANDLE_IPV6          = 0x2000  /* Handle is bound to a IPv6 socket. */
141
};
142

    
143
typedef enum {
144
  UV_CLOCK_PRECISE = 0,  /* Use the highest resolution clock available. */
145
  UV_CLOCK_FAST = 1      /* Use the fastest clock with <= 1ms granularity. */
146
} uv_clocktype_t;
147

    
148
struct uv__stream_queued_fds_s {
149
  unsigned int size;
150
  unsigned int offset;
151
  int fds[1];
152
};
153

    
154

    
155
/* core */
156
int uv__nonblock(int fd, int set);
157
int uv__close(int fd);
158
int uv__cloexec(int fd, int set);
159
int uv__socket(int domain, int type, int protocol);
160
int uv__dup(int fd);
161
ssize_t uv__recvmsg(int fd, struct msghdr *msg, int flags);
162
void uv__make_close_pending(uv_handle_t* handle);
163

    
164
void uv__io_init(uv__io_t* w, uv__io_cb cb, int fd);
165
void uv__io_start(uv_loop_t* loop, uv__io_t* w, unsigned int events);
166
void uv__io_stop(uv_loop_t* loop, uv__io_t* w, unsigned int events);
167
void uv__io_close(uv_loop_t* loop, uv__io_t* w);
168
void uv__io_feed(uv_loop_t* loop, uv__io_t* w);
169
int uv__io_active(const uv__io_t* w, unsigned int events);
170
void uv__io_poll(uv_loop_t* loop, int timeout); /* in milliseconds or -1 */
171

    
172
/* async */
173
void uv__async_send(struct uv__async* wa);
174
void uv__async_init(struct uv__async* wa);
175
int uv__async_start(uv_loop_t* loop, struct uv__async* wa, uv__async_cb cb);
176
void uv__async_stop(uv_loop_t* loop, struct uv__async* wa);
177

    
178
/* loop */
179
void uv__run_idle(uv_loop_t* loop);
180
void uv__run_check(uv_loop_t* loop);
181
void uv__run_prepare(uv_loop_t* loop);
182

    
183
/* stream */
184
void uv__stream_init(uv_loop_t* loop, uv_stream_t* stream,
185
    uv_handle_type type);
186
int uv__stream_open(uv_stream_t*, int fd, int flags);
187
void uv__stream_destroy(uv_stream_t* stream);
188
#if defined(__APPLE__)
189
int uv__stream_try_select(uv_stream_t* stream, int* fd);
190
#endif /* defined(__APPLE__) */
191
void uv__server_io(uv_loop_t* loop, uv__io_t* w, unsigned int events);
192
int uv__accept(int sockfd);
193
int uv__dup2_cloexec(int oldfd, int newfd);
194
int uv__open_cloexec(const char* path, int flags);
195

    
196
/* tcp */
197
int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb);
198
int uv__tcp_nodelay(int fd, int on);
199
int uv__tcp_keepalive(int fd, int on, unsigned int delay);
200

    
201
/* pipe */
202
int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb);
203

    
204
/* timer */
205
void uv__run_timers(uv_loop_t* loop);
206
int uv__next_timeout(const uv_loop_t* loop);
207

    
208
/* signal */
209
void uv__signal_close(uv_signal_t* handle);
210
void uv__signal_global_once_init(void);
211
void uv__signal_loop_cleanup(uv_loop_t* loop);
212

    
213
/* thread pool */
214
void uv__work_submit(uv_loop_t* loop,
215
                     struct uv__work *w,
216
                     void (*work)(struct uv__work *w),
217
                     void (*done)(struct uv__work *w, int status));
218
void uv__work_done(uv_async_t* handle);
219

    
220
/* platform specific */
221
uint64_t uv__hrtime(uv_clocktype_t type);
222
int uv__kqueue_init(uv_loop_t* loop);
223
int uv__platform_loop_init(uv_loop_t* loop, int default_loop);
224
void uv__platform_loop_delete(uv_loop_t* loop);
225
void uv__platform_invalidate_fd(uv_loop_t* loop, int fd);
226

    
227
/* various */
228
void uv__async_close(uv_async_t* handle);
229
void uv__check_close(uv_check_t* handle);
230
void uv__fs_event_close(uv_fs_event_t* handle);
231
void uv__idle_close(uv_idle_t* handle);
232
void uv__pipe_close(uv_pipe_t* handle);
233
void uv__poll_close(uv_poll_t* handle);
234
void uv__prepare_close(uv_prepare_t* handle);
235
void uv__process_close(uv_process_t* handle);
236
void uv__stream_close(uv_stream_t* handle);
237
void uv__tcp_close(uv_tcp_t* handle);
238
void uv__timer_close(uv_timer_t* handle);
239
void uv__udp_close(uv_udp_t* handle);
240
void uv__udp_finish_close(uv_udp_t* handle);
241
uv_handle_type uv__handle_type(int fd);
242

    
243
#if defined(__APPLE__)
244
int uv___stream_fd(uv_stream_t* handle);
245
#define uv__stream_fd(handle) (uv___stream_fd((uv_stream_t*) (handle)))
246
#else
247
#define uv__stream_fd(handle) ((handle)->io_watcher.fd)
248
#endif /* defined(__APPLE__) */
249

    
250
#ifdef UV__O_NONBLOCK
251
# define UV__F_NONBLOCK UV__O_NONBLOCK
252
#else
253
# define UV__F_NONBLOCK 1
254
#endif
255

    
256
int uv__make_socketpair(int fds[2], int flags);
257
int uv__make_pipe(int fds[2], int flags);
258

    
259
#if defined(__APPLE__)
260

    
261
int uv__fsevents_init(uv_fs_event_t* handle);
262
int uv__fsevents_close(uv_fs_event_t* handle);
263
void uv__fsevents_loop_delete(uv_loop_t* loop);
264

    
265
/* OSX < 10.7 has no file events, polyfill them */
266
#ifndef MAC_OS_X_VERSION_10_7
267

    
268
static const int kFSEventStreamCreateFlagFileEvents = 0x00000010;
269
static const int kFSEventStreamEventFlagItemCreated = 0x00000100;
270
static const int kFSEventStreamEventFlagItemRemoved = 0x00000200;
271
static const int kFSEventStreamEventFlagItemInodeMetaMod = 0x00000400;
272
static const int kFSEventStreamEventFlagItemRenamed = 0x00000800;
273
static const int kFSEventStreamEventFlagItemModified = 0x00001000;
274
static const int kFSEventStreamEventFlagItemFinderInfoMod = 0x00002000;
275
static const int kFSEventStreamEventFlagItemChangeOwner = 0x00004000;
276
static const int kFSEventStreamEventFlagItemXattrMod = 0x00008000;
277
static const int kFSEventStreamEventFlagItemIsFile = 0x00010000;
278
static const int kFSEventStreamEventFlagItemIsDir = 0x00020000;
279
static const int kFSEventStreamEventFlagItemIsSymlink = 0x00040000;
280

    
281
#endif /* __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070 */
282

    
283
#endif /* defined(__APPLE__) */
284

    
285
UV_UNUSED(static void uv__req_init(uv_loop_t* loop,
286
                                   uv_req_t* req,
287
                                   uv_req_type type)) {
288
  req->type = type;
289
  uv__req_register(loop, req);
290
}
291
#define uv__req_init(loop, req, type) \
292
  uv__req_init((loop), (uv_req_t*)(req), (type))
293

    
294
UV_UNUSED(static void uv__update_time(uv_loop_t* loop)) {
295
  /* Use a fast time source if available.  We only need millisecond precision.
296
   */
297
  loop->time = uv__hrtime(UV_CLOCK_FAST) / 1000000;
298
}
299

    
300
UV_UNUSED(static char* uv__basename_r(const char* path)) {
301
  char* s;
302

    
303
  s = strrchr(path, '/');
304
  if (s == NULL)
305
    return (char*) path;
306

    
307
  return s + 1;
308
}
309

    
310

    
311
#ifdef HAVE_DTRACE
312
#include "uv-dtrace.h"
313
#else
314
#define UV_TICK_START(arg0, arg1)
315
#define UV_TICK_STOP(arg0, arg1)
316
#endif
317

    
318
#endif /* UV_UNIX_INTERNAL_H_ */