Last active
August 29, 2015 14:10
-
-
Save trevnorris/9dbf06fa2847ff2d61cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/haywire/http_server.c b/src/haywire/http_server.c | |
index 6f18b9b..3591aca 100644 | |
--- a/src/haywire/http_server.c | |
+++ b/src/haywire/http_server.c | |
@@ -78,6 +78,7 @@ void free_http_connection(http_connection* connection) | |
free_http_request(connection->request); | |
} | |
+ free(connection->thread); | |
free(connection); | |
INCREMENT_STAT(stat_connections_destroyed_total); | |
} | |
@@ -186,6 +187,8 @@ int hw_http_open(int threads) | |
printf("Listening on %s:%d\n", config->http_listen_address, config->http_listen_port); | |
nub_loop_run(uv_loop, UV_RUN_DEFAULT); | |
+ nub_loop_dispose(uv_loop); | |
+ free(uv_loop); | |
return 0; | |
} | |
@@ -198,9 +201,11 @@ void http_stream_on_connect(uv_stream_t* stream, int status) | |
connection->parser.data = connection; | |
connection->stream.data = connection; | |
- nub_thread_t thread; | |
- nub_thread_create(uv_loop, &thread); | |
- connection->thread = &thread; | |
+ nub_thread_t* thread; | |
+ thread = (nub_thread_t*) malloc(sizeof(*thread)); | |
+ assert(thread != NULL); | |
+ nub_thread_create(uv_loop, thread); | |
+ connection->thread = thread; | |
/* TODO: Use the return values from uv_accept() and uv_read_start() */ | |
uv_accept(stream, (uv_stream_t*)&connection->stream); | |
@@ -262,7 +267,10 @@ static void thread_after_read(nub_thread_t* thread, void* arg) | |
} | |
else | |
{ | |
+ nub_loop_block(thread); | |
uv_close((uv_handle_t*) &connection->stream, http_stream_on_close); | |
+ nub_loop_resume(thread); | |
+ nub_thread_dispose(thread); | |
} | |
free(nubuf->base); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/build.sh b/build.sh | |
index ed1ff23..9900651 100755 | |
--- a/build.sh | |
+++ b/build.sh | |
@@ -4,7 +4,7 @@ PLATFORM_WINDOWS="Windows_NT" | |
PLATFORM_LINUX="Linux" | |
PLATFORM_MACOSX="Darwin" | |
GYP=./lib/libuv/build/gyp/gyp | |
-CONFIGURATION="debug" | |
+CONFIGURATION=${CONFIGURATION:-debug} | |
# Parse command line arguments | |
while getopts ":o:c:" option | |
diff --git a/src/haywire/http_server.c b/src/haywire/http_server.c | |
index 6f18b9b..2f7f880 100644 | |
--- a/src/haywire/http_server.c | |
+++ b/src/haywire/http_server.c | |
@@ -30,7 +30,7 @@ | |
#define UVERR(err, msg) fprintf(stderr, "%s: %s\n", msg, uv_strerror(err)) | |
#define CHECK(r, msg) \ | |
if (r) { \ | |
-uv_err_t err = uv_last_error(uv_loop); \ | |
+uv_err_t err = uv_last_error(nloop); \ | |
UVERR(err, msg); \ | |
exit(1); \ | |
} | |
@@ -42,7 +42,8 @@ static uv_tcp_t server; | |
static http_parser_settings parser_settings; | |
static struct sockaddr_in listen_address; | |
-nub_loop_t* uv_loop; | |
+nub_loop_t* nloop; | |
+nub_thread_t* nthread; | |
void* routes; | |
hw_string* http_v1_0; | |
hw_string* http_v1_1; | |
@@ -162,9 +163,11 @@ int hw_http_open(int threads) | |
listener_count = threads; | |
/* TODO: Use the return values from uv_tcp_init() and uv_tcp_bind() */ | |
- uv_loop = malloc(sizeof(nub_loop_t)); | |
- nub_loop_init(uv_loop); | |
- uv_tcp_init(&uv_loop->uvloop, &server); | |
+ nloop = malloc(sizeof(*nloop)); | |
+ nub_loop_init(nloop); | |
+ nthread = malloc(sizeof(*nthread)); | |
+ nub_thread_create(nloop, nthread); | |
+ uv_tcp_init(&nloop->uvloop, &server); | |
listener_async_handles = calloc(listener_count, sizeof(uv_async_t)); | |
listener_event_loops = calloc(listener_count, sizeof(uv_loop_t)); | |
@@ -173,7 +176,7 @@ int hw_http_open(int threads) | |
uv_barrier_init(listeners_created_barrier, listener_count + 1); | |
service_handle = malloc(sizeof(uv_async_t)); | |
- uv_async_init(&uv_loop->uvloop, service_handle, NULL); | |
+ uv_async_init(&nloop->uvloop, service_handle, NULL); | |
/* If running single threaded there is no need to use the IPC pipe | |
to distribute requests between threads so lets avoid the IPC overhead */ | |
@@ -184,7 +187,7 @@ int hw_http_open(int threads) | |
uv_tcp_bind(&server, (const struct sockaddr*)&listen_address, 0); | |
uv_listen((uv_stream_t*)&server, 128, http_stream_on_connect); | |
printf("Listening on %s:%d\n", config->http_listen_address, config->http_listen_port); | |
- nub_loop_run(uv_loop, UV_RUN_DEFAULT); | |
+ nub_loop_run(nloop, UV_RUN_DEFAULT); | |
return 0; | |
} | |
@@ -192,15 +195,13 @@ int hw_http_open(int threads) | |
void http_stream_on_connect(uv_stream_t* stream, int status) | |
{ | |
http_connection* connection = create_http_connection(); | |
- uv_tcp_init(&uv_loop->uvloop, &connection->stream); | |
+ uv_tcp_init(&nloop->uvloop, &connection->stream); | |
http_parser_init(&connection->parser, HTTP_REQUEST); | |
connection->parser.data = connection; | |
connection->stream.data = connection; | |
- nub_thread_t thread; | |
- nub_thread_create(uv_loop, &thread); | |
- connection->thread = &thread; | |
+ connection->thread = nthread; | |
/* TODO: Use the return values from uv_accept() and uv_read_start() */ | |
uv_accept(stream, (uv_stream_t*)&connection->stream); | |
@@ -262,7 +263,9 @@ static void thread_after_read(nub_thread_t* thread, void* arg) | |
} | |
else | |
{ | |
+ nub_loop_block(thread); | |
uv_close((uv_handle_t*) &connection->stream, http_stream_on_close); | |
+ nub_loop_resume(thread); | |
} | |
free(nubuf->base); | |
@@ -283,9 +286,8 @@ int http_server_write_response(hw_write_context* write_context, hw_string* respo | |
nub_loop_block(write_context->connection->thread); | |
/* TODO: Use the return values from uv_write() */ | |
int r = uv_write(write_req, (uv_stream_t*)&write_context->connection->stream, resbuf, 1, http_server_after_write); | |
- | |
- check_error(r, "write error"); | |
nub_loop_resume(write_context->connection->thread); | |
+ check_error(r, "write error"); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment