From 0184bdfb2303e8785753711bc2e31445d8db14f4 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Orsini Date: Fri, 7 Sep 2012 07:48:40 +0000 Subject: [PATCH] slog in a separate thread --- src/server/server.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/server/server.c b/src/server/server.c index bdb785c..afe5a3c 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -306,11 +306,33 @@ static int cbk_http_request(void *cls, return ret; } +static void *slog_routine(void *data) +{ + char *path; + + path = (char *)data; + + log_debug("slog_routine"); + + slog_init(path, server_data.sensors); + + while (1) { + pthread_mutex_lock(&mutex); + slog_write_sensors(server_data.sensors); + pthread_mutex_unlock(&mutex); + + sleep(5); + } + + pthread_exit(0); +} + int main(int argc, char *argv[]) { struct MHD_Daemon *d; - int port, opti, optc, cmdok; + int port, opti, optc, cmdok, ret; char *log_file, *slog_file; + pthread_t slog_thread; program_name = argv[0]; @@ -404,8 +426,14 @@ int main(int argc, char *argv[]) log_info(_("WWW directory: %s"), server_data.www_dir); log_info(_("URL: http://localhost:%d"), port); - if (slog_file) - slog_init(slog_file, server_data.sensors); + if (slog_file) { + ret = pthread_create(&slog_thread, + NULL, + slog_routine, + slog_file); + if (ret) + log_err(_("Failed to create sensor log thread.")); + } while (!server_stop_requested) { pthread_mutex_lock(&mutex); @@ -418,8 +446,6 @@ int main(int argc, char *argv[]) psensor_log_measures(server_data.sensors); - slog_write_sensors(server_data.sensors); - pthread_mutex_unlock(&mutex); sleep(5); } -- 2.7.4