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
17 #include "llvm/Config/config.h"
21 #include <libkern/OSAtomic.h>
30 #if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
31 inline void MemoryFence() {
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;
43 #elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
44 inline void MemoryFence() {
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);
53 #elif defined(__APPLE__)
54 inline void MemoryFence() {
58 typedef volatile int32_t cas_flag;
59 inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
61 OSAtomicCompareAndSwap32(c, exc, dest);
64 #elif defined(LLVM_ON_WIN32)
65 #warning Memory fence implementation requires Windows 2003 or later.
66 inline void MemoryFence() {
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);
75 #error No memory atomics implementation for your platform!