unix: Fix test-gethostbyname

This commit is contained in:
Ryan Dahl 2011-08-31 15:37:16 -07:00
parent 6fd340b8ca
commit 836cc204b6
3 changed files with 17 additions and 9 deletions

View File

@ -27,25 +27,29 @@
#include <stdlib.h>
#define container_of ngx_queue_data
/*
* This is called once per second by loop->timer. It is used to
* constantly callback into c-ares for possibly processing timeouts.
*/
static void uv__ares_timeout(struct ev_loop* ev, struct ev_timer* watcher,
int revents) {
uv_loop_t* loop = container_of(ev, uv_loop_t, ev);
uv_loop_t* loop = ev_userdata(ev);
assert(ev == loop->ev);
assert((uv_loop_t*)watcher->data == loop);
assert(watcher == &loop->timer);
assert(revents == EV_TIMER);
assert(!uv_ares_handles_empty(loop));
ares_process_fd(loop->channel, ARES_SOCKET_BAD, ARES_SOCKET_BAD);
}
static void uv__ares_io(struct ev_loop* ev, struct ev_io* watcher,
int revents) {
uv_loop_t* loop = container_of(ev, uv_loop_t, ev);
uv_loop_t* loop = ev_userdata(ev);
assert(ev == loop->ev);
/* Reset the idle timer */
ev_timer_again(ev, &loop->timer);
@ -83,6 +87,8 @@ static void uv__ares_sockstate_cb(void* data, ares_socket_t sock,
uv_loop_t* loop = data;
uv_ares_task_t* h;
assert((uv_loop_t*)loop->timer.data == loop);
h = uv_find_ares_handle(loop, sock);
if (read || write) {
@ -155,14 +161,14 @@ int uv_ares_init_options(uv_loop_t* loop, ares_channel *channelptr,
/* if success, save channel */
if (rc == ARES_SUCCESS) {
loop->channel = *channelptr;
}
}
/*
* Initialize the timeout timer. The timer won't be started until the
* first socket is opened.
*/
ev_init(&loop->timer, uv__ares_timeout);
loop->timer.repeat = 1.0;
ev_timer_init(&loop->timer, uv__ares_timeout, 1., 1.);
loop->timer.data = loop;
return rc;
}
@ -171,7 +177,7 @@ int uv_ares_init_options(uv_loop_t* loop, ares_channel *channelptr,
/* TODO share this with windows? */
void uv_ares_destroy(uv_loop_t* loop, ares_channel channel) {
/* only allow destroy if did init */
if (loop->channel != NULL) {
if (loop->channel) {
ev_timer_stop(loop->ev, &loop->timer);
ares_destroy(channel);
loop->channel = NULL;

View File

@ -96,6 +96,7 @@ void uv_init() {
#else
default_loop_struct.ev = ev_default_loop(EVFLAG_AUTO);
#endif
ev_set_userdata(default_loop_struct.ev, default_loop_ptr);
}
@ -186,6 +187,7 @@ void uv_close(uv_handle_t* handle, uv_close_cb close_cb) {
uv_loop_t* uv_loop_new() {
uv_loop_t* loop = calloc(1, sizeof(uv_loop_t));
loop->ev = ev_loop_new(0);
ev_set_userdata(loop->ev, loop);
return loop;
}
@ -198,6 +200,7 @@ void uv_loop_delete(uv_loop_t* loop) {
uv_loop_t* uv_default_loop() {
assert(default_loop_ptr->ev == EV_DEFAULT_UC);
return default_loop_ptr;
}

View File

@ -75,7 +75,6 @@ static void prep_tcploopback() {
options.flags = ARES_FLAG_USEVC;
rc = uv_ares_init_options(uv_default_loop(), &channel, &options, optmask);
ASSERT(rc == ARES_SUCCESS);
}