1
0

atomics.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Win64 (amd64) implementation
  3. */
  4. #pragma once
  5. #include "../../foundation/types.h"
  6. #include <Windows.h>
  7. #include <intrin.h>
  8. #ifdef __cplusplus
  9. #define NX_ATOMIC_INLINE inline
  10. #else
  11. #define NX_ATOMIC_INLINE
  12. #endif
  13. NX_ATOMIC_INLINE static size_t nx_atomic_inc(volatile size_t *addr)
  14. {
  15. return (size_t)_InterlockedIncrement64((volatile LONGLONG *)addr);
  16. }
  17. NX_ATOMIC_INLINE static size_t nx_atomic_dec(volatile size_t *addr)
  18. {
  19. return (size_t)_InterlockedDecrement64((volatile LONGLONG *)addr);
  20. }
  21. NX_ATOMIC_INLINE static size_t nx_atomic_dec_release(volatile size_t *addr)
  22. {
  23. return (size_t)_InterlockedDecrement64((volatile LONGLONG *)addr);
  24. }
  25. NX_ATOMIC_INLINE static void nx_atomic_write(size_t value, volatile size_t *addr)
  26. {
  27. InterlockedExchange64((volatile LONG64 *)addr, value);
  28. }
  29. NX_ATOMIC_INLINE static void nx_atomic_write_pointer(void *value, void* volatile *addr)
  30. {
  31. InterlockedExchangePointer(addr, value);
  32. }
  33. NX_ATOMIC_INLINE static size_t nx_atomic_add(size_t value, volatile size_t* addr)
  34. {
  35. return (size_t)InterlockedExchangeAdd64 ((volatile LONGLONG *)addr, (LONGLONG)value);
  36. }
  37. NX_ATOMIC_INLINE static size_t nx_atomic_sub(size_t value, volatile size_t* addr)
  38. {
  39. return (size_t)InterlockedExchangeAdd64((volatile LONGLONG *)addr, -(LONGLONG)value);
  40. }
  41. NX_ATOMIC_INLINE static void *nx_atomic_swap_pointer(void *value, void* volatile *addr)
  42. {
  43. return InterlockedExchangePointer(addr, value);
  44. }
  45. NX_ATOMIC_INLINE static int nx_atomic_cmpxchg_pointer(void *oldvalue, void *newvalue, void* volatile *addr)
  46. {
  47. return InterlockedCompareExchangePointer(addr, newvalue, oldvalue) == oldvalue;
  48. }
  49. /*
  50. NX_ATOMIC_INLINE static int nx_atomic_cmpxchg2(size_t *oldvalue, size_t *newvalue, volatile size_t *addr)
  51. {
  52. return InterlockedCompare64Exchange128((LONG64 volatile *)addr, (LONG64)newvalue[1], (LONG64)newvalue[0], (LONG64)oldvalue[0]) == oldvalue[0];
  53. }
  54. */
  55. #if 0
  56. NX_ATOMIC_INLINE static size_t atomic_increment(volatile size_t *val)
  57. {
  58. return (size_t)InterlockedIncrement((volatile LONG *)val);
  59. }
  60. NX_ATOMIC_INLINE static size_t atomic_decrement(volatile size_t *val)
  61. {
  62. return (size_t)InterlockedDecrement((volatile LONG *)val);
  63. }
  64. NX_ATOMIC_INLINE static void atomic_add(volatile size_t *val, size_t add)
  65. {
  66. InterlockedExchangeAdd64((volatile LONGLONG *)val, (LONGLONG)add);
  67. }
  68. NX_ATOMIC_INLINE static void atomic_sub(volatile size_t *val, size_t sub)
  69. {
  70. InterlockedExchangeAdd64((volatile LONGLONG *)val, -((LONGLONG)sub));
  71. }
  72. NX_ATOMIC_INLINE static void *atomic_exchange_pointer(void* volatile *target, void *value)
  73. {
  74. return InterlockedExchangePointer(target, value);
  75. }
  76. NX_ATOMIC_INLINE static bool atomic_compare_exchange_pointer(void* volatile *destination, void *exchange, void *compare)
  77. {
  78. return InterlockedCompareExchangePointer(destination, exchange, compare) == compare;
  79. }
  80. NX_ATOMIC_INLINE static void atomic_write(volatile size_t *dest, size_t src)
  81. {
  82. InterlockedExchange64((volatile LONG64 *)dest, src);
  83. }
  84. #endif