Atomic_Win32.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2009 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. // IWYU pragma: private, include "Common/Atomic.h"
  5. #pragma once
  6. #include <Windows.h>
  7. #include "Common/Common.h"
  8. #include "Common/Intrinsics.h"
  9. // Atomic operations are performed in a single step by the CPU. It is
  10. // impossible for other threads to see the operation "half-done."
  11. //
  12. // Some atomic operations can be combined with different types of memory
  13. // barriers called "Acquire semantics" and "Release semantics", defined below.
  14. //
  15. // Acquire semantics: Future memory accesses cannot be relocated to before the
  16. // operation.
  17. //
  18. // Release semantics: Past memory accesses cannot be relocated to after the
  19. // operation.
  20. //
  21. // These barriers affect not only the compiler, but also the CPU.
  22. //
  23. // NOTE: Acquire and Release are not differentiated right now. They perform a
  24. // full memory barrier instead of a "one-way" memory barrier. The newest
  25. // Windows SDK has Acquire and Release versions of some Interlocked* functions.
  26. namespace Common
  27. {
  28. inline void AtomicAdd(volatile u32& target, u32 value)
  29. {
  30. _InterlockedExchangeAdd((volatile LONG*)&target, (LONG)value);
  31. }
  32. inline void AtomicAnd(volatile u32& target, u32 value)
  33. {
  34. _InterlockedAnd((volatile LONG*)&target, (LONG)value);
  35. }
  36. inline void AtomicIncrement(volatile u32& target)
  37. {
  38. _InterlockedIncrement((volatile LONG*)&target);
  39. }
  40. inline void AtomicDecrement(volatile u32& target)
  41. {
  42. _InterlockedDecrement((volatile LONG*)&target);
  43. }
  44. inline void AtomicOr(volatile u32& target, u32 value)
  45. {
  46. _InterlockedOr((volatile LONG*)&target, (LONG)value);
  47. }
  48. template <typename T>
  49. inline T AtomicLoad(volatile T& src)
  50. {
  51. return src; // 32-bit reads are always atomic.
  52. }
  53. template <typename T>
  54. inline T AtomicLoadAcquire(volatile T& src)
  55. {
  56. T result = src; // 32-bit reads are always atomic.
  57. _ReadBarrier(); // Compiler instruction only. x86 loads always have acquire semantics.
  58. return result;
  59. }
  60. template <typename T, typename U>
  61. inline void AtomicStore(volatile T& dest, U value)
  62. {
  63. dest = (T) value; // 32-bit writes are always atomic.
  64. }
  65. template <typename T, typename U>
  66. inline void AtomicStoreRelease(volatile T& dest, U value)
  67. {
  68. _WriteBarrier(); // Compiler instruction only. x86 stores always have release semantics.
  69. dest = (T) value; // 32-bit writes are always atomic.
  70. }
  71. template <typename T, typename U>
  72. inline T* AtomicExchangeAcquire(T* volatile& loc, U newval)
  73. {
  74. return (T*) _InterlockedExchangePointer_acq((void* volatile*) &loc, (void*) newval);
  75. }
  76. }