diff options
author | Emil Renner Berthing <esmil@mailme.dk> | 2012-12-15 02:57:42 +0100 |
---|---|---|
committer | Emil Renner Berthing <esmil@mailme.dk> | 2012-12-17 10:11:06 +0100 |
commit | 3ee86123ba489b7d820d5c5e7d3761ff8cca7977 (patch) | |
tree | e60a4275deff745779fd5fb2fef58c98a9a3caf4 | |
parent | bfc5c4fc2c51aae7f7b08f925ba00251503e969f (diff) | |
download | lem-3ee86123ba489b7d820d5c5e7d3761ff8cca7977.tar.gz lem-3ee86123ba489b7d820d5c5e7d3761ff8cca7977.tar.xz lem-3ee86123ba489b7d820d5c5e7d3761ff8cca7977.zip |
pool: use two locks, spinlock if available
-rw-r--r-- | bin/lem.c | 1 | ||||
-rw-r--r-- | bin/pool.c | 23 |
2 files changed, 19 insertions, 5 deletions
@@ -19,6 +19,7 @@ #include <stdlib.h> #include <errno.h> #include <string.h> +#include <unistd.h> #include <signal.h> #include <stdio.h> #include <assert.h> @@ -21,6 +21,17 @@ static unsigned int pool_jobs; static unsigned int pool_threads; static time_t pool_delay; static pthread_mutex_t pool_mutex; +#if _POSIX_SPIN_LOCKS >= 200112L +static pthread_spinlock_t pool_dlock; +#define pool_done_init() pthread_spin_init(&pool_dlock, PTHREAD_PROCESS_PRIVATE) +#define pool_done_lock() pthread_spin_lock(&pool_dlock) +#define pool_done_unlock() pthread_spin_unlock(&pool_dlock) +#else +static pthread_mutex_t pool_dlock; +#define pool_done_init() pthread_mutex_init(&pool_dlock, NULL) +#define pool_done_lock() pthread_mutex_lock(&pool_dlock) +#define pool_done_unlock() pthread_mutex_unlock(&pool_dlock) +#endif static pthread_cond_t pool_cond; static struct lem_async *pool_head; static struct lem_async *pool_tail; @@ -59,10 +70,10 @@ pool_threadfunc(void *arg) a->work(a); lem_debug("Bye %p", a); - pthread_mutex_lock(&pool_mutex); + pool_done_lock(); a->next = pool_done; pool_done = a; - pthread_mutex_unlock(&pool_mutex); + pool_done_unlock(); ev_async_send(LEM_ &pool_watch); } @@ -80,10 +91,10 @@ pool_cb(EV_A_ struct ev_async *w, int revents) (void)revents; - pthread_mutex_lock(&pool_mutex); + pool_done_lock(); a = pool_done; pool_done = NULL; - pthread_mutex_unlock(&pool_mutex); + pool_done_unlock(); for (; a; a = next) { pool_jobs--; @@ -114,8 +125,10 @@ pool_init(time_t delay) ev_async_init(&pool_watch, pool_cb); ret = pthread_mutex_init(&pool_mutex, NULL); + if (ret == 0) + ret = pool_done_init(); if (ret) { - lem_log_error("error initializing mutex: %s", + lem_log_error("error initializing lock: %s", strerror(ret)); return -1; } |