1 //===-- Atomic.cpp - 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 header file implements atomic operations.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/System/Atomic.h"
15 #include "llvm/Config/config.h"
24 void sys::MemoryFence() {
25 #if LLVM_MULTITHREADED==0
28 # if defined(__GNUC__)
30 # elif defined(_MSC_VER)
33 # error No memory fence implementation for your platform!
38 uint32_t sys::CompareAndSwap32(volatile uint32_t* ptr,
41 #if LLVM_MULTITHREADED==0
42 uint32_t result = *ptr;
43 if (result == old_value)
46 #elif defined(__GNUC__)
47 return __sync_val_compare_and_swap(ptr, old_value, new_value);
48 #elif defined(_MSC_VER)
49 return InterlockedCompareExchange(ptr, new_value, old_value);
51 # error No compare-and-swap implementation for your platform!
55 int32_t sys::AtomicIncrement32(volatile int32_t* ptr) {
56 #if LLVM_MULTITHREADED==0
59 #elif defined(__GNUC__)
60 return __sync_add_and_fetch(ptr, 1);
61 #elif defined(_MSC_VER)
62 return InterlockedIncrement(ptr);
64 # error No atomic increment implementation for your platform!
68 int32_t sys::AtomicDecrement32(volatile int32_t* ptr) {
69 #if LLVM_MULTITHREADED==0
72 #elif defined(__GNUC__)
73 return __sync_sub_and_fetch(ptr, 1);
74 #elif defined(_MSC_VER)
75 return InterlockedDecrement(ptr);
77 # error No atomic decrement implementation for your platform!
81 int32_t sys::AtomicAdd32(volatile int32_t* ptr, int32_t val) {
82 #if LLVM_MULTITHREADED==0
85 #elif defined(__GNUC__)
86 return __sync_add_and_fetch(ptr, val);
87 #elif defined(_MSC_VER)
88 return InterlockedAdd(ptr, val);
90 # error No atomic add implementation for your platform!
94 int64_t sys::AtomicAdd64(volatile int64_t* ptr, int64_t val) {
95 #if LLVM_MULTITHREADED==0
98 #elif defined(__GNUC__)
99 return __sync_add_and_fetch(ptr, val);
100 #elif defined(_MSC_VER)
101 return InterlockedAdd64(ptr, val);
103 # error No atomic add implementation for your platform!