uv-common.h
Go to the documentation of this 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 /*
23  * This file is private to libuv. It provides common functionality to both
24  * Windows and Unix backends.
25  */
26 
27 #ifndef UV_COMMON_H_
28 #define UV_COMMON_H_
29 
30 #include <assert.h>
31 #include <stdarg.h>
32 #include <stddef.h>
33 
34 #if defined(_MSC_VER) && _MSC_VER < 1600
35 # include "uv/stdint-msvc2008.h"
36 #else
37 # include <stdint.h>
38 #endif
39 
40 #include "uv.h"
41 #include "uv/tree.h"
42 #include "queue.h"
43 #include "strscpy.h"
44 
45 #if EDOM > 0
46 # define UV__ERR(x) (-(x))
47 #else
48 # define UV__ERR(x) (x)
49 #endif
50 
51 #if !defined(snprintf) && defined(_MSC_VER) && _MSC_VER < 1900
52 extern int snprintf(char*, size_t, const char*, ...);
53 #endif
54 
55 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
56 
57 #define container_of(ptr, type, member) \
58  ((type *) ((char *) (ptr) - offsetof(type, member)))
59 
60 #define STATIC_ASSERT(expr) \
61  void uv__static_assert(int static_assert_failed[1 - 2 * !(expr)])
62 
63 /* Handle flags. Some flags are specific to Windows or UNIX. */
64 enum {
65  /* Used by all handles. */
66  UV_HANDLE_CLOSING = 0x00000001,
67  UV_HANDLE_CLOSED = 0x00000002,
68  UV_HANDLE_ACTIVE = 0x00000004,
69  UV_HANDLE_REF = 0x00000008,
70  UV_HANDLE_INTERNAL = 0x00000010,
72 
73  /* Used by streams. */
74  UV_HANDLE_LISTENING = 0x00000040,
75  UV_HANDLE_CONNECTION = 0x00000080,
76  UV_HANDLE_SHUTTING = 0x00000100,
77  UV_HANDLE_SHUT = 0x00000200,
78  UV_HANDLE_READ_PARTIAL = 0x00000400,
79  UV_HANDLE_READ_EOF = 0x00000800,
80 
81  /* Used by streams and UDP handles. */
82  UV_HANDLE_READING = 0x00001000,
83  UV_HANDLE_BOUND = 0x00002000,
84  UV_HANDLE_READABLE = 0x00004000,
85  UV_HANDLE_WRITABLE = 0x00008000,
86  UV_HANDLE_READ_PENDING = 0x00010000,
88  UV_HANDLE_ZERO_READ = 0x00040000,
89  UV_HANDLE_EMULATE_IOCP = 0x00080000,
92 
93  /* Used by uv_tcp_t and uv_udp_t handles */
94  UV_HANDLE_IPV6 = 0x00400000,
95 
96  /* Only used by uv_tcp_t handles. */
97  UV_HANDLE_TCP_NODELAY = 0x01000000,
103 
104  /* Only used by uv_udp_t handles. */
108 
109  /* Only used by uv_pipe_t handles. */
111  UV_HANDLE_PIPESERVER = 0x02000000,
112 
113  /* Only used by uv_tty_t handles. */
115  UV_HANDLE_TTY_RAW = 0x02000000,
118 
119  /* Only used by uv_signal_t handles. */
121  UV_SIGNAL_ONE_SHOT = 0x02000000,
122 
123  /* Only used by uv_poll_t handles. */
124  UV_HANDLE_POLL_SLOW = 0x01000000
125 };
126 
128 
130 
132  const struct sockaddr* addr,
133  unsigned int addrlen,
134  unsigned int flags);
135 
137  uv_tcp_t* handle,
138  const struct sockaddr* addr,
139  unsigned int addrlen,
140  uv_connect_cb cb);
141 
143  const struct sockaddr* addr,
144  unsigned int addrlen,
145  unsigned int flags);
146 
148  const struct sockaddr* addr,
149  unsigned int addrlen);
150 
152 
154 
156  uv_udp_t* handle,
157  const uv_buf_t bufs[],
158  unsigned int nbufs,
159  const struct sockaddr* addr,
160  unsigned int addrlen,
162 
164  const uv_buf_t bufs[],
165  unsigned int nbufs,
166  const struct sockaddr* addr,
167  unsigned int addrlen);
168 
171 
173 
175 
176 int uv__getaddrinfo_translate_error(int sys_err); /* EAI_* error. */
177 
182 };
183 
185  struct uv__work *w,
186  enum uv__work_kind kind,
187  void (*work)(struct uv__work *w),
188  void (*done)(struct uv__work *w, int status));
189 
191 
192 size_t uv__count_bufs(const uv_buf_t bufs[], unsigned int nbufs);
193 
194 int uv__socket_sockopt(uv_handle_t* handle, int optname, int* value);
195 
199 
200 int uv__next_timeout(const uv_loop_t* loop);
203 
204 #define uv__has_active_reqs(loop) \
205  ((loop)->active_reqs.count > 0)
206 
207 #define uv__req_register(loop, req) \
208  do { \
209  (loop)->active_reqs.count++; \
210  } \
211  while (0)
212 
213 #define uv__req_unregister(loop, req) \
214  do { \
215  assert(uv__has_active_reqs(loop)); \
216  (loop)->active_reqs.count--; \
217  } \
218  while (0)
219 
220 #define uv__has_active_handles(loop) \
221  ((loop)->active_handles > 0)
222 
223 #define uv__active_handle_add(h) \
224  do { \
225  (h)->loop->active_handles++; \
226  } \
227  while (0)
228 
229 #define uv__active_handle_rm(h) \
230  do { \
231  (h)->loop->active_handles--; \
232  } \
233  while (0)
234 
235 #define uv__is_active(h) \
236  (((h)->flags & UV_HANDLE_ACTIVE) != 0)
237 
238 #define uv__is_closing(h) \
239  (((h)->flags & (UV_HANDLE_CLOSING | UV_HANDLE_CLOSED)) != 0)
240 
241 #define uv__handle_start(h) \
242  do { \
243  if (((h)->flags & UV_HANDLE_ACTIVE) != 0) break; \
244  (h)->flags |= UV_HANDLE_ACTIVE; \
245  if (((h)->flags & UV_HANDLE_REF) != 0) uv__active_handle_add(h); \
246  } \
247  while (0)
248 
249 #define uv__handle_stop(h) \
250  do { \
251  if (((h)->flags & UV_HANDLE_ACTIVE) == 0) break; \
252  (h)->flags &= ~UV_HANDLE_ACTIVE; \
253  if (((h)->flags & UV_HANDLE_REF) != 0) uv__active_handle_rm(h); \
254  } \
255  while (0)
256 
257 #define uv__handle_ref(h) \
258  do { \
259  if (((h)->flags & UV_HANDLE_REF) != 0) break; \
260  (h)->flags |= UV_HANDLE_REF; \
261  if (((h)->flags & UV_HANDLE_CLOSING) != 0) break; \
262  if (((h)->flags & UV_HANDLE_ACTIVE) != 0) uv__active_handle_add(h); \
263  } \
264  while (0)
265 
266 #define uv__handle_unref(h) \
267  do { \
268  if (((h)->flags & UV_HANDLE_REF) == 0) break; \
269  (h)->flags &= ~UV_HANDLE_REF; \
270  if (((h)->flags & UV_HANDLE_CLOSING) != 0) break; \
271  if (((h)->flags & UV_HANDLE_ACTIVE) != 0) uv__active_handle_rm(h); \
272  } \
273  while (0)
274 
275 #define uv__has_ref(h) \
276  (((h)->flags & UV_HANDLE_REF) != 0)
277 
278 #if defined(_WIN32)
279 # define uv__handle_platform_init(h) ((h)->u.fd = -1)
280 #else
281 # define uv__handle_platform_init(h) ((h)->next_closing = NULL)
282 #endif
283 
284 #define uv__handle_init(loop_, h, type_) \
285  do { \
286  (h)->loop = (loop_); \
287  (h)->type = (type_); \
288  (h)->flags = UV_HANDLE_REF; /* Ref the loop when active. */ \
289  QUEUE_INSERT_TAIL(&(loop_)->handle_queue, &(h)->handle_queue); \
290  uv__handle_platform_init(h); \
291  } \
292  while (0)
293 
294 /* Note: uses an open-coded version of SET_REQ_SUCCESS() because of
295  * a circular dependency between src/uv-common.h and src/win/internal.h.
296  */
297 #if defined(_WIN32)
298 # define UV_REQ_INIT(req, typ) \
299  do { \
300  (req)->type = (typ); \
301  (req)->u.io.overlapped.Internal = 0; /* SET_REQ_SUCCESS() */ \
302  } \
303  while (0)
304 #else
305 # define UV_REQ_INIT(req, typ) \
306  do { \
307  (req)->type = (typ); \
308  } \
309  while (0)
310 #endif
311 
312 #define uv__req_init(loop, req, typ) \
313  do { \
314  UV_REQ_INIT(req, typ); \
315  uv__req_register(loop, req); \
316  } \
317  while (0)
318 
319 /* Allocator prototypes */
320 void *uv__calloc(size_t count, size_t size);
321 char *uv__strdup(const char* s);
322 char *uv__strndup(const char* s, size_t n);
323 void* uv__malloc(size_t size);
324 void uv__free(void* ptr);
325 void* uv__realloc(void* ptr, size_t size);
326 void* uv__reallocf(void* ptr, size_t size);
327 
328 #endif /* UV_COMMON_H_ */
UV_HANDLE_WRITABLE
@ UV_HANDLE_WRITABLE
Definition: uv-common.h:85
UV_HANDLE_READ_PENDING
@ UV_HANDLE_READ_PENDING
Definition: uv-common.h:86
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
UV_HANDLE_TCP_KEEPALIVE
@ UV_HANDLE_TCP_KEEPALIVE
Definition: uv-common.h:98
UV_HANDLE_PIPESERVER
@ UV_HANDLE_PIPESERVER
Definition: uv-common.h:111
uv__work
Definition: third_party/libuv/include/uv/threadpool.h:30
UV_HANDLE_CLOSED
@ UV_HANDLE_CLOSED
Definition: uv-common.h:67
uv__timer_close
void uv__timer_close(uv_timer_t *handle)
Definition: timer.c:174
UV_HANDLE_NON_OVERLAPPED_PIPE
@ UV_HANDLE_NON_OVERLAPPED_PIPE
Definition: uv-common.h:110
uv__socket_sockopt
int uv__socket_sockopt(uv_handle_t *handle, int optname, int *value)
Definition: unix/core.c:184
UV_HANDLE_BOUND
@ UV_HANDLE_BOUND
Definition: uv-common.h:83
UV_HANDLE_TCP_SOCKET_CLOSED
@ UV_HANDLE_TCP_SOCKET_CLOSED
Definition: uv-common.h:101
UV_HANDLE_TCP_ACCEPT_STATE_CHANGING
@ UV_HANDLE_TCP_ACCEPT_STATE_CHANGING
Definition: uv-common.h:100
uv_connect_s
Definition: uv.h:580
uv__fs_poll_close
void uv__fs_poll_close(uv_fs_poll_t *handle)
Definition: fs-poll.c:164
uv__udp_connect
int uv__udp_connect(uv_udp_t *handle, const struct sockaddr *addr, unsigned int addrlen)
Definition: unix/udp.c:605
UV_HANDLE_TCP_SINGLE_ACCEPT
@ UV_HANDLE_TCP_SINGLE_ACCEPT
Definition: uv-common.h:99
strscpy.h
UV_HANDLE_UDP_CONNECTED
@ UV_HANDLE_UDP_CONNECTED
Definition: uv-common.h:106
UV_HANDLE_CONNECTION
@ UV_HANDLE_CONNECTION
Definition: uv-common.h:75
uv__udp_recv_stop
int uv__udp_recv_stop(uv_udp_t *handle)
Definition: unix/udp.c:1329
UV_HANDLE_ENDGAME_QUEUED
@ UV_HANDLE_ENDGAME_QUEUED
Definition: uv-common.h:71
UV_SIGNAL_ONE_SHOT_DISPATCHED
@ UV_SIGNAL_ONE_SHOT_DISPATCHED
Definition: uv-common.h:120
uv__dirent_t
struct dirent uv__dirent_t
Definition: unix.h:169
tcp
static uv_tcp_t tcp
Definition: test-connection-fail.c:29
send_cb
static void send_cb(uv_udp_send_t *req, int status)
Definition: benchmark-udp-pummel.c:72
UV_HANDLE_READ_PARTIAL
@ UV_HANDLE_READ_PARTIAL
Definition: uv-common.h:78
status
absl::Status status
Definition: rls.cc:251
uv__work_submit
void uv__work_submit(uv_loop_t *loop, struct uv__work *w, enum uv__work_kind kind, void(*work)(struct uv__work *w), void(*done)(struct uv__work *w, int status))
Definition: threadpool.c:256
UV_HANDLE_POLL_SLOW
@ UV_HANDLE_POLL_SLOW
Definition: uv-common.h:124
UV_HANDLE_BLOCKING_WRITES
@ UV_HANDLE_BLOCKING_WRITES
Definition: uv-common.h:90
uv_fs_s
Definition: uv.h:1294
uv__udp_recv_start
int uv__udp_recv_start(uv_udp_t *handle, uv_alloc_cb alloccb, uv_udp_recv_cb recv_cb)
Definition: unix/udp.c:1304
UV_HANDLE_UDP_RECVMMSG
@ UV_HANDLE_UDP_RECVMMSG
Definition: uv-common.h:107
UV_HANDLE_ZERO_READ
@ UV_HANDLE_ZERO_READ
Definition: uv-common.h:88
uv__udp_try_send
int uv__udp_try_send(uv_udp_t *handle, const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr *addr, unsigned int addrlen)
Definition: unix/udp.c:715
uv_alloc_cb
void(* uv_alloc_cb)(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
Definition: uv.h:306
UV_HANDLE_CANCELLATION_PENDING
@ UV_HANDLE_CANCELLATION_PENDING
Definition: uv-common.h:91
uv__malloc
void * uv__malloc(size_t size)
Definition: uv-common.c:75
uv__udp_send
int uv__udp_send(uv_udp_send_t *req, uv_udp_t *handle, const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr *addr, unsigned int addrlen, uv_udp_send_cb send_cb)
Definition: unix/udp.c:649
uv_async_s
Definition: uv.h:834
uv__free
void uv__free(void *ptr)
Definition: uv-common.c:81
UV_HANDLE_UDP_PROCESSING
@ UV_HANDLE_UDP_PROCESSING
Definition: uv-common.h:105
uv__tcp_connect
int uv__tcp_connect(uv_connect_t *req, uv_tcp_t *handle, const struct sockaddr *addr, unsigned int addrlen, uv_connect_cb cb)
Definition: unix/tcp.c:204
uv__work_done
void uv__work_done(uv_async_t *handle)
Definition: threadpool.c:295
req
static uv_connect_t req
Definition: test-connection-fail.c:30
UV_HANDLE_TCP_NODELAY
@ UV_HANDLE_TCP_NODELAY
Definition: uv-common.h:97
uv__next_timeout
int uv__next_timeout(const uv_loop_t *loop)
Definition: timer.c:133
UV_SIGNAL_ONE_SHOT
@ UV_SIGNAL_ONE_SHOT
Definition: uv-common.h:121
uv_udp_s
Definition: uv.h:629
uv__run_timers
void uv__run_timers(uv_loop_t *loop)
Definition: timer.c:154
UV_HANDLE_INTERNAL
@ UV_HANDLE_INTERNAL
Definition: uv-common.h:70
done
struct tab * done
Definition: bloaty/third_party/zlib/examples/enough.c:176
uv__strdup
char * uv__strdup(const char *s)
Definition: uv-common.c:55
uv_loop_option
uv_loop_option
Definition: uv.h:249
UV_HANDLE_TTY_SAVED_ATTRIBUTES
@ UV_HANDLE_TTY_SAVED_ATTRIBUTES
Definition: uv-common.h:117
uv_udp_send_cb
void(* uv_udp_send_cb)(uv_udp_send_t *req, int status)
Definition: uv.h:621
UV_HANDLE_LISTENING
@ UV_HANDLE_LISTENING
Definition: uv-common.h:74
uv__reallocf
void * uv__reallocf(void *ptr, size_t size)
Definition: uv-common.c:103
UV_HANDLE_SHUTTING
@ UV_HANDLE_SHUTTING
Definition: uv-common.h:76
UV__WORK_CPU
@ UV__WORK_CPU
Definition: uv-common.h:179
recv_cb
static void recv_cb(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr, unsigned flags)
Definition: benchmark-udp-pummel.c:109
uv_udp_recv_cb
void(* uv_udp_recv_cb)(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr, unsigned flags)
Definition: uv.h:622
uv__calloc
void * uv__calloc(size_t count, size_t size)
Definition: uv-common.c:92
UV_HANDLE_TTY_READABLE
@ UV_HANDLE_TTY_READABLE
Definition: uv-common.h:114
UV_HANDLE_IPV6
@ UV_HANDLE_IPV6
Definition: uv-common.h:94
uv_timer_s
Definition: uv.h:850
queue.h
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
uv_tcp_s
Definition: uv.h:544
stdint.h
uv_connect_cb
void(* uv_connect_cb)(uv_connect_t *req, int status)
Definition: uv.h:313
uv__fs_get_dirent_type
uv_dirent_type_t uv__fs_get_dirent_type(uv__dirent_t *dent)
Definition: uv-common.c:658
tree.h
uv_udp_send_s
Definition: uv.h:645
UV_HANDLE_SHUT
@ UV_HANDLE_SHUT
Definition: uv-common.h:77
bufs
static uv_buf_t bufs[5]
Definition: benchmark-udp-pummel.c:51
UV_HANDLE_EMULATE_IOCP
@ UV_HANDLE_EMULATE_IOCP
Definition: uv-common.h:89
value
const char * value
Definition: hpack_parser_table.cc:165
uv.h
UV_HANDLE_SYNC_BYPASS_IOCP
@ UV_HANDLE_SYNC_BYPASS_IOCP
Definition: uv-common.h:87
UV_HANDLE_CLOSING
@ UV_HANDLE_CLOSING
Definition: uv-common.h:66
uv__tcp_bind
int uv__tcp_bind(uv_tcp_t *tcp, const struct sockaddr *addr, unsigned int addrlen, unsigned int flags)
Definition: unix/tcp.c:148
uv_buf_t
Definition: unix.h:121
UV_HANDLE_READABLE
@ UV_HANDLE_READABLE
Definition: uv-common.h:84
UV__WORK_SLOW_IO
@ UV__WORK_SLOW_IO
Definition: uv-common.h:181
uv__fs_scandir_cleanup
void uv__fs_scandir_cleanup(uv_fs_t *req)
Definition: uv-common.c:605
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
uv__strndup
char * uv__strndup(const char *s, size_t n)
Definition: uv-common.c:63
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
uv_fs_poll_s
Definition: uv.h:1544
uv__getaddrinfo_translate_error
int uv__getaddrinfo_translate_error(int sys_err)
Definition: unix/getaddrinfo.c:42
UV_HANDLE_READ_EOF
@ UV_HANDLE_READ_EOF
Definition: uv-common.h:79
uv__count_bufs
size_t uv__count_bufs(const uv_buf_t bufs[], unsigned int nbufs)
Definition: uv-common.c:543
uv__loop_configure
int uv__loop_configure(uv_loop_t *loop, uv_loop_option option, va_list ap)
Definition: loop.c:185
UV_HANDLE_TTY_SAVED_POSITION
@ UV_HANDLE_TTY_SAVED_POSITION
Definition: uv-common.h:116
uv__work_kind
uv__work_kind
Definition: uv-common.h:178
UV_HANDLE_REF
@ UV_HANDLE_REF
Definition: uv-common.h:69
UV__WORK_FAST_IO
@ UV__WORK_FAST_IO
Definition: uv-common.h:180
UV_HANDLE_SHARED_TCP_SOCKET
@ UV_HANDLE_SHARED_TCP_SOCKET
Definition: uv-common.h:102
uv__udp_is_connected
int uv__udp_is_connected(uv_udp_t *handle)
Definition: uv-common.c:363
uv__loop_close
void uv__loop_close(uv_loop_t *loop)
Definition: loop.c:146
handle
static csh handle
Definition: test_arm_regression.c:16
UV_HANDLE_READING
@ UV_HANDLE_READING
Definition: uv-common.h:82
uv_handle_s
Definition: uv.h:441
UV_HANDLE_TTY_RAW
@ UV_HANDLE_TTY_RAW
Definition: uv-common.h:115
uv_loop_s
Definition: uv.h:1767
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
uv_dirent_type_t
uv_dirent_type_t
Definition: uv.h:1128
uv__udp_bind
int uv__udp_bind(uv_udp_t *handle, const struct sockaddr *addr, unsigned int addrlen, unsigned int flags)
Definition: unix/udp.c:503
stdint-msvc2008.h
google_benchmark.option
option
Definition: third_party/benchmark/bindings/python/google_benchmark/__init__.py:115
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
uv__fs_readdir_cleanup
void uv__fs_readdir_cleanup(uv_fs_t *req)
Definition: uv-common.c:694
cb
OPENSSL_EXPORT pem_password_cb * cb
Definition: pem.h:351
uv__realloc
void * uv__realloc(void *ptr, size_t size)
Definition: uv-common.c:96
UV_HANDLE_ACTIVE
@ UV_HANDLE_ACTIVE
Definition: uv-common.h:68
uv__udp_disconnect
int uv__udp_disconnect(uv_udp_t *handle)
Definition: unix/udp.c:628


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:49