From 40c29b190bbd0e3a6b6f377985f4b9fa28fa850d Mon Sep 17 00:00:00 2001 From: Sam Stevens Date: Wed, 6 Aug 2014 22:29:28 +0100 Subject: [PATCH] Replaced some thread un-safe functions with their safe counterparts --- src/http-server.c | 4 +++- src/log.c | 4 +++- src/main.c | 1 - src/queue.c | 7 +++++++ src/queue.h | 1 + src/thread-pool.c | 14 +++++++------- src/thread-pool.h | 2 +- src/util.c | 5 +++-- 8 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/http-server.c b/src/http-server.c index fc4f6a6..ba37385 100644 --- a/src/http-server.c +++ b/src/http-server.c @@ -262,11 +262,13 @@ FILE * server_generate_directory_index(config_host *hconfig, const char* dirpath } time_t file_mtime = (time_t)filestat->st_mtim.tv_sec; - char* file_mod_time = ctime(&file_mtime); + char* file_mod_time = calloc(32, sizeof(char)); + ctime_r(&file_mtime, file_mod_time); utstring_printf(index, "%s%s%s\r\n", uri, (filesize!=NULL)?filesize:"N/A", (file_mod_time!=NULL)?file_mod_time:"N/A"); + free(file_mod_time); free(filepath); free(filesize); free(uri); diff --git a/src/log.c b/src/log.c index 4680ef4..e2cc666 100644 --- a/src/log.c +++ b/src/log.c @@ -71,7 +71,9 @@ void*log_loop(void* arg) { if (l->running == false) { break; } - fprintf(stderr, "log[%s] read failed: %s. logger aborted\n", l->name, strerror(errno)); + char errnostr[64]; + strerror_r(errno, errnostr, 64); + fprintf(stderr, "log[%s] read failed: %s. logger aborted\n", l->name, errnostr); log_stop(l); break; } diff --git a/src/main.c b/src/main.c index 3863ccd..b9e7410 100644 --- a/src/main.c +++ b/src/main.c @@ -44,7 +44,6 @@ int main(int argc, char** argv) { log_register_add(log_new("stderr", stderr), true, LALL & ~(LINFO|LDEBUG)); log_register_add(log_new("stdout", stdout), false, LDEBUG | LINFO); - mime_init(NULL); config_server *config = config_server_new(); if (config_read_ini("khttpd.ini", config) < 0) { diff --git a/src/queue.c b/src/queue.c index 70d1673..a30dffa 100644 --- a/src/queue.c +++ b/src/queue.c @@ -90,4 +90,11 @@ void queue_ping(queue *q) { QUEUE_LOCK(q); pthread_cond_broadcast(q->cond); QUEUE_UNLOCK(q); +} +size_t queue_count(queue *q) { + size_t count; + QUEUE_LOCK(q); + count = q->count; + QUEUE_UNLOCK(q); + return count; } \ No newline at end of file diff --git a/src/queue.h b/src/queue.h index 0e8da26..99083ec 100644 --- a/src/queue.h +++ b/src/queue.h @@ -53,6 +53,7 @@ extern "C" { queue_item* queue_fetchone(queue *q, bool blocking); void queue_clear(queue *q); void queue_ping(queue *q); + size_t queue_count(queue *q); #ifdef __cplusplus } diff --git a/src/thread-pool.c b/src/thread-pool.c index be7ad39..aa9d8f7 100644 --- a/src/thread-pool.c +++ b/src/thread-pool.c @@ -78,7 +78,7 @@ void thread_pool_delete(thread_pool *pool) { void thread_pool_start(thread_pool *pool) { info("Starting thread pool %s", pool->name); pool->management_thread = thread_new(pool); - thread_start(pool->management_thread, thread_mgt); + thread_start(pool->management_thread, thread_pool_loop); } void thread_pool_stop(thread_pool *pool) { info("Stopping thread pool %s", pool->name); @@ -98,7 +98,7 @@ void thread_pool_remove_thread(thread_pool *pool, thread *th) { pool->thread_count--; } -void* thread_mgt(void* arg) { +void* thread_pool_loop(void* arg) { thread* th = (thread*)arg; thread_pool *pool = th->pool; @@ -122,18 +122,18 @@ void* thread_mgt(void* arg) { int64_t last_queue_count = 0; while(pool->shutdown == false) { - - int64_t queue_count_diff = pool->queue->count - last_queue_count; + size_t queue_size = queue_count(pool->queue); + int64_t queue_count_diff = queue_size - last_queue_count; int64_t watermark = pool->thread_count * pool->queue_factor; if (queue_count_diff > watermark && pool->thread_count < pool->max_threads) { //New thread thread *th = thread_new(pool); thread_pool_add_thread(pool, th); - last_queue_count = pool->queue->count; + last_queue_count = queue_size; } else if ((queue_count_diff*-1) > watermark && pool->thread_count > pool->min_threads) { //Remove thread (stop the first in the list) pool->threads->stop = true; - last_queue_count = pool->queue->count; + last_queue_count = queue_size; } //cleanup finished threads @@ -156,7 +156,7 @@ void* thread_mgt(void* arg) { nanosleep(&loopdelay, NULL); } //Wait until queue is empty - while(pool->queue->count > 0) { + while(queue_count(pool->queue) > 0) { nanosleep(&loopdelay, NULL); } diff --git a/src/thread-pool.h b/src/thread-pool.h index 5c041a2..94384b2 100644 --- a/src/thread-pool.h +++ b/src/thread-pool.h @@ -59,7 +59,7 @@ extern "C" { void thread_pool_add_thread(thread_pool *pool, thread *th); void thread_pool_remove_thread(thread_pool *pool, thread *th); - void* thread_mgt(void* th); + void* thread_pool_loop(void* th); #ifdef __cplusplus } diff --git a/src/util.c b/src/util.c index a546337..c86dbc7 100644 --- a/src/util.c +++ b/src/util.c @@ -79,7 +79,8 @@ char** str_splitlines(char *str, size_t *line_count) { } size_t i=0, linelen = 0; - char *line = strtok(str, "\n"); + char* saveptr=NULL; + char *line = strtok_r(str, "\n", &saveptr); while(line) { linelen = strlen(line); result[i] = calloc(linelen+1, sizeof(char)); @@ -91,7 +92,7 @@ char** str_splitlines(char *str, size_t *line_count) { result[i][linelen-1] = '\0'; result[i] = realloc(result[i], linelen); } - line = strtok(NULL, "\n"); + line = strtok_r(NULL, "\n", &saveptr); i++; }