2 * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2001, 2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: refcount.h,v 1.15 2007/06/19 23:47:18 tbox Exp $ */
20 #ifndef ISC_REFCOUNT_H
21 #define ISC_REFCOUNT_H 1
23 #include <isc/atomic.h>
25 #include <isc/mutex.h>
26 #include <isc/platform.h>
27 #include <isc/types.h>
30 /*! \file isc/refcount.h
31 * \brief Implements a locked reference counter.
33 * These functions may actually be
34 * implemented using macros, and implementations of these macros are below.
35 * The isc_refcount_t type should not be accessed directly, as its contents
36 * depend on the implementation.
47 * isc_refcount_init(isc_refcount_t *ref, unsigned int n);
49 * Initialize the reference counter. There will be 'n' initial references.
57 * isc_refcount_destroy(isc_refcount_t *ref);
59 * Destroys a reference counter.
63 * The number of references is 0.
68 * isc_refcount_increment(isc_refcount_t *ref, unsigned int *targetp);
69 * isc_refcount_increment0(isc_refcount_t *ref, unsigned int *targetp);
71 * Increments the reference count, returning the new value in targetp if it's
72 * not NULL. The reference counter typically begins with the initial counter
73 * of 1, and will be destroyed once the counter reaches 0. Thus,
74 * isc_refcount_increment() additionally requires the previous counter be
75 * larger than 0 so that an error which violates the usage can be easily
76 * caught. isc_refcount_increment0() does not have this restriction.
84 * isc_refcount_decrement(isc_refcount_t *ref, unsigned int *targetp);
86 * Decrements the reference count, returning the new value in targetp if it's
95 * Sample implementations
97 #ifdef ISC_PLATFORM_USETHREADS
98 #ifdef ISC_PLATFORM_HAVEXADD
100 #define ISC_REFCOUNT_HAVEATOMIC 1
102 typedef struct isc_refcount {
106 #define isc_refcount_destroy(rp) (REQUIRE((rp)->refs == 0))
107 #define isc_refcount_current(rp) ((unsigned int)((rp)->refs))
109 #define isc_refcount_increment0(rp, tp) \
111 unsigned int *_tmp = (unsigned int *)(tp); \
113 prev = isc_atomic_xadd(&(rp)->refs, 1); \
118 #define isc_refcount_increment(rp, tp) \
120 unsigned int *_tmp = (unsigned int *)(tp); \
122 prev = isc_atomic_xadd(&(rp)->refs, 1); \
128 #define isc_refcount_decrement(rp, tp) \
130 unsigned int *_tmp = (unsigned int *)(tp); \
132 prev = isc_atomic_xadd(&(rp)->refs, -1); \
138 #else /* ISC_PLATFORM_HAVEXADD */
140 typedef struct isc_refcount {
145 /*% Destroys a reference counter. */
146 #define isc_refcount_destroy(rp) \
148 REQUIRE((rp)->refs == 0); \
149 DESTROYLOCK(&(rp)->lock); \
152 #define isc_refcount_current(rp) ((unsigned int)((rp)->refs))
154 /*% Increments the reference count, returning the new value in targetp if it's not NULL. */
155 #define isc_refcount_increment0(rp, tp) \
157 unsigned int *_tmp = (unsigned int *)(tp); \
161 *_tmp = ((rp)->refs); \
162 UNLOCK(&(rp)->lock); \
165 #define isc_refcount_increment(rp, tp) \
167 unsigned int *_tmp = (unsigned int *)(tp); \
169 REQUIRE((rp)->refs > 0); \
172 *_tmp = ((rp)->refs); \
173 UNLOCK(&(rp)->lock); \
176 /*% Decrements the reference count, returning the new value in targetp if it's not NULL. */
177 #define isc_refcount_decrement(rp, tp) \
179 unsigned int *_tmp = (unsigned int *)(tp); \
181 REQUIRE((rp)->refs > 0); \
184 *_tmp = ((rp)->refs); \
185 UNLOCK(&(rp)->lock); \
188 #endif /* ISC_PLATFORM_HAVEXADD */
189 #else /* ISC_PLATFORM_USETHREADS */
191 typedef struct isc_refcount {
195 #define isc_refcount_destroy(rp) (REQUIRE((rp)->refs == 0))
196 #define isc_refcount_current(rp) ((unsigned int)((rp)->refs))
198 #define isc_refcount_increment0(rp, tp) \
200 unsigned int *_tmp = (unsigned int *)(tp); \
201 int _n = ++(rp)->refs; \
206 #define isc_refcount_increment(rp, tp) \
208 unsigned int *_tmp = (unsigned int *)(tp); \
210 REQUIRE((rp)->refs > 0); \
216 #define isc_refcount_decrement(rp, tp) \
218 unsigned int *_tmp = (unsigned int *)(tp); \
220 REQUIRE((rp)->refs > 0); \
226 #endif /* ISC_PLATFORM_USETHREADS */
229 isc_refcount_init(isc_refcount_t *ref, unsigned int n);
233 #endif /* ISC_REFCOUNT_H */