From 14d48527afbf1574cbc38ea0ee22c068fe942a53 Mon Sep 17 00:00:00 2001 From: Thomas Porzelt Date: Sun, 19 Sep 2010 18:00:31 +0200 Subject: [PATCH] [core] Make log.timestamp creation/destruction threadsafe --- include/lighttpd/server.h | 3 ++- src/main/log.c | 7 +++++++ src/main/plugin_core.c | 4 ++-- src/main/server.c | 6 ++++-- src/main/throttle.c | 12 ++++++------ 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/include/lighttpd/server.h b/include/lighttpd/server.h index 15a9d89..c42a74c 100644 --- a/include/lighttpd/server.h +++ b/include/lighttpd/server.h @@ -101,6 +101,8 @@ struct liServer { GArray *optionptr_def_values;/** array of liOptionPtrValue* */ liAction *mainaction; + GMutex *action_mutex; /** used to synchronize action creation/destruction */ + gboolean exiting; /** atomic access */ struct { @@ -129,7 +131,6 @@ struct liServer { gdouble io_timeout; GArray *throttle_pools; - GStaticMutex throttle_pools_mutex; gdouble stat_cache_ttl; gint tasklet_pool_threads; diff --git a/src/main/log.c b/src/main/log.c index 069bdbf..bb12146 100644 --- a/src/main/log.c +++ b/src/main/log.c @@ -494,11 +494,13 @@ liLogTimestamp *li_log_timestamp_new(liServer *srv, GString *format) { liLogTimestamp *ts; /* check if there already exists a timestamp entry with the same format */ + g_mutex_lock(srv->action_mutex); for (guint i = 0; i < srv->logs.timestamps->len; i++) { ts = g_array_index(srv->logs.timestamps, liLogTimestamp*, i); if (g_string_equal(ts->format, format)) { g_atomic_int_inc(&(ts->refcount)); g_string_free(format, TRUE); + g_mutex_unlock(srv->action_mutex); return ts; } } @@ -511,18 +513,23 @@ liLogTimestamp *li_log_timestamp_new(liServer *srv, GString *format) { ts->format = format; g_array_append_val(srv->logs.timestamps, ts); + g_mutex_unlock(srv->action_mutex); return ts; } gboolean li_log_timestamp_free(liServer *srv, liLogTimestamp *ts) { if (g_atomic_int_dec_and_test(&(ts->refcount))) { + g_mutex_lock(srv->action_mutex); + for (guint i = 0; i < srv->logs.timestamps->len; i++) { if (g_string_equal(g_array_index(srv->logs.timestamps, liLogTimestamp*, i)->format, ts->format)) { g_array_remove_index_fast(srv->logs.timestamps, i); break; } } + + g_mutex_unlock(srv->action_mutex); g_string_free(ts->cached, TRUE); g_string_free(ts->format, TRUE); g_slice_free(liLogTimestamp, ts); diff --git a/src/main/plugin_core.c b/src/main/plugin_core.c index bced404..de5384b 100644 --- a/src/main/plugin_core.c +++ b/src/main/plugin_core.c @@ -1868,7 +1868,7 @@ static void plugin_core_prepare_worker(liServer *srv, liPlugin *p, liWorker *wrk UNUSED(p); /* initialize throttle pools that have not been yet */ - g_static_mutex_lock(&srv->throttle_pools_mutex); + g_mutex_lock(srv->action_mutex); for (i = 0; i < srv->throttle_pools->len; i++) { liThrottlePool *pool = g_array_index(srv->throttle_pools, liThrottlePool*, i); @@ -1884,7 +1884,7 @@ static void plugin_core_prepare_worker(liServer *srv, liPlugin *p, liWorker *wrk } } } - g_static_mutex_unlock(&srv->throttle_pools_mutex); + g_mutex_unlock(srv->action_mutex); #if defined(LIGHTY_OS_LINUX) /* sched_setaffinity is only available on linux */ diff --git a/src/main/server.c b/src/main/server.c index f9bba16..49ed7fe 100644 --- a/src/main/server.c +++ b/src/main/server.c @@ -131,6 +131,8 @@ liServer* li_server_new(const gchar *module_dir, gboolean module_resident) { srv->mainaction = NULL; + srv->action_mutex = g_mutex_new(); + srv->exiting = FALSE; srv->ts_formats = g_array_new(FALSE, TRUE, sizeof(GString*)); @@ -140,7 +142,6 @@ liServer* li_server_new(const gchar *module_dir, gboolean module_resident) { li_server_ts_format_add(srv, g_string_new("%a, %d %b %Y %H:%M:%S GMT")); srv->throttle_pools = g_array_new(FALSE, TRUE, sizeof(liThrottlePool*)); - g_static_mutex_init(&srv->throttle_pools_mutex); li_log_init(srv); @@ -220,7 +221,6 @@ void li_server_free(liServer* srv) { li_throttle_pool_free(srv, g_array_index(srv->throttle_pools, liThrottlePool*, i)); } g_array_free(srv->throttle_pools, TRUE); - g_static_mutex_free(&srv->throttle_pools_mutex); } if (srv->acon) { @@ -281,6 +281,8 @@ void li_server_free(liServer* srv) { g_array_free(srv->li_plugins_handle_close, TRUE); g_array_free(srv->li_plugins_handle_vrclose, TRUE); + g_mutex_free(srv->action_mutex); + #ifdef LIGHTY_OS_LINUX li_value_free(srv->workers_cpu_affinity); #endif diff --git a/src/main/throttle.c b/src/main/throttle.c index 3b1ba92..40c4204 100644 --- a/src/main/throttle.c +++ b/src/main/throttle.c @@ -10,7 +10,7 @@ liThrottlePool *li_throttle_pool_new(liServer *srv, GString *name, guint rate) { liThrottlePool *pool; guint i; - g_static_mutex_lock(&srv->throttle_pools_mutex); + g_mutex_lock(srv->action_mutex); /* check if we already have a pool with that name */ for (i = 0; i < srv->throttle_pools->len; i++) { @@ -18,14 +18,14 @@ liThrottlePool *li_throttle_pool_new(liServer *srv, GString *name, guint rate) { if (g_string_equal(pool->name, name)) { g_atomic_int_inc(&pool->refcount); - g_static_mutex_unlock(&srv->throttle_pools_mutex); + g_mutex_unlock(srv->action_mutex); g_string_free(name, TRUE); return pool; } } if (rate == 0) { - g_static_mutex_unlock(&srv->throttle_pools_mutex); + g_mutex_unlock(srv->action_mutex); return NULL; } @@ -53,7 +53,7 @@ liThrottlePool *li_throttle_pool_new(liServer *srv, GString *name, guint rate) { g_array_append_val(srv->throttle_pools, pool); - g_static_mutex_unlock(&srv->throttle_pools_mutex); + g_mutex_unlock(srv->action_mutex); return pool; } @@ -64,14 +64,14 @@ void li_throttle_pool_free(liServer *srv, liThrottlePool *pool) { if (!g_atomic_int_dec_and_test(&pool->refcount)) return; - g_static_mutex_lock(&srv->throttle_pools_mutex); + g_mutex_lock(srv->action_mutex); for (i = 0; i < srv->throttle_pools->len; i++) { if (pool == g_array_index(srv->throttle_pools, liThrottlePool*, i)) { g_array_remove_index_fast(srv->throttle_pools, i); break; } } - g_static_mutex_unlock(&srv->throttle_pools_mutex); + g_mutex_unlock(srv->action_mutex); if (pool->worker_queues) { for (i = 0; i < srv->worker_count; i++) {