Skip to content

Instantly share code, notes, and snippets.

@ry
Created August 17, 2011 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ry/3e59c81f98f21ccd72a7 to your computer and use it in GitHub Desktop.
Save ry/3e59c81f98f21ccd72a7 to your computer and use it in GitHub Desktop.
diff --git a/include/uv.h b/include/uv.h
index 0d7bc85..6857877 100644
--- a/include/uv.h
+++ b/include/uv.h
@@ -41,6 +41,8 @@ extern "C" {
typedef intptr_t ssize_t;
#endif
+typedef struct uv_loop_s uv_loop_t;
+typedef struct uv_ares_task_s uv_ares_task_t;
typedef struct uv_err_s uv_err_t;
typedef struct uv_handle_s uv_handle_t;
typedef struct uv_stream_s uv_stream_t;
@@ -53,6 +55,7 @@ typedef struct uv_idle_s uv_idle_t;
typedef struct uv_async_s uv_async_t;
typedef struct uv_getaddrinfo_s uv_getaddrinfo_t;
typedef struct uv_process_s uv_process_t;
+typedef struct uv_counters_s uv_counters_t;
/* Request types */
typedef struct uv_req_s uv_req_t;
typedef struct uv_shutdown_s uv_shutdown_t;
@@ -69,39 +72,42 @@ typedef struct uv_connect_s uv_connect_t;
/*
* This function must be called before any other functions in libuv.
*
- * At the moment libuv is single threaded but this will likely change in the
- * near future. Basically it will change by uv_init() taking a 'loop'
- * argument and all other _init having a first argument as the the 'loop'.
- *
* All functions besides uv_run() are non-blocking.
*
* All callbacks in libuv are made asynchronously. That is they are never
* made by the function that takes them as a parameter.
*/
-void uv_init();
+uv_loop_t* uv_loop_new();
+
+void uv_loop_delete(uv_loop_t*);
+
+/*
+ * Returns the default loop.
+ */
+uv_loop_t* uv_default_loop();
/*
* This function starts the event loop. It blocks until the reference count
* of the loop drops to zero.
*/
-int uv_run();
+int uv_run(uv_loop_t*);
/*
* Manually modify the event loop's reference count. Useful if the user wants
* to have a handle or timeout that doesn't keep the loop alive.
*/
-void uv_ref();
-void uv_unref();
+void uv_ref(uv_loop_t*);
+void uv_unref(uv_loop_t*);
-void uv_update_time();
-int64_t uv_now();
+void uv_update_time(uv_loop_t*);
+int64_t uv_now(uv_loop_t*);
/*
* Most functions return boolean: 0 for success and -1 for failure.
* On error the user should then call uv_last_error() to determine
* the error code.
*/
-uv_err_t uv_last_error();
+uv_err_t uv_last_error(uv_loop_t*);
char* uv_strerror(uv_err_t err);
const char* uv_err_name(uv_err_t err);
@@ -219,6 +225,7 @@ struct uv_err_s {
#define UV_REQ_FIELDS \
/* read-only */ \
+ uv_loop_t* loop; \
uv_req_type type; \
/* public */ \
void* data; \
@@ -255,6 +262,7 @@ struct uv_shutdown_s {
#define UV_HANDLE_FIELDS \
/* read-only */ \
+ uv_loop_t* loop; \
uv_handle_type type; \
/* public */ \
uv_close_cb close_cb; \
@@ -338,7 +346,7 @@ typedef enum {
UV_STDERR
} uv_std_type;
-uv_stream_t* uv_std_handle(uv_std_type type);
+uv_stream_t* uv_std_handle(uv_loop_t*, uv_std_type type);
/*
* Write data to stream. Buffers are written in order. Example:
@@ -382,7 +390,7 @@ struct uv_tcp_s {
UV_TCP_PRIVATE_FIELDS
};
-int uv_tcp_init(uv_tcp_t* handle);
+int uv_tcp_init(uv_loop_t*, uv_tcp_t* handle);
int uv_tcp_bind(uv_tcp_t* handle, struct sockaddr_in);
int uv_tcp_bind6(uv_tcp_t* handle, struct sockaddr_in6);
@@ -422,7 +430,7 @@ struct uv_pipe_s {
UV_PIPE_PRIVATE_FIELDS
};
-int uv_pipe_init(uv_pipe_t* handle);
+int uv_pipe_init(uv_loop_t*, uv_pipe_t* handle);
int uv_pipe_bind(uv_pipe_t* handle, const char* name);
@@ -442,7 +450,7 @@ struct uv_prepare_s {
UV_PREPARE_PRIVATE_FIELDS
};
-int uv_prepare_init(uv_prepare_t* prepare);
+int uv_prepare_init(uv_loop_t*, uv_prepare_t* prepare);
int uv_prepare_start(uv_prepare_t* prepare, uv_prepare_cb cb);
@@ -460,7 +468,7 @@ struct uv_check_s {
UV_CHECK_PRIVATE_FIELDS
};
-int uv_check_init(uv_check_t* check);
+int uv_check_init(uv_loop_t*, uv_check_t* check);
int uv_check_start(uv_check_t* check, uv_check_cb cb);
@@ -480,7 +488,7 @@ struct uv_idle_s {
UV_IDLE_PRIVATE_FIELDS
};
-int uv_idle_init(uv_idle_t* idle);
+int uv_idle_init(uv_loop_t*, uv_idle_t* idle);
int uv_idle_start(uv_idle_t* idle, uv_idle_cb cb);
@@ -502,7 +510,7 @@ struct uv_async_s {
UV_ASYNC_PRIVATE_FIELDS
};
-int uv_async_init(uv_async_t* async, uv_async_cb async_cb);
+int uv_async_init(uv_loop_t*, uv_async_t* async, uv_async_cb async_cb);
/*
* This can be called from other threads to wake up a libuv thread.
@@ -523,7 +531,7 @@ struct uv_timer_s {
UV_TIMER_PRIVATE_FIELDS
};
-int uv_timer_init(uv_timer_t* timer);
+int uv_timer_init(uv_loop_t*, uv_timer_t* timer);
int uv_timer_start(uv_timer_t* timer, uv_timer_cb cb, int64_t timeout,
int64_t repeat);
@@ -549,11 +557,13 @@ int64_t uv_timer_get_repeat(uv_timer_t* timer);
/* c-ares integration initialize and terminate */
-int uv_ares_init_options(ares_channel *channelptr,
+int uv_ares_init_options(uv_loop_t*,
+ ares_channel *channelptr,
struct ares_options *options,
int optmask);
-void uv_ares_destroy(ares_channel channel);
+/* TODO remove the loop argument from this function? */
+void uv_ares_destroy(uv_loop_t*, ares_channel channel);
/*
@@ -577,7 +587,8 @@ struct uv_getaddrinfo_s {
* Input arguments may be released after return from this call. Callback
* must not call freeaddrinfo.
*/
- int uv_getaddrinfo(uv_getaddrinfo_t* handle,
+ int uv_getaddrinfo(uv_loop_t*,
+ uv_getaddrinfo_t* handle,
uv_getaddrinfo_cb getaddrinfo_cb,
const char* node,
const char* service,
@@ -631,7 +642,7 @@ struct uv_process_s {
};
/* Initializes uv_process_t and starts the process. */
-int uv_spawn(uv_process_t*, uv_process_options_t options);
+int uv_spawn(uv_loop_t*, uv_process_t*, uv_process_options_t options);
/*
* Kills the process with the specified signal. The user must still
@@ -685,8 +696,7 @@ union uv_any_req {
};
-/* Diagnostic counters */
-typedef struct {
+struct uv_counters_s {
uint64_t req_init;
uint64_t handle_init;
uint64_t stream_init;
@@ -698,9 +708,25 @@ typedef struct {
uint64_t async_init;
uint64_t timer_init;
uint64_t process_init;
-} uv_counters_t;
+};
-uv_counters_t* uv_counters();
+
+struct uv_loop_s {
+ UV_LOOP_PRIVATE_FIELDS
+ /* list used for ares task handles */
+ uv_ares_task_t* uv_ares_handles_;
+ /* Various thing for libeio. */
+ uv_async_t uv_eio_want_poll_notifier;
+ uv_async_t uv_eio_done_poll_notifier;
+ uv_idle_t uv_eio_poller;
+ int uv_eio_init_count;
+ /* Diagnostic counters */
+ uv_counters_t counters;
+ /* The last error */
+ uv_err_t last_err;
+ /* User data - use this for whatever. */
+ void* data;
+};
/* Don't export the private CPP symbols. */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment