Now that we have atomics support properly detected by configure,
[oota-llvm.git] / include / llvm / System / Atomic.h
1 //===- llvm/System/Atomic.h - 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 file declares the llvm::sys atomic operations.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SYSTEM_ATOMIC_H
15 #define LLVM_SYSTEM_ATOMIC_H
16
17 #if defined(_MSC_VER)
18 #include <windows.h>
19 #endif
20
21
22 namespace llvm {
23   namespace sys {
24     
25     inline void MemoryFence() {
26 #if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
27 #  if defined(__GNUC__)
28       __asm__ __volatile__("" : : : "memory");
29 #  elif defined(_MSC_VER)
30       __asm { };
31 #  else
32 #    error No memory fence implementation for your platform!
33 #  endif
34 #else
35 #  if defined(__GNUC__)
36       __sync_synchronize();
37 #  elif defined(_MSC_VER)
38       MemoryBarrier();
39 #  else
40 #    error No memory fence implementation for your platform!
41 #  endif
42 #endif
43 }
44
45 #if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
46     typedef unsigned long cas_flag;
47     inline cas_flag CompareAndSwap(cas_flag* dest, cas_flag exc, cas_flag c) {
48       cas_flag result = *dest;
49       if (result == c)
50         *dest = exc;
51       return result;
52     }
53 #elif defined(__GNUC__)
54     typedef unsigned long cas_flag;
55     inline cas_flag CompareAndSwap(cas_flag* ptr,
56                                    cas_flag new_value,
57                                    cas_flag old_value) {
58       return __sync_val_compare_and_swap(ptr, old_value, new_value);
59     }
60 #elif defined(_MSC_VER) && _M_IX86 > 400
61     typedef LONG cas_flag;
62     inline cas_flag CompareAndSwap(cas_flag* ptr,
63                                    cas_flag new_value,
64                                    cas_flag old_value) {
65       return InterlockedCompareExchange(addr, new_value, old_value);
66     }
67 #else
68 #  error No compare-and-swap implementation for your platform!
69 #endif
70
71   }
72 }
73
74 #endif