diff options
Diffstat (limited to 'src/libstrongswan/threading/rwlock.h')
-rw-r--r-- | src/libstrongswan/threading/rwlock.h | 82 |
1 files changed, 40 insertions, 42 deletions
diff --git a/src/libstrongswan/threading/rwlock.h b/src/libstrongswan/threading/rwlock.h index 2f4330ffb..a86a241c5 100644 --- a/src/libstrongswan/threading/rwlock.h +++ b/src/libstrongswan/threading/rwlock.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008 Tobias Brunner + * Copyright (C) 2008-2009 Tobias Brunner * Copyright (C) 2008 Martin Willi * Hochschule fuer Technik Rapperswil * @@ -14,69 +14,67 @@ * for more details. */ +/** + * @defgroup rwlock rwlock + * @{ @ingroup threading + */ + #ifndef THREADING_RWLOCK_H_ #define THREADING_RWLOCK_H_ -#include "lock_profiler.h" - -typedef struct private_rwlock_t private_rwlock_t; +typedef struct rwlock_t rwlock_t; +typedef enum rwlock_type_t rwlock_type_t; /** - * private data of rwlock + * Type of read-write lock. */ -struct private_rwlock_t { - - /** - * public functions - */ - rwlock_t public; - -#ifdef HAVE_PTHREAD_RWLOCK_INIT - - /** - * wrapped pthread rwlock - */ - pthread_rwlock_t rwlock; - -#else - - /** - * mutex to emulate a native rwlock - */ - mutex_t *mutex; +enum rwlock_type_t { + /** default condvar */ + RWLOCK_TYPE_DEFAULT = 0, +}; - /** - * condvar to handle writers - */ - condvar_t *writers; +/** + * Read-Write lock wrapper. + */ +struct rwlock_t { /** - * condvar to handle readers + * Acquire the read lock. */ - condvar_t *readers; + void (*read_lock)(rwlock_t *this); /** - * number of waiting writers + * Acquire the write lock. */ - u_int waiting_writers; + void (*write_lock)(rwlock_t *this); /** - * number of readers holding the lock + * Try to acquire the write lock. + * + * Never blocks, but returns FALSE if the lock was already occupied. + * + * @return TRUE if lock acquired */ - u_int reader_count; + bool (*try_write_lock)(rwlock_t *this); /** - * current writer thread, if any + * Release any acquired lock. */ - pthread_t writer; - -#endif /* HAVE_PTHREAD_RWLOCK_INIT */ + void (*unlock)(rwlock_t *this); /** - * profiling info, if enabled + * Destroy the read-write lock. */ - lock_profile_t profile; + void (*destroy)(rwlock_t *this); }; -#endif /* THREADING_THREADING_H_ */ +/** + * Create a read-write lock instance. + * + * @param type type of rwlock to create + * @return unlocked rwlock instance + */ +rwlock_t *rwlock_create(rwlock_type_t type); + +#endif /** THREADING_RWLOCK_H_ @} */ |