3f937b3b43164c6f8a97015bcff49fc6632324b3
[mono.git] / mono / utils / mono-membar.h
1 /**
2  * \file
3  * Memory barrier inline functions
4  *
5  * Author:
6  *      Mark Probst (mark.probst@gmail.com)
7  *
8  * (C) 2007 Novell, Inc
9  */
10
11 #ifndef _MONO_UTILS_MONO_MEMBAR_H_
12 #define _MONO_UTILS_MONO_MEMBAR_H_
13
14 #include <config.h>
15
16 #include <glib.h>
17
18 #ifdef _MSC_VER
19 #ifndef WIN32_LEAN_AND_MEAN
20 #define WIN32_LEAN_AND_MEAN
21 #endif
22 #include <windows.h>
23 #include <intrin.h>
24
25 static inline void mono_memory_barrier (void)
26 {
27         /* NOTE: _ReadWriteBarrier and friends only prevent the
28            compiler from reordering loads and stores. To prevent
29            the CPU from doing the same, we have to use the
30            MemoryBarrier macro which expands to e.g. a serializing
31            XCHG instruction on x86. Also note that the MemoryBarrier
32            macro does *not* imply _ReadWriteBarrier, so that call
33            cannot be eliminated. */
34         _ReadWriteBarrier ();
35         MemoryBarrier ();
36 }
37
38 static inline void mono_memory_read_barrier (void)
39 {
40         _ReadBarrier ();
41         MemoryBarrier ();
42 }
43
44 static inline void mono_memory_write_barrier (void)
45 {
46         _WriteBarrier ();
47         MemoryBarrier ();
48 }
49 #elif defined(USE_GCC_ATOMIC_OPS)
50 static inline void mono_memory_barrier (void)
51 {
52         __sync_synchronize ();
53 }
54
55 static inline void mono_memory_read_barrier (void)
56 {
57         mono_memory_barrier ();
58 }
59
60 static inline void mono_memory_write_barrier (void)
61 {
62         mono_memory_barrier ();
63 }
64 #else
65 #error "Don't know how to do memory barriers!"
66 #endif
67
68 #endif  /* _MONO_UTILS_MONO_MEMBAR_H_ */