VR-Forces 5.0.3 Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtAtomicScalar.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2017 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
8 
9 #pragma once
10 
12 
13 namespace makVrv
14 {
15 #if _WIN32
16  template<class T> class DtAtomicScalar
17  {
18 
19  public:
20 
21  DtAtomicScalar (const T &initial)
22  : mField(initial)
23  { }
24 
25  DtAtomicScalar (const DtAtomicScalar<T> &cousin)
26  : mField(cousin.mField)
27  { }
28 
29  DtAtomicScalar ()
30  { }
31 
32  void operator= (const DtAtomicScalar<T> &cousin)
33  {
34  mField = cousin.mField;
35  }
36 
37  T get (void) const
38  {
39  return mField;
40  }
41 
42  void set (const T &v)
43  {
44  mField = v;
45  }
46 
47  bool cas (const T &old, const T &nu)
48  {
49  if (sizeof(T)==2) {
50  return _InterlockedCompareExchange16((SHORT*)&mField, static_cast<SHORT>(nu), static_cast<SHORT>(old)) == static_cast<SHORT>(old);
51  }
52  else if (sizeof(T)==4)
53  {
54  return _InterlockedCompareExchange((LONG*)&mField, static_cast<LONG>(nu), static_cast<LONG>(old)) == static_cast<LONG>(old);
55  }
56  else if (sizeof(T)==8) {
57  return _InterlockedCompareExchange64((LONGLONG*)&mField, static_cast<LONGLONG>(nu), static_cast<LONGLONG>(old)) == static_cast<LONGLONG>(old);
58  }
59  else {
60  ASSERT_PREDICATE(false&&"AtomicScalar::cas: Only 16, 32, and 64 bit scalars supported in win32.");
61  return false;
62  }
63  }
64 
65  T operator++ (void)
66  {
67  if (sizeof(T)==2) {
68  return _InterlockedIncrement16((SHORT*)&mField);
69  } else if (sizeof(T)==4) {
70  return InterlockedIncrement((LONG*)&mField);
71  } else if (sizeof(T)==8) {
72  return InterlockedIncrement64((LONGLONG*)&mField);
73  } else {
74  ASSERT_PREDICATE(false&&"AtomicScalar::operator++(prefix): Only 16, 32, and 64 bit scalars supported in win32.");
75  return T(0);
76  }
77  }
78 
79  T operator-- (void)
80  {
81  if (sizeof(T)==2) {
82  return InterlockedDecrement16((SHORT*)&mField);
83  } else if (sizeof(T)==4) {
84  return InterlockedDecrement((LONG*)&mField);
85  } else if (sizeof(T)==8) {
86  return InterlockedDecrement64((LONGLONG*)&mField);
87  } else {
88  ASSERT_PREDICATE(false&&"AtomicScalar::operator--(prefix): Only 16, 32, and 64 bit scalars supported in win32.");
89  return T(0);
90  }
91  }
92 
93  T operator++ (int)
94  {
95  if (sizeof(T)==2) {
96  return _InterlockedIncrement16((SHORT*)&mField)-1;
97  } else if (sizeof(T)==4) {
98  return InterlockedIncrement((LONG*)&mField)-1;
99  } else if (sizeof(T)==8) {
100  return InterlockedIncrement64((LONGLONG*)&mField)-1;
101  } else {
102  ASSERT_PREDICATE(false&&"AtomicScalar::operator++(postfix): Only 16, 32, and 64 bit scalars supported in win32.");
103  return T(0);
104  }
105  }
106 
107  T operator-- (int)
108  {
109  if (sizeof(T)==2) {
110  return InterlockedDecrement16((SHORT*)&mField)+1;
111  } else if (sizeof(T)==4) {
112  return InterlockedDecrement((LONG*)&mField)+1;
113  } else if (sizeof(T)==8) {
114  return InterlockedDecrement64((LONGLONG*)&mField)+1;
115  } else {
116  ASSERT_PREDICATE(false&&"AtomicScalar::operator--(postfix): Only 16, 32, and 64 bit scalars supported in win32.");
117  return T(0);
118  }
119  }
120 
121  volatile T mField;
122 
123  };
124 #else
125  template<class T> class DtAtomicScalar
126  {
127 
128  public:
129 
130  DtAtomicScalar (const T &initial)
131  : mField(initial)
132  { }
133 
135  : mField(cousin.mField)
136  { }
137 
139  { }
140 
141  void operator= (const DtAtomicScalar<T> &cousin)
142  {
143  mField = cousin.mField;
144  }
145 
146  T get (void) const
147  {
148  return mField;
149  }
150 
151  void set (const T &v)
152  {
153  mField = v;
154  }
155 
156  bool cas (const T &old, const T &nu)
157  {
158  return __sync_bool_compare_and_swap (&mField, old, nu);
159  }
160 
161  T operator++ (void)
162  {
163  return __sync_add_and_fetch (&mField, 1);
164  }
165 
166  T operator-- (void)
167  {
168  return __sync_add_and_fetch (&mField, -1);
169  }
170 
171  T operator++ (int)
172  {
173  return __sync_fetch_and_add (&mField, 1);
174  }
175 
176  T operator-- (int)
177  {
178  return __sync_fetch_and_add (&mField, -1);
179  }
180 
181 
182  volatile T mField;
183 
184  };
185 #endif
186 
187 }
void set(const T &v)
Definition: DtAtomicScalar.h:151
DtAtomicScalar(const DtAtomicScalar< T > &cousin)
Definition: DtAtomicScalar.h:134
bool cas(const T &old, const T &nu)
Definition: DtAtomicScalar.h:156
DtAtomicScalar(const T &initial)
Definition: DtAtomicScalar.h:130
T operator--(void)
Definition: DtAtomicScalar.h:166
DtAtomicScalar()
Definition: DtAtomicScalar.h:138
T operator++(void)
Definition: DtAtomicScalar.h:161
Definition: DtAtomicScalar.h:125
#define ASSERT_PREDICATE(p)
Definition: DtAssert.h:28
assert macros
void operator=(const DtAtomicScalar< T > &cousin)
Definition: DtAtomicScalar.h:141
volatile T mField
Definition: DtAtomicScalar.h:182

Document ID: Generated on Thu Jun 1 17:58:13 EDT 2023 from SVN revision 255404
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)