From efedd0d21e4caf6edae6872f29c470a464e1917a Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Fri, 11 Apr 2014 15:13:22 +0200 Subject: utils: Add ref_cur() to retrieve the current value of a reference counter On many architectures it is safe to read the value directly (those using cache coherency protocols, and with atomic loads for 32-bit values) but it is not if that's not the case or if we ever decide to make refcount_t 64-bit (load not atomic on x86). So make sure the operation is actually atomic and that users do not have to care about the size of refcount_t. --- src/libstrongswan/utils/utils.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'src/libstrongswan/utils/utils.c') diff --git a/src/libstrongswan/utils/utils.c b/src/libstrongswan/utils/utils.c index fe80edb82..e4da6ddb9 100644 --- a/src/libstrongswan/utils/utils.c +++ b/src/libstrongswan/utils/utils.c @@ -528,7 +528,6 @@ refcount_t ref_get(refcount_t *ref) pthread_mutex_lock(&ref_mutex); current = ++(*ref); pthread_mutex_unlock(&ref_mutex); - return current; } @@ -545,6 +544,19 @@ bool ref_put(refcount_t *ref) return !more_refs; } +/** + * Current refcount + */ +refcount_t ref_cur(refcount_t *ref) +{ + refcount_t current; + + pthread_mutex_lock(&ref_mutex); + current = *ref; + pthread_mutex_unlock(&ref_mutex); + return current; +} + /** * Single mutex for all compare and swap operations. */ -- cgit v1.2.3