diff --git a/src/lock/lock.c b/src/lock/lock.c index 1f5f2de08..cc8c2d6db 100644 --- a/src/lock/lock.c +++ b/src/lock/lock.c @@ -1,10 +1,9 @@ /** - * @file lock/lock.c Pthread mutex locking + * @file lock.c Pthread read/write locking * * Copyright (C) 2010 Creytiv.com */ -#define _DEFAULT_SOURCE 1 -#define __USE_UNIX98 1 +#define _GNU_SOURCE 1 #include #include #include @@ -16,14 +15,8 @@ #include -#ifndef RELEASE -#define LOCK_DEBUG 0 -#endif - - -/** Defines a lock */ struct lock { - pthread_mutex_t m; + pthread_rwlock_t lock; }; @@ -31,24 +24,17 @@ static void lock_destructor(void *data) { struct lock *l = data; - int err = pthread_mutex_destroy(&l->m); + int err = pthread_rwlock_destroy(&l->lock); if (err) { - DEBUG_WARNING("pthread_mutex_destroy: %m\n", err); + DEBUG_WARNING("pthread_rwlock_destroy: %m\n", err); } } -/** - * Allocate a new lock - * - * @param lp Pointer to allocated lock object - * - * @return 0 if success, otherwise errorcode - */ int lock_alloc(struct lock **lp) { - pthread_mutexattr_t attr; struct lock *l; + int err; if (!lp) return EINVAL; @@ -57,85 +43,71 @@ int lock_alloc(struct lock **lp) if (!l) return ENOMEM; - (void)pthread_mutex_init(&l->m, NULL); - - pthread_mutexattr_init(&attr); - -#if LOCK_DEBUG - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); - DEBUG_NOTICE("init debug lock\n"); -#else - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); -#endif - pthread_mutex_init(&l->m, &attr); + err = pthread_rwlock_init(&l->lock, NULL); + if (err) + goto out; *lp = l; - return 0; + + out: + if (err) + mem_deref(l); + return err; } -/** - * Get the lock for reading - * - * @param l Lock object - */ void lock_read_get(struct lock *l) { - const int err = pthread_mutex_lock(&l->m); + int err; + + if (!l) + return; + + err = pthread_rwlock_rdlock(&l->lock); if (err) { DEBUG_WARNING("lock_read_get: %m\n", err); } } -/** - * Get the lock for writing - * - * @param l Lock object - */ void lock_write_get(struct lock *l) { - const int err = pthread_mutex_lock(&l->m); + int err; + + if (!l) + return; + + err = pthread_rwlock_wrlock(&l->lock); if (err) { DEBUG_WARNING("lock_write_get: %m\n", err); } } -/** - * Attempt to get a lock for reading - * - * @param l Lock object - * - * @return 0 if success, otherwise errorcode - */ int lock_read_try(struct lock *l) { - return pthread_mutex_trylock(&l->m); + if (!l) + return EINVAL; + return pthread_rwlock_tryrdlock(&l->lock); } -/** - * Attempt to get a lock for writing - * - * @param l Lock object - * - * @return 0 if success, otherwise errorcode - */ int lock_write_try(struct lock *l) { - return pthread_mutex_trylock(&l->m); + if (!l) + return EINVAL; + return pthread_rwlock_trywrlock(&l->lock); } -/** - * Release a lock - * - * @param l Lock object - */ void lock_rel(struct lock *l) { - const int err = pthread_mutex_unlock(&l->m); + int err; + + if (!l) + return; + + err = pthread_rwlock_unlock(&l->lock); if (err) { DEBUG_WARNING("lock_rel: %m\n", err); } diff --git a/src/lock/mod.mk b/src/lock/mod.mk index 6884652a8..78598c1ab 100644 --- a/src/lock/mod.mk +++ b/src/lock/mod.mk @@ -4,9 +4,7 @@ # Copyright (C) 2010 Creytiv.com # -ifdef HAVE_PTHREAD_RWLOCK -SRCS += lock/rwlock.c -else ifdef HAVE_PTHREAD +ifdef HAVE_PTHREAD SRCS += lock/lock.c else ifeq ($(OS),win32) SRCS += lock/win32/lock.c diff --git a/src/lock/rwlock.c b/src/lock/rwlock.c deleted file mode 100644 index 0f6605f05..000000000 --- a/src/lock/rwlock.c +++ /dev/null @@ -1,114 +0,0 @@ -/** - * @file rwlock.c Pthread read/write locking - * - * Copyright (C) 2010 Creytiv.com - */ -#define _GNU_SOURCE 1 -#include -#include -#include -#include - - -#define DEBUG_MODULE "rwlock" -#define DEBUG_LEVEL 5 -#include - - -struct lock { - pthread_rwlock_t lock; -}; - - -static void lock_destructor(void *data) -{ - struct lock *l = data; - - int err = pthread_rwlock_destroy(&l->lock); - if (err) { - DEBUG_WARNING("pthread_rwlock_destroy: %m\n", err); - } -} - - -int lock_alloc(struct lock **lp) -{ - struct lock *l; - int err; - - if (!lp) - return EINVAL; - - l = mem_zalloc(sizeof(*l), lock_destructor); - if (!l) - return ENOMEM; - - err = pthread_rwlock_init(&l->lock, NULL); - if (err) - goto out; - - *lp = l; - - out: - if (err) - mem_deref(l); - return err; -} - - -void lock_read_get(struct lock *l) -{ - int err; - - if (!l) - return; - - err = pthread_rwlock_rdlock(&l->lock); - if (err) { - DEBUG_WARNING("lock_read_get: %m\n", err); - } -} - - -void lock_write_get(struct lock *l) -{ - int err; - - if (!l) - return; - - err = pthread_rwlock_wrlock(&l->lock); - if (err) { - DEBUG_WARNING("lock_write_get: %m\n", err); - } -} - - -int lock_read_try(struct lock *l) -{ - if (!l) - return EINVAL; - return pthread_rwlock_tryrdlock(&l->lock); -} - - -int lock_write_try(struct lock *l) -{ - if (!l) - return EINVAL; - return pthread_rwlock_trywrlock(&l->lock); -} - - -void lock_rel(struct lock *l) -{ - int err; - - if (!l) - return; - - err = pthread_rwlock_unlock(&l->lock); - if (err) { - DEBUG_WARNING("lock_rel: %m\n", err); - } -}