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"
20 #if defined(__APPLE__)
21 #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ == 0)
22 #include <libkern/OSAtomic.h>
32 #if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
33 inline void MemoryFence() {
37 typedef uint32_t cas_flag;
38 inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
39 cas_flag result = *dest;
45 #elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
46 inline void MemoryFence() {
50 typedef volatile uint32_t cas_flag;
51 inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
52 return __sync_val_compare_and_swap(dest, exc, c);
55 #elif defined(__APPLE__)
56 inline void MemoryFence() {
60 typedef volatile int32_t cas_flag;
61 inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
63 OSAtomicCompareAndSwap32(c, exc, dest);
66 #elif defined(LLVM_ON_WIN32)
67 #warning Memory fence implementation requires Windows 2003 or later.
68 inline void MemoryFence() {
72 typedef volatile long cas_flag;
73 inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
74 return _InterlockedCompareExchange(dest, exc, c);
77 #error No memory atomics implementation for your platform!