diff options
author | Adam Chlipala <adam@chlipala.net> | 2012-04-08 16:24:16 -0400 |
---|---|---|
committer | Adam Chlipala <adam@chlipala.net> | 2012-04-08 16:24:16 -0400 |
commit | b4fdf677c13d1d718640c22b94989d8bb516bd6f (patch) | |
tree | d6449a3ee5fd57b04ef2daea40740f92834e7fc0 /src/c | |
parent | b02e528023100b07d57faf7bbeba1203feee9bcc (diff) |
Refactor to avoid dependence on recursive mutexes
Diffstat (limited to 'src/c')
-rw-r--r-- | src/c/urweb.c | 18 |
1 files changed, 8 insertions, 10 deletions
diff --git a/src/c/urweb.c b/src/c/urweb.c index b105d5ff..9f318c95 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -159,13 +159,7 @@ typedef struct client { static client **clients, *clients_free, *clients_used; static unsigned n_clients; -static pthread_mutex_t clients_mutex = - #ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER - PTHREAD_RECURSIVE_MUTEX_INITIALIZER - #else - PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP - #endif - ; +static pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER; size_t uw_messages_max = SIZE_MAX; size_t uw_clients_max = SIZE_MAX; @@ -230,20 +224,22 @@ static void release_client(client *c) { } static const char begin_msgs[] = "Content-type: text/plain\r\n\r\n"; +static pthread_t pruning_thread; +static int pruning_thread_initialized = 0; static client *find_client(unsigned id) { client *c; - pthread_mutex_lock(&clients_mutex); + if (!pruning_thread_initialized || !pthread_equal(pruning_thread, pthread_self())) pthread_mutex_lock(&clients_mutex); if (id >= n_clients) { - pthread_mutex_unlock(&clients_mutex); + if (!pruning_thread_initialized || !pthread_equal(pruning_thread, pthread_self())) pthread_mutex_unlock(&clients_mutex); return NULL; } c = clients[id]; - pthread_mutex_unlock(&clients_mutex); + if (!pruning_thread_initialized || !pthread_equal(pruning_thread, pthread_self())) pthread_mutex_unlock(&clients_mutex); return c; } @@ -3291,6 +3287,8 @@ void uw_prune_clients(uw_context ctx) { cutoff = time(NULL) - ctx->app->timeout; pthread_mutex_lock(&clients_mutex); + pruning_thread = pthread_self(); + pruning_thread_initialized = 1; for (c = clients_used; c; c = next) { next = c->next; |