1 // The MIT License (MIT)
2 //
3 // 	Copyright (c) 2015 Sergey Makeev, Vadim Slyusarev
4 //
5 // 	Permission is hereby granted, free of charge, to any person obtaining a copy
6 // 	of this software and associated documentation files (the "Software"), to deal
7 // 	in the Software without restriction, including without limitation the rights
8 // 	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // 	copies of the Software, and to permit persons to whom the Software is
10 // 	furnished to do so, subject to the following conditions:
11 //
12 //  The above copyright notice and this permission notice shall be included in
13 // 	all copies or substantial portions of the Software.
14 //
15 // 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // 	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // 	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // 	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // 	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // 	THE SOFTWARE.
22 
23 #pragma once
24 
25 #ifndef __MT_ATOMIC__
26 #define __MT_ATOMIC__
27 
28 #include <MTConfig.h>
29 
30 #include <type_traits>
31 
32 #if MT_SSE_INTRINSICS_SUPPORTED
33 #include <xmmintrin.h>
34 #else
35 #include <unistd.h>
36 #endif
37 
38 
39 
40 #define MT_ATOMIC_COMPILE_TIME_CHECK \
41 	static_assert(std::is_pod< Atomic32Base<T> >::value == true, "Atomic32Base must be a POD (plain old data type)"); \
42 	static_assert(sizeof(T) == sizeof(int32), "Atomic32Base, type T must be equal size as int32"); \
43 
44 #define MT_ATOMICPTR_COMPILE_TIME_CHECK \
45 	static_assert(std::is_pod< AtomicPtrBase<T> >::value == true, "AtomicPtrBase must be a POD (plain old data type)");
46 
47 
48 namespace MT
49 {
50 	//
51 	// Full memory barrier
52 	//
53 	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
HardwareFullMemoryBarrier()54 	inline void HardwareFullMemoryBarrier()
55 	{
56 		__sync_synchronize();
57 	}
58 
59 	//
60 	// Signals to the processor to give resources to threads that are waiting for them.
61 	//
62 	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
YieldProcessor()63 	inline void YieldProcessor()
64 	{
65 #if MT_SSE_INTRINSICS_SUPPORTED
66 		_mm_pause();
67 #else
68 		usleep(0);
69 #endif
70 	}
71 
72 
73 	//
74 	// Atomic int (pod type)
75 	// The operation is ordered in a sequentially consistent manner except for functions marked as relaxed.
76 	//
77 	// Note: You must use this type when you need to declare static variable instead of Atomic32
78 	//
79 	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
80 	template<typename T>
81 	struct Atomic32Base
82 	{
83 		T _value;
84 
85 		// The function returns the resulting added value.
AddFetchAtomic32Base86 		T AddFetch(T sum)
87 		{ MT_ATOMIC_COMPILE_TIME_CHECK
88 
89 			mt_release_fence();
90 			T tmp = __sync_add_and_fetch(&_value, sum);
91 			mt_acquire_fence();
92 			return tmp;
93 		}
94 
95 		// The function returns the resulting incremented value.
IncFetchAtomic32Base96 		T IncFetch()
97 		{ MT_ATOMIC_COMPILE_TIME_CHECK
98 
99 			mt_release_fence();
100 			T tmp = __sync_add_and_fetch(&_value, 1);
101 			mt_acquire_fence();
102 			return tmp;
103 		}
104 
105 		// The function returns the resulting decremented value.
DecFetchAtomic32Base106 		T DecFetch()
107 		{ MT_ATOMIC_COMPILE_TIME_CHECK
108 
109 			mt_release_fence();
110 			T tmp = __sync_sub_and_fetch(&_value, 1);
111 			mt_acquire_fence();
112 			return tmp;
113 		}
114 
LoadAtomic32Base115 		T Load() const
116 		{ MT_ATOMIC_COMPILE_TIME_CHECK
117 
118 			T tmp = LoadRelaxed();
119 			mt_acquire_fence();
120 			return tmp;
121 		}
122 
StoreAtomic32Base123 		void Store(T val)
124 		{ MT_ATOMIC_COMPILE_TIME_CHECK
125 
126 			mt_release_fence();
127 			StoreRelaxed(val);
128 		}
129 
130 		// The function returns the initial value.
ExchangeAtomic32Base131 		T Exchange(T val)
132 		{ MT_ATOMIC_COMPILE_TIME_CHECK
133 
134 			mt_release_fence();
135 			T tmp = __sync_lock_test_and_set(&_value, val);
136 			mt_acquire_fence();
137 			return tmp;
138 		}
139 
140 		// The function returns the initial value.
CompareAndSwapAtomic32Base141 		T CompareAndSwap(T compareValue, T newValue)
142 		{ MT_ATOMIC_COMPILE_TIME_CHECK
143 
144 			mt_release_fence();
145 			T tmp = __sync_val_compare_and_swap(&_value, compareValue, newValue);
146 			mt_acquire_fence();
147 			return tmp;
148 		}
149 
150 		// Relaxed operation: there are no synchronization or ordering constraints
LoadRelaxedAtomic32Base151 		T LoadRelaxed() const
152 		{ MT_ATOMIC_COMPILE_TIME_CHECK
153 
154 			return _value;
155 		}
156 
157 		// Relaxed operation: there are no synchronization or ordering constraints
StoreRelaxedAtomic32Base158 		void StoreRelaxed(T val)
159 		{ MT_ATOMIC_COMPILE_TIME_CHECK
160 
161 			_value = val;
162 		}
163 
164 	};
165 
166 
167 
168 
169 
170 	//
171 	// Atomic pointer (pod type)
172 	//
173 	// You must use this type when you need to declare static variable instead of AtomicPtr
174 	//
175 	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
176 	template<typename T>
177 	struct AtomicPtrBase
178 	{
179 		T* _value;
180 
LoadAtomicPtrBase181 		T* Load() const
182 		{ MT_ATOMICPTR_COMPILE_TIME_CHECK
183 
184 			T* tmp = LoadRelaxed();
185 			mt_acquire_fence();
186 			return tmp;
187 		}
188 
StoreAtomicPtrBase189 		void Store(const T* val)
190 		{ MT_ATOMICPTR_COMPILE_TIME_CHECK
191 
192 			mt_release_fence();
193 			StoreRelaxed(val);
194 		}
195 
196 		// The function returns the initial value.
ExchangeAtomicPtrBase197 		T* Exchange(const T* val)
198 		{ MT_ATOMICPTR_COMPILE_TIME_CHECK
199 
200 			mt_release_fence();
201 			T* tmp = (T*)__sync_lock_test_and_set((void**)&_value, (void*)val);
202 			mt_acquire_fence();
203 			return tmp;
204 		}
205 
206 		// The function returns the initial value.
CompareAndSwapAtomicPtrBase207 		T* CompareAndSwap(const T* compareValue, const T* newValue)
208 		{ MT_ATOMICPTR_COMPILE_TIME_CHECK
209 
210 			mt_release_fence();
211 			T* tmp = (T*)__sync_val_compare_and_swap((void**)&_value, (void*)compareValue, (void*)newValue);
212 			mt_acquire_fence();
213 			return tmp;
214 		}
215 
216 		// Relaxed operation: there are no synchronization or ordering constraints
LoadRelaxedAtomicPtrBase217 		T* LoadRelaxed() const
218 		{ MT_ATOMICPTR_COMPILE_TIME_CHECK
219 
220 			return _value;
221 		}
222 
223 		// Relaxed operation: there are no synchronization or ordering constraints
StoreRelaxedAtomicPtrBase224 		void StoreRelaxed(const T* val)
225 		{ MT_ATOMICPTR_COMPILE_TIME_CHECK
226 
227 			_value = (T*)val;
228 		}
229 
230 	};
231 
232 
233 
234 }
235 
236 
237 #undef MT_ATOMIC_COMPILE_TIME_CHECK
238 
239 
240 #endif
241