(Hopefully) unbreak Apple-style builds.
[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 volatile 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 volatile 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 #warning Memory fence implementation requires Windows 2003 or later.
67     inline void MemoryFence() {
68       MemoryBarrier();
69     }
70     
71     typedef volatile long cas_flag;
72     inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
73       return _InterlockedCompareExchange(dest, exc, c);
74     }
75 #else
76 #error No memory atomics implementation for your platform!
77 #endif
78     
79   }
80 }
81
82 #endif