atomic_win32.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2013 Dolphin Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #ifndef _ATOMIC_WIN32_H_
  5. #define _ATOMIC_WIN32_H_
  6. #include "common/common.h"
  7. #include <intrin.h>
  8. #include <Windows.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. InterlockedExchangeAdd((volatile LONG*)&target, (LONG)value);
  30. }
  31. inline void AtomicAnd(volatile u32& target, u32 value) {
  32. _InterlockedAnd((volatile LONG*)&target, (LONG)value);
  33. }
  34. inline void AtomicIncrement(volatile u32& target) {
  35. InterlockedIncrement((volatile LONG*)&target);
  36. }
  37. inline void AtomicDecrement(volatile u32& target) {
  38. InterlockedDecrement((volatile LONG*)&target);
  39. }
  40. inline u32 AtomicLoad(volatile u32& src) {
  41. return src; // 32-bit reads are always atomic.
  42. }
  43. inline u32 AtomicLoadAcquire(volatile u32& src) {
  44. u32 result = src; // 32-bit reads are always atomic.
  45. _ReadBarrier(); // Compiler instruction only. x86 loads always have acquire semantics.
  46. return result;
  47. }
  48. inline void AtomicOr(volatile u32& target, u32 value) {
  49. _InterlockedOr((volatile LONG*)&target, (LONG)value);
  50. }
  51. inline void AtomicStore(volatile u32& dest, u32 value) {
  52. dest = value; // 32-bit writes are always atomic.
  53. }
  54. inline void AtomicStoreRelease(volatile u32& dest, u32 value) {
  55. _WriteBarrier(); // Compiler instruction only. x86 stores always have release semantics.
  56. dest = value; // 32-bit writes are always atomic.
  57. }
  58. }
  59. #endif