AtomicCounter.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //
  2. // AtomicCounter.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/AtomicCounter.h#4 $
  5. //
  6. // Library: Foundation
  7. // Package: Core
  8. // Module: AtomicCounter
  9. //
  10. // Definition of the AtomicCounter class.
  11. //
  12. // Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_AtomicCounter_INCLUDED
  18. #define Foundation_AtomicCounter_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #if POCO_OS == POCO_OS_WINDOWS_NT
  21. #include "Poco/UnWindows.h"
  22. #elif POCO_OS == POCO_OS_MAC_OS_X
  23. #include <libkern/OSAtomic.h>
  24. #elif ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2) || __GNUC__ > 4) && (defined(__x86_64__) || defined(__i386__))
  25. #if !defined(POCO_HAVE_GCC_ATOMICS) && !defined(POCO_NO_GCC_ATOMICS)
  26. #define POCO_HAVE_GCC_ATOMICS
  27. #endif
  28. #elif ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3) || __GNUC__ > 4)
  29. #if !defined(POCO_HAVE_GCC_ATOMICS) && !defined(POCO_NO_GCC_ATOMICS)
  30. #define POCO_HAVE_GCC_ATOMICS
  31. #endif
  32. #endif // POCO_OS
  33. #include "Poco/Mutex.h"
  34. namespace Poco {
  35. class Foundation_API AtomicCounter
  36. /// This class implements a simple counter, which
  37. /// provides atomic operations that are safe to
  38. /// use in a multithreaded environment.
  39. ///
  40. /// Typical usage of AtomicCounter is for implementing
  41. /// reference counting and similar things.
  42. ///
  43. /// On some platforms, the implementation of AtomicCounter
  44. /// is based on atomic primitives specific to the platform
  45. /// (such as InterlockedIncrement, etc. on Windows), and
  46. /// thus very efficient. On platforms that do not support
  47. /// atomic primitives, operations are guarded by a FastMutex.
  48. ///
  49. /// The following platforms currently have atomic
  50. /// primitives:
  51. /// - Windows
  52. /// - Mac OS X
  53. /// - GCC 4.1+ (Intel platforms only)
  54. {
  55. public:
  56. typedef int ValueType; /// The underlying integer type.
  57. AtomicCounter();
  58. /// Creates a new AtomicCounter and initializes it to zero.
  59. explicit AtomicCounter(ValueType initialValue);
  60. /// Creates a new AtomicCounter and initializes it with
  61. /// the given value.
  62. AtomicCounter(const AtomicCounter& counter);
  63. /// Creates the counter by copying another one.
  64. ~AtomicCounter();
  65. /// Destroys the AtomicCounter.
  66. AtomicCounter& operator = (const AtomicCounter& counter);
  67. /// Assigns the value of another AtomicCounter.
  68. AtomicCounter& operator = (ValueType value);
  69. /// Assigns a value to the counter.
  70. operator ValueType () const;
  71. /// Returns the value of the counter.
  72. ValueType value() const;
  73. /// Returns the value of the counter.
  74. ValueType operator ++ (); // prefix
  75. /// Increments the counter and returns the result.
  76. ValueType operator ++ (int); // postfix
  77. /// Increments the counter and returns the previous value.
  78. ValueType operator -- (); // prefix
  79. /// Decrements the counter and returns the result.
  80. ValueType operator -- (int); // postfix
  81. /// Decrements the counter and returns the previous value.
  82. bool operator ! () const;
  83. /// Returns true if the counter is zero, false otherwise.
  84. private:
  85. #if POCO_OS == POCO_OS_WINDOWS_NT
  86. typedef volatile LONG ImplType;
  87. #elif POCO_OS == POCO_OS_MAC_OS_X
  88. typedef int32_t ImplType;
  89. #elif defined(POCO_HAVE_GCC_ATOMICS)
  90. typedef int ImplType;
  91. #else // generic implementation based on FastMutex
  92. struct ImplType
  93. {
  94. mutable FastMutex mutex;
  95. volatile int value;
  96. };
  97. #endif // POCO_OS
  98. ImplType _counter;
  99. };
  100. //
  101. // inlines
  102. //
  103. #if POCO_OS == POCO_OS_WINDOWS_NT
  104. //
  105. // Windows
  106. //
  107. inline AtomicCounter::operator AtomicCounter::ValueType () const
  108. {
  109. return _counter;
  110. }
  111. inline AtomicCounter::ValueType AtomicCounter::value() const
  112. {
  113. return _counter;
  114. }
  115. inline AtomicCounter::ValueType AtomicCounter::operator ++ () // prefix
  116. {
  117. return InterlockedIncrement(&_counter);
  118. }
  119. inline AtomicCounter::ValueType AtomicCounter::operator ++ (int) // postfix
  120. {
  121. ValueType result = InterlockedIncrement(&_counter);
  122. return --result;
  123. }
  124. inline AtomicCounter::ValueType AtomicCounter::operator -- () // prefix
  125. {
  126. return InterlockedDecrement(&_counter);
  127. }
  128. inline AtomicCounter::ValueType AtomicCounter::operator -- (int) // postfix
  129. {
  130. ValueType result = InterlockedDecrement(&_counter);
  131. return ++result;
  132. }
  133. inline bool AtomicCounter::operator ! () const
  134. {
  135. return _counter == 0;
  136. }
  137. #elif POCO_OS == POCO_OS_MAC_OS_X
  138. //
  139. // Mac OS X
  140. //
  141. inline AtomicCounter::operator AtomicCounter::ValueType () const
  142. {
  143. return _counter;
  144. }
  145. inline AtomicCounter::ValueType AtomicCounter::value() const
  146. {
  147. return _counter;
  148. }
  149. inline AtomicCounter::ValueType AtomicCounter::operator ++ () // prefix
  150. {
  151. return OSAtomicIncrement32(&_counter);
  152. }
  153. inline AtomicCounter::ValueType AtomicCounter::operator ++ (int) // postfix
  154. {
  155. ValueType result = OSAtomicIncrement32(&_counter);
  156. return --result;
  157. }
  158. inline AtomicCounter::ValueType AtomicCounter::operator -- () // prefix
  159. {
  160. return OSAtomicDecrement32(&_counter);
  161. }
  162. inline AtomicCounter::ValueType AtomicCounter::operator -- (int) // postfix
  163. {
  164. ValueType result = OSAtomicDecrement32(&_counter);
  165. return ++result;
  166. }
  167. inline bool AtomicCounter::operator ! () const
  168. {
  169. return _counter == 0;
  170. }
  171. #elif defined(POCO_HAVE_GCC_ATOMICS)
  172. //
  173. // GCC 4.1+ atomic builtins.
  174. //
  175. inline AtomicCounter::operator AtomicCounter::ValueType () const
  176. {
  177. return _counter;
  178. }
  179. inline AtomicCounter::ValueType AtomicCounter::value() const
  180. {
  181. return _counter;
  182. }
  183. inline AtomicCounter::ValueType AtomicCounter::operator ++ () // prefix
  184. {
  185. return __sync_add_and_fetch(&_counter, 1);
  186. }
  187. inline AtomicCounter::ValueType AtomicCounter::operator ++ (int) // postfix
  188. {
  189. return __sync_fetch_and_add(&_counter, 1);
  190. }
  191. inline AtomicCounter::ValueType AtomicCounter::operator -- () // prefix
  192. {
  193. return __sync_sub_and_fetch(&_counter, 1);
  194. }
  195. inline AtomicCounter::ValueType AtomicCounter::operator -- (int) // postfix
  196. {
  197. return __sync_fetch_and_sub(&_counter, 1);
  198. }
  199. inline bool AtomicCounter::operator ! () const
  200. {
  201. return _counter == 0;
  202. }
  203. #else
  204. //
  205. // Generic implementation based on FastMutex
  206. //
  207. inline AtomicCounter::operator AtomicCounter::ValueType () const
  208. {
  209. ValueType result;
  210. {
  211. FastMutex::ScopedLock lock(_counter.mutex);
  212. result = _counter.value;
  213. }
  214. return result;
  215. }
  216. inline AtomicCounter::ValueType AtomicCounter::value() const
  217. {
  218. ValueType result;
  219. {
  220. FastMutex::ScopedLock lock(_counter.mutex);
  221. result = _counter.value;
  222. }
  223. return result;
  224. }
  225. inline AtomicCounter::ValueType AtomicCounter::operator ++ () // prefix
  226. {
  227. ValueType result;
  228. {
  229. FastMutex::ScopedLock lock(_counter.mutex);
  230. result = ++_counter.value;
  231. }
  232. return result;
  233. }
  234. inline AtomicCounter::ValueType AtomicCounter::operator ++ (int) // postfix
  235. {
  236. ValueType result;
  237. {
  238. FastMutex::ScopedLock lock(_counter.mutex);
  239. result = _counter.value++;
  240. }
  241. return result;
  242. }
  243. inline AtomicCounter::ValueType AtomicCounter::operator -- () // prefix
  244. {
  245. ValueType result;
  246. {
  247. FastMutex::ScopedLock lock(_counter.mutex);
  248. result = --_counter.value;
  249. }
  250. return result;
  251. }
  252. inline AtomicCounter::ValueType AtomicCounter::operator -- (int) // postfix
  253. {
  254. ValueType result;
  255. {
  256. FastMutex::ScopedLock lock(_counter.mutex);
  257. result = _counter.value--;
  258. }
  259. return result;
  260. }
  261. inline bool AtomicCounter::operator ! () const
  262. {
  263. bool result;
  264. {
  265. FastMutex::ScopedLock lock(_counter.mutex);
  266. result = _counter.value == 0;
  267. }
  268. return result;
  269. }
  270. #endif // POCO_OS
  271. } // namespace Poco
  272. #endif // Foundation_AtomicCounter_INCLUDED