uv: Upgrade to v0.11.17
[platform/upstream/nodejs.git] / deps / uv / include / uv.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 /* See http://nikhilm.github.com/uvbook/ for an introduction. */
23
24 #ifndef UV_H
25 #define UV_H
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 #ifdef _WIN32
31   /* Windows - set up dll import/export decorators. */
32 # if defined(BUILDING_UV_SHARED)
33     /* Building shared library. */
34 #   define UV_EXTERN __declspec(dllexport)
35 # elif defined(USING_UV_SHARED)
36     /* Using shared library. */
37 #   define UV_EXTERN __declspec(dllimport)
38 # else
39     /* Building static library. */
40 #   define UV_EXTERN /* nothing */
41 # endif
42 #elif __GNUC__ >= 4
43 # define UV_EXTERN __attribute__((visibility("default")))
44 #else
45 # define UV_EXTERN /* nothing */
46 #endif
47
48 #include "uv-errno.h"
49 #include <stddef.h>
50
51 #if defined(_MSC_VER) && _MSC_VER < 1600
52 # include "stdint-msvc2008.h"
53 #else
54 # include <stdint.h>
55 #endif
56
57 #if defined(_WIN32)
58 # include "uv-win.h"
59 #else
60 # include "uv-unix.h"
61 #endif
62
63 /* Expand this list if necessary. */
64 #define UV_ERRNO_MAP(XX)                                                      \
65   XX(E2BIG, "argument list too long")                                         \
66   XX(EACCES, "permission denied")                                             \
67   XX(EADDRINUSE, "address already in use")                                    \
68   XX(EADDRNOTAVAIL, "address not available")                                  \
69   XX(EAFNOSUPPORT, "address family not supported")                            \
70   XX(EAGAIN, "resource temporarily unavailable")                              \
71   XX(EAI_ADDRFAMILY, "address family not supported")                          \
72   XX(EAI_AGAIN, "temporary failure")                                          \
73   XX(EAI_BADFLAGS, "bad ai_flags value")                                      \
74   XX(EAI_BADHINTS, "invalid value for hints")                                 \
75   XX(EAI_CANCELED, "request canceled")                                        \
76   XX(EAI_FAIL, "permanent failure")                                           \
77   XX(EAI_FAMILY, "ai_family not supported")                                   \
78   XX(EAI_MEMORY, "out of memory")                                             \
79   XX(EAI_NODATA, "no address")                                                \
80   XX(EAI_NONAME, "unknown node or service")                                   \
81   XX(EAI_OVERFLOW, "argument buffer overflow")                                \
82   XX(EAI_PROTOCOL, "resolved protocol is unknown")                            \
83   XX(EAI_SERVICE, "service not available for socket type")                    \
84   XX(EAI_SOCKTYPE, "socket type not supported")                               \
85   XX(EAI_SYSTEM, "system error")                                              \
86   XX(EALREADY, "connection already in progress")                              \
87   XX(EBADF, "bad file descriptor")                                            \
88   XX(EBUSY, "resource busy or locked")                                        \
89   XX(ECANCELED, "operation canceled")                                         \
90   XX(ECHARSET, "invalid Unicode character")                                   \
91   XX(ECONNABORTED, "software caused connection abort")                        \
92   XX(ECONNREFUSED, "connection refused")                                      \
93   XX(ECONNRESET, "connection reset by peer")                                  \
94   XX(EDESTADDRREQ, "destination address required")                            \
95   XX(EEXIST, "file already exists")                                           \
96   XX(EFAULT, "bad address in system call argument")                           \
97   XX(EHOSTUNREACH, "host is unreachable")                                     \
98   XX(EINTR, "interrupted system call")                                        \
99   XX(EINVAL, "invalid argument")                                              \
100   XX(EIO, "i/o error")                                                        \
101   XX(EISCONN, "socket is already connected")                                  \
102   XX(EISDIR, "illegal operation on a directory")                              \
103   XX(ELOOP, "too many symbolic links encountered")                            \
104   XX(EMFILE, "too many open files")                                           \
105   XX(EMSGSIZE, "message too long")                                            \
106   XX(ENAMETOOLONG, "name too long")                                           \
107   XX(ENETDOWN, "network is down")                                             \
108   XX(ENETUNREACH, "network is unreachable")                                   \
109   XX(ENFILE, "file table overflow")                                           \
110   XX(ENOBUFS, "no buffer space available")                                    \
111   XX(ENODEV, "no such device")                                                \
112   XX(ENOENT, "no such file or directory")                                     \
113   XX(ENOMEM, "not enough memory")                                             \
114   XX(ENONET, "machine is not on the network")                                 \
115   XX(ENOSPC, "no space left on device")                                       \
116   XX(ENOSYS, "function not implemented")                                      \
117   XX(ENOTCONN, "socket is not connected")                                     \
118   XX(ENOTDIR, "not a directory")                                              \
119   XX(ENOTEMPTY, "directory not empty")                                        \
120   XX(ENOTSOCK, "socket operation on non-socket")                              \
121   XX(ENOTSUP, "operation not supported on socket")                            \
122   XX(EPERM, "operation not permitted")                                        \
123   XX(EPIPE, "broken pipe")                                                    \
124   XX(EPROTO, "protocol error")                                                \
125   XX(EPROTONOSUPPORT, "protocol not supported")                               \
126   XX(EPROTOTYPE, "protocol wrong type for socket")                            \
127   XX(EROFS, "read-only file system")                                          \
128   XX(ESHUTDOWN, "cannot send after transport endpoint shutdown")              \
129   XX(ESPIPE, "invalid seek")                                                  \
130   XX(ESRCH, "no such process")                                                \
131   XX(ETIMEDOUT, "connection timed out")                                       \
132   XX(EXDEV, "cross-device link not permitted")                                \
133   XX(UNKNOWN, "unknown error")                                                \
134   XX(EOF, "end of file")                                                      \
135
136 #define UV_HANDLE_TYPE_MAP(XX)                                                \
137   XX(ASYNC, async)                                                            \
138   XX(CHECK, check)                                                            \
139   XX(FS_EVENT, fs_event)                                                      \
140   XX(FS_POLL, fs_poll)                                                        \
141   XX(HANDLE, handle)                                                          \
142   XX(IDLE, idle)                                                              \
143   XX(NAMED_PIPE, pipe)                                                        \
144   XX(POLL, poll)                                                              \
145   XX(PREPARE, prepare)                                                        \
146   XX(PROCESS, process)                                                        \
147   XX(STREAM, stream)                                                          \
148   XX(TCP, tcp)                                                                \
149   XX(TIMER, timer)                                                            \
150   XX(TTY, tty)                                                                \
151   XX(UDP, udp)                                                                \
152   XX(SIGNAL, signal)                                                          \
153
154 #define UV_REQ_TYPE_MAP(XX)                                                   \
155   XX(REQ, req)                                                                \
156   XX(CONNECT, connect)                                                        \
157   XX(WRITE, write)                                                            \
158   XX(SHUTDOWN, shutdown)                                                      \
159   XX(UDP_SEND, udp_send)                                                      \
160   XX(FS, fs)                                                                  \
161   XX(WORK, work)                                                              \
162   XX(GETADDRINFO, getaddrinfo)                                                \
163
164 typedef enum {
165 #define XX(code, _) UV_ ## code = UV__ ## code,
166   UV_ERRNO_MAP(XX)
167 #undef XX
168   UV_ERRNO_MAX = UV__EOF - 1
169 } uv_errno_t;
170
171 typedef enum {
172   UV_UNKNOWN_HANDLE = 0,
173 #define XX(uc, lc) UV_##uc,
174   UV_HANDLE_TYPE_MAP(XX)
175 #undef XX
176   UV_FILE,
177   UV_HANDLE_TYPE_MAX
178 } uv_handle_type;
179
180 typedef enum {
181   UV_UNKNOWN_REQ = 0,
182 #define XX(uc, lc) UV_##uc,
183   UV_REQ_TYPE_MAP(XX)
184 #undef XX
185   UV_REQ_TYPE_PRIVATE
186   UV_REQ_TYPE_MAX
187 } uv_req_type;
188
189
190 /* Handle types. */
191 typedef struct uv_loop_s uv_loop_t;
192 typedef struct uv_handle_s uv_handle_t;
193 typedef struct uv_stream_s uv_stream_t;
194 typedef struct uv_tcp_s uv_tcp_t;
195 typedef struct uv_udp_s uv_udp_t;
196 typedef struct uv_pipe_s uv_pipe_t;
197 typedef struct uv_tty_s uv_tty_t;
198 typedef struct uv_poll_s uv_poll_t;
199 typedef struct uv_timer_s uv_timer_t;
200 typedef struct uv_prepare_s uv_prepare_t;
201 typedef struct uv_check_s uv_check_t;
202 typedef struct uv_idle_s uv_idle_t;
203 typedef struct uv_async_s uv_async_t;
204 typedef struct uv_process_s uv_process_t;
205 typedef struct uv_fs_event_s uv_fs_event_t;
206 typedef struct uv_fs_poll_s uv_fs_poll_t;
207 typedef struct uv_signal_s uv_signal_t;
208
209 /* Request types. */
210 typedef struct uv_req_s uv_req_t;
211 typedef struct uv_getaddrinfo_s uv_getaddrinfo_t;
212 typedef struct uv_shutdown_s uv_shutdown_t;
213 typedef struct uv_write_s uv_write_t;
214 typedef struct uv_connect_s uv_connect_t;
215 typedef struct uv_udp_send_s uv_udp_send_t;
216 typedef struct uv_fs_s uv_fs_t;
217 typedef struct uv_work_s uv_work_t;
218
219 /* None of the above. */
220 typedef struct uv_cpu_info_s uv_cpu_info_t;
221 typedef struct uv_interface_address_s uv_interface_address_t;
222
223
224 typedef enum {
225   UV_RUN_DEFAULT = 0,
226   UV_RUN_ONCE,
227   UV_RUN_NOWAIT
228 } uv_run_mode;
229
230
231 /*
232  * Returns the libuv version packed into a single integer. 8 bits are used for
233  * each component, with the patch number stored in the 8 least significant
234  * bits. E.g. for libuv 1.2.3 this would return 0x010203.
235  */
236 UV_EXTERN unsigned int uv_version(void);
237
238 /*
239  * Returns the libuv version number as a string. For non-release versions
240  * "-pre" is appended, so the version number could be "1.2.3-pre".
241  */
242 UV_EXTERN const char* uv_version_string(void);
243
244
245 /*
246  * This function must be called before any other functions in libuv.
247  *
248  * All functions besides uv_run() are non-blocking.
249  *
250  * All callbacks in libuv are made asynchronously. That is they are never
251  * made by the function that takes them as a parameter.
252  */
253 UV_EXTERN uv_loop_t* uv_loop_new(void);
254 UV_EXTERN void uv_loop_delete(uv_loop_t*);
255
256 /*
257  * Returns the default loop.
258  */
259 UV_EXTERN uv_loop_t* uv_default_loop(void);
260
261 /*
262  * This function runs the event loop. It will act differently depending on the
263  * specified mode:
264  *  - UV_RUN_DEFAULT: Runs the event loop until the reference count drops to
265  *    zero. Always returns zero.
266  *  - UV_RUN_ONCE: Poll for new events once. Note that this function blocks if
267  *    there are no pending events. Returns zero when done (no active handles
268  *    or requests left), or non-zero if more events are expected (meaning you
269  *    should run the event loop again sometime in the future).
270  *  - UV_RUN_NOWAIT: Poll for new events once but don't block if there are no
271  *    pending events.
272  */
273 UV_EXTERN int uv_run(uv_loop_t*, uv_run_mode mode);
274
275 /*
276  * This function checks whether the reference count, the number of active
277  * handles or requests left in the event loop, is non-zero.
278  */
279 UV_EXTERN int uv_loop_alive(const uv_loop_t* loop);
280
281 /*
282  * This function will stop the event loop by forcing uv_run to end
283  * as soon as possible, but not sooner than the next loop iteration.
284  * If this function was called before blocking for i/o, the loop won't
285  * block for i/o on this iteration.
286  */
287 UV_EXTERN void uv_stop(uv_loop_t*);
288
289 /*
290  * Manually modify the event loop's reference count. Useful if the user wants
291  * to have a handle or timeout that doesn't keep the loop alive.
292  */
293 UV_EXTERN void uv_ref(uv_handle_t*);
294 UV_EXTERN void uv_unref(uv_handle_t*);
295 UV_EXTERN int uv_has_ref(const uv_handle_t*);
296
297 /*
298  * Update the event loop's concept of "now". Libuv caches the current time
299  * at the start of the event loop tick in order to reduce the number of
300  * time-related system calls.
301  *
302  * You won't normally need to call this function unless you have callbacks
303  * that block the event loop for longer periods of time, where "longer" is
304  * somewhat subjective but probably on the order of a millisecond or more.
305  */
306 UV_EXTERN void uv_update_time(uv_loop_t*);
307
308 /*
309  * Return the current timestamp in milliseconds. The timestamp is cached at
310  * the start of the event loop tick, see |uv_update_time()| for details and
311  * rationale.
312  *
313  * The timestamp increases monotonically from some arbitrary point in time.
314  * Don't make assumptions about the starting point, you will only get
315  * disappointed.
316  *
317  * Use uv_hrtime() if you need sub-millisecond granularity.
318  */
319 UV_EXTERN uint64_t uv_now(uv_loop_t*);
320
321 /*
322  * Get backend file descriptor. Only kqueue, epoll and event ports are
323  * supported.
324  *
325  * This can be used in conjunction with `uv_run(loop, UV_RUN_NOWAIT)` to
326  * poll in one thread and run the event loop's event callbacks in another.
327  *
328  * Useful for embedding libuv's event loop in another event loop.
329  * See test/test-embed.c for an example.
330  *
331  * Note that embedding a kqueue fd in another kqueue pollset doesn't work on
332  * all platforms. It's not an error to add the fd but it never generates
333  * events.
334  */
335 UV_EXTERN int uv_backend_fd(const uv_loop_t*);
336
337 /*
338  * Get the poll timeout. The return value is in milliseconds, or -1 for no
339  * timeout.
340  */
341 UV_EXTERN int uv_backend_timeout(const uv_loop_t*);
342
343
344 /*
345  * Should prepare a buffer that libuv can use to read data into.
346  *
347  * `suggested_size` is a hint. Returning a buffer that is smaller is perfectly
348  * okay as long as `buf.len > 0`.
349  *
350  * If you return a buffer with `buf.len == 0`, libuv skips the read and calls
351  * your read or recv callback with nread=UV_ENOBUFS.
352  *
353  * Note that returning a zero-length buffer does not stop the handle, call
354  * uv_read_stop() or uv_udp_recv_stop() for that.
355  */
356 typedef void (*uv_alloc_cb)(uv_handle_t* handle,
357                             size_t suggested_size,
358                             uv_buf_t* buf);
359
360 /*
361  * `nread` is > 0 if there is data available, 0 if libuv is done reading for
362  * now, or < 0 on error.
363  *
364  * The callee is responsible for closing the stream when an error happens.
365  * Trying to read from the stream again is undefined.
366  *
367  * The callee is responsible for freeing the buffer, libuv does not reuse it.
368  * The buffer may be a null buffer (where buf->base=NULL and buf->len=0) on
369  * EOF or error.
370  */
371 typedef void (*uv_read_cb)(uv_stream_t* stream,
372                            ssize_t nread,
373                            const uv_buf_t* buf);
374
375 /*
376  * Just like the uv_read_cb except that if the pending parameter is true
377  * then you can use uv_accept() to pull the new handle into the process.
378  * If no handle is pending then pending will be UV_UNKNOWN_HANDLE.
379  */
380 typedef void (*uv_read2_cb)(uv_pipe_t* pipe,
381                             ssize_t nread,
382                             const uv_buf_t* buf,
383                             uv_handle_type pending);
384
385 typedef void (*uv_write_cb)(uv_write_t* req, int status);
386 typedef void (*uv_connect_cb)(uv_connect_t* req, int status);
387 typedef void (*uv_shutdown_cb)(uv_shutdown_t* req, int status);
388 typedef void (*uv_connection_cb)(uv_stream_t* server, int status);
389 typedef void (*uv_close_cb)(uv_handle_t* handle);
390 typedef void (*uv_poll_cb)(uv_poll_t* handle, int status, int events);
391 typedef void (*uv_timer_cb)(uv_timer_t* handle, int status);
392 /* TODO: do these really need a status argument? */
393 typedef void (*uv_async_cb)(uv_async_t* handle, int status);
394 typedef void (*uv_prepare_cb)(uv_prepare_t* handle, int status);
395 typedef void (*uv_check_cb)(uv_check_t* handle, int status);
396 typedef void (*uv_idle_cb)(uv_idle_t* handle, int status);
397 typedef void (*uv_exit_cb)(uv_process_t*, int64_t exit_status, int term_signal);
398 typedef void (*uv_walk_cb)(uv_handle_t* handle, void* arg);
399 typedef void (*uv_fs_cb)(uv_fs_t* req);
400 typedef void (*uv_work_cb)(uv_work_t* req);
401 typedef void (*uv_after_work_cb)(uv_work_t* req, int status);
402 typedef void (*uv_getaddrinfo_cb)(uv_getaddrinfo_t* req,
403                                   int status,
404                                   struct addrinfo* res);
405
406 typedef struct {
407   long tv_sec;
408   long tv_nsec;
409 } uv_timespec_t;
410
411
412 typedef struct {
413   uint64_t st_dev;
414   uint64_t st_mode;
415   uint64_t st_nlink;
416   uint64_t st_uid;
417   uint64_t st_gid;
418   uint64_t st_rdev;
419   uint64_t st_ino;
420   uint64_t st_size;
421   uint64_t st_blksize;
422   uint64_t st_blocks;
423   uint64_t st_flags;
424   uint64_t st_gen;
425   uv_timespec_t st_atim;
426   uv_timespec_t st_mtim;
427   uv_timespec_t st_ctim;
428   uv_timespec_t st_birthtim;
429 } uv_stat_t;
430
431
432 /*
433 * This will be called repeatedly after the uv_fs_event_t is initialized.
434 * If uv_fs_event_t was initialized with a directory the filename parameter
435 * will be a relative path to a file contained in the directory.
436 * The events parameter is an ORed mask of enum uv_fs_event elements.
437 */
438 typedef void (*uv_fs_event_cb)(uv_fs_event_t* handle, const char* filename,
439     int events, int status);
440
441 typedef void (*uv_fs_poll_cb)(uv_fs_poll_t* handle,
442                               int status,
443                               const uv_stat_t* prev,
444                               const uv_stat_t* curr);
445
446 typedef void (*uv_signal_cb)(uv_signal_t* handle, int signum);
447
448
449 typedef enum {
450   UV_LEAVE_GROUP = 0,
451   UV_JOIN_GROUP
452 } uv_membership;
453
454
455 /*
456  * Most functions return 0 on success or an error code < 0 on failure.
457  */
458 UV_EXTERN const char* uv_strerror(int err);
459 UV_EXTERN const char* uv_err_name(int err);
460
461
462 #define UV_REQ_FIELDS                                                         \
463   /* public */                                                                \
464   void* data;                                                                 \
465   /* read-only */                                                             \
466   uv_req_type type;                                                           \
467   /* private */                                                               \
468   void* active_queue[2];                                                      \
469   UV_REQ_PRIVATE_FIELDS                                                       \
470
471 /* Abstract base class of all requests. */
472 struct uv_req_s {
473   UV_REQ_FIELDS
474 };
475
476
477 /* Platform-specific request types */
478 UV_PRIVATE_REQ_TYPES
479
480
481 /*
482  * uv_shutdown_t is a subclass of uv_req_t
483  *
484  * Shutdown the outgoing (write) side of a duplex stream. It waits for
485  * pending write requests to complete. The handle should refer to a
486  * initialized stream. req should be an uninitialized shutdown request
487  * struct. The cb is called after shutdown is complete.
488  */
489 UV_EXTERN int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle,
490     uv_shutdown_cb cb);
491
492 struct uv_shutdown_s {
493   UV_REQ_FIELDS
494   uv_stream_t* handle;
495   uv_shutdown_cb cb;
496   UV_SHUTDOWN_PRIVATE_FIELDS
497 };
498
499
500 #define UV_HANDLE_FIELDS                                                      \
501   /* public */                                                                \
502   uv_close_cb close_cb;                                                       \
503   void* data;                                                                 \
504   /* read-only */                                                             \
505   uv_loop_t* loop;                                                            \
506   uv_handle_type type;                                                        \
507   /* private */                                                               \
508   void* handle_queue[2];                                                      \
509   UV_HANDLE_PRIVATE_FIELDS                                                    \
510
511 /* The abstract base class of all handles.  */
512 struct uv_handle_s {
513   UV_HANDLE_FIELDS
514 };
515
516 /*
517  * Returns size of various handle types, useful for FFI
518  * bindings to allocate correct memory without copying struct
519  * definitions
520  */
521 UV_EXTERN size_t uv_handle_size(uv_handle_type type);
522
523 /*
524  * Returns size of request types, useful for dynamic lookup with FFI
525  */
526 UV_EXTERN size_t uv_req_size(uv_req_type type);
527
528 /*
529  * Returns non-zero if the handle is active, zero if it's inactive.
530  *
531  * What "active" means depends on the type of handle:
532  *
533  *  - A uv_async_t handle is always active and cannot be deactivated, except
534  *    by closing it with uv_close().
535  *
536  *  - A uv_pipe_t, uv_tcp_t, uv_udp_t, etc. handle - basically any handle that
537  *    deals with I/O - is active when it is doing something that involves I/O,
538  *    like reading, writing, connecting, accepting new connections, etc.
539  *
540  *  - A uv_check_t, uv_idle_t, uv_timer_t, etc. handle is active when it has
541  *    been started with a call to uv_check_start(), uv_idle_start(), etc.
542  *
543  *      Rule of thumb: if a handle of type uv_foo_t has a uv_foo_start()
544  *      function, then it's active from the moment that function is called.
545  *      Likewise, uv_foo_stop() deactivates the handle again.
546  *
547  */
548 UV_EXTERN int uv_is_active(const uv_handle_t* handle);
549
550 /*
551  * Walk the list of open handles.
552  */
553 UV_EXTERN void uv_walk(uv_loop_t* loop, uv_walk_cb walk_cb, void* arg);
554
555
556 /*
557  * Request handle to be closed. close_cb will be called asynchronously after
558  * this call. This MUST be called on each handle before memory is released.
559  *
560  * Note that handles that wrap file descriptors are closed immediately but
561  * close_cb will still be deferred to the next iteration of the event loop.
562  * It gives you a chance to free up any resources associated with the handle.
563  *
564  * In-progress requests, like uv_connect_t or uv_write_t, are cancelled and
565  * have their callbacks called asynchronously with status=UV_ECANCELED.
566  */
567 UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb);
568
569
570 /*
571  * Constructor for uv_buf_t.
572  * Due to platform differences the user cannot rely on the ordering of the
573  * base and len members of the uv_buf_t struct. The user is responsible for
574  * freeing base after the uv_buf_t is done. Return struct passed by value.
575  */
576 UV_EXTERN uv_buf_t uv_buf_init(char* base, unsigned int len);
577
578
579 #define UV_STREAM_FIELDS                                                      \
580   /* number of bytes queued for writing */                                    \
581   size_t write_queue_size;                                                    \
582   uv_alloc_cb alloc_cb;                                                       \
583   uv_read_cb read_cb;                                                         \
584   uv_read2_cb read2_cb;                                                       \
585   /* private */                                                               \
586   UV_STREAM_PRIVATE_FIELDS
587
588 /*
589  * uv_stream_t is a subclass of uv_handle_t
590  *
591  * uv_stream is an abstract class.
592  *
593  * uv_stream_t is the parent class of uv_tcp_t, uv_pipe_t, uv_tty_t, and
594  * soon uv_file_t.
595  */
596 struct uv_stream_s {
597   UV_HANDLE_FIELDS
598   UV_STREAM_FIELDS
599 };
600
601 UV_EXTERN int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb);
602
603 /*
604  * This call is used in conjunction with uv_listen() to accept incoming
605  * connections. Call uv_accept after receiving a uv_connection_cb to accept
606  * the connection. Before calling uv_accept use uv_*_init() must be
607  * called on the client. Non-zero return value indicates an error.
608  *
609  * When the uv_connection_cb is called it is guaranteed that uv_accept will
610  * complete successfully the first time. If you attempt to use it more than
611  * once, it may fail. It is suggested to only call uv_accept once per
612  * uv_connection_cb call.
613  */
614 UV_EXTERN int uv_accept(uv_stream_t* server, uv_stream_t* client);
615
616 /*
617  * Read data from an incoming stream. The callback will be made several
618  * times until there is no more data to read or uv_read_stop is called.
619  * When we've reached EOF nread will be set to UV_EOF.
620  *
621  * When nread < 0, the buf parameter might not point to a valid buffer;
622  * in that case buf.len and buf.base are both set to 0.
623  *
624  * Note that nread might also be 0, which does *not* indicate an error or
625  * eof; it happens when libuv requested a buffer through the alloc callback
626  * but then decided that it didn't need that buffer.
627  */
628 UV_EXTERN int uv_read_start(uv_stream_t*, uv_alloc_cb alloc_cb,
629     uv_read_cb read_cb);
630
631 UV_EXTERN int uv_read_stop(uv_stream_t*);
632
633 /*
634  * Extended read methods for receiving handles over a pipe. The pipe must be
635  * initialized with ipc == 1.
636  */
637 UV_EXTERN int uv_read2_start(uv_stream_t*, uv_alloc_cb alloc_cb,
638     uv_read2_cb read_cb);
639
640
641 /*
642  * Write data to stream. Buffers are written in order. Example:
643  *
644  *   uv_buf_t a[] = {
645  *     { .base = "1", .len = 1 },
646  *     { .base = "2", .len = 1 }
647  *   };
648  *
649  *   uv_buf_t b[] = {
650  *     { .base = "3", .len = 1 },
651  *     { .base = "4", .len = 1 }
652  *   };
653  *
654  *   uv_write_t req1;
655  *   uv_write_t req2;
656  *
657  *   // writes "1234"
658  *   uv_write(&req1, stream, a, 2);
659  *   uv_write(&req2, stream, b, 2);
660  *
661  */
662 UV_EXTERN int uv_write(uv_write_t* req,
663                        uv_stream_t* handle,
664                        const uv_buf_t bufs[],
665                        unsigned int nbufs,
666                        uv_write_cb cb);
667
668 /*
669  * Extended write function for sending handles over a pipe. The pipe must be
670  * initialized with ipc == 1.
671  * send_handle must be a TCP socket or pipe, which is a server or a connection
672  * (listening or connected state).  Bound sockets or pipes will be assumed to
673  * be servers.
674  */
675 UV_EXTERN int uv_write2(uv_write_t* req,
676                         uv_stream_t* handle,
677                         const uv_buf_t bufs[],
678                         unsigned int nbufs,
679                         uv_stream_t* send_handle,
680                         uv_write_cb cb);
681
682 /*
683  * Same as `uv_write()`, but won't queue write request if it can't be completed
684  * immediately.
685  * Will return either:
686  * - positive number of bytes written
687  * - zero - if queued write is needed
688  * - negative error code
689  */
690 UV_EXTERN int uv_try_write(uv_stream_t* handle,
691                            const uv_buf_t bufs[],
692                            unsigned int nbufs);
693
694 /* uv_write_t is a subclass of uv_req_t */
695 struct uv_write_s {
696   UV_REQ_FIELDS
697   uv_write_cb cb;
698   uv_stream_t* send_handle;
699   uv_stream_t* handle;
700   UV_WRITE_PRIVATE_FIELDS
701 };
702
703
704 /*
705  * Used to determine whether a stream is readable or writable.
706  */
707 UV_EXTERN int uv_is_readable(const uv_stream_t* handle);
708 UV_EXTERN int uv_is_writable(const uv_stream_t* handle);
709
710
711 /*
712  * Enable or disable blocking mode for a stream.
713  *
714  * When blocking mode is enabled all writes complete synchronously. The
715  * interface remains unchanged otherwise, e.g. completion or failure of the
716  * operation will still be reported through a callback which is made
717  * asychronously.
718  *
719  * Relying too much on this API is not recommended. It is likely to change
720  * significantly in the future.
721  *
722  * On windows this currently works only for uv_pipe_t instances. On unix it
723  * works for tcp, pipe and tty instances. Be aware that changing the blocking
724  * mode on unix sets or clears the O_NONBLOCK bit. If you are sharing a handle
725  * with another process, the other process is affected by the change too,
726  * which can lead to unexpected results.
727  *
728  * Also libuv currently makes no ordering guarantee when the blocking mode
729  * is changed after write requests have already been submitted. Therefore it is
730  * recommended to set the blocking mode immediately after opening or creating
731  * the stream.
732  */
733 UV_EXTERN int uv_stream_set_blocking(uv_stream_t* handle, int blocking);
734
735
736 /*
737  * Used to determine whether a stream is closing or closed.
738  *
739  * N.B. is only valid between the initialization of the handle
740  *      and the arrival of the close callback, and cannot be used
741  *      to validate the handle.
742  */
743 UV_EXTERN int uv_is_closing(const uv_handle_t* handle);
744
745
746 /*
747  * uv_tcp_t is a subclass of uv_stream_t
748  *
749  * Represents a TCP stream or TCP server.
750  */
751 struct uv_tcp_s {
752   UV_HANDLE_FIELDS
753   UV_STREAM_FIELDS
754   UV_TCP_PRIVATE_FIELDS
755 };
756
757 UV_EXTERN int uv_tcp_init(uv_loop_t*, uv_tcp_t* handle);
758
759 /*
760  * Opens an existing file descriptor or SOCKET as a tcp handle.
761  */
762 UV_EXTERN int uv_tcp_open(uv_tcp_t* handle, uv_os_sock_t sock);
763
764 /* Enable/disable Nagle's algorithm. */
765 UV_EXTERN int uv_tcp_nodelay(uv_tcp_t* handle, int enable);
766
767 /*
768  * Enable/disable TCP keep-alive.
769  *
770  * `delay` is the initial delay in seconds, ignored when `enable` is zero.
771  */
772 UV_EXTERN int uv_tcp_keepalive(uv_tcp_t* handle,
773                                int enable,
774                                unsigned int delay);
775
776 /*
777  * Enable/disable simultaneous asynchronous accept requests that are
778  * queued by the operating system when listening for new tcp connections.
779  * This setting is used to tune a tcp server for the desired performance.
780  * Having simultaneous accepts can significantly improve the rate of
781  * accepting connections (which is why it is enabled by default) but
782  * may lead to uneven load distribution in multi-process setups.
783  */
784 UV_EXTERN int uv_tcp_simultaneous_accepts(uv_tcp_t* handle, int enable);
785
786 /*
787  * Bind the handle to an address and port.  `addr` should point to an
788  * initialized struct sockaddr_in or struct sockaddr_in6.
789  *
790  * When the port is already taken, you can expect to see an UV_EADDRINUSE
791  * error from either uv_tcp_bind(), uv_listen() or uv_tcp_connect().
792  *
793  * That is, a successful call to uv_tcp_bind() does not guarantee that
794  * the call to uv_listen() or uv_tcp_connect() will succeed as well.
795  */
796 UV_EXTERN int uv_tcp_bind(uv_tcp_t* handle, const struct sockaddr* addr);
797
798 UV_EXTERN int uv_tcp_getsockname(uv_tcp_t* handle, struct sockaddr* name,
799     int* namelen);
800 UV_EXTERN int uv_tcp_getpeername(uv_tcp_t* handle, struct sockaddr* name,
801     int* namelen);
802
803 /*
804  * Establish an IPv4 or IPv6 TCP connection.  Provide an initialized TCP handle
805  * and an uninitialized uv_connect_t*.  `addr` should point to an initialized
806  * struct sockaddr_in or struct sockaddr_in6.
807  *
808  * The callback is made when the connection has been established or when a
809  * connection error happened.
810  */
811 UV_EXTERN int uv_tcp_connect(uv_connect_t* req,
812                              uv_tcp_t* handle,
813                              const struct sockaddr* addr,
814                              uv_connect_cb cb);
815
816 /* uv_connect_t is a subclass of uv_req_t */
817 struct uv_connect_s {
818   UV_REQ_FIELDS
819   uv_connect_cb cb;
820   uv_stream_t* handle;
821   UV_CONNECT_PRIVATE_FIELDS
822 };
823
824
825 /*
826  * UDP support.
827  */
828
829 enum uv_udp_flags {
830   /* Disables dual stack mode. */
831   UV_UDP_IPV6ONLY = 1,
832   /*
833    * Indicates message was truncated because read buffer was too small. The
834    * remainder was discarded by the OS. Used in uv_udp_recv_cb.
835    */
836   UV_UDP_PARTIAL = 2
837 };
838
839 /*
840  * Called after a uv_udp_send() or uv_udp_send6(). status 0 indicates
841  * success otherwise error.
842  */
843 typedef void (*uv_udp_send_cb)(uv_udp_send_t* req, int status);
844
845 /*
846  * Callback that is invoked when a new UDP datagram is received.
847  *
848  *  handle  UDP handle.
849  *  nread   Number of bytes that have been received.
850  *          0 if there is no more data to read. You may
851  *          discard or repurpose the read buffer.
852  *          < 0 if a transmission error was detected.
853  *  buf     uv_buf_t with the received data.
854  *  addr    struct sockaddr_in or struct sockaddr_in6.
855  *          Valid for the duration of the callback only.
856  *  flags   One or more OR'ed UV_UDP_* constants.
857  *          Right now only UV_UDP_PARTIAL is used.
858  */
859 typedef void (*uv_udp_recv_cb)(uv_udp_t* handle,
860                                ssize_t nread,
861                                const uv_buf_t* buf,
862                                const struct sockaddr* addr,
863                                unsigned flags);
864
865 /* uv_udp_t is a subclass of uv_handle_t */
866 struct uv_udp_s {
867   UV_HANDLE_FIELDS
868   UV_UDP_PRIVATE_FIELDS
869 };
870
871 /* uv_udp_send_t is a subclass of uv_req_t */
872 struct uv_udp_send_s {
873   UV_REQ_FIELDS
874   uv_udp_t* handle;
875   uv_udp_send_cb cb;
876   UV_UDP_SEND_PRIVATE_FIELDS
877 };
878
879 /*
880  * Initialize a new UDP handle. The actual socket is created lazily.
881  * Returns 0 on success.
882  */
883 UV_EXTERN int uv_udp_init(uv_loop_t*, uv_udp_t* handle);
884
885 /*
886  * Opens an existing file descriptor or SOCKET as a udp handle.
887  *
888  * Unix only:
889  *  The only requirement of the sock argument is that it follows the
890  *  datagram contract (works in unconnected mode, supports sendmsg()/recvmsg(),
891  *  etc.). In other words, other datagram-type sockets like raw sockets or
892  *  netlink sockets can also be passed to this function.
893  *
894  * This sets the SO_REUSEPORT socket flag on the BSDs and OS X. On other
895  * UNIX platforms, it sets the SO_REUSEADDR flag.  What that means is that
896  * multiple threads or processes can bind to the same address without error
897  * (provided they all set the flag) but only the last one to bind will receive
898  * any traffic, in effect "stealing" the port from the previous listener.
899  * This behavior is something of an anomaly and may be replaced by an explicit
900  * opt-in mechanism in future versions of libuv.
901  */
902 UV_EXTERN int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock);
903
904 /*
905  * Bind to a IPv4 address and port.
906  *
907  * Arguments:
908  *  handle    UDP handle. Should have been initialized with `uv_udp_init`.
909  *  addr      struct sockaddr_in or struct sockaddr_in6 with the address and
910  *            port to bind to.
911  *  flags     Unused.
912  *
913  * Returns:
914  *  0 on success, or an error code < 0 on failure.
915  *
916  * This sets the SO_REUSEPORT socket flag on the BSDs and OS X. On other
917  * UNIX platforms, it sets the SO_REUSEADDR flag.  What that means is that
918  * multiple threads or processes can bind to the same address without error
919  * (provided they all set the flag) but only the last one to bind will receive
920  * any traffic, in effect "stealing" the port from the previous listener.
921  * This behavior is something of an anomaly and may be replaced by an explicit
922  * opt-in mechanism in future versions of libuv.
923  */
924 UV_EXTERN int uv_udp_bind(uv_udp_t* handle,
925                           const struct sockaddr* addr,
926                           unsigned int flags);
927
928 UV_EXTERN int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name,
929     int* namelen);
930
931 /*
932  * Set membership for a multicast address
933  *
934  * Arguments:
935  *  handle              UDP handle. Should have been initialized with
936  *                      `uv_udp_init`.
937  *  multicast_addr      multicast address to set membership for
938  *  interface_addr      interface address
939  *  membership          Should be UV_JOIN_GROUP or UV_LEAVE_GROUP
940  *
941  * Returns:
942  *  0 on success, or an error code < 0 on failure.
943  */
944 UV_EXTERN int uv_udp_set_membership(uv_udp_t* handle,
945     const char* multicast_addr, const char* interface_addr,
946     uv_membership membership);
947
948 /*
949  * Set IP multicast loop flag. Makes multicast packets loop back to
950  * local sockets.
951  *
952  * Arguments:
953  *  handle              UDP handle. Should have been initialized with
954  *                      `uv_udp_init`.
955  *  on                  1 for on, 0 for off
956  *
957  * Returns:
958  *  0 on success, or an error code < 0 on failure.
959  */
960 UV_EXTERN int uv_udp_set_multicast_loop(uv_udp_t* handle, int on);
961
962 /*
963  * Set the multicast ttl
964  *
965  * Arguments:
966  *  handle              UDP handle. Should have been initialized with
967  *                      `uv_udp_init`.
968  *  ttl                 1 through 255
969  *
970  * Returns:
971  *  0 on success, or an error code < 0 on failure.
972  */
973 UV_EXTERN int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl);
974
975 /*
976  * Set broadcast on or off
977  *
978  * Arguments:
979  *  handle              UDP handle. Should have been initialized with
980  *                      `uv_udp_init`.
981  *  on                  1 for on, 0 for off
982  *
983  * Returns:
984  *  0 on success, or an error code < 0 on failure.
985  */
986 UV_EXTERN int uv_udp_set_broadcast(uv_udp_t* handle, int on);
987
988 /*
989  * Set the time to live
990  *
991  * Arguments:
992  *  handle              UDP handle. Should have been initialized with
993  *                      `uv_udp_init`.
994  *  ttl                 1 through 255
995  *
996  * Returns:
997  *  0 on success, or an error code < 0 on failure.
998  */
999 UV_EXTERN int uv_udp_set_ttl(uv_udp_t* handle, int ttl);
1000
1001 /*
1002  * Send data. If the socket has not previously been bound with `uv_udp_bind`
1003  * or `uv_udp_bind6`, it is bound to 0.0.0.0 (the "all interfaces" address)
1004  * and a random port number.
1005  *
1006  * Arguments:
1007  *  req       UDP request handle. Need not be initialized.
1008  *  handle    UDP handle. Should have been initialized with `uv_udp_init`.
1009  *  bufs      List of buffers to send.
1010  *  nbufs     Number of buffers in `bufs`.
1011  *  addr      Address of the remote peer. See `uv_ip4_addr`.
1012  *  send_cb   Callback to invoke when the data has been sent out.
1013  *
1014  * Returns:
1015  *  0 on success, or an error code < 0 on failure.
1016  */
1017 UV_EXTERN int uv_udp_send(uv_udp_send_t* req,
1018                           uv_udp_t* handle,
1019                           const uv_buf_t bufs[],
1020                           unsigned int nbufs,
1021                           const struct sockaddr* addr,
1022                           uv_udp_send_cb send_cb);
1023
1024 /*
1025  * Receive data. If the socket has not previously been bound with `uv_udp_bind`
1026  * or `uv_udp_bind6`, it is bound to 0.0.0.0 (the "all interfaces" address)
1027  * and a random port number.
1028  *
1029  * Arguments:
1030  *  handle    UDP handle. Should have been initialized with `uv_udp_init`.
1031  *  alloc_cb  Callback to invoke when temporary storage is needed.
1032  *  recv_cb   Callback to invoke with received data.
1033  *
1034  * Returns:
1035  *  0 on success, or an error code < 0 on failure.
1036  */
1037 UV_EXTERN int uv_udp_recv_start(uv_udp_t* handle, uv_alloc_cb alloc_cb,
1038     uv_udp_recv_cb recv_cb);
1039
1040 /*
1041  * Stop listening for incoming datagrams.
1042  *
1043  * Arguments:
1044  *  handle    UDP handle. Should have been initialized with `uv_udp_init`.
1045  *
1046  * Returns:
1047  *  0 on success, or an error code < 0 on failure.
1048  */
1049 UV_EXTERN int uv_udp_recv_stop(uv_udp_t* handle);
1050
1051
1052 /*
1053  * uv_tty_t is a subclass of uv_stream_t
1054  *
1055  * Representing a stream for the console.
1056  */
1057 struct uv_tty_s {
1058   UV_HANDLE_FIELDS
1059   UV_STREAM_FIELDS
1060   UV_TTY_PRIVATE_FIELDS
1061 };
1062
1063 /*
1064  * Initialize a new TTY stream with the given file descriptor. Usually the
1065  * file descriptor will be
1066  *   0 = stdin
1067  *   1 = stdout
1068  *   2 = stderr
1069  * The last argument, readable, specifies if you plan on calling
1070  * uv_read_start with this stream. stdin is readable, stdout is not.
1071  *
1072  * TTY streams which are not readable have blocking writes.
1073  */
1074 UV_EXTERN int uv_tty_init(uv_loop_t*, uv_tty_t*, uv_file fd, int readable);
1075
1076 /*
1077  * Set mode. 0 for normal, 1 for raw.
1078  */
1079 UV_EXTERN int uv_tty_set_mode(uv_tty_t*, int mode);
1080
1081 /*
1082  * To be called when the program exits. Resets TTY settings to default
1083  * values for the next process to take over.
1084  *
1085  * This function is async signal-safe on UNIX platforms but can fail with error
1086  * code UV_EBUSY if you call it when execution is inside uv_tty_set_mode().
1087  */
1088 UV_EXTERN int uv_tty_reset_mode(void);
1089
1090 /*
1091  * Gets the current Window size. On success zero is returned.
1092  */
1093 UV_EXTERN int uv_tty_get_winsize(uv_tty_t*, int* width, int* height);
1094
1095 /*
1096  * Used to detect what type of stream should be used with a given file
1097  * descriptor. Usually this will be used during initialization to guess the
1098  * type of the stdio streams.
1099  * For isatty() functionality use this function and test for UV_TTY.
1100  */
1101 UV_EXTERN uv_handle_type uv_guess_handle(uv_file file);
1102
1103 /*
1104  * uv_pipe_t is a subclass of uv_stream_t
1105  *
1106  * Representing a pipe stream or pipe server. On Windows this is a Named
1107  * Pipe. On Unix this is a UNIX domain socket.
1108  */
1109 struct uv_pipe_s {
1110   UV_HANDLE_FIELDS
1111   UV_STREAM_FIELDS
1112   int ipc; /* non-zero if this pipe is used for passing handles */
1113   UV_PIPE_PRIVATE_FIELDS
1114 };
1115
1116 /*
1117  * Initialize a pipe. The last argument is a boolean to indicate if
1118  * this pipe will be used for handle passing between processes.
1119  */
1120 UV_EXTERN int uv_pipe_init(uv_loop_t*, uv_pipe_t* handle, int ipc);
1121
1122 /*
1123  * Opens an existing file descriptor or HANDLE as a pipe.
1124  */
1125 UV_EXTERN int uv_pipe_open(uv_pipe_t*, uv_file file);
1126
1127 /*
1128  * Bind the pipe to a file path (UNIX) or a name (Windows.)
1129  *
1130  * Paths on UNIX get truncated to `sizeof(sockaddr_un.sun_path)` bytes,
1131  * typically between 92 and 108 bytes.
1132  */
1133 UV_EXTERN int uv_pipe_bind(uv_pipe_t* handle, const char* name);
1134
1135 /*
1136  * Connect to the UNIX domain socket or the named pipe.
1137  *
1138  * Paths on UNIX get truncated to `sizeof(sockaddr_un.sun_path)` bytes,
1139  * typically between 92 and 108 bytes.
1140  */
1141 UV_EXTERN void uv_pipe_connect(uv_connect_t* req, uv_pipe_t* handle,
1142     const char* name, uv_connect_cb cb);
1143
1144 /*
1145  * This setting applies to Windows only.
1146  * Set the number of pending pipe instance handles when the pipe server
1147  * is waiting for connections.
1148  */
1149 UV_EXTERN void uv_pipe_pending_instances(uv_pipe_t* handle, int count);
1150
1151
1152 /*
1153  * uv_poll_t is a subclass of uv_handle_t.
1154  *
1155  * The uv_poll watcher is used to watch file descriptors for readability and
1156  * writability, similar to the purpose of poll(2).
1157  *
1158  * The purpose of uv_poll is to enable integrating external libraries that
1159  * rely on the event loop to signal it about the socket status changes, like
1160  * c-ares or libssh2. Using uv_poll_t for any other other purpose is not
1161  * recommended; uv_tcp_t, uv_udp_t, etc. provide an implementation that is
1162  * much faster and more scalable than what can be achieved with uv_poll_t,
1163  * especially on Windows.
1164  *
1165  * It is possible that uv_poll occasionally signals that a file descriptor is
1166  * readable or writable even when it isn't. The user should therefore always
1167  * be prepared to handle EAGAIN or equivalent when it attempts to read from or
1168  * write to the fd.
1169  *
1170  * It is not okay to have multiple active uv_poll watchers for the same socket.
1171  * This can cause libuv to busyloop or otherwise malfunction.
1172  *
1173  * The user should not close a file descriptor while it is being polled by an
1174  * active uv_poll watcher. This can cause the poll watcher to report an error,
1175  * but it might also start polling another socket. However the fd can be safely
1176  * closed immediately after a call to uv_poll_stop() or uv_close().
1177  *
1178  * On windows only sockets can be polled with uv_poll. On unix any file
1179  * descriptor that would be accepted by poll(2) can be used with uv_poll.
1180  */
1181 struct uv_poll_s {
1182   UV_HANDLE_FIELDS
1183   uv_poll_cb poll_cb;
1184   UV_POLL_PRIVATE_FIELDS
1185 };
1186
1187 enum uv_poll_event {
1188   UV_READABLE = 1,
1189   UV_WRITABLE = 2
1190 };
1191
1192 /* Initialize the poll watcher using a file descriptor. */
1193 UV_EXTERN int uv_poll_init(uv_loop_t* loop, uv_poll_t* handle, int fd);
1194
1195 /* Initialize the poll watcher using a socket descriptor. On unix this is */
1196 /* identical to uv_poll_init. On windows it takes a SOCKET handle. */
1197 UV_EXTERN int uv_poll_init_socket(uv_loop_t* loop, uv_poll_t* handle,
1198     uv_os_sock_t socket);
1199
1200 /*
1201  * Starts polling the file descriptor. `events` is a bitmask consisting made up
1202  * of UV_READABLE and UV_WRITABLE. As soon as an event is detected the callback
1203  * will be called with `status` set to 0, and the detected events set en the
1204  * `events` field.
1205  *
1206  * If an error happens while polling status, `status` < 0 and corresponds
1207  * with one of the UV_E* error codes. The user should not close the socket
1208  * while uv_poll is active. If the user does that anyway, the callback *may*
1209  * be called reporting an error status, but this is not guaranteed.
1210  *
1211  * Calling uv_poll_start on an uv_poll watcher that is already active is fine.
1212  * Doing so will update the events mask that is being watched for.
1213  */
1214 UV_EXTERN int uv_poll_start(uv_poll_t* handle, int events, uv_poll_cb cb);
1215
1216 /* Stops polling the file descriptor. */
1217 UV_EXTERN int uv_poll_stop(uv_poll_t* handle);
1218
1219
1220 /*
1221  * uv_prepare_t is a subclass of uv_handle_t.
1222  *
1223  * Every active prepare handle gets its callback called exactly once per loop
1224  * iteration, just before the system blocks to wait for completed i/o.
1225  */
1226 struct uv_prepare_s {
1227   UV_HANDLE_FIELDS
1228   UV_PREPARE_PRIVATE_FIELDS
1229 };
1230
1231 UV_EXTERN int uv_prepare_init(uv_loop_t*, uv_prepare_t* prepare);
1232
1233 UV_EXTERN int uv_prepare_start(uv_prepare_t* prepare, uv_prepare_cb cb);
1234
1235 UV_EXTERN int uv_prepare_stop(uv_prepare_t* prepare);
1236
1237
1238 /*
1239  * uv_check_t is a subclass of uv_handle_t.
1240  *
1241  * Every active check handle gets its callback called exactly once per loop
1242  * iteration, just after the system returns from blocking.
1243  */
1244 struct uv_check_s {
1245   UV_HANDLE_FIELDS
1246   UV_CHECK_PRIVATE_FIELDS
1247 };
1248
1249 UV_EXTERN int uv_check_init(uv_loop_t*, uv_check_t* check);
1250
1251 UV_EXTERN int uv_check_start(uv_check_t* check, uv_check_cb cb);
1252
1253 UV_EXTERN int uv_check_stop(uv_check_t* check);
1254
1255
1256 /*
1257  * uv_idle_t is a subclass of uv_handle_t.
1258  *
1259  * Every active idle handle gets its callback called repeatedly until it is
1260  * stopped. This happens after all other types of callbacks are processed.
1261  * When there are multiple "idle" handles active, their callbacks are called
1262  * in turn.
1263  */
1264 struct uv_idle_s {
1265   UV_HANDLE_FIELDS
1266   UV_IDLE_PRIVATE_FIELDS
1267 };
1268
1269 UV_EXTERN int uv_idle_init(uv_loop_t*, uv_idle_t* idle);
1270
1271 UV_EXTERN int uv_idle_start(uv_idle_t* idle, uv_idle_cb cb);
1272
1273 UV_EXTERN int uv_idle_stop(uv_idle_t* idle);
1274
1275
1276 /*
1277  * uv_async_t is a subclass of uv_handle_t.
1278  *
1279  * uv_async_send wakes up the event loop and calls the async handle's callback.
1280  * There is no guarantee that every uv_async_send call leads to exactly one
1281  * invocation of the callback; the only guarantee is that the callback function
1282  * is called at least once after the call to async_send. Unlike all other
1283  * libuv functions, uv_async_send can be called from another thread.
1284  */
1285 struct uv_async_s {
1286   UV_HANDLE_FIELDS
1287   UV_ASYNC_PRIVATE_FIELDS
1288 };
1289
1290 /*
1291  * Initialize the uv_async_t handle. A NULL callback is allowed.
1292  *
1293  * Note that uv_async_init(), unlike other libuv functions, immediately
1294  * starts the handle. To stop the handle again, close it with uv_close().
1295  */
1296 UV_EXTERN int uv_async_init(uv_loop_t*, uv_async_t* async,
1297     uv_async_cb async_cb);
1298
1299 /*
1300  * This can be called from other threads to wake up a libuv thread.
1301  *
1302  * libuv is single threaded at the moment.
1303  */
1304 UV_EXTERN int uv_async_send(uv_async_t* async);
1305
1306
1307 /*
1308  * uv_timer_t is a subclass of uv_handle_t.
1309  *
1310  * Used to get woken up at a specified time in the future.
1311  */
1312 struct uv_timer_s {
1313   UV_HANDLE_FIELDS
1314   UV_TIMER_PRIVATE_FIELDS
1315 };
1316
1317 UV_EXTERN int uv_timer_init(uv_loop_t*, uv_timer_t* handle);
1318
1319 /*
1320  * Start the timer. `timeout` and `repeat` are in milliseconds.
1321  *
1322  * If timeout is zero, the callback fires on the next tick of the event loop.
1323  *
1324  * If repeat is non-zero, the callback fires first after timeout milliseconds
1325  * and then repeatedly after repeat milliseconds.
1326  */
1327 UV_EXTERN int uv_timer_start(uv_timer_t* handle,
1328                              uv_timer_cb cb,
1329                              uint64_t timeout,
1330                              uint64_t repeat);
1331
1332 UV_EXTERN int uv_timer_stop(uv_timer_t* handle);
1333
1334 /*
1335  * Stop the timer, and if it is repeating restart it using the repeat value
1336  * as the timeout. If the timer has never been started before it returns
1337  * UV_EINVAL.
1338  */
1339 UV_EXTERN int uv_timer_again(uv_timer_t* handle);
1340
1341 /*
1342  * Set the repeat value in milliseconds. Note that if the repeat value is set
1343  * from a timer callback it does not immediately take effect. If the timer was
1344  * non-repeating before, it will have been stopped. If it was repeating, then
1345  * the old repeat value will have been used to schedule the next timeout.
1346  */
1347 UV_EXTERN void uv_timer_set_repeat(uv_timer_t* handle, uint64_t repeat);
1348
1349 UV_EXTERN uint64_t uv_timer_get_repeat(const uv_timer_t* handle);
1350
1351
1352 /*
1353  * uv_getaddrinfo_t is a subclass of uv_req_t
1354  *
1355  * Request object for uv_getaddrinfo.
1356  */
1357 struct uv_getaddrinfo_s {
1358   UV_REQ_FIELDS
1359   /* read-only */
1360   uv_loop_t* loop;
1361   UV_GETADDRINFO_PRIVATE_FIELDS
1362 };
1363
1364
1365 /*
1366  * Asynchronous getaddrinfo(3).
1367  *
1368  * Either node or service may be NULL but not both.
1369  *
1370  * hints is a pointer to a struct addrinfo with additional address type
1371  * constraints, or NULL. Consult `man -s 3 getaddrinfo` for details.
1372  *
1373  * Returns 0 on success or an error code < 0 on failure.
1374  *
1375  * If successful, your callback gets called sometime in the future with the
1376  * lookup result, which is either:
1377  *
1378  *  a) err == 0, the res argument points to a valid struct addrinfo, or
1379  *  b) err < 0, the res argument is NULL. See the UV_EAI_* constants.
1380  *
1381  * Call uv_freeaddrinfo() to free the addrinfo structure.
1382  */
1383 UV_EXTERN int uv_getaddrinfo(uv_loop_t* loop,
1384                              uv_getaddrinfo_t* req,
1385                              uv_getaddrinfo_cb getaddrinfo_cb,
1386                              const char* node,
1387                              const char* service,
1388                              const struct addrinfo* hints);
1389
1390 /*
1391  * Free the struct addrinfo. Passing NULL is allowed and is a no-op.
1392  */
1393 UV_EXTERN void uv_freeaddrinfo(struct addrinfo* ai);
1394
1395
1396 /* uv_spawn() options */
1397 typedef enum {
1398   UV_IGNORE         = 0x00,
1399   UV_CREATE_PIPE    = 0x01,
1400   UV_INHERIT_FD     = 0x02,
1401   UV_INHERIT_STREAM = 0x04,
1402
1403   /* When UV_CREATE_PIPE is specified, UV_READABLE_PIPE and UV_WRITABLE_PIPE
1404    * determine the direction of flow, from the child process' perspective. Both
1405    * flags may be specified to create a duplex data stream.
1406    */
1407   UV_READABLE_PIPE  = 0x10,
1408   UV_WRITABLE_PIPE  = 0x20
1409 } uv_stdio_flags;
1410
1411 typedef struct uv_stdio_container_s {
1412   uv_stdio_flags flags;
1413
1414   union {
1415     uv_stream_t* stream;
1416     int fd;
1417   } data;
1418 } uv_stdio_container_t;
1419
1420 typedef struct uv_process_options_s {
1421   uv_exit_cb exit_cb; /* Called after the process exits. */
1422   const char* file; /* Path to program to execute. */
1423   /*
1424    * Command line arguments. args[0] should be the path to the program. On
1425    * Windows this uses CreateProcess which concatenates the arguments into a
1426    * string this can cause some strange errors. See the note at
1427    * windows_verbatim_arguments.
1428    */
1429   char** args;
1430   /*
1431    * This will be set as the environ variable in the subprocess. If this is
1432    * NULL then the parents environ will be used.
1433    */
1434   char** env;
1435   /*
1436    * If non-null this represents a directory the subprocess should execute
1437    * in. Stands for current working directory.
1438    */
1439   const char* cwd;
1440   /*
1441    * Various flags that control how uv_spawn() behaves. See the definition of
1442    * `enum uv_process_flags` below.
1443    */
1444   unsigned int flags;
1445   /*
1446    * The `stdio` field points to an array of uv_stdio_container_t structs that
1447    * describe the file descriptors that will be made available to the child
1448    * process. The convention is that stdio[0] points to stdin, fd 1 is used for
1449    * stdout, and fd 2 is stderr.
1450    *
1451    * Note that on windows file descriptors greater than 2 are available to the
1452    * child process only if the child processes uses the MSVCRT runtime.
1453    */
1454   int stdio_count;
1455   uv_stdio_container_t* stdio;
1456   /*
1457    * Libuv can change the child process' user/group id. This happens only when
1458    * the appropriate bits are set in the flags fields. This is not supported on
1459    * windows; uv_spawn() will fail and set the error to UV_ENOTSUP.
1460    */
1461   uv_uid_t uid;
1462   uv_gid_t gid;
1463 } uv_process_options_t;
1464
1465 /*
1466  * These are the flags that can be used for the uv_process_options.flags field.
1467  */
1468 enum uv_process_flags {
1469   /*
1470    * Set the child process' user id. The user id is supplied in the `uid` field
1471    * of the options struct. This does not work on windows; setting this flag
1472    * will cause uv_spawn() to fail.
1473    */
1474   UV_PROCESS_SETUID = (1 << 0),
1475   /*
1476    * Set the child process' group id. The user id is supplied in the `gid`
1477    * field of the options struct. This does not work on windows; setting this
1478    * flag will cause uv_spawn() to fail.
1479    */
1480   UV_PROCESS_SETGID = (1 << 1),
1481   /*
1482    * Do not wrap any arguments in quotes, or perform any other escaping, when
1483    * converting the argument list into a command line string. This option is
1484    * only meaningful on Windows systems. On unix it is silently ignored.
1485    */
1486   UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS = (1 << 2),
1487   /*
1488    * Spawn the child process in a detached state - this will make it a process
1489    * group leader, and will effectively enable the child to keep running after
1490    * the parent exits.  Note that the child process will still keep the
1491    * parent's event loop alive unless the parent process calls uv_unref() on
1492    * the child's process handle.
1493    */
1494   UV_PROCESS_DETACHED = (1 << 3),
1495   /*
1496    * Hide the subprocess console window that would normally be created. This
1497    * option is only meaningful on Windows systems. On unix it is silently
1498    * ignored.
1499    */
1500   UV_PROCESS_WINDOWS_HIDE = (1 << 4)
1501 };
1502
1503 /*
1504  * uv_process_t is a subclass of uv_handle_t
1505  */
1506 struct uv_process_s {
1507   UV_HANDLE_FIELDS
1508   uv_exit_cb exit_cb;
1509   int pid;
1510   UV_PROCESS_PRIVATE_FIELDS
1511 };
1512
1513 /*
1514  * Initializes the uv_process_t and starts the process. If the process is
1515  * successfully spawned, then this function will return 0. Otherwise, the
1516  * negative error code corresponding to the reason it couldn't spawn is
1517  * returned.
1518  *
1519  * Possible reasons for failing to spawn would include (but not be limited to)
1520  * the file to execute not existing, not having permissions to use the setuid or
1521  * setgid specified, or not having enough memory to allocate for the new
1522  * process.
1523  */
1524 UV_EXTERN int uv_spawn(uv_loop_t* loop,
1525                        uv_process_t* handle,
1526                        const uv_process_options_t* options);
1527
1528
1529 /*
1530  * Kills the process with the specified signal. The user must still
1531  * call uv_close on the process.
1532  */
1533 UV_EXTERN int uv_process_kill(uv_process_t*, int signum);
1534
1535
1536 /* Kills the process with the specified signal. */
1537 UV_EXTERN int uv_kill(int pid, int signum);
1538
1539
1540 /*
1541  * uv_work_t is a subclass of uv_req_t
1542  */
1543 struct uv_work_s {
1544   UV_REQ_FIELDS
1545   uv_loop_t* loop;
1546   uv_work_cb work_cb;
1547   uv_after_work_cb after_work_cb;
1548   UV_WORK_PRIVATE_FIELDS
1549 };
1550
1551 /* Queues a work request to execute asynchronously on the thread pool. */
1552 UV_EXTERN int uv_queue_work(uv_loop_t* loop, uv_work_t* req,
1553     uv_work_cb work_cb, uv_after_work_cb after_work_cb);
1554
1555 /* Cancel a pending request. Fails if the request is executing or has finished
1556  * executing.
1557  *
1558  * Returns 0 on success, or an error code < 0 on failure.
1559  *
1560  * Only cancellation of uv_fs_t, uv_getaddrinfo_t and uv_work_t requests is
1561  * currently supported.
1562  *
1563  * Cancelled requests have their callbacks invoked some time in the future.
1564  * It's _not_ safe to free the memory associated with the request until your
1565  * callback is called.
1566  *
1567  * Here is how cancellation is reported to your callback:
1568  *
1569  * - A uv_fs_t request has its req->result field set to UV_ECANCELED.
1570  *
1571  * - A uv_work_t or uv_getaddrinfo_t request has its callback invoked with
1572  *   status == UV_ECANCELED.
1573  *
1574  * This function is currently only implemented on UNIX platforms. On Windows,
1575  * it always returns UV_ENOSYS.
1576  */
1577 UV_EXTERN int uv_cancel(uv_req_t* req);
1578
1579
1580 struct uv_cpu_info_s {
1581   char* model;
1582   int speed;
1583   struct uv_cpu_times_s {
1584     uint64_t user;
1585     uint64_t nice;
1586     uint64_t sys;
1587     uint64_t idle;
1588     uint64_t irq;
1589   } cpu_times;
1590 };
1591
1592 struct uv_interface_address_s {
1593   char* name;
1594   char phys_addr[6];
1595   int is_internal;
1596   union {
1597     struct sockaddr_in address4;
1598     struct sockaddr_in6 address6;
1599   } address;
1600   union {
1601     struct sockaddr_in netmask4;
1602     struct sockaddr_in6 netmask6;
1603   } netmask;
1604 };
1605
1606 UV_EXTERN char** uv_setup_args(int argc, char** argv);
1607 UV_EXTERN int uv_get_process_title(char* buffer, size_t size);
1608 UV_EXTERN int uv_set_process_title(const char* title);
1609 UV_EXTERN int uv_resident_set_memory(size_t* rss);
1610 UV_EXTERN int uv_uptime(double* uptime);
1611
1612 /*
1613  * This allocates cpu_infos array, and sets count.  The array
1614  * is freed using uv_free_cpu_info().
1615  */
1616 UV_EXTERN int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count);
1617 UV_EXTERN void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count);
1618
1619 /*
1620  * This allocates addresses array, and sets count.  The array
1621  * is freed using uv_free_interface_addresses().
1622  */
1623 UV_EXTERN int uv_interface_addresses(uv_interface_address_t** addresses,
1624   int* count);
1625 UV_EXTERN void uv_free_interface_addresses(uv_interface_address_t* addresses,
1626   int count);
1627
1628 /*
1629  * File System Methods.
1630  *
1631  * The uv_fs_* functions execute a blocking system call asynchronously (in a
1632  * thread pool) and call the specified callback in the specified loop after
1633  * completion. If the user gives NULL as the callback the blocking system
1634  * call will be called synchronously. req should be a pointer to an
1635  * uninitialized uv_fs_t object.
1636  *
1637  * uv_fs_req_cleanup() must be called after completion of the uv_fs_
1638  * function to free any internal memory allocations associated with the
1639  * request.
1640  */
1641
1642 typedef enum {
1643   UV_FS_UNKNOWN = -1,
1644   UV_FS_CUSTOM,
1645   UV_FS_OPEN,
1646   UV_FS_CLOSE,
1647   UV_FS_READ,
1648   UV_FS_WRITE,
1649   UV_FS_SENDFILE,
1650   UV_FS_STAT,
1651   UV_FS_LSTAT,
1652   UV_FS_FSTAT,
1653   UV_FS_FTRUNCATE,
1654   UV_FS_UTIME,
1655   UV_FS_FUTIME,
1656   UV_FS_CHMOD,
1657   UV_FS_FCHMOD,
1658   UV_FS_FSYNC,
1659   UV_FS_FDATASYNC,
1660   UV_FS_UNLINK,
1661   UV_FS_RMDIR,
1662   UV_FS_MKDIR,
1663   UV_FS_RENAME,
1664   UV_FS_READDIR,
1665   UV_FS_LINK,
1666   UV_FS_SYMLINK,
1667   UV_FS_READLINK,
1668   UV_FS_CHOWN,
1669   UV_FS_FCHOWN
1670 } uv_fs_type;
1671
1672 /* uv_fs_t is a subclass of uv_req_t */
1673 struct uv_fs_s {
1674   UV_REQ_FIELDS
1675   uv_fs_type fs_type;
1676   uv_loop_t* loop;
1677   uv_fs_cb cb;
1678   ssize_t result;
1679   void* ptr;
1680   const char* path;
1681   uv_stat_t statbuf;  /* Stores the result of uv_fs_stat and uv_fs_fstat. */
1682   UV_FS_PRIVATE_FIELDS
1683 };
1684
1685 UV_EXTERN void uv_fs_req_cleanup(uv_fs_t* req);
1686
1687 UV_EXTERN int uv_fs_close(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1688     uv_fs_cb cb);
1689
1690 UV_EXTERN int uv_fs_open(uv_loop_t* loop, uv_fs_t* req, const char* path,
1691     int flags, int mode, uv_fs_cb cb);
1692
1693 UV_EXTERN int uv_fs_read(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1694     void* buf, size_t length, int64_t offset, uv_fs_cb cb);
1695
1696 UV_EXTERN int uv_fs_unlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
1697     uv_fs_cb cb);
1698
1699 UV_EXTERN int uv_fs_write(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1700     const void* buf, size_t length, int64_t offset, uv_fs_cb cb);
1701
1702 UV_EXTERN int uv_fs_mkdir(uv_loop_t* loop, uv_fs_t* req, const char* path,
1703     int mode, uv_fs_cb cb);
1704
1705 UV_EXTERN int uv_fs_rmdir(uv_loop_t* loop, uv_fs_t* req, const char* path,
1706     uv_fs_cb cb);
1707
1708 UV_EXTERN int uv_fs_readdir(uv_loop_t* loop, uv_fs_t* req,
1709     const char* path, int flags, uv_fs_cb cb);
1710
1711 UV_EXTERN int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path,
1712     uv_fs_cb cb);
1713
1714 UV_EXTERN int uv_fs_fstat(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1715     uv_fs_cb cb);
1716
1717 UV_EXTERN int uv_fs_rename(uv_loop_t* loop, uv_fs_t* req, const char* path,
1718     const char* new_path, uv_fs_cb cb);
1719
1720 UV_EXTERN int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1721     uv_fs_cb cb);
1722
1723 UV_EXTERN int uv_fs_fdatasync(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1724     uv_fs_cb cb);
1725
1726 UV_EXTERN int uv_fs_ftruncate(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1727     int64_t offset, uv_fs_cb cb);
1728
1729 UV_EXTERN int uv_fs_sendfile(uv_loop_t* loop, uv_fs_t* req, uv_file out_fd,
1730     uv_file in_fd, int64_t in_offset, size_t length, uv_fs_cb cb);
1731
1732 UV_EXTERN int uv_fs_chmod(uv_loop_t* loop, uv_fs_t* req, const char* path,
1733     int mode, uv_fs_cb cb);
1734
1735 UV_EXTERN int uv_fs_utime(uv_loop_t* loop, uv_fs_t* req, const char* path,
1736     double atime, double mtime, uv_fs_cb cb);
1737
1738 UV_EXTERN int uv_fs_futime(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1739     double atime, double mtime, uv_fs_cb cb);
1740
1741 UV_EXTERN int uv_fs_lstat(uv_loop_t* loop, uv_fs_t* req, const char* path,
1742     uv_fs_cb cb);
1743
1744 UV_EXTERN int uv_fs_link(uv_loop_t* loop, uv_fs_t* req, const char* path,
1745     const char* new_path, uv_fs_cb cb);
1746
1747 /*
1748  * This flag can be used with uv_fs_symlink on Windows
1749  * to specify whether path argument points to a directory.
1750  */
1751 #define UV_FS_SYMLINK_DIR          0x0001
1752
1753 /*
1754  * This flag can be used with uv_fs_symlink on Windows
1755  * to specify whether the symlink is to be created using junction points.
1756  */
1757 #define UV_FS_SYMLINK_JUNCTION     0x0002
1758
1759 UV_EXTERN int uv_fs_symlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
1760     const char* new_path, int flags, uv_fs_cb cb);
1761
1762 UV_EXTERN int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
1763     uv_fs_cb cb);
1764
1765 UV_EXTERN int uv_fs_fchmod(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1766     int mode, uv_fs_cb cb);
1767
1768 UV_EXTERN int uv_fs_chown(uv_loop_t* loop, uv_fs_t* req, const char* path,
1769     uv_uid_t uid, uv_gid_t gid, uv_fs_cb cb);
1770
1771 UV_EXTERN int uv_fs_fchown(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1772     uv_uid_t uid, uv_gid_t gid, uv_fs_cb cb);
1773
1774
1775 enum uv_fs_event {
1776   UV_RENAME = 1,
1777   UV_CHANGE = 2
1778 };
1779
1780
1781 struct uv_fs_event_s {
1782   UV_HANDLE_FIELDS
1783   char* filename;
1784   UV_FS_EVENT_PRIVATE_FIELDS
1785 };
1786
1787
1788 /*
1789  * uv_fs_stat() based polling file watcher.
1790  */
1791 struct uv_fs_poll_s {
1792   UV_HANDLE_FIELDS
1793   /* Private, don't touch. */
1794   void* poll_ctx;
1795 };
1796
1797 UV_EXTERN int uv_fs_poll_init(uv_loop_t* loop, uv_fs_poll_t* handle);
1798
1799 /*
1800  * Check the file at `path` for changes every `interval` milliseconds.
1801  *
1802  * Your callback is invoked with `status < 0` if `path` does not exist
1803  * or is inaccessible. The watcher is *not* stopped but your callback is
1804  * not called again until something changes (e.g. when the file is created
1805  * or the error reason changes).
1806  *
1807  * When `status == 0`, your callback receives pointers to the old and new
1808  * `uv_stat_t` structs. They are valid for the duration of the callback
1809  * only!
1810  *
1811  * For maximum portability, use multi-second intervals. Sub-second intervals
1812  * will not detect all changes on many file systems.
1813  */
1814 UV_EXTERN int uv_fs_poll_start(uv_fs_poll_t* handle,
1815                                uv_fs_poll_cb poll_cb,
1816                                const char* path,
1817                                unsigned int interval);
1818
1819 UV_EXTERN int uv_fs_poll_stop(uv_fs_poll_t* handle);
1820
1821
1822 /*
1823  * UNIX signal handling on a per-event loop basis. The implementation is not
1824  * ultra efficient so don't go creating a million event loops with a million
1825  * signal watchers.
1826  *
1827  * Note to Linux users: SIGRT0 and SIGRT1 (signals 32 and 33) are used by the
1828  * NPTL pthreads library to manage threads. Installing watchers for those
1829  * signals will lead to unpredictable behavior and is strongly discouraged.
1830  * Future versions of libuv may simply reject them.
1831  *
1832  * Some signal support is available on Windows:
1833  *
1834  *   SIGINT is normally delivered when the user presses CTRL+C. However, like
1835  *   on Unix, it is not generated when terminal raw mode is enabled.
1836  *
1837  *   SIGBREAK is delivered when the user pressed CTRL+BREAK.
1838  *
1839  *   SIGHUP is generated when the user closes the console window. On SIGHUP the
1840  *   program is given approximately 10 seconds to perform cleanup. After that
1841  *   Windows will unconditionally terminate it.
1842  *
1843  *   SIGWINCH is raised whenever libuv detects that the console has been
1844  *   resized. SIGWINCH is emulated by libuv when the program uses an uv_tty_t
1845  *   handle to write to the console. SIGWINCH may not always be delivered in a
1846  *   timely manner; libuv will only detect size changes when the cursor is
1847  *   being moved. When a readable uv_tty_handle is used in raw mode, resizing
1848  *   the console buffer will also trigger a SIGWINCH signal.
1849  *
1850  * Watchers for other signals can be successfully created, but these signals
1851  * are never generated. These signals are: SIGILL, SIGABRT, SIGFPE, SIGSEGV,
1852  * SIGTERM and SIGKILL.
1853  *
1854  * Note that calls to raise() or abort() to programmatically raise a signal are
1855  * not detected by libuv; these will not trigger a signal watcher.
1856  */
1857 struct uv_signal_s {
1858   UV_HANDLE_FIELDS
1859   uv_signal_cb signal_cb;
1860   int signum;
1861   UV_SIGNAL_PRIVATE_FIELDS
1862 };
1863
1864 UV_EXTERN int uv_signal_init(uv_loop_t* loop, uv_signal_t* handle);
1865
1866 UV_EXTERN int uv_signal_start(uv_signal_t* handle,
1867                               uv_signal_cb signal_cb,
1868                               int signum);
1869
1870 UV_EXTERN int uv_signal_stop(uv_signal_t* handle);
1871
1872
1873 /*
1874  * Gets load average.
1875  * See: http://en.wikipedia.org/wiki/Load_(computing)
1876  * Returns [0,0,0] on Windows.
1877  */
1878 UV_EXTERN void uv_loadavg(double avg[3]);
1879
1880
1881 /*
1882  * Flags to be passed to uv_fs_event_start.
1883  */
1884 enum uv_fs_event_flags {
1885   /*
1886    * By default, if the fs event watcher is given a directory name, we will
1887    * watch for all events in that directory. This flags overrides this behavior
1888    * and makes fs_event report only changes to the directory entry itself. This
1889    * flag does not affect individual files watched.
1890    * This flag is currently not implemented yet on any backend.
1891    */
1892   UV_FS_EVENT_WATCH_ENTRY = 1,
1893
1894   /*
1895    * By default uv_fs_event will try to use a kernel interface such as inotify
1896    * or kqueue to detect events. This may not work on remote filesystems such
1897    * as NFS mounts. This flag makes fs_event fall back to calling stat() on a
1898    * regular interval.
1899    * This flag is currently not implemented yet on any backend.
1900    */
1901   UV_FS_EVENT_STAT = 2,
1902
1903   /*
1904    * By default, event watcher, when watching directory, is not registering
1905    * (is ignoring) changes in it's subdirectories.
1906    * This flag will override this behaviour on platforms that support it.
1907    */
1908   UV_FS_EVENT_RECURSIVE = 4
1909 };
1910
1911
1912 UV_EXTERN int uv_fs_event_init(uv_loop_t* loop, uv_fs_event_t* handle);
1913
1914 UV_EXTERN int uv_fs_event_start(uv_fs_event_t* handle,
1915                                 uv_fs_event_cb cb,
1916                                 const char* filename,
1917                                 unsigned int flags);
1918
1919 UV_EXTERN int uv_fs_event_stop(uv_fs_event_t* handle);
1920
1921
1922 /* Utility */
1923
1924 /* Convert string ip addresses to binary structures */
1925 UV_EXTERN int uv_ip4_addr(const char* ip, int port, struct sockaddr_in* addr);
1926 UV_EXTERN int uv_ip6_addr(const char* ip, int port, struct sockaddr_in6* addr);
1927
1928 /* Convert binary addresses to strings */
1929 UV_EXTERN int uv_ip4_name(struct sockaddr_in* src, char* dst, size_t size);
1930 UV_EXTERN int uv_ip6_name(struct sockaddr_in6* src, char* dst, size_t size);
1931
1932 /* Cross-platform IPv6-capable implementation of the 'standard' inet_ntop */
1933 /* and inet_pton functions. On success they return 0. If an error */
1934 /* the target of the `dst` pointer is unmodified. */
1935 UV_EXTERN int uv_inet_ntop(int af, const void* src, char* dst, size_t size);
1936 UV_EXTERN int uv_inet_pton(int af, const char* src, void* dst);
1937
1938 /* Gets the executable path */
1939 UV_EXTERN int uv_exepath(char* buffer, size_t* size);
1940
1941 /* Gets the current working directory */
1942 UV_EXTERN int uv_cwd(char* buffer, size_t size);
1943
1944 /* Changes the current working directory */
1945 UV_EXTERN int uv_chdir(const char* dir);
1946
1947 /* Gets memory info in bytes */
1948 UV_EXTERN uint64_t uv_get_free_memory(void);
1949 UV_EXTERN uint64_t uv_get_total_memory(void);
1950
1951 /*
1952  * Returns the current high-resolution real time. This is expressed in
1953  * nanoseconds. It is relative to an arbitrary time in the past. It is not
1954  * related to the time of day and therefore not subject to clock drift. The
1955  * primary use is for measuring performance between intervals.
1956  *
1957  * Note not every platform can support nanosecond resolution; however, this
1958  * value will always be in nanoseconds.
1959  */
1960 UV_EXTERN extern uint64_t uv_hrtime(void);
1961
1962
1963 /*
1964  * Disables inheritance for file descriptors / handles that this process
1965  * inherited from its parent. The effect is that child processes spawned by
1966  * this process don't accidentally inherit these handles.
1967  *
1968  * It is recommended to call this function as early in your program as possible,
1969  * before the inherited file descriptors can be closed or duplicated.
1970  *
1971  * Note that this function works on a best-effort basis: there is no guarantee
1972  * that libuv can discover all file descriptors that were inherited. In general
1973  * it does a better job on Windows than it does on unix.
1974  */
1975 UV_EXTERN void uv_disable_stdio_inheritance(void);
1976
1977 /*
1978  * Opens a shared library. The filename is in utf-8. Returns 0 on success and
1979  * -1 on error. Call `uv_dlerror(uv_lib_t*)` to get the error message.
1980  */
1981 UV_EXTERN int uv_dlopen(const char* filename, uv_lib_t* lib);
1982
1983 /*
1984  * Close the shared library.
1985  */
1986 UV_EXTERN void uv_dlclose(uv_lib_t* lib);
1987
1988 /*
1989  * Retrieves a data pointer from a dynamic library. It is legal for a symbol to
1990  * map to NULL. Returns 0 on success and -1 if the symbol was not found.
1991  */
1992 UV_EXTERN int uv_dlsym(uv_lib_t* lib, const char* name, void** ptr);
1993
1994 /*
1995  * Returns the last uv_dlopen() or uv_dlsym() error message.
1996  */
1997 UV_EXTERN const char* uv_dlerror(uv_lib_t* lib);
1998
1999 /*
2000  * The mutex functions return 0 on success or an error code < 0
2001  * (unless the return type is void, of course).
2002  */
2003 UV_EXTERN int uv_mutex_init(uv_mutex_t* handle);
2004 UV_EXTERN void uv_mutex_destroy(uv_mutex_t* handle);
2005 UV_EXTERN void uv_mutex_lock(uv_mutex_t* handle);
2006 UV_EXTERN int uv_mutex_trylock(uv_mutex_t* handle);
2007 UV_EXTERN void uv_mutex_unlock(uv_mutex_t* handle);
2008
2009 /*
2010  * Same goes for the read/write lock functions.
2011  */
2012 UV_EXTERN int uv_rwlock_init(uv_rwlock_t* rwlock);
2013 UV_EXTERN void uv_rwlock_destroy(uv_rwlock_t* rwlock);
2014 UV_EXTERN void uv_rwlock_rdlock(uv_rwlock_t* rwlock);
2015 UV_EXTERN int uv_rwlock_tryrdlock(uv_rwlock_t* rwlock);
2016 UV_EXTERN void uv_rwlock_rdunlock(uv_rwlock_t* rwlock);
2017 UV_EXTERN void uv_rwlock_wrlock(uv_rwlock_t* rwlock);
2018 UV_EXTERN int uv_rwlock_trywrlock(uv_rwlock_t* rwlock);
2019 UV_EXTERN void uv_rwlock_wrunlock(uv_rwlock_t* rwlock);
2020
2021 /*
2022  * Same goes for the semaphore functions.
2023  */
2024 UV_EXTERN int uv_sem_init(uv_sem_t* sem, unsigned int value);
2025 UV_EXTERN void uv_sem_destroy(uv_sem_t* sem);
2026 UV_EXTERN void uv_sem_post(uv_sem_t* sem);
2027 UV_EXTERN void uv_sem_wait(uv_sem_t* sem);
2028 UV_EXTERN int uv_sem_trywait(uv_sem_t* sem);
2029
2030 /*
2031  * Same goes for the condition variable functions.
2032  */
2033 UV_EXTERN int uv_cond_init(uv_cond_t* cond);
2034 UV_EXTERN void uv_cond_destroy(uv_cond_t* cond);
2035 UV_EXTERN void uv_cond_signal(uv_cond_t* cond);
2036 UV_EXTERN void uv_cond_broadcast(uv_cond_t* cond);
2037 /* Waits on a condition variable without a timeout.
2038  *
2039  * Note:
2040  * 1. callers should be prepared to deal with spurious wakeups.
2041  */
2042 UV_EXTERN void uv_cond_wait(uv_cond_t* cond, uv_mutex_t* mutex);
2043 /* Waits on a condition variable with a timeout in nano seconds.
2044  * Returns 0 for success or UV_ETIMEDOUT on timeout, It aborts when other
2045  * errors happen.
2046  *
2047  * Note:
2048  * 1. callers should be prepared to deal with spurious wakeups.
2049  * 2. the granularity of timeout on Windows is never less than one millisecond.
2050  * 3. uv_cond_timedwait takes a relative timeout, not an absolute time.
2051  */
2052 UV_EXTERN int uv_cond_timedwait(uv_cond_t* cond, uv_mutex_t* mutex,
2053     uint64_t timeout);
2054
2055 UV_EXTERN int uv_barrier_init(uv_barrier_t* barrier, unsigned int count);
2056 UV_EXTERN void uv_barrier_destroy(uv_barrier_t* barrier);
2057 UV_EXTERN void uv_barrier_wait(uv_barrier_t* barrier);
2058
2059 /* Runs a function once and only once. Concurrent calls to uv_once() with the
2060  * same guard will block all callers except one (it's unspecified which one).
2061  * The guard should be initialized statically with the UV_ONCE_INIT macro.
2062  */
2063 UV_EXTERN void uv_once(uv_once_t* guard, void (*callback)(void));
2064
2065 /* Thread-local storage.  These functions largely follow the semantics of
2066  * pthread_key_create(), pthread_key_delete(), pthread_getspecific() and
2067  * pthread_setspecific().
2068  *
2069  * Note that the total thread-local storage size may be limited.
2070  * That is, it may not be possible to create many TLS keys.
2071  */
2072 UV_EXTERN int uv_key_create(uv_key_t* key);
2073 UV_EXTERN void uv_key_delete(uv_key_t* key);
2074 UV_EXTERN void* uv_key_get(uv_key_t* key);
2075 UV_EXTERN void uv_key_set(uv_key_t* key, void* value);
2076
2077 UV_EXTERN int uv_thread_create(uv_thread_t *tid,
2078     void (*entry)(void *arg), void *arg);
2079 UV_EXTERN unsigned long uv_thread_self(void);
2080 UV_EXTERN int uv_thread_join(uv_thread_t *tid);
2081
2082 /* The presence of these unions force similar struct layout. */
2083 #define XX(_, name) uv_ ## name ## _t name;
2084 union uv_any_handle {
2085   UV_HANDLE_TYPE_MAP(XX)
2086 };
2087
2088 union uv_any_req {
2089   UV_REQ_TYPE_MAP(XX)
2090 };
2091 #undef XX
2092
2093
2094 struct uv_loop_s {
2095   /* User data - use this for whatever. */
2096   void* data;
2097   /* Loop reference counting */
2098   unsigned int active_handles;
2099   void* handle_queue[2];
2100   void* active_reqs[2];
2101   /* Internal flag to signal loop stop */
2102   unsigned int stop_flag;
2103   UV_LOOP_PRIVATE_FIELDS
2104 };
2105
2106
2107 /* Don't export the private CPP symbols. */
2108 #undef UV_HANDLE_TYPE_PRIVATE
2109 #undef UV_REQ_TYPE_PRIVATE
2110 #undef UV_REQ_PRIVATE_FIELDS
2111 #undef UV_STREAM_PRIVATE_FIELDS
2112 #undef UV_TCP_PRIVATE_FIELDS
2113 #undef UV_PREPARE_PRIVATE_FIELDS
2114 #undef UV_CHECK_PRIVATE_FIELDS
2115 #undef UV_IDLE_PRIVATE_FIELDS
2116 #undef UV_ASYNC_PRIVATE_FIELDS
2117 #undef UV_TIMER_PRIVATE_FIELDS
2118 #undef UV_GETADDRINFO_PRIVATE_FIELDS
2119 #undef UV_FS_REQ_PRIVATE_FIELDS
2120 #undef UV_WORK_PRIVATE_FIELDS
2121 #undef UV_FS_EVENT_PRIVATE_FIELDS
2122 #undef UV_SIGNAL_PRIVATE_FIELDS
2123 #undef UV_LOOP_PRIVATE_FIELDS
2124 #undef UV_LOOP_PRIVATE_PLATFORM_FIELDS
2125
2126 #ifdef __cplusplus
2127 }
2128 #endif
2129 #endif /* UV_H */