threading-windows.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2015 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #pragma once
  17. #include <intrin.h>
  18. #include <string.h>
  19. #if !defined(_M_IX86) && !defined(_M_X64) && !defined(_M_ARM) && \
  20. !defined(_M_ARM64)
  21. #error Processor not supported
  22. #endif
  23. static inline long os_atomic_inc_long(volatile long *val)
  24. {
  25. return _InterlockedIncrement(val);
  26. }
  27. static inline long os_atomic_dec_long(volatile long *val)
  28. {
  29. return _InterlockedDecrement(val);
  30. }
  31. static inline void os_atomic_store_long(volatile long *ptr, long val)
  32. {
  33. #if defined(_M_ARM) || defined(_M_ARM64)
  34. __dmb(_ARM64_BARRIER_ISH);
  35. __iso_volatile_store32((volatile __int32 *)ptr, val);
  36. __dmb(_ARM64_BARRIER_ISH);
  37. #else
  38. _InterlockedExchange(ptr, val);
  39. #endif
  40. }
  41. static inline long os_atomic_set_long(volatile long *ptr, long val)
  42. {
  43. return _InterlockedExchange(ptr, val);
  44. }
  45. static inline long os_atomic_exchange_long(volatile long *ptr, long val)
  46. {
  47. return os_atomic_set_long(ptr, val);
  48. }
  49. static inline long os_atomic_load_long(const volatile long *ptr)
  50. {
  51. const long val = __iso_volatile_load32((const volatile __int32 *)ptr);
  52. #if defined(_M_ARM) || defined(_M_ARM64)
  53. __dmb(_ARM64_BARRIER_ISH);
  54. #else
  55. _ReadWriteBarrier();
  56. #endif
  57. return val;
  58. }
  59. static inline bool os_atomic_compare_swap_long(volatile long *val, long old_val,
  60. long new_val)
  61. {
  62. return _InterlockedCompareExchange(val, new_val, old_val) == old_val;
  63. }
  64. static inline bool os_atomic_compare_exchange_long(volatile long *val,
  65. long *old_ptr, long new_val)
  66. {
  67. const long old_val = *old_ptr;
  68. const long previous =
  69. _InterlockedCompareExchange(val, new_val, old_val);
  70. *old_ptr = previous;
  71. return previous == old_val;
  72. }
  73. static inline void os_atomic_store_bool(volatile bool *ptr, bool val)
  74. {
  75. #if defined(_M_ARM) || defined(_M_ARM64)
  76. __dmb(_ARM64_BARRIER_ISH);
  77. __iso_volatile_store8((volatile char *)ptr, val);
  78. __dmb(_ARM64_BARRIER_ISH);
  79. #else
  80. _InterlockedExchange8((volatile char *)ptr, (char)val);
  81. #endif
  82. }
  83. static inline bool os_atomic_set_bool(volatile bool *ptr, bool val)
  84. {
  85. const char c = _InterlockedExchange8((volatile char *)ptr, (char)val);
  86. bool b;
  87. /* Avoid unnecesary char to bool conversion. Value known 0 or 1. */
  88. memcpy(&b, &c, sizeof(b));
  89. return b;
  90. }
  91. static inline bool os_atomic_exchange_bool(volatile bool *ptr, bool val)
  92. {
  93. return os_atomic_set_bool(ptr, val);
  94. }
  95. static inline bool os_atomic_load_bool(const volatile bool *ptr)
  96. {
  97. const char val = __iso_volatile_load8((const volatile char *)ptr);
  98. #if defined(_M_ARM) || defined(_M_ARM64)
  99. __dmb(_ARM64_BARRIER_ISH);
  100. #else
  101. _ReadWriteBarrier();
  102. #endif
  103. return val;
  104. }