Remove the volatile marker from the cas_flag typedef, fixing some warnings.
[oota-llvm.git] / include / llvm / System / Atomic.h
1 //===- llvm/System/Atomic.h - Atomic Operations -----------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the llvm::sys atomic operations.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SYSTEM_ATOMIC_H
15 #define LLVM_SYSTEM_ATOMIC_H
16
17 #include <stdint.h>
18
19 #if defined(__APPLE__)
20 #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ == 0)
21 #include <libkern/OSAtomic.h>
22 #endif
23 #elif LLVM_ON_WIN32
24 #include <windows.h>
25 #endif
26
27
28 namespace llvm {
29   namespace sys {
30
31 #if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
32     inline void MemoryFence() {
33       return;
34     }
35
36     typedef uint32_t cas_flag;
37     inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
38       cas_flag result = *dest;
39       if (result == c)
40         *dest = exc;
41       return result;
42     }
43
44 #elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
45     inline void MemoryFence() {
46       __sync_synchronize();
47     }
48
49     typedef uint32_t cas_flag;
50     inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
51       return __sync_val_compare_and_swap(dest, exc, c);
52     }
53
54 #elif defined(__APPLE__)
55     inline void MemoryFence() {
56       OSMemoryBarrier();
57     }
58
59     typedef int32_t cas_flag;
60     inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
61       cas_flag old = *dest;
62       OSAtomicCompareAndSwap32(c, exc, dest);
63       return old;
64     }
65 #elif defined(LLVM_ON_WIN32)
66     inline void MemoryFence() {
67 #ifdef _MSC_VER
68       MemoryBarrier();
69 #elif 0
70       // FIXME: Requires SSE2 support
71       __asm__ __volatile__("mfence":::"memory");
72 #else
73       // FIXME: just temporary workaround. We need to emit some fence...
74       __asm__ __volatile__("":::"memory");
75 #endif
76     }
77
78     typedef volatile long cas_flag;
79     inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
80       return InterlockedCompareExchange(dest, exc, c);
81     }
82 #else
83 #error No memory atomics implementation for your platform!
84 #endif
85
86   }
87 }
88
89 #endif