Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
unix: multiplicity
Browse files Browse the repository at this point in the history
  • Loading branch information
ry authored and piscisaureus committed Aug 31, 2011
1 parent 0ba44cf commit 56dcaf9
Show file tree
Hide file tree
Showing 38 changed files with 637 additions and 562 deletions.
4 changes: 2 additions & 2 deletions include/eio.h
Expand Up @@ -195,15 +195,15 @@ enum
enum
{
EIO_MCL_CURRENT = 1,
EIO_MCL_FUTURE = 2,
EIO_MCL_FUTURE = 2
};

/* request priorities */

enum {
EIO_PRI_MIN = -4,
EIO_PRI_MAX = 4,
EIO_PRI_DEFAULT = 0,
EIO_PRI_DEFAULT = 0
};

/* eio request structure */
Expand Down
10 changes: 10 additions & 0 deletions include/uv-unix.h
Expand Up @@ -42,6 +42,16 @@ typedef struct {

typedef int uv_file;

#define UV_LOOP_PRIVATE_FIELDS \
ares_channel channel; \
/* \
* While the channel is active this timer is called once per second to be \
* sure that we're always calling ares_process. See the warning above the \
* definition of ares_timeout(). \
*/ \
ev_timer timer; \
struct ev_loop* ev;

#define UV_REQ_BUFSML_SIZE (4)

#define UV_REQ_PRIVATE_FIELDS /* empty */
Expand Down
84 changes: 55 additions & 29 deletions include/uv.h
Expand Up @@ -19,7 +19,7 @@
* IN THE SOFTWARE.
*/

/* See uv_init for an introduction. */
/* See uv_loop_new for an introduction. */

#ifndef UV_H
#define UV_H
Expand All @@ -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;
Expand All @@ -54,6 +56,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;
Expand All @@ -73,32 +76,35 @@ typedef struct uv_work_s uv_work_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*);


/*
Expand Down Expand Up @@ -223,13 +229,14 @@ struct uv_err_s {
* 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);


#define UV_REQ_FIELDS \
/* read-only */ \
uv_loop_t* loop; \
uv_req_type type; \
/* public */ \
void* data; \
Expand Down Expand Up @@ -266,6 +273,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; \
Expand Down Expand Up @@ -358,7 +366,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:
Expand Down Expand Up @@ -402,7 +410,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);
Expand Down Expand Up @@ -485,7 +493,7 @@ struct uv_udp_send_s {
* Initialize a new UDP handle. The actual socket is created lazily.
* Returns 0 on success.
*/
int uv_udp_init(uv_udp_t* handle);
int uv_udp_init(uv_loop_t*, uv_udp_t* handle);

/*
* Bind to a IPv4 address and port.
Expand Down Expand Up @@ -590,7 +598,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);

Expand All @@ -610,7 +618,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);

Expand All @@ -628,7 +636,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);

Expand All @@ -648,7 +656,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);

Expand All @@ -670,7 +678,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.
Expand All @@ -691,7 +699,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);
Expand All @@ -717,11 +725,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);


/*
Expand All @@ -745,7 +755,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,
Expand Down Expand Up @@ -799,7 +810,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
Expand Down Expand Up @@ -941,8 +952,8 @@ union uv_any_req {
};


/* Diagnostic counters */
typedef struct {
struct uv_counters_s {
uint64_t eio_init;
uint64_t req_init;
uint64_t handle_init;
uint64_t stream_init;
Expand All @@ -955,9 +966,24 @@ 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;
/* 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. */
Expand Down
30 changes: 16 additions & 14 deletions src/uv-common.c
Expand Up @@ -31,9 +31,6 @@
#include "ares/inet_net_pton.h"
#include "ares/inet_ntop.h"

/* list used for ares task handles */
static uv_ares_task_t* uv_ares_handles_ = NULL;


static uv_counters_t counters;

Expand Down Expand Up @@ -135,20 +132,23 @@ int uv_ip6_name(struct sockaddr_in6* src, char* dst, size_t size) {


/* find matching ares handle in list */
void uv_add_ares_handle(uv_ares_task_t* handle) {
handle->ares_next = uv_ares_handles_;
void uv_add_ares_handle(uv_loop_t* loop, uv_ares_task_t* handle) {
handle->loop = loop;
handle->ares_next = loop->uv_ares_handles_;
handle->ares_prev = NULL;

if (uv_ares_handles_) {
uv_ares_handles_->ares_prev = handle;
if (loop->uv_ares_handles_) {
loop->uv_ares_handles_->ares_prev = handle;
}
uv_ares_handles_ = handle;

loop->uv_ares_handles_ = handle;
}

/* find matching ares handle in list */
/* TODO: faster lookup */
uv_ares_task_t* uv_find_ares_handle(ares_socket_t sock) {
uv_ares_task_t* handle = uv_ares_handles_;
uv_ares_task_t* uv_find_ares_handle(uv_loop_t* loop, ares_socket_t sock) {
uv_ares_task_t* handle = loop->uv_ares_handles_;

while (handle != NULL) {
if (handle->sock == sock) {
break;
Expand All @@ -161,8 +161,10 @@ uv_ares_task_t* uv_find_ares_handle(ares_socket_t sock) {

/* remove ares handle in list */
void uv_remove_ares_handle(uv_ares_task_t* handle) {
if (handle == uv_ares_handles_) {
uv_ares_handles_ = handle->ares_next;
uv_loop_t* loop = handle->loop;

if (handle == loop->uv_ares_handles_) {
loop->uv_ares_handles_ = handle->ares_next;
}

if (handle->ares_next) {
Expand All @@ -176,6 +178,6 @@ void uv_remove_ares_handle(uv_ares_task_t* handle) {


/* Returns 1 if the uv_ares_handles_ list is empty. 0 otherwise. */
int uv_ares_handles_empty() {
return uv_ares_handles_ ? 0 : 1;
int uv_ares_handles_empty(uv_loop_t* loop) {
return loop->uv_ares_handles_ ? 0 : 1;
}
14 changes: 6 additions & 8 deletions src/uv-common.h
Expand Up @@ -38,11 +38,6 @@
req->errorno = errno; \
}

/*
* Subclass of uv_handle_t. Used for integration of c-ares.
*/
typedef struct uv_ares_task_s uv_ares_task_t;

struct uv_ares_task_s {
UV_HANDLE_FIELDS
UV_ARES_TASK_PRIVATE_FIELDS
Expand All @@ -52,9 +47,12 @@ struct uv_ares_task_s {


void uv_remove_ares_handle(uv_ares_task_t* handle);
uv_ares_task_t* uv_find_ares_handle(ares_socket_t sock);
void uv_add_ares_handle(uv_ares_task_t* handle);
int uv_ares_handles_empty();
uv_ares_task_t* uv_find_ares_handle(uv_loop_t*, ares_socket_t sock);

/* TODO Rename to uv_ares_task_init? */
void uv_add_ares_handle(uv_loop_t* loop, uv_ares_task_t* handle);

int uv_ares_handles_empty(uv_loop_t* loop);


#endif /* UV_COMMON_H_ */

0 comments on commit 56dcaf9

Please sign in to comment.