summaryrefslogtreecommitdiff
path: root/src/libstrongswan/threading
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstrongswan/threading')
-rw-r--r--src/libstrongswan/threading/mutex.c40
-rw-r--r--src/libstrongswan/threading/rwlock.c233
-rw-r--r--src/libstrongswan/threading/rwlock_condvar.h90
-rw-r--r--src/libstrongswan/threading/semaphore.c179
-rw-r--r--src/libstrongswan/threading/semaphore.h85
-rw-r--r--src/libstrongswan/threading/spinlock.c136
-rw-r--r--src/libstrongswan/threading/spinlock.h58
-rw-r--r--src/libstrongswan/threading/thread.c4
-rw-r--r--src/libstrongswan/threading/thread_value.c21
9 files changed, 792 insertions, 54 deletions
diff --git a/src/libstrongswan/threading/mutex.c b/src/libstrongswan/threading/mutex.c
index 3bdb3bf29..2ef918a28 100644
--- a/src/libstrongswan/threading/mutex.c
+++ b/src/libstrongswan/threading/mutex.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2008-2009 Tobias Brunner
+ * Copyright (C) 2008-2012 Tobias Brunner
* Copyright (C) 2008 Martin Willi
* Hochschule fuer Technik Rapperswil
*
@@ -73,9 +73,9 @@ struct private_r_mutex_t {
pthread_t thread;
/**
- * times we have locked the lock, stored per thread
+ * times the current thread locked the mutex
*/
- pthread_key_t times;
+ u_int times;
};
/**
@@ -127,35 +127,24 @@ METHOD(mutex_t, lock_r, void,
{
pthread_t self = pthread_self();
- if (this->thread == self)
+ if (pthread_equal(this->thread, self))
{
- uintptr_t times;
-
- /* times++ */
- times = (uintptr_t)pthread_getspecific(this->times);
- pthread_setspecific(this->times, (void*)times + 1);
+ this->times++;
}
else
{
lock(&this->generic);
this->thread = self;
- /* times = 1 */
- pthread_setspecific(this->times, (void*)1);
+ this->times = 1;
}
}
METHOD(mutex_t, unlock_r, void,
private_r_mutex_t *this)
{
- uintptr_t times;
-
- /* times-- */
- times = (uintptr_t)pthread_getspecific(this->times);
- pthread_setspecific(this->times, (void*)--times);
-
- if (times == 0)
+ if (--this->times == 0)
{
- this->thread = 0;
+ memset(&this->thread, 0, sizeof(this->thread));
unlock(&this->generic);
}
}
@@ -173,7 +162,6 @@ METHOD(mutex_t, mutex_destroy_r, void,
{
profiler_cleanup(&this->generic.profile);
pthread_mutex_destroy(&this->generic.mutex);
- pthread_key_delete(this->times);
free(this);
}
@@ -200,7 +188,6 @@ mutex_t *mutex_create(mutex_type_t type)
);
pthread_mutex_init(&this->generic.mutex, NULL);
- pthread_key_create(&this->times, NULL);
profiler_init(&this->generic.profile);
return &this->generic.public;
@@ -233,11 +220,15 @@ METHOD(condvar_t, wait_, void,
if (mutex->recursive)
{
private_r_mutex_t* recursive = (private_r_mutex_t*)mutex;
+ u_int times;
+ /* keep track of the number of times this thread locked the mutex */
+ times = recursive->times;
/* mutex owner gets cleared during condvar wait */
- recursive->thread = 0;
+ memset(&recursive->thread, 0, sizeof(recursive->thread));
pthread_cond_wait(&this->condvar, &mutex->mutex);
recursive->thread = pthread_self();
+ recursive->times = times;
}
else
{
@@ -262,11 +253,14 @@ METHOD(condvar_t, timed_wait_abs, bool,
if (mutex->recursive)
{
private_r_mutex_t* recursive = (private_r_mutex_t*)mutex;
+ u_int times;
- recursive->thread = 0;
+ times = recursive->times;
+ memset(&recursive->thread, 0, sizeof(recursive->thread));
timed_out = pthread_cond_timedwait(&this->condvar, &mutex->mutex,
&ts) == ETIMEDOUT;
recursive->thread = pthread_self();
+ recursive->times = times;
}
else
{
diff --git a/src/libstrongswan/threading/rwlock.c b/src/libstrongswan/threading/rwlock.c
index 15dc0b334..7097a8e8c 100644
--- a/src/libstrongswan/threading/rwlock.c
+++ b/src/libstrongswan/threading/rwlock.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2008-2009 Tobias Brunner
+ * Copyright (C) 2008-2012 Tobias Brunner
* Copyright (C) 2008 Martin Willi
* Hochschule fuer Technik Rapperswil
*
@@ -21,11 +21,14 @@
#include <debug.h>
#include "rwlock.h"
+#include "rwlock_condvar.h"
+#include "thread.h"
#include "condvar.h"
#include "mutex.h"
#include "lock_profiler.h"
typedef struct private_rwlock_t private_rwlock_t;
+typedef struct private_rwlock_condvar_t private_rwlock_condvar_t;
/**
* private data of rwlock
@@ -72,9 +75,9 @@ struct private_rwlock_t {
u_int reader_count;
/**
- * current writer thread, if any
+ * TRUE, if a writer is holding the lock currently
*/
- pthread_t writer;
+ bool writer;
#endif /* HAVE_PTHREAD_RWLOCK_INIT */
@@ -84,6 +87,27 @@ struct private_rwlock_t {
lock_profile_t profile;
};
+/**
+ * private data of condvar
+ */
+struct private_rwlock_condvar_t {
+
+ /**
+ * public interface
+ */
+ rwlock_condvar_t public;
+
+ /**
+ * mutex used to implement rwlock condvar
+ */
+ mutex_t *mutex;
+
+ /**
+ * regular condvar to implement rwlock condvar
+ */
+ condvar_t *condvar;
+};
+
#ifdef HAVE_PTHREAD_RWLOCK_INIT
@@ -175,37 +199,81 @@ rwlock_t *rwlock_create(rwlock_type_t type)
/**
* This implementation of the rwlock_t interface uses mutex_t and condvar_t
- * primitives, if the pthread_rwlock_* group of functions is not available.
+ * primitives, if the pthread_rwlock_* group of functions is not available or
+ * don't allow recursive locking for readers.
*
* The following constraints are enforced:
* - Multiple readers can hold the lock at the same time.
* - Only a single writer can hold the lock at any given time.
* - A writer must block until all readers have released the lock before
* obtaining the lock exclusively.
- * - Readers that arrive while a writer is waiting to acquire the lock will
- * block until after the writer has obtained and released the lock.
+ * - Readers that don't hold any read lock and arrive while a writer is
+ * waiting to acquire the lock will block until after the writer has
+ * obtained and released the lock.
* These constraints allow for read sharing, prevent write sharing, prevent
- * read-write sharing and prevent starvation of writers by a steady stream
- * of incoming readers. Reader starvation is not prevented (this could happen
- * if there are more writers than readers).
+ * read-write sharing and (largely) prevent starvation of writers by a steady
+ * stream of incoming readers. Reader starvation is not prevented (this could
+ * happen if there are more writers than readers).
*
- * The implementation does not support recursive locking and readers must not
- * acquire the lock exclusively at the same time and vice-versa (this is not
- * checked or enforced so behave yourself to prevent deadlocks).
+ * The implementation supports recursive locking of the read lock but not of
+ * the write lock. Readers must not acquire the lock exclusively at the same
+ * time and vice-versa (this is not checked or enforced so behave yourself to
+ * prevent deadlocks).
+ *
+ * Since writers are preferred a thread currently holding the read lock that
+ * tries to acquire the read lock recursively while a writer is waiting would
+ * result in a deadlock. In order to avoid having to use a thread-specific
+ * value for each rwlock_t (or a list of threads) to keep track if a thread
+ * already acquired the read lock we use a single thread-specific value for all
+ * rwlock_t objects that keeps track of how many read locks a thread currently
+ * holds. Preferring readers that already hold ANY read locks prevents this
+ * deadlock while it still largely avoids writer starvation (for locks that can
+ * only be acquired while holding another read lock this will obviously not
+ * work).
*/
+/**
+ * Keep track of how many read locks a thread holds.
+ */
+static pthread_key_t is_reader;
+
+/**
+ * Only initialize the read lock counter once.
+ */
+static pthread_once_t is_reader_initialized = PTHREAD_ONCE_INIT;
+
+/**
+ * Initialize the read lock counter.
+ */
+static void initialize_is_reader()
+{
+ pthread_key_create(&is_reader, NULL);
+}
+
METHOD(rwlock_t, read_lock, void,
private_rwlock_t *this)
{
+ uintptr_t reading;
+
+ reading = (uintptr_t)pthread_getspecific(is_reader);
profiler_start(&this->profile);
this->mutex->lock(this->mutex);
- while (this->writer || this->waiting_writers)
+ if (!this->writer && reading > 0)
{
- this->readers->wait(this->readers, this->mutex);
+ /* directly allow threads that hold ANY read locks, to avoid a deadlock
+ * caused by preferring writers in the loop below */
+ }
+ else
+ {
+ while (this->writer || this->waiting_writers)
+ {
+ this->readers->wait(this->readers, this->mutex);
+ }
}
this->reader_count++;
profiler_end(&this->profile);
this->mutex->unlock(this->mutex);
+ pthread_setspecific(is_reader, (void*)(reading + 1));
}
METHOD(rwlock_t, write_lock, void,
@@ -219,7 +287,7 @@ METHOD(rwlock_t, write_lock, void,
this->writers->wait(this->writers, this->mutex);
}
this->waiting_writers--;
- this->writer = pthread_self();
+ this->writer = TRUE;
profiler_end(&this->profile);
this->mutex->unlock(this->mutex);
}
@@ -231,8 +299,7 @@ METHOD(rwlock_t, try_write_lock, bool,
this->mutex->lock(this->mutex);
if (!this->writer && !this->reader_count)
{
- res = TRUE;
- this->writer = pthread_self();
+ res = this->writer = TRUE;
}
this->mutex->unlock(this->mutex);
return res;
@@ -242,9 +309,20 @@ METHOD(rwlock_t, unlock, void,
private_rwlock_t *this)
{
this->mutex->lock(this->mutex);
- if (this->writer == pthread_self())
+ if (this->writer)
+ {
+ this->writer = FALSE;
+ }
+ else
+ {
+ uintptr_t reading;
+
+ this->reader_count--;
+ reading = (uintptr_t)pthread_getspecific(is_reader);
+ pthread_setspecific(is_reader, (void*)(reading - 1));
+ }
+ if (!this->reader_count)
{
- this->writer = 0;
if (this->waiting_writers)
{
this->writers->signal(this->writers);
@@ -254,14 +332,6 @@ METHOD(rwlock_t, unlock, void,
this->readers->broadcast(this->readers);
}
}
- else
- {
- this->reader_count--;
- if (!this->reader_count)
- {
- this->writers->signal(this->writers);
- }
- }
this->mutex->unlock(this->mutex);
}
@@ -280,6 +350,8 @@ METHOD(rwlock_t, destroy, void,
*/
rwlock_t *rwlock_create(rwlock_type_t type)
{
+ pthread_once(&is_reader_initialized, initialize_is_reader);
+
switch (type)
{
case RWLOCK_TYPE_DEFAULT:
@@ -309,3 +381,110 @@ rwlock_t *rwlock_create(rwlock_type_t type)
#endif /* HAVE_PTHREAD_RWLOCK_INIT */
+
+METHOD(rwlock_condvar_t, wait_, void,
+ private_rwlock_condvar_t *this, rwlock_t *lock)
+{
+ /* at this point we have the write lock locked, to make signals more
+ * predictable we try to prevent other threads from signaling by acquiring
+ * the mutex while we still hold the write lock (this assumes they will
+ * hold the write lock themselves when signaling, which is not mandatory) */
+ this->mutex->lock(this->mutex);
+ /* unlock the rwlock and wait for a signal */
+ lock->unlock(lock);
+ /* if the calling thread enabled thread cancelability we want to replicate
+ * the behavior of the regular condvar, i.e. the lock will be held again
+ * before executing cleanup functions registered by the calling thread */
+ thread_cleanup_push((thread_cleanup_t)lock->write_lock, lock);
+ thread_cleanup_push((thread_cleanup_t)this->mutex->unlock, this->mutex);
+ this->condvar->wait(this->condvar, this->mutex);
+ /* we release the mutex to allow other threads into the condvar (might even
+ * be required so we can acquire the lock again below) */
+ thread_cleanup_pop(TRUE);
+ /* finally we reacquire the lock we held previously */
+ thread_cleanup_pop(TRUE);
+}
+
+METHOD(rwlock_condvar_t, timed_wait_abs, bool,
+ private_rwlock_condvar_t *this, rwlock_t *lock, timeval_t time)
+{
+ bool timed_out;
+
+ /* see wait() above for details on what is going on here */
+ this->mutex->lock(this->mutex);
+ lock->unlock(lock);
+ thread_cleanup_push((thread_cleanup_t)lock->write_lock, lock);
+ thread_cleanup_push((thread_cleanup_t)this->mutex->unlock, this->mutex);
+ timed_out = this->condvar->timed_wait_abs(this->condvar, this->mutex, time);
+ thread_cleanup_pop(TRUE);
+ thread_cleanup_pop(!timed_out);
+ return timed_out;
+}
+
+METHOD(rwlock_condvar_t, timed_wait, bool,
+ private_rwlock_condvar_t *this, rwlock_t *lock, u_int timeout)
+{
+ timeval_t tv;
+ u_int s, ms;
+
+ time_monotonic(&tv);
+
+ s = timeout / 1000;
+ ms = timeout % 1000;
+
+ tv.tv_sec += s;
+ tv.tv_usec += ms * 1000;
+
+ if (tv.tv_usec > 1000000 /* 1s */)
+ {
+ tv.tv_usec -= 1000000;
+ tv.tv_sec++;
+ }
+ return timed_wait_abs(this, lock, tv);
+}
+
+METHOD(rwlock_condvar_t, signal_, void,
+ private_rwlock_condvar_t *this)
+{
+ this->mutex->lock(this->mutex);
+ this->condvar->signal(this->condvar);
+ this->mutex->unlock(this->mutex);
+}
+
+METHOD(rwlock_condvar_t, broadcast, void,
+ private_rwlock_condvar_t *this)
+{
+ this->mutex->lock(this->mutex);
+ this->condvar->broadcast(this->condvar);
+ this->mutex->unlock(this->mutex);
+}
+
+METHOD(rwlock_condvar_t, condvar_destroy, void,
+ private_rwlock_condvar_t *this)
+{
+ this->condvar->destroy(this->condvar);
+ this->mutex->destroy(this->mutex);
+ free(this);
+}
+
+/*
+ * see header file
+ */
+rwlock_condvar_t *rwlock_condvar_create()
+{
+ private_rwlock_condvar_t *this;
+
+ INIT(this,
+ .public = {
+ .wait = _wait_,
+ .timed_wait = _timed_wait,
+ .timed_wait_abs = _timed_wait_abs,
+ .signal = _signal_,
+ .broadcast = _broadcast,
+ .destroy = _condvar_destroy,
+ },
+ .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
+ .condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
+ );
+ return &this->public;
+}
diff --git a/src/libstrongswan/threading/rwlock_condvar.h b/src/libstrongswan/threading/rwlock_condvar.h
new file mode 100644
index 000000000..2b40c3fc6
--- /dev/null
+++ b/src/libstrongswan/threading/rwlock_condvar.h
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2012 Tobias Brunner
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+/**
+ * @defgroup rwlock_condvar rwlock_condvar
+ * @{ @ingroup threading
+ */
+
+#ifndef RWLOCK_CONDVAR_H_
+#define RWLOCK_CONDVAR_H_
+
+typedef struct rwlock_condvar_t rwlock_condvar_t;
+
+#include "rwlock.h"
+
+/**
+ * A special condvar implementation that can be used in conjunction
+ * with rwlock_t (the write lock to be precise).
+ *
+ * @note The implementation does not verify that the current thread actually
+ * holds the write lock and not the read lock, so watch out.
+ */
+struct rwlock_condvar_t {
+
+ /**
+ * Wait on a condvar until it gets signalized.
+ *
+ * @param lock lock to release while waiting (write lock)
+ */
+ void (*wait)(rwlock_condvar_t *this, rwlock_t *lock);
+
+ /**
+ * Wait on a condvar until it gets signalized, or times out.
+ *
+ * @param lock lock to release while waiting (write lock)
+ * @param timeout timeout im ms
+ * @return TRUE if timed out, FALSE otherwise
+ */
+ bool (*timed_wait)(rwlock_condvar_t *this, rwlock_t *lock, u_int timeout);
+
+ /**
+ * Wait on a condvar until it gets signalized, or times out.
+ *
+ * The passed timeval should be calculated based on the time_monotonic()
+ * function.
+ *
+ * @param lock lock to release while waiting (write lock)
+ * @param tv absolute time until timeout
+ * @return TRUE if timed out, FALSE otherwise
+ */
+ bool (*timed_wait_abs)(rwlock_condvar_t *this, rwlock_t *lock,
+ timeval_t tv);
+
+ /**
+ * Wake up a single thread in a condvar.
+ */
+ void (*signal)(rwlock_condvar_t *this);
+
+ /**
+ * Wake up all threads in a condvar.
+ */
+ void (*broadcast)(rwlock_condvar_t *this);
+
+ /**
+ * Destroy a condvar and free its resources.
+ */
+ void (*destroy)(rwlock_condvar_t *this);
+};
+
+/**
+ * Create a condvar instance.
+ *
+ * @return condvar instance
+ */
+rwlock_condvar_t *rwlock_condvar_create();
+
+#endif /** RWLOCK_CONDVAR_H_ @} */
+
diff --git a/src/libstrongswan/threading/semaphore.c b/src/libstrongswan/threading/semaphore.c
new file mode 100644
index 000000000..b785ff944
--- /dev/null
+++ b/src/libstrongswan/threading/semaphore.c
@@ -0,0 +1,179 @@
+/*
+ * Copyright (C) 2011 Tobias Brunner
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+#include <library.h>
+
+#if defined(HAVE_CLOCK_GETTIME) && \
+ (defined(HAVE_CONDATTR_CLOCK_MONOTONIC) || \
+ defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC))
+/* if we use MONOTONIC times, we can't use POSIX_SEMAPHORES since they use
+ * times based on CLOCK_REALTIME */
+#undef HAVE_SEM_TIMEDWAIT
+#endif /* HAVE_CLOCK_GETTIME && ... */
+
+#ifdef HAVE_SEM_TIMEDWAIT
+#include <semaphore.h>
+#else /* !HAVE_SEM_TIMEDWAIT */
+#include <threading/condvar.h>
+#endif /* HAVE_SEM_TIMEDWAIT */
+
+#include "semaphore.h"
+
+typedef struct private_semaphore_t private_semaphore_t;
+
+/**
+ * private data of a semaphore
+ */
+struct private_semaphore_t {
+ /**
+ * public interface
+ */
+ semaphore_t public;
+
+#ifdef HAVE_SEM_TIMEDWAIT
+ /**
+ * wrapped POSIX semaphore object
+ */
+ sem_t sem;
+#else /* !HAVE_SEM_TIMEDWAIT */
+
+ /**
+ * Mutex to lock count variable
+ */
+ mutex_t *mutex;
+
+ /**
+ * Condvar to signal count increase
+ */
+ condvar_t *cond;
+
+ /**
+ * Semaphore count value
+ */
+ u_int count;
+#endif /* HAVE_SEM_TIMEDWAIT */
+};
+
+METHOD(semaphore_t, wait_, void,
+ private_semaphore_t *this)
+{
+#ifdef HAVE_SEM_TIMEDWAIT
+ sem_wait(&this->sem);
+#else /* !HAVE_SEM_TIMEDWAIT */
+ this->mutex->lock(this->mutex);
+ while (this->count == 0)
+ {
+ this->cond->wait(this->cond, this->mutex);
+ }
+ this->count--;
+ this->mutex->unlock(this->mutex);
+#endif /* HAVE_SEM_TIMEDWAIT */
+}
+
+METHOD(semaphore_t, timed_wait_abs, bool,
+ private_semaphore_t *this, timeval_t tv)
+{
+#ifdef HAVE_SEM_TIMEDWAIT
+ timespec_t ts;
+
+ ts.tv_sec = tv.tv_sec;
+ ts.tv_nsec = tv.tv_usec * 1000;
+
+ /* there are errors other than ETIMEDOUT possible, but we consider them
+ * all as timeout */
+ return sem_timedwait(&this->sem, &ts) == -1;
+#else /* !HAVE_SEM_TIMEDWAIT */
+ this->mutex->lock(this->mutex);
+ while (this->count == 0)
+ {
+ if (this->cond->timed_wait_abs(this->cond, this->mutex, tv))
+ {
+ this->mutex->unlock(this->mutex);
+ return TRUE;
+ }
+ }
+ this->count--;
+ this->mutex->unlock(this->mutex);
+ return FALSE;
+#endif /* HAVE_SEM_TIMEDWAIT */
+}
+
+METHOD(semaphore_t, timed_wait, bool,
+ private_semaphore_t *this, u_int timeout)
+{
+ timeval_t tv, add;
+
+ add.tv_sec = timeout / 1000;
+ add.tv_usec = (timeout % 1000) * 1000;
+
+ time_monotonic(&tv);
+ timeradd(&tv, &add, &tv);
+
+ return timed_wait_abs(this, tv);
+}
+
+METHOD(semaphore_t, post, void,
+ private_semaphore_t *this)
+{
+#ifdef HAVE_SEM_TIMEDWAIT
+ sem_post(&this->sem);
+#else /* !HAVE_SEM_TIMEDWAIT */
+ this->mutex->lock(this->mutex);
+ this->count++;
+ this->mutex->unlock(this->mutex);
+ this->cond->signal(this->cond);
+#endif /* HAVE_SEM_TIMEDWAIT */
+}
+
+METHOD(semaphore_t, destroy, void,
+ private_semaphore_t *this)
+{
+#ifdef HAVE_SEM_TIMEDWAIT
+ sem_destroy(&this->sem);
+#else /* !HAVE_SEM_TIMEDWAIT */
+ this->cond->destroy(this->cond);
+ this->mutex->destroy(this->mutex);
+#endif /* HAVE_SEM_TIMEDWAIT */
+ free(this);
+}
+
+/*
+ * Described in header
+ */
+semaphore_t *semaphore_create(u_int value)
+{
+ private_semaphore_t *this;
+
+ INIT(this,
+ .public = {
+ .wait = _wait_,
+ .timed_wait = _timed_wait,
+ .timed_wait_abs = _timed_wait_abs,
+ .post = _post,
+ .destroy = _destroy,
+ },
+ );
+
+#ifdef HAVE_SEM_TIMEDWAIT
+ sem_init(&this->sem, 0, value);
+#else /* !HAVE_SEM_TIMEDWAIT */
+ this->mutex = mutex_create(MUTEX_TYPE_DEFAULT);
+ this->cond = condvar_create(CONDVAR_TYPE_DEFAULT);
+ this->count = value;
+#endif /* HAVE_SEM_TIMEDWAIT */
+
+ return &this->public;
+}
+
diff --git a/src/libstrongswan/threading/semaphore.h b/src/libstrongswan/threading/semaphore.h
new file mode 100644
index 000000000..cdb0a6f19
--- /dev/null
+++ b/src/libstrongswan/threading/semaphore.h
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2011 Tobias Brunner
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+/**
+ * @defgroup semaphore semaphore
+ * @{ @ingroup threading
+ */
+
+#ifndef THREADING_SEMAPHORE_H_
+#define THREADING_SEMAPHORE_H_
+
+typedef struct semaphore_t semaphore_t;
+
+/**
+ * A semaphore is basically an integer whose value is never allowed to be
+ * lower than 0. Two operations can be performed on it: increment the
+ * value by one, and decrement the value by one. If the value is currently
+ * zero, then the decrement operation will blcok until the value becomes
+ * greater than zero.
+ */
+struct semaphore_t {
+
+ /**
+ * Decrease the value by one, if it is greater than zero. Otherwise the
+ * current thread is blocked and it waits until the value increases.
+ */
+ void (*wait)(semaphore_t *this);
+
+ /**
+ * Decrease the value by one, if it is greater than zero. Otherwise the
+ * current thread is blocked and it waits until the value increases, or the
+ * call times out.
+ *
+ * @param timeout timeout im ms
+ * @return TRUE if timed out, FALSE otherwise
+ */
+ bool (*timed_wait)(semaphore_t *this, u_int timeout);
+
+ /**
+ * Decrease the value by one, if it is greater than zero. Otherwise the
+ * current thread is blocked and it waits until the value increases, or the
+ * call times out.
+ *
+ * The passed timeval should be calculated based on the time_monotonic()
+ * function.
+ *
+ * @param tv absolute time until timeout
+ * @return TRUE if timed out, FALSE otherwise
+ */
+ bool (*timed_wait_abs)(semaphore_t *this, timeval_t tv);
+
+ /**
+ * Increase the value by one. If the value becomes greater than zero, then
+ * another thread waiting will be woken up.
+ */
+ void (*post)(semaphore_t *this);
+
+ /**
+ * Destroy a semaphore and free its resources.
+ */
+ void (*destroy)(semaphore_t *this);
+};
+
+/**
+ * Create a semaphore instance.
+ *
+ * @param value initial value (typically 0)
+ * @return semaphore instance
+ */
+semaphore_t *semaphore_create(u_int value);
+
+#endif /** THREADING_SEMAPHORE_H_ @} */
+
diff --git a/src/libstrongswan/threading/spinlock.c b/src/libstrongswan/threading/spinlock.c
new file mode 100644
index 000000000..812cf696b
--- /dev/null
+++ b/src/libstrongswan/threading/spinlock.c
@@ -0,0 +1,136 @@
+/*
+ * Copyright (C) 2012 Tobias Brunner
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+#include <unistd.h> /* for _POSIX_SPIN_LOCKS */
+#include <pthread.h>
+
+#include <library.h>
+#include <debug.h>
+
+#include "spinlock.h"
+#include "mutex.h"
+#include "lock_profiler.h"
+
+#if defined(_POSIX_SPIN_LOCKS) && _POSIX_SPIN_LOCKS == -1
+#undef _POSIX_SPIN_LOCKS
+#endif
+
+typedef struct private_spinlock_t private_spinlock_t;
+
+/**
+ * private data
+ */
+struct private_spinlock_t {
+
+ /**
+ * public functions
+ */
+ spinlock_t public;
+
+#ifdef _POSIX_SPIN_LOCKS
+
+ /**
+ * wrapped pthread spin lock
+ */
+ pthread_spinlock_t spinlock;
+
+ /**
+ * profiling info, if enabled (the mutex below does profile itself)
+ */
+ lock_profile_t profile;
+
+#else /* _POSIX_SPIN_LOCKS */
+
+ /**
+ * use a mutex if spin locks are not available
+ */
+ mutex_t *mutex;
+
+#endif /* _POSIX_SPIN_LOCKS */
+};
+
+METHOD(spinlock_t, lock, void,
+ private_spinlock_t *this)
+{
+#ifdef _POSIX_SPIN_LOCKS
+ int err;
+
+ profiler_start(&this->profile);
+ err = pthread_spin_lock(&this->spinlock);
+ if (err)
+ {
+ DBG1(DBG_LIB, "!!! SPIN LOCK LOCK ERROR: %s !!!", strerror(err));
+ }
+ profiler_end(&this->profile);
+#else
+ this->mutex->lock(this->mutex);
+#endif
+}
+
+METHOD(spinlock_t, unlock, void,
+ private_spinlock_t *this)
+{
+#ifdef _POSIX_SPIN_LOCKS
+ int err;
+
+ err = pthread_spin_unlock(&this->spinlock);
+ if (err)
+ {
+ DBG1(DBG_LIB, "!!! SPIN LOCK UNLOCK ERROR: %s !!!", strerror(err));
+ }
+#else
+ this->mutex->unlock(this->mutex);
+#endif
+}
+
+METHOD(spinlock_t, destroy, void,
+ private_spinlock_t *this)
+{
+#ifdef _POSIX_SPIN_LOCKS
+ profiler_cleanup(&this->profile);
+ pthread_spin_destroy(&this->spinlock);
+#else
+ this->mutex->destroy(this->mutex);
+#endif
+ free(this);
+}
+
+/*
+ * Described in header
+ */
+spinlock_t *spinlock_create()
+{
+ private_spinlock_t *this;
+
+ INIT(this,
+ .public = {
+ .lock = _lock,
+ .unlock = _unlock,
+ .destroy = _destroy,
+ },
+ );
+
+#ifdef _POSIX_SPIN_LOCKS
+ pthread_spin_init(&this->spinlock, PTHREAD_PROCESS_PRIVATE);
+ profiler_init(&this->profile);
+#else
+ #warning Using mutexes as spin lock alternatives
+ this->mutex = mutex_create(MUTEX_TYPE_DEFAULT);
+#endif
+
+ return &this->public;
+}
+
+
diff --git a/src/libstrongswan/threading/spinlock.h b/src/libstrongswan/threading/spinlock.h
new file mode 100644
index 000000000..883980cc2
--- /dev/null
+++ b/src/libstrongswan/threading/spinlock.h
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2012 Tobias Brunner
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+/**
+ * @defgroup spinlock spinlock
+ * @{ @ingroup threading
+ */
+
+#ifndef THREADING_SPINLOCK_H_
+#define THREADING_SPINLOCK_H_
+
+typedef struct spinlock_t spinlock_t;
+
+/**
+ * Spin lock wrapper implements a lock with low overhead when the lock is held
+ * only for a short time (waiting wastes processor cycles, though).
+ *
+ * If native spin locks are not available regular mutexes are used as fallback.
+ */
+struct spinlock_t {
+
+ /**
+ * Acquire the lock.
+ */
+ void (*lock)(spinlock_t *this);
+
+ /**
+ * Release the lock.
+ */
+ void (*unlock)(spinlock_t *this);
+
+ /**
+ * Destroy the instance.
+ */
+ void (*destroy)(spinlock_t *this);
+};
+
+/**
+ * Create a spin lock instance.
+ *
+ * @return unlocked instance
+ */
+spinlock_t *spinlock_create();
+
+#endif /** THREADING_SPINLOCK_H_ @} */
+
diff --git a/src/libstrongswan/threading/thread.c b/src/libstrongswan/threading/thread.c
index 49a1b8430..9ef514ebc 100644
--- a/src/libstrongswan/threading/thread.c
+++ b/src/libstrongswan/threading/thread.c
@@ -114,7 +114,7 @@ typedef struct {
/**
* Next thread ID.
*/
-static u_int next_id = 1;
+static u_int next_id;
/**
* Mutex to safely access the next thread ID.
@@ -452,6 +452,7 @@ void threads_init()
dummy1 = thread_value_create(NULL);
+ next_id = 1;
main_thread->id = 0;
main_thread->thread_id = pthread_self();
current_thread = thread_value_create(NULL);
@@ -482,4 +483,3 @@ void threads_deinit()
current_thread->destroy(current_thread);
id_mutex->destroy(id_mutex);
}
-
diff --git a/src/libstrongswan/threading/thread_value.c b/src/libstrongswan/threading/thread_value.c
index 3fa70acb2..190b7434f 100644
--- a/src/libstrongswan/threading/thread_value.c
+++ b/src/libstrongswan/threading/thread_value.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009 Tobias Brunner
+ * Copyright (C) 2009-2012 Tobias Brunner
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
@@ -33,6 +33,11 @@ struct private_thread_value_t {
*/
pthread_key_t key;
+ /**
+ * Destructor to cleanup the value of the thread destroying this object
+ */
+ thread_cleanup_t destructor;
+
};
METHOD(thread_value_t, set, void,
@@ -50,11 +55,22 @@ METHOD(thread_value_t, get, void*,
METHOD(thread_value_t, destroy, void,
private_thread_value_t *this)
{
+ void *val;
+
+ /* the destructor is not called automatically for the thread calling
+ * pthread_key_delete() */
+ if (this->destructor)
+ {
+ val = pthread_getspecific(this->key);
+ if (val)
+ {
+ this->destructor(val);
+ }
+ }
pthread_key_delete(this->key);
free(this);
}
-
/**
* Described in header.
*/
@@ -68,6 +84,7 @@ thread_value_t *thread_value_create(thread_cleanup_t destructor)
.get = _get,
.destroy = _destroy,
},
+ .destructor = destructor,
);
pthread_key_create(&this->key, destructor);