AtomicCounter.h 8.3 KB

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