AtomicCounter.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // AtomicCounter.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/AtomicCounter.cpp#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Core
  8. // Module: AtomicCounter
  9. //
  10. // Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/AtomicCounter.h"
  16. namespace Poco {
  17. #if POCO_OS == POCO_OS_WINDOWS_NT
  18. //
  19. // Windows
  20. //
  21. AtomicCounter::AtomicCounter():
  22. _counter(0)
  23. {
  24. }
  25. AtomicCounter::AtomicCounter(AtomicCounter::ValueType initialValue):
  26. _counter(initialValue)
  27. {
  28. }
  29. AtomicCounter::AtomicCounter(const AtomicCounter& counter):
  30. _counter(counter.value())
  31. {
  32. }
  33. AtomicCounter::~AtomicCounter()
  34. {
  35. }
  36. AtomicCounter& AtomicCounter::operator = (const AtomicCounter& counter)
  37. {
  38. InterlockedExchange(&_counter, counter.value());
  39. return *this;
  40. }
  41. AtomicCounter& AtomicCounter::operator = (AtomicCounter::ValueType value)
  42. {
  43. InterlockedExchange(&_counter, value);
  44. return *this;
  45. }
  46. #elif POCO_OS == POCO_OS_MAC_OS_X
  47. //
  48. // Mac OS X
  49. //
  50. AtomicCounter::AtomicCounter():
  51. _counter(0)
  52. {
  53. }
  54. AtomicCounter::AtomicCounter(AtomicCounter::ValueType initialValue):
  55. _counter(initialValue)
  56. {
  57. }
  58. AtomicCounter::AtomicCounter(const AtomicCounter& counter):
  59. _counter(counter.value())
  60. {
  61. }
  62. AtomicCounter::~AtomicCounter()
  63. {
  64. }
  65. AtomicCounter& AtomicCounter::operator = (const AtomicCounter& counter)
  66. {
  67. _counter = counter.value();
  68. return *this;
  69. }
  70. AtomicCounter& AtomicCounter::operator = (AtomicCounter::ValueType value)
  71. {
  72. _counter = value;
  73. return *this;
  74. }
  75. #elif defined(POCO_HAVE_GCC_ATOMICS)
  76. //
  77. // GCC 4.1+ atomic builtins.
  78. //
  79. AtomicCounter::AtomicCounter():
  80. _counter(0)
  81. {
  82. }
  83. AtomicCounter::AtomicCounter(AtomicCounter::ValueType initialValue):
  84. _counter(initialValue)
  85. {
  86. }
  87. AtomicCounter::AtomicCounter(const AtomicCounter& counter):
  88. _counter(counter.value())
  89. {
  90. }
  91. AtomicCounter::~AtomicCounter()
  92. {
  93. }
  94. AtomicCounter& AtomicCounter::operator = (const AtomicCounter& counter)
  95. {
  96. __sync_lock_test_and_set(&_counter, counter.value());
  97. return *this;
  98. }
  99. AtomicCounter& AtomicCounter::operator = (AtomicCounter::ValueType value)
  100. {
  101. __sync_lock_test_and_set(&_counter, value);
  102. return *this;
  103. }
  104. #else
  105. //
  106. // Generic implementation based on FastMutex
  107. //
  108. AtomicCounter::AtomicCounter()
  109. {
  110. _counter.value = 0;
  111. }
  112. AtomicCounter::AtomicCounter(AtomicCounter::ValueType initialValue)
  113. {
  114. _counter.value = initialValue;
  115. }
  116. AtomicCounter::AtomicCounter(const AtomicCounter& counter)
  117. {
  118. _counter.value = counter.value();
  119. }
  120. AtomicCounter::~AtomicCounter()
  121. {
  122. }
  123. AtomicCounter& AtomicCounter::operator = (const AtomicCounter& counter)
  124. {
  125. FastMutex::ScopedLock lock(_counter.mutex);
  126. _counter.value = counter.value();
  127. return *this;
  128. }
  129. AtomicCounter& AtomicCounter::operator = (AtomicCounter::ValueType value)
  130. {
  131. FastMutex::ScopedLock lock(_counter.mutex);
  132. _counter.value = value;
  133. return *this;
  134. }
  135. #endif // POCO_OS
  136. } // namespace Poco