unlocked.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * \file
  3. * Contains inline functions to explicitly mark data races that should not be changed.
  4. * This way, instruments like Clang's ThreadSanitizer can be told to ignore very specific instructions.
  5. *
  6. * Please keep this file and its methods organised:
  7. * * Increment, Decrement, Add, Subtract, Write, Read
  8. * * gint32 (""), guint32 ("Unsigned"),
  9. * gint64 ("64"), guint64 ("Unsigned64"),
  10. * gsize ("Size"), gboolean ("Bool"),
  11. * gpointer ("Pointer")
  12. *
  13. * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  14. */
  15. #ifndef _UNLOCKED_H_
  16. #define _UNLOCKED_H_
  17. #include <glib.h>
  18. #include <mono/utils/mono-compiler.h>
  19. #if MONO_HAS_CLANG_THREAD_SANITIZER
  20. #define MONO_UNLOCKED_ATTRS MONO_NO_SANITIZE_THREAD MONO_NEVER_INLINE static
  21. #elif defined(_MSC_VER)
  22. #define MONO_UNLOCKED_ATTRS MONO_ALWAYS_INLINE static
  23. #else
  24. #define MONO_UNLOCKED_ATTRS MONO_ALWAYS_INLINE static inline
  25. #endif
  26. MONO_UNLOCKED_ATTRS
  27. gint32
  28. UnlockedIncrement (volatile gint32 *val)
  29. {
  30. return ++*val;
  31. }
  32. MONO_UNLOCKED_ATTRS
  33. gint64
  34. UnlockedIncrement64 (volatile gint64 *val)
  35. {
  36. return ++*val;
  37. }
  38. MONO_UNLOCKED_ATTRS
  39. gint64
  40. UnlockedDecrement64 (volatile gint64 *val)
  41. {
  42. return --*val;
  43. }
  44. MONO_UNLOCKED_ATTRS
  45. gint32
  46. UnlockedDecrement (volatile gint32 *val)
  47. {
  48. return --*val;
  49. }
  50. MONO_UNLOCKED_ATTRS
  51. gint32
  52. UnlockedAdd (volatile gint32 *dest, gint32 add)
  53. {
  54. return *dest += add;
  55. }
  56. MONO_UNLOCKED_ATTRS
  57. gint64
  58. UnlockedAdd64 (volatile gint64 *dest, gint64 add)
  59. {
  60. return *dest += add;
  61. }
  62. MONO_UNLOCKED_ATTRS
  63. gdouble
  64. UnlockedAddDouble (volatile gdouble *dest, gdouble add)
  65. {
  66. return *dest += add;
  67. }
  68. MONO_UNLOCKED_ATTRS
  69. gint64
  70. UnlockedSubtract64 (volatile gint64 *dest, gint64 sub)
  71. {
  72. return *dest -= sub;
  73. }
  74. MONO_UNLOCKED_ATTRS
  75. void
  76. UnlockedWrite (volatile gint32 *dest, gint32 val)
  77. {
  78. *dest = val;
  79. }
  80. MONO_UNLOCKED_ATTRS
  81. void
  82. UnlockedWrite64 (volatile gint64 *dest, gint64 val)
  83. {
  84. *dest = val;
  85. }
  86. MONO_UNLOCKED_ATTRS
  87. void
  88. UnlockedWriteBool (volatile gboolean *dest, gboolean val)
  89. {
  90. *dest = val;
  91. }
  92. MONO_UNLOCKED_ATTRS
  93. void
  94. UnlockedWritePointer (volatile gpointer *dest, gpointer val)
  95. {
  96. *dest = val;
  97. }
  98. MONO_UNLOCKED_ATTRS
  99. gint32
  100. UnlockedRead (volatile gint32 *src)
  101. {
  102. return *src;
  103. }
  104. MONO_UNLOCKED_ATTRS
  105. gint64
  106. UnlockedRead64 (volatile gint64 *src)
  107. {
  108. return *src;
  109. }
  110. MONO_UNLOCKED_ATTRS
  111. gboolean
  112. UnlockedReadBool (volatile gboolean *src)
  113. {
  114. return *src;
  115. }
  116. MONO_UNLOCKED_ATTRS
  117. gpointer
  118. UnlockedReadPointer (volatile gpointer *src)
  119. {
  120. return *src;
  121. }
  122. #endif /* _UNLOCKED_H_ */