Now with working on Leopard!
[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 "llvm/Config/config.h"
18 #include <stdint.h>
19
20 #ifdef __APPLE__
21 #include <libkern/OSAtomic.h>
22 #elif LLVM_ON_WIN32
23 #include <windows.h>
24 #endif
25
26
27 namespace llvm {
28   namespace sys {
29     
30 #if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
31     inline void MemoryFence() {
32       return;
33     }
34     
35     typedef uint32_t cas_flag;
36     inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
37       cas_flag result = *dest;
38       if (result == c)
39         *dest = exc;
40       return result;
41     }
42     
43 #elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
44     inline void MemoryFence() {
45       __sync_synchronize();
46     }
47     
48     typedef volatile uint32_t cas_flag;
49     inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
50       return __sync_val_compare_and_swap(dest, exc, c);
51     }
52     
53 #elif defined(__APPLE__)
54     inline void MemoryFence() {
55       OSMemoryBarrier();
56     }
57     
58     typedef volatile int32_t cas_flag;
59     inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
60       cas_flag old = *dest;
61       OSAtomicCompareAndSwap32(c, exc, dest);
62       return old;
63     }
64 #elif defined(LLVM_ON_WIN32)
65 #warning Memory fence implementation requires Windows 2003 or later.
66     inline void MemoryFence() {
67       MemoryBarrier();
68     }
69     
70     typedef volatile long cas_flag;
71     inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
72       return _InterlockedCompareExchange(dest, exc, c);
73     }
74 #else
75 #error No memory atomics implementation for your platform!
76 #endif
77     
78   }
79 }
80
81 #endif