atomic.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/atomic.h
  3. // Purpose: functions to manipulate atomically integers and pointers
  4. // Author: Armel Asselin
  5. // Created: 12/13/2006
  6. // Copyright: (c) Armel Asselin
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_ATOMIC_H_
  10. #define _WX_ATOMIC_H_
  11. // ----------------------------------------------------------------------------
  12. // headers
  13. // ----------------------------------------------------------------------------
  14. // get the value of wxUSE_THREADS configuration flag
  15. #include "wx/defs.h"
  16. // constraints on the various functions:
  17. // - wxAtomicDec must return a zero value if the value is zero once
  18. // decremented else it must return any non-zero value (the true value is OK
  19. // but not necessary).
  20. #if wxUSE_THREADS
  21. #if defined(HAVE_GCC_ATOMIC_BUILTINS)
  22. // NB: we intentionally don't use Linux's asm/atomic.h header, because it's
  23. // an internal kernel header that doesn't always work in userspace:
  24. // http://bugs.mysql.com/bug.php?id=28456
  25. // http://golubenco.org/blog/atomic-operations/
  26. inline void wxAtomicInc (wxUint32 &value)
  27. {
  28. __sync_fetch_and_add(&value, 1);
  29. }
  30. inline wxUint32 wxAtomicDec (wxUint32 &value)
  31. {
  32. return __sync_sub_and_fetch(&value, 1);
  33. }
  34. #elif defined(__WINDOWS__)
  35. // include standard Windows headers
  36. #include "wx/msw/wrapwin.h"
  37. inline void wxAtomicInc (wxUint32 &value)
  38. {
  39. InterlockedIncrement ((LONG*)&value);
  40. }
  41. inline wxUint32 wxAtomicDec (wxUint32 &value)
  42. {
  43. return InterlockedDecrement ((LONG*)&value);
  44. }
  45. #elif defined(__WXMAC__) || defined(__DARWIN__)
  46. #include "libkern/OSAtomic.h"
  47. inline void wxAtomicInc (wxUint32 &value)
  48. {
  49. OSAtomicIncrement32 ((int32_t*)&value);
  50. }
  51. inline wxUint32 wxAtomicDec (wxUint32 &value)
  52. {
  53. return OSAtomicDecrement32 ((int32_t*)&value);
  54. }
  55. #elif defined (__SOLARIS__)
  56. #include <atomic.h>
  57. inline void wxAtomicInc (wxUint32 &value)
  58. {
  59. atomic_add_32 ((uint32_t*)&value, 1);
  60. }
  61. inline wxUint32 wxAtomicDec (wxUint32 &value)
  62. {
  63. return atomic_add_32_nv ((uint32_t*)&value, (uint32_t)-1);
  64. }
  65. #else // unknown platform
  66. // it will result in inclusion if the generic implementation code a bit later in this page
  67. #define wxNEEDS_GENERIC_ATOMIC_OPS
  68. #endif // unknown platform
  69. #else // else of wxUSE_THREADS
  70. // if no threads are used we can safely use simple ++/--
  71. inline void wxAtomicInc (wxUint32 &value) { ++value; }
  72. inline wxUint32 wxAtomicDec (wxUint32 &value) { return --value; }
  73. #endif // !wxUSE_THREADS
  74. // ----------------------------------------------------------------------------
  75. // proxies to actual implementations, but for various other types with same
  76. // behaviour
  77. // ----------------------------------------------------------------------------
  78. #ifdef wxNEEDS_GENERIC_ATOMIC_OPS
  79. #include "wx/thread.h" // for wxCriticalSection
  80. class wxAtomicInt32
  81. {
  82. public:
  83. wxAtomicInt32() { } // non initialized for consistency with basic int type
  84. wxAtomicInt32(wxInt32 v) : m_value(v) { }
  85. wxAtomicInt32(const wxAtomicInt32& a) : m_value(a.m_value) {}
  86. operator wxInt32() const { return m_value; }
  87. operator volatile wxInt32&() { return m_value; }
  88. wxAtomicInt32& operator=(wxInt32 v) { m_value = v; return *this; }
  89. void Inc()
  90. {
  91. wxCriticalSectionLocker lock(m_locker);
  92. ++m_value;
  93. }
  94. wxInt32 Dec()
  95. {
  96. wxCriticalSectionLocker lock(m_locker);
  97. return --m_value;
  98. }
  99. private:
  100. volatile wxInt32 m_value;
  101. wxCriticalSection m_locker;
  102. };
  103. inline void wxAtomicInc(wxAtomicInt32 &value) { value.Inc(); }
  104. inline wxInt32 wxAtomicDec(wxAtomicInt32 &value) { return value.Dec(); }
  105. #else // !wxNEEDS_GENERIC_ATOMIC_OPS
  106. #define wxHAS_ATOMIC_OPS
  107. inline void wxAtomicInc(wxInt32 &value) { wxAtomicInc((wxUint32&)value); }
  108. inline wxInt32 wxAtomicDec(wxInt32 &value) { return wxAtomicDec((wxUint32&)value); }
  109. typedef wxInt32 wxAtomicInt32;
  110. #endif // wxNEEDS_GENERIC_ATOMIC_OPS
  111. // all the native implementations use 32 bits currently
  112. // for a 64 bits implementation we could use (a future) wxAtomicInt64 as
  113. // default type
  114. typedef wxAtomicInt32 wxAtomicInt;
  115. #endif // _WX_ATOMIC_H_