/contrib/bind9/lib/isc/include/isc/refcount.h

https://bitbucket.org/freebsd/freebsd-head/ · C++ Header · 233 lines · 120 code · 35 blank · 78 comment · 31 complexity · a00ff0b684f4b42f4404c79e897c7c97 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004-2007, 2009 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 2001, 2003 Internet Software Consortium.
  4. *
  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.
  8. *
  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.
  16. */
  17. /* $Id: refcount.h,v 1.17 2009/09/29 23:48:04 tbox Exp $ */
  18. #ifndef ISC_REFCOUNT_H
  19. #define ISC_REFCOUNT_H 1
  20. #include <isc/atomic.h>
  21. #include <isc/lang.h>
  22. #include <isc/mutex.h>
  23. #include <isc/platform.h>
  24. #include <isc/types.h>
  25. #include <isc/util.h>
  26. /*! \file isc/refcount.h
  27. * \brief Implements a locked reference counter.
  28. *
  29. * These functions may actually be
  30. * implemented using macros, and implementations of these macros are below.
  31. * The isc_refcount_t type should not be accessed directly, as its contents
  32. * depend on the implementation.
  33. */
  34. ISC_LANG_BEGINDECLS
  35. /*
  36. * Function prototypes
  37. */
  38. /*
  39. * isc_result_t
  40. * isc_refcount_init(isc_refcount_t *ref, unsigned int n);
  41. *
  42. * Initialize the reference counter. There will be 'n' initial references.
  43. *
  44. * Requires:
  45. * ref != NULL
  46. */
  47. /*
  48. * void
  49. * isc_refcount_destroy(isc_refcount_t *ref);
  50. *
  51. * Destroys a reference counter.
  52. *
  53. * Requires:
  54. * ref != NULL
  55. * The number of references is 0.
  56. */
  57. /*
  58. * void
  59. * isc_refcount_increment(isc_refcount_t *ref, unsigned int *targetp);
  60. * isc_refcount_increment0(isc_refcount_t *ref, unsigned int *targetp);
  61. *
  62. * Increments the reference count, returning the new value in targetp if it's
  63. * not NULL. The reference counter typically begins with the initial counter
  64. * of 1, and will be destroyed once the counter reaches 0. Thus,
  65. * isc_refcount_increment() additionally requires the previous counter be
  66. * larger than 0 so that an error which violates the usage can be easily
  67. * caught. isc_refcount_increment0() does not have this restriction.
  68. *
  69. * Requires:
  70. * ref != NULL.
  71. */
  72. /*
  73. * void
  74. * isc_refcount_decrement(isc_refcount_t *ref, unsigned int *targetp);
  75. *
  76. * Decrements the reference count, returning the new value in targetp if it's
  77. * not NULL.
  78. *
  79. * Requires:
  80. * ref != NULL.
  81. */
  82. /*
  83. * Sample implementations
  84. */
  85. #ifdef ISC_PLATFORM_USETHREADS
  86. #ifdef ISC_PLATFORM_HAVEXADD
  87. #define ISC_REFCOUNT_HAVEATOMIC 1
  88. typedef struct isc_refcount {
  89. isc_int32_t refs;
  90. } isc_refcount_t;
  91. #define isc_refcount_destroy(rp) REQUIRE((rp)->refs == 0)
  92. #define isc_refcount_current(rp) ((unsigned int)((rp)->refs))
  93. #define isc_refcount_increment0(rp, tp) \
  94. do { \
  95. unsigned int *_tmp = (unsigned int *)(tp); \
  96. isc_int32_t prev; \
  97. prev = isc_atomic_xadd(&(rp)->refs, 1); \
  98. if (_tmp != NULL) \
  99. *_tmp = prev + 1; \
  100. } while (0)
  101. #define isc_refcount_increment(rp, tp) \
  102. do { \
  103. unsigned int *_tmp = (unsigned int *)(tp); \
  104. isc_int32_t prev; \
  105. prev = isc_atomic_xadd(&(rp)->refs, 1); \
  106. REQUIRE(prev > 0); \
  107. if (_tmp != NULL) \
  108. *_tmp = prev + 1; \
  109. } while (0)
  110. #define isc_refcount_decrement(rp, tp) \
  111. do { \
  112. unsigned int *_tmp = (unsigned int *)(tp); \
  113. isc_int32_t prev; \
  114. prev = isc_atomic_xadd(&(rp)->refs, -1); \
  115. REQUIRE(prev > 0); \
  116. if (_tmp != NULL) \
  117. *_tmp = prev - 1; \
  118. } while (0)
  119. #else /* ISC_PLATFORM_HAVEXADD */
  120. typedef struct isc_refcount {
  121. int refs;
  122. isc_mutex_t lock;
  123. } isc_refcount_t;
  124. /*% Destroys a reference counter. */
  125. #define isc_refcount_destroy(rp) \
  126. do { \
  127. REQUIRE((rp)->refs == 0); \
  128. DESTROYLOCK(&(rp)->lock); \
  129. } while (0)
  130. #define isc_refcount_current(rp) ((unsigned int)((rp)->refs))
  131. /*% Increments the reference count, returning the new value in targetp if it's not NULL. */
  132. #define isc_refcount_increment0(rp, tp) \
  133. do { \
  134. unsigned int *_tmp = (unsigned int *)(tp); \
  135. LOCK(&(rp)->lock); \
  136. ++((rp)->refs); \
  137. if (_tmp != NULL) \
  138. *_tmp = ((rp)->refs); \
  139. UNLOCK(&(rp)->lock); \
  140. } while (0)
  141. #define isc_refcount_increment(rp, tp) \
  142. do { \
  143. unsigned int *_tmp = (unsigned int *)(tp); \
  144. LOCK(&(rp)->lock); \
  145. REQUIRE((rp)->refs > 0); \
  146. ++((rp)->refs); \
  147. if (_tmp != NULL) \
  148. *_tmp = ((rp)->refs); \
  149. UNLOCK(&(rp)->lock); \
  150. } while (0)
  151. /*% Decrements the reference count, returning the new value in targetp if it's not NULL. */
  152. #define isc_refcount_decrement(rp, tp) \
  153. do { \
  154. unsigned int *_tmp = (unsigned int *)(tp); \
  155. LOCK(&(rp)->lock); \
  156. REQUIRE((rp)->refs > 0); \
  157. --((rp)->refs); \
  158. if (_tmp != NULL) \
  159. *_tmp = ((rp)->refs); \
  160. UNLOCK(&(rp)->lock); \
  161. } while (0)
  162. #endif /* ISC_PLATFORM_HAVEXADD */
  163. #else /* ISC_PLATFORM_USETHREADS */
  164. typedef struct isc_refcount {
  165. int refs;
  166. } isc_refcount_t;
  167. #define isc_refcount_destroy(rp) REQUIRE((rp)->refs == 0)
  168. #define isc_refcount_current(rp) ((unsigned int)((rp)->refs))
  169. #define isc_refcount_increment0(rp, tp) \
  170. do { \
  171. unsigned int *_tmp = (unsigned int *)(tp); \
  172. int _n = ++(rp)->refs; \
  173. if (_tmp != NULL) \
  174. *_tmp = _n; \
  175. } while (0)
  176. #define isc_refcount_increment(rp, tp) \
  177. do { \
  178. unsigned int *_tmp = (unsigned int *)(tp); \
  179. int _n; \
  180. REQUIRE((rp)->refs > 0); \
  181. _n = ++(rp)->refs; \
  182. if (_tmp != NULL) \
  183. *_tmp = _n; \
  184. } while (0)
  185. #define isc_refcount_decrement(rp, tp) \
  186. do { \
  187. unsigned int *_tmp = (unsigned int *)(tp); \
  188. int _n; \
  189. REQUIRE((rp)->refs > 0); \
  190. _n = --(rp)->refs; \
  191. if (_tmp != NULL) \
  192. *_tmp = _n; \
  193. } while (0)
  194. #endif /* ISC_PLATFORM_USETHREADS */
  195. isc_result_t
  196. isc_refcount_init(isc_refcount_t *ref, unsigned int n);
  197. ISC_LANG_ENDDECLS
  198. #endif /* ISC_REFCOUNT_H */