Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Refcount refactor almost done

  • Loading branch information
piscisaureus committed May 15, 2012
1 parent 939c009 commit 7f80678516882bf3defab71940592107a5e7be4d
@@ -204,8 +204,6 @@ RB_HEAD(uv_timer_tree_s, uv_timer_s);
#define UV_LOOP_PRIVATE_FIELDS \
/* The loop's I/O completion port */ \
HANDLE iocp; \
/* Reference count that keeps the event loop alive */ \
int refs; \
/* The current time according to the event loop. in msecs. */ \
int64_t time; \
/* Tail of a single-linked circular queue of pending reqs. If the queue */ \
@@ -310,6 +308,7 @@ RB_HEAD(uv_timer_tree_s, uv_timer_s);

#define UV_STREAM_PRIVATE_FIELDS \
unsigned int reqs_pending; \
int activecnt; \
uv_read_t read_req; \
union { \
struct { uv_stream_connection_fields }; \
@@ -337,6 +336,7 @@ RB_HEAD(uv_timer_tree_s, uv_timer_s);
#define UV_UDP_PRIVATE_FIELDS \
SOCKET socket; \
unsigned int reqs_pending; \
int activecnt; \
uv_req_t recv_req; \
uv_buf_t recv_buffer; \
struct sockaddr_storage recv_from; \
@@ -810,7 +810,7 @@ static void uv__stream_connect(uv_stream_t* stream) {


int uv__connect(uv_connect_t* req, uv_stream_t* stream, struct sockaddr* addr,
socklen_t addrlen, uv_connect_cb cb) {
socklen_t addrlen, uv_connect_cb cb) {
int sockfd;
int r;

@@ -20,6 +20,12 @@
*/

#include "uv.h"

/* FIXME: This hack sucks. */
#ifdef _WIN32
# include "win/internal.h"
#endif

#include "uv-common.h"

#include <assert.h>
@@ -27,23 +27,28 @@
#ifndef UV_COMMON_H_
#define UV_COMMON_H_

#include <assert.h>

#include "uv.h"
#include "tree.h"
#include <assert.h>


#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

#ifdef _MSC_VER
# define INLINE __declspec(inline)
#if !defined(NDEBUG)
# define INLINE /* empty */
#elif defined(_MSC_VER)
# define INLINE __inline
#else
# define INLINE inline
#endif


#ifndef _WIN32
enum {
UV__ACTIVE = 0x4000,
UV__REF = 0x8000
};
#endif

struct uv_ares_task_s {
UV_HANDLE_FIELDS
@@ -114,7 +119,11 @@ INLINE static void uv__active_handle_rm(uv_handle_t* h) {
ngx_queue_remove(&h->active_queue);
}

INLINE static void uv__req_unref(uv_loop_t* loop, uv_req_t* req) {
INLINE static void uv__req_register(uv_loop_t* loop, uv_req_t* req) {
ngx_queue_insert_tail(&loop->active_reqs, &req->active_queue);
}

INLINE static void uv__req_unregister(uv_loop_t* loop, uv_req_t* req) {
assert(uv__has_active_reqs(loop));
ngx_queue_remove(&req->active_queue);
}
@@ -138,7 +147,12 @@ INLINE static void uv__active_handle_rm(uv_handle_t* h) {
h->loop->active_handles--;
}

INLINE static void uv__req_unref(uv_loop_t* loop, uv_req_t* req) {
INLINE static void uv__req_register(uv_loop_t* loop, uv_req_t* req) {
loop->active_reqs++;
(void) req;
}

INLINE static void uv__req_unregister(uv_loop_t* loop, uv_req_t* req) {
assert(loop->active_reqs > 0);
loop->active_reqs--;
(void) req;
@@ -148,7 +162,9 @@ INLINE static void uv__req_unref(uv_loop_t* loop, uv_req_t* req) {

#define uv__active_handle_add(h) uv__active_handle_add((uv_handle_t*)(h))
#define uv__active_handle_rm(h) uv__active_handle_rm((uv_handle_t*)(h))
#define uv__req_unref(loop, req) uv__req_unref((loop), (uv_req_t*)(req))

#define uv__req_register(loop, req) uv__req_register((loop), (uv_req_t*)(req))
#define uv__req_unregister(loop, req) uv__req_unregister((loop), (uv_req_t*)(req))

INLINE static int uv__is_active(const uv_handle_t* h) {
return !!(h->flags & UV__ACTIVE);
@@ -23,6 +23,7 @@

#include "uv.h"
#include "internal.h"
#include "../uv-common.h"


/* Atomic set operation on char */
@@ -59,25 +60,20 @@ void uv_async_endgame(uv_loop_t* loop, uv_async_t* handle) {
!handle->async_sent) {
assert(!(handle->flags & UV_HANDLE_CLOSED));
handle->flags |= UV_HANDLE_CLOSED;
uv__handle_stop(handle);

if (handle->close_cb) {
handle->close_cb((uv_handle_t*)handle);
}

uv_unref(loop);
}
}


int uv_async_init(uv_loop_t* loop, uv_async_t* handle, uv_async_cb async_cb) {
uv_req_t* req;

loop->counters.handle_init++;
loop->counters.async_init++;

uv_handle_init(loop, (uv_handle_t*) handle);
handle->type = UV_ASYNC;
handle->loop = loop;
handle->flags = 0;
handle->async_sent = 0;
handle->async_cb = async_cb;

@@ -86,7 +82,9 @@ int uv_async_init(uv_loop_t* loop, uv_async_t* handle, uv_async_cb async_cb) {
req->type = UV_WAKEUP;
req->data = handle;

uv_ref(loop);
loop->counters.async_init++;

uv__handle_start(handle);

return 0;
}
@@ -96,6 +94,8 @@ void uv_async_close(uv_loop_t* loop, uv_async_t* handle) {
if (!((uv_async_t*)handle)->async_sent) {
uv_want_endgame(loop, (uv_handle_t*) handle);
}

uv__handle_start(handle);
}


@@ -22,8 +22,8 @@
#include <assert.h>

#include "uv.h"
#include "../uv-common.h"
#include "internal.h"
#include "../uv-common.h"


/*
@@ -132,6 +132,7 @@ static void uv_ares_sockstate_cb(void *data, ares_socket_t sock, int read,
}
/* remove handle from list */
uv_remove_ares_handle(uv_handle_ares);
uv__handle_stop(uv_handle_ares);

/* Post request to cleanup the Task */
uv_ares_req = &uv_handle_ares->ares_req;
@@ -178,7 +179,7 @@ static void uv_ares_sockstate_cb(void *data, ares_socket_t sock, int read,

/* add handle to list */
uv_add_ares_handle(loop, uv_handle_ares);
uv_ref(loop);
uv__handle_start(uv_handle_ares);

/*
* we have a single polling timer for all ares sockets.
@@ -229,7 +230,7 @@ void uv_process_ares_cleanup_req(uv_loop_t* loop, uv_ares_task_t* handle,
unsigned int signaled = WaitForSingleObject(handle->h_close_event, 0);

if (signaled != WAIT_TIMEOUT) {
uv_unref(loop);
uv__handle_stop(loop);

/* close event handle and free uv handle memory */
CloseHandle(handle->h_close_event);
@@ -27,8 +27,8 @@
#include <string.h>

#include "uv.h"
#include "../uv-common.h"
#include "internal.h"
#include "../uv-common.h"


/* The only event loop we support right now */
@@ -65,10 +65,16 @@ static void uv_loop_init(uv_loop_t* loop) {
uv_fatal_error(GetLastError(), "CreateIoCompletionPort");
}

loop->refs = 0;

uv_update_time(loop);

#ifndef UV_LEAN_AND_MEAN
ngx_queue_init(&loop->active_handles);
ngx_queue_init(&loop->active_reqs);
#else
loop->active_handles = 0;
loop->active_reqs = 0;
#endif

loop->pending_reqs_tail = NULL;

loop->endgame_handles = NULL;
@@ -211,6 +217,19 @@ static void uv_poll_ex(uv_loop_t* loop, int block) {
}
}

#ifndef UV_LEAN_AND_MEAN
# define UV_LOOP_ALIVE(loop) \
(!ngx_queue_empty(&(loop)->active_handles) || \
!ngx_queue_empty(&(loop)->active_reqs) || \
(loop)->endgame_handles != NULL)
#else
# define UV_LOOP_ALIVE(loop) \
((loop)->active_handles > 0 && \
(loop)->active_reqs > 0 && \
(loop)->endgame_handles != NULL)
#endif


#define UV_LOOP_ONCE(loop, poll) \
do { \
uv_update_time((loop)); \
@@ -225,7 +244,7 @@ static void uv_poll_ex(uv_loop_t* loop, int block) {
uv_process_reqs((loop)); \
uv_process_endgames((loop)); \
\
if ((loop)->refs <= 0) { \
if (!UV_LOOP_ALIVE((loop))) { \
break; \
} \
\
@@ -234,13 +253,13 @@ static void uv_poll_ex(uv_loop_t* loop, int block) {
poll((loop), (loop)->idle_handles == NULL && \
(loop)->pending_reqs_tail == NULL && \
(loop)->endgame_handles == NULL && \
(loop)->refs > 0); \
UV_LOOP_ALIVE((loop))); \
\
uv_check_invoke((loop)); \
} while (0);

#define UV_LOOP(loop, poll) \
while ((loop)->refs > 0) { \
while (UV_LOOP_ALIVE((loop))) { \
UV_LOOP_ONCE(loop, poll) \
}

@@ -262,6 +281,6 @@ int uv_run(uv_loop_t* loop) {
UV_LOOP(loop, uv_poll);
}

assert(loop->refs == 0);
assert(!UV_LOOP_ALIVE((loop)));
return 0;
}
@@ -21,6 +21,7 @@

#include "uv.h"
#include "internal.h"
#include "../uv-common.h"

static int uv__dlerror(uv_lib_t* lib, int errorno);

@@ -26,8 +26,8 @@
#include <string.h>

#include "uv.h"
#include "../uv-common.h"
#include "internal.h"
#include "../uv-common.h"


/*

0 comments on commit 7f80678

Please sign in to comment.
You can’t perform that action at this time.