[TSan] Unlock fastpath when checking domains_to_finalize (#5598)
[mono.git] / mono / utils / unlocked.h
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  *
12  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13  */
14
15 #ifndef _UNLOCKED_H_
16 #define _UNLOCKED_H_
17
18 #include <glib.h>
19 #include <mono/utils/mono-compiler.h>
20
21 #if MONO_HAS_CLANG_THREAD_SANITIZER
22 #define MONO_UNLOCKED_ATTRS MONO_NO_SANITIZE_THREAD MONO_NEVER_INLINE static
23 #else
24 #define MONO_UNLOCKED_ATTRS MONO_ALWAYS_INLINE static inline
25 #endif
26
27 MONO_UNLOCKED_ATTRS
28 gint32
29 UnlockedIncrement (gint32 *val)
30 {
31         return ++*val;
32 }
33
34 MONO_UNLOCKED_ATTRS
35 gint64
36 UnlockedIncrement64 (gint64 *val)
37 {
38         return ++*val;
39 }
40
41 MONO_UNLOCKED_ATTRS
42 gint64
43 UnlockedDecrement64 (gint64 *val)
44 {
45         return --*val;
46 }
47
48 MONO_UNLOCKED_ATTRS
49 gint32
50 UnlockedDecrement (gint32 *val)
51 {
52         return --*val;
53 }
54
55 MONO_UNLOCKED_ATTRS
56 gint32
57 UnlockedAdd (gint32 *dest, gint32 add)
58 {
59         return *dest += add;
60 }
61
62 MONO_UNLOCKED_ATTRS
63 gint64
64 UnlockedAdd64 (gint64 *dest, gint64 add)
65 {
66         return *dest += add;
67 }
68
69 MONO_UNLOCKED_ATTRS
70 gdouble
71 UnlockedAddDouble (gdouble *dest, gdouble add)
72 {
73         return *dest += add;
74 }
75
76 MONO_UNLOCKED_ATTRS
77 gint64
78 UnlockedSubtract64 (gint64 *dest, gint64 sub)
79 {
80         return *dest -= sub;
81 }
82
83 MONO_UNLOCKED_ATTRS
84 void
85 UnlockedWrite (gint32 *dest, gint32 val)
86 {
87         *dest = val;
88 }
89
90 MONO_UNLOCKED_ATTRS
91 void
92 UnlockedWrite64 (gint64 *dest, gint64 val)
93 {
94         *dest = val;
95 }
96
97 MONO_UNLOCKED_ATTRS
98 void
99 UnlockedWriteBool (gboolean *dest, gboolean val)
100 {
101         *dest = val;
102 }
103
104 MONO_UNLOCKED_ATTRS
105 gint32
106 UnlockedRead (gint32 *src)
107 {
108         return *src;
109 }
110
111 MONO_UNLOCKED_ATTRS
112 gint64
113 UnlockedRead64 (gint64 *src)
114 {
115         return *src;
116 }
117
118 MONO_UNLOCKED_ATTRS
119 gboolean
120 UnlockedReadBool (gboolean *src)
121 {
122         return *src;
123 }
124
125 MONO_UNLOCKED_ATTRS
126 gpointer
127 UnlockedReadPointer (volatile gpointer *src)
128 {
129         return *src;
130 }
131
132 #endif /* _UNLOCKED_H_ */