Copy-and-paste-o.
[oota-llvm.git] / lib / System / Atomic.cpp
1 //===-- Atomic.cpp - 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 header file implements atomic operations.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/System/Atomic.h"
15 #include "llvm/Config/config.h"
16
17 using namespace llvm;
18
19 #if defined(_MSC_VER)
20 #include <windows.h>
21 #endif
22
23 void sys::MemoryFence() {
24 #if LLVM_MULTITHREADED==0
25   return;
26 #else
27 #  if defined(__GNUC__)
28   __sync_synchronize();
29 #  elif defined(_MSC_VER)
30   MemoryBarrier();
31 #  else
32 # error No memory fence implementation for your platform!
33 #  endif
34 #endif
35 }
36
37 sys::cas_flag sys::CompareAndSwap(volatile sys::cas_flag* ptr,
38                                         sys::cas_flag new_value,
39                                         sys::cas_flag old_value) {
40 #if LLVM_MULTITHREADED==0
41   sys::cas_flag result = *dest;
42   if (result == c)
43     *dest = exc;
44   return result;
45 #elif defined(__GNUC__)
46   return __sync_val_compare_and_swap(ptr, old_value, new_value);
47 #elif defined(_MSC_VER)
48         return InterlockedCompareExchange(ptr, new_value, old_value);
49 #else
50 #  error No compare-and-swap implementation for your platform!
51 #endif
52 }