Compile fix for MSVC.
[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 #include "llvm/Config/config.h"
18
19 #if defined(_MSC_VER)
20 #define NOMINMAX
21 #include <windows.h>
22 #endif
23
24
25 namespace llvm {
26   namespace sys {
27     
28     inline void MemoryFence() {
29 #if LLVM_MULTITHREADED==0
30       return;
31 #else
32 #  if defined(__GNUC__)
33       __sync_synchronize();
34 #  elif defined(_MSC_VER)
35       MemoryBarrier();
36 #  else
37 #    error No memory fence implementation for your platform!
38 #  endif
39 #endif
40 }
41
42 #if LLVM_MULTITHREADED==0
43     typedef unsigned long cas_flag;
44     template<typename T>
45     inline T CompareAndSwap(volatile T* dest,
46                             T exc, T c) {
47       T result = *dest;
48       if (result == c)
49         *dest = exc;
50       return result;
51     }
52 #elif defined(__GNUC__)
53     typedef unsigned long cas_flag;
54     template<typename T>
55     inline T CompareAndSwap(volatile T* ptr,
56                             T new_value,
57                             T old_value) {
58       return __sync_val_compare_and_swap(ptr, old_value, new_value);
59     }
60 #elif defined(_MSC_VER)
61     typedef LONG cas_flag;
62     template<typename T>
63     inline T CompareAndSwap(volatile T* ptr,
64                             T new_value,
65                             T old_value) {
66       if (sizeof(T) == 4)
67         return InterlockedCompareExchange(ptr, new_value, old_value);
68       else if (sizeof(T) == 8)
69         return InterlockedCompareExchange64(ptr, new_value, old_value);
70       else
71         assert(0 && "Unsupported compare-and-swap size!");
72     }
73     
74     template<typename T>
75     inline T* CompareAndSwap<T*>(volatile T** ptr,
76                                  T* new_value,
77                                  T* old_value) {
78       return InterlockedCompareExchangePtr(ptr, new_value, old_value);
79     }
80
81
82 #else
83 #  error No compare-and-swap implementation for your platform!
84 #endif
85
86   }
87 }
88
89 #endif