summaryrefslogtreecommitdiff
path: root/include/llvm/System/Atomic.h
blob: 38388a4b28a9e0a062cd56ad51423244e6ce79ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//===- llvm/System/Atomic.h - Atomic Operations -----------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file declares the llvm::sys atomic operations.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SYSTEM_ATOMIC_H
#define LLVM_SYSTEM_ATOMIC_H

#if defined(_MSC_VER)
#include <windows.h>
#endif


namespace llvm {
  namespace sys {
    
    inline void MemoryFence() {
#if LLVM_MULTITHREADED==0
      return;
#else
#  if defined(__GNUC__)
      __sync_synchronize();
#  elif defined(_MSC_VER)
      MemoryBarrier();
#  else
#    error No memory fence implementation for your platform!
#  endif
#endif
}

#if LLVM_MULTITHREADED==0
    typedef unsigned long cas_flag;
    template<typename T>
    inline T CompareAndSwap(volatile T* dest,
			    T exc, T c) {
      T result = *dest;
      if (result == c)
        *dest = exc;
      return result;
    }
#elif defined(__GNUC__)
    typedef unsigned long cas_flag;
    template<typename T>
    inline T CompareAndSwap(volatile T* ptr,
			    T new_value,
			    T old_value) {
      return __sync_val_compare_and_swap(ptr, old_value, new_value);
    }
#elif defined(_MSC_VER)
    typedef LONG cas_flag;
    template<typename T>
    inline T CompareAndSwap(volatile T* ptr,
			    T new_value,
			    T old_value) {
      if (sizeof(T) == 4)
	return InterlockedCompareExchange(ptr, new_value, old_value);
      else if (sizeof(T) == 8)
	return InterlockedCompareExchange64(ptr, new_value, old_value);
      else
	assert(0 && "Unsupported compare-and-swap size!");
    }
    
    template<typename T>
    inline T* CompareAndSwap<T*>(volatile T** ptr,
				 T* new_value,
				 T* old_value) {
      return InterlockedCompareExchangePtr(ptr, new_value, old_value);
    }


#else
#  error No compare-and-swap implementation for your platform!
#endif

  }
}

#endif