BeAtomic.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 
11 
12 #include "Bentley.h"
13 
14 
15 #if !defined (_MANAGED) && (_MSC_VER > 1600)
16 
17 #if !defined (BEATOMIC_TEMPLATED)
18 #define BEATOMIC_TEMPLATED
19 
20 #include <atomic>
21 
22 template<class T> struct BeAtomic : std::atomic<T>
23 {
24  explicit BeAtomic(T val=T(0)) : std::atomic<T>(val){}
25  T IncrementAtomicPre(std::memory_order order=std::memory_order_seq_cst) {return this->fetch_add(1, order)+1;}
26  T DecrementAtomicPost(std::memory_order order=std::memory_order_seq_cst) {return this->fetch_sub(1, order);}
27 };
28 #endif // BEATOMIC_TEMPLATED
29 
30 #else
31 
32  extern "C" long __cdecl _InterlockedIncrement (long volatile*);
33  extern "C" long __cdecl _InterlockedDecrement (long volatile*);
34  extern "C" long __cdecl _InterlockedExchange (long volatile*, long);
35  extern "C" long __cdecl _InterlockedExchangeAdd(long volatile*, long);
36  extern "C" void* __cdecl _InterlockedExchangePointer (void* volatile*, void*);
37  extern "C" long __cdecl _InterlockedCompareExchange (long volatile *, long, long);
38  extern "C" void* __cdecl _InterlockedCompareExchangePointer (void* volatile*, void*, void*);
39 
41 
42  #define BE_ATOMIC_IMPL(UTYPE,TYPE_SUFFIX,CAST_TYPE)\
43  private:\
44  UTYPE volatile m_value;\
45  BeAtomic& operator= (BeAtomic const&);\
46  BeAtomic (BeAtomic const&);\
47  public:\
48  BeAtomic() : m_value(0) {;}\
49  BeAtomic (UInt32 v) : m_value(v) {;}\
50  UTYPE operator++() volatile {return _InterlockedIncrement ## TYPE_SUFFIX ((CAST_TYPE volatile*)&m_value);}\
51  UTYPE operator++(int) volatile {return _InterlockedIncrement ## TYPE_SUFFIX ((CAST_TYPE volatile*)&m_value)-1;}\
52  UTYPE operator--() volatile {return _InterlockedDecrement ## TYPE_SUFFIX ((CAST_TYPE volatile*)&m_value);}\
53  UTYPE operator--(int) volatile {return _InterlockedDecrement ## TYPE_SUFFIX ((CAST_TYPE volatile*)&m_value)+1;}\
54  UTYPE load() const {return _InterlockedCompareExchange ## TYPE_SUFFIX ((CAST_TYPE volatile*)&m_value,0,0);}\
55  void store (UTYPE v) const {_InterlockedExchange ## TYPE_SUFFIX ((CAST_TYPE volatile*)&m_value,v);} \
56  UTYPE IncrementAtomicPre () {return _InterlockedIncrement ## TYPE_SUFFIX ((CAST_TYPE volatile*)&m_value);}
57 
58  /*=================================================================================**/
62  template <typename T>
63  struct BeAtomic
64  {
65  };
66 
67  template <>
68  struct BeAtomic<UInt32>
69  {
71  };
72 
73  template <>
74  struct BeAtomic<int>
75  {
76  BE_ATOMIC_IMPL(int,,long)
77  };
78 
80 
81 #endif // _MANAGED
82 
#define BE_ATOMIC_IMPL(UTYPE, TYPE_SUFFIX, CAST_TYPE)
Definition: BeAtomic.h:42
void *__cdecl _InterlockedExchangePointer(void *volatile *, void *)
long __cdecl _InterlockedIncrement(long volatile *)
long __cdecl _InterlockedExchangeAdd(long volatile *, long)
uint32_t UInt32
Definition: Bentley.r.h:128
#define BEGIN_BENTLEY_NAMESPACE
Definition: Bentley.r.h:24
long __cdecl _InterlockedExchange(long volatile *, long)
Defines typedefs and constants that can be used across other namespaces. All Bentley-authored C++ sou...
std::atomic work-alike for use by C++/CLI managed code
Definition: BeAtomic.h:63
void *__cdecl _InterlockedCompareExchangePointer(void *volatile *, void *, void *)
#define END_BENTLEY_NAMESPACE
Definition: Bentley.r.h:25
long __cdecl _InterlockedDecrement(long volatile *)
long __cdecl _InterlockedCompareExchange(long volatile *, long, long)

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.