1 //===- llvm/System/Atomic.h - Atomic Operations -----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the llvm::sys atomic operations.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SYSTEM_ATOMIC_H
15 #define LLVM_SYSTEM_ATOMIC_H
19 #if defined(__APPLE__)
20 #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ == 0)
21 #include <libkern/OSAtomic.h>
31 #if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
32 inline void MemoryFence() {
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;
44 #elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
45 inline void MemoryFence() {
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);
54 #elif defined(__APPLE__)
55 inline void MemoryFence() {
59 typedef volatile int32_t cas_flag;
60 inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
62 OSAtomicCompareAndSwap32(c, exc, dest);
65 #elif defined(LLVM_ON_WIN32)
66 #warning Memory fence implementation requires Windows 2003 or later.
67 inline void MemoryFence() {
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);
76 #error No memory atomics implementation for your platform!