#ifndef ISC_REFCOUNT_H
#define ISC_REFCOUNT_H 1
+#include <isc/assertions.h>
#include <isc/atomic.h>
+#include <isc/error.h>
#include <isc/lang.h>
#include <isc/mutex.h>
#include <isc/platform.h>
#include <isc/types.h>
-#include <isc/util.h>
#if defined(ISC_PLATFORM_HAVESTDATOMIC)
#include <stdatomic.h>
#define isc_refcount_current(rp) \
((unsigned int)(atomic_load_explicit(&(rp)->refs, \
memory_order_relaxed)))
-#define isc_refcount_destroy(rp) REQUIRE(isc_refcount_current(rp) == 0)
+#define isc_refcount_destroy(rp) ISC_REQUIRE(isc_refcount_current(rp) == 0)
#define isc_refcount_increment0(rp, tp) \
do { \
isc_int32_t prev; \
prev = atomic_fetch_add_explicit \
(&(rp)->refs, 1, memory_order_relaxed); \
- REQUIRE(prev > 0); \
+ ISC_REQUIRE(prev > 0); \
if (_tmp != NULL) \
*_tmp = prev + 1; \
} while (0)
isc_int32_t prev; \
prev = atomic_fetch_sub_explicit \
(&(rp)->refs, 1, memory_order_relaxed); \
- REQUIRE(prev > 0); \
+ ISC_REQUIRE(prev > 0); \
if (_tmp != NULL) \
*_tmp = prev - 1; \
} while (0)
#define isc_refcount_current(rp) \
((unsigned int)(isc_atomic_xadd(&(rp)->refs, 0)))
-#define isc_refcount_destroy(rp) REQUIRE(isc_refcount_current(rp) == 0)
+#define isc_refcount_destroy(rp) ISC_REQUIRE(isc_refcount_current(rp) == 0)
#define isc_refcount_increment0(rp, tp) \
do { \
unsigned int *_tmp = (unsigned int *)(tp); \
isc_int32_t prev; \
prev = isc_atomic_xadd(&(rp)->refs, 1); \
- REQUIRE(prev > 0); \
+ ISC_REQUIRE(prev > 0); \
if (_tmp != NULL) \
*_tmp = prev + 1; \
} while (0)
unsigned int *_tmp = (unsigned int *)(tp); \
isc_int32_t prev; \
prev = isc_atomic_xadd(&(rp)->refs, -1); \
- REQUIRE(prev > 0); \
+ ISC_REQUIRE(prev > 0); \
if (_tmp != NULL) \
*_tmp = prev - 1; \
} while (0)
} isc_refcount_t;
/*% Destroys a reference counter. */
-#define isc_refcount_destroy(rp) \
- do { \
- REQUIRE((rp)->refs == 0); \
- DESTROYLOCK(&(rp)->lock); \
+#define isc_refcount_destroy(rp) \
+ do { \
+ isc_result_t _result; \
+ ISC_REQUIRE((rp)->refs == 0); \
+ _result = isc_mutex_destroy(&(rp)->lock); \
+ ISC_ERROR_RUNTIMECHECK(_result == ISC_R_SUCCESS); \
} while (0)
#define isc_refcount_current(rp) ((unsigned int)((rp)->refs))
-/*% Increments the reference count, returning the new value in targetp if it's not NULL. */
-#define isc_refcount_increment0(rp, tp) \
- do { \
- unsigned int *_tmp = (unsigned int *)(tp); \
- LOCK(&(rp)->lock); \
- ++((rp)->refs); \
- if (_tmp != NULL) \
- *_tmp = ((rp)->refs); \
- UNLOCK(&(rp)->lock); \
+/*%
+ * Increments the reference count, returning the new value in
+ * 'tp' if it's not NULL.
+ */
+#define isc_refcount_increment0(rp, tp) \
+ do { \
+ isc_result_t _result; \
+ unsigned int *_tmp = (unsigned int *)(tp); \
+ _result = isc_mutex_lock(&(rp)->lock); \
+ ISC_ERROR_RUNTIMECHECK(_result == ISC_R_SUCCESS); \
+ ++((rp)->refs); \
+ if (_tmp != NULL) \
+ *_tmp = ((rp)->refs); \
+ _result = isc_mutex_unlock(&(rp)->lock); \
+ ISC_ERROR_RUNTIMECHECK(_result == ISC_R_SUCCESS); \
} while (0)
-#define isc_refcount_increment(rp, tp) \
- do { \
- unsigned int *_tmp = (unsigned int *)(tp); \
- LOCK(&(rp)->lock); \
- REQUIRE((rp)->refs > 0); \
- ++((rp)->refs); \
- if (_tmp != NULL) \
- *_tmp = ((rp)->refs); \
- UNLOCK(&(rp)->lock); \
+#define isc_refcount_increment(rp, tp) \
+ do { \
+ isc_result_t _result; \
+ unsigned int *_tmp = (unsigned int *)(tp); \
+ _result = isc_mutex_lock(&(rp)->lock); \
+ ISC_ERROR_RUNTIMECHECK(_result == ISC_R_SUCCESS); \
+ ISC_REQUIRE((rp)->refs > 0); \
+ ++((rp)->refs); \
+ if (_tmp != NULL) \
+ *_tmp = ((rp)->refs); \
+ _result = isc_mutex_unlock(&(rp)->lock); \
+ ISC_ERROR_RUNTIMECHECK(_result == ISC_R_SUCCESS); \
} while (0)
-/*% Decrements the reference count, returning the new value in targetp if it's not NULL. */
-#define isc_refcount_decrement(rp, tp) \
- do { \
- unsigned int *_tmp = (unsigned int *)(tp); \
- LOCK(&(rp)->lock); \
- REQUIRE((rp)->refs > 0); \
- --((rp)->refs); \
- if (_tmp != NULL) \
- *_tmp = ((rp)->refs); \
- UNLOCK(&(rp)->lock); \
+/*%
+ * Decrements the reference count, returning the new value in 'tp'
+ * if it's not NULL.
+ */
+#define isc_refcount_decrement(rp, tp) \
+ do { \
+ isc_result_t _result; \
+ unsigned int *_tmp = (unsigned int *)(tp); \
+ _result = isc_mutex_lock(&(rp)->lock); \
+ ISC_ERROR_RUNTIMECHECK(_result == ISC_R_SUCCESS); \
+ ISC_REQUIRE((rp)->refs > 0); \
+ --((rp)->refs); \
+ if (_tmp != NULL) \
+ *_tmp = ((rp)->refs); \
+ _result = isc_mutex_unlock(&(rp)->lock); \
+ ISC_ERROR_RUNTIMECHECK(_result == ISC_R_SUCCESS); \
} while (0)
#endif /* (defined(ISC_PLATFORM_HAVESTDATOMIC) && defined(ATOMIC_INT_LOCK_FREE)) || defined(ISC_PLATFORM_HAVEXADD) */
int refs;
} isc_refcount_t;
-#define isc_refcount_destroy(rp) REQUIRE((rp)->refs == 0)
+#define isc_refcount_destroy(rp) ISC_REQUIRE((rp)->refs == 0)
#define isc_refcount_current(rp) ((unsigned int)((rp)->refs))
#define isc_refcount_increment0(rp, tp) \
do { \
unsigned int *_tmp = (unsigned int *)(tp); \
int _n; \
- REQUIRE((rp)->refs > 0); \
+ ISC_REQUIRE((rp)->refs > 0); \
_n = ++(rp)->refs; \
if (_tmp != NULL) \
*_tmp = _n; \
do { \
unsigned int *_tmp = (unsigned int *)(tp); \
int _n; \
- REQUIRE((rp)->refs > 0); \
+ ISC_REQUIRE((rp)->refs > 0); \
_n = --(rp)->refs; \
if (_tmp != NULL) \
*_tmp = _n; \