From c532ad114ef3f9964077522d137e6e1355a1ec1b Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 30 Sep 2010 17:28:21 -0400 Subject: Drop rwlock in progress.c on errors. --- libdimension/progress.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/libdimension/progress.c b/libdimension/progress.c index f7a8b62..51e7992 100644 --- a/libdimension/progress.c +++ b/libdimension/progress.c @@ -28,9 +28,20 @@ typedef struct { } dmnsn_progress_element; /* For thread synchronization */ -static void dmnsn_progress_rdlock(const dmnsn_progress *progress); -static void dmnsn_progress_wrlock(dmnsn_progress *progress); -static void dmnsn_progress_unlock(const dmnsn_progress *progress); + +static void dmnsn_progress_rdlock_impl(const dmnsn_progress *progress); +static void dmnsn_progress_wrlock_impl(dmnsn_progress *progress); +static void dmnsn_progress_unlock_impl(void *arg); + +#define dmnsn_progress_rdlock(progress) \ + dmnsn_progress_rdlock_impl(progress); \ + pthread_cleanup_push(&dmnsn_progress_unlock_impl, (void *)progress); +#define dmnsn_progress_wrlock(progress) \ + dmnsn_progress_wrlock_impl(progress); \ + pthread_cleanup_push(&dmnsn_progress_unlock_impl, (void *)progress); +#define dmnsn_progress_unlock(progress) \ + pthread_cleanup_pop(1); + /* Allocate a new dmnsn_progress* */ dmnsn_progress * @@ -222,7 +233,7 @@ dmnsn_done_progress(dmnsn_progress *progress) /* Thread synchronization */ static void -dmnsn_progress_rdlock(const dmnsn_progress *progress) +dmnsn_progress_rdlock_impl(const dmnsn_progress *progress) { if (pthread_rwlock_rdlock(progress->rwlock) != 0) { dmnsn_error(DMNSN_SEVERITY_MEDIUM, "Couldn't acquire read-lock."); @@ -230,7 +241,7 @@ dmnsn_progress_rdlock(const dmnsn_progress *progress) } static void -dmnsn_progress_wrlock(dmnsn_progress *progress) +dmnsn_progress_wrlock_impl(dmnsn_progress *progress) { if (pthread_rwlock_wrlock(progress->rwlock) != 0) { dmnsn_error(DMNSN_SEVERITY_MEDIUM, "Couldn't acquire write-lock."); @@ -238,8 +249,9 @@ dmnsn_progress_wrlock(dmnsn_progress *progress) } static void -dmnsn_progress_unlock(const dmnsn_progress *progress) +dmnsn_progress_unlock_impl(void *arg) { + const dmnsn_progress *progress = arg; if (pthread_rwlock_unlock(progress->rwlock) != 0) { dmnsn_error(DMNSN_SEVERITY_MEDIUM, "Couldn't unlock read-write lock."); } -- cgit v1.2.3