| 
									
										
										
										
											2015-05-24 06:55:12 +02:00
										 |  |  | // Copyright 2009 Dolphin Emulator Project
 | 
					
						
							| 
									
										
										
										
											2015-05-18 01:08:10 +02:00
										 |  |  | // Licensed under GPLv2+
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | // IWYU pragma: private, include "Common/Atomic.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-10 13:54:46 -05:00
										 |  |  | #pragma once
 | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <Windows.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-22 23:08:45 -08:00
										 |  |  | #include "Common/CommonTypes.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | // Atomic operations are performed in a single step by the CPU. It is
 | 
					
						
							|  |  |  | // impossible for other threads to see the operation "half-done."
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Some atomic operations can be combined with different types of memory
 | 
					
						
							|  |  |  | // barriers called "Acquire semantics" and "Release semantics", defined below.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Acquire semantics: Future memory accesses cannot be relocated to before the
 | 
					
						
							|  |  |  | //                    operation.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Release semantics: Past memory accesses cannot be relocated to after the
 | 
					
						
							|  |  |  | //                    operation.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // These barriers affect not only the compiler, but also the CPU.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // NOTE: Acquire and Release are not differentiated right now. They perform a
 | 
					
						
							|  |  |  | // full memory barrier instead of a "one-way" memory barrier. The newest
 | 
					
						
							|  |  |  | // Windows SDK has Acquire and Release versions of some Interlocked* functions.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Common | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-08-30 16:14:56 -04:00
										 |  |  | inline void AtomicAdd(volatile u32& target, u32 value) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   _InterlockedExchangeAdd((volatile LONG*)&target, (LONG)value); | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-30 16:14:56 -04:00
										 |  |  | inline void AtomicAnd(volatile u32& target, u32 value) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   _InterlockedAnd((volatile LONG*)&target, (LONG)value); | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-30 16:14:56 -04:00
										 |  |  | inline void AtomicIncrement(volatile u32& target) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   _InterlockedIncrement((volatile LONG*)&target); | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-30 16:14:56 -04:00
										 |  |  | inline void AtomicDecrement(volatile u32& target) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   _InterlockedDecrement((volatile LONG*)&target); | 
					
						
							| 
									
										
										
										
											2010-04-14 13:57:16 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-30 16:14:56 -04:00
										 |  |  | inline void AtomicOr(volatile u32& target, u32 value) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   _InterlockedOr((volatile LONG*)&target, (LONG)value); | 
					
						
							| 
									
										
										
										
											2013-09-22 16:07:45 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename T> | 
					
						
							| 
									
										
										
										
											2014-08-30 16:14:56 -04:00
										 |  |  | inline T AtomicLoad(volatile T& src) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return src;  // 32-bit reads are always atomic.
 | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-09-22 16:07:45 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | template <typename T> | 
					
						
							| 
									
										
										
										
											2014-08-30 16:14:56 -04:00
										 |  |  | inline T AtomicLoadAcquire(volatile T& src) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   T result = src;  // 32-bit reads are always atomic.
 | 
					
						
							|  |  |  |   _ReadBarrier();  // Compiler instruction only. x86 loads always have acquire semantics.
 | 
					
						
							|  |  |  |   return result; | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 16:07:45 -04:00
										 |  |  | template <typename T, typename U> | 
					
						
							| 
									
										
										
										
											2014-08-30 16:14:56 -04:00
										 |  |  | inline void AtomicStore(volatile T& dest, U value) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   dest = (T)value;  // 32-bit writes are always atomic.
 | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 16:07:45 -04:00
										 |  |  | template <typename T, typename U> | 
					
						
							| 
									
										
										
										
											2014-08-30 16:14:56 -04:00
										 |  |  | inline void AtomicStoreRelease(volatile T& dest, U value) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   _WriteBarrier();  // Compiler instruction only. x86 stores always have release semantics.
 | 
					
						
							|  |  |  |   dest = (T)value;  // 32-bit writes are always atomic.
 | 
					
						
							| 
									
										
										
										
											2013-09-22 16:07:45 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename T, typename U> | 
					
						
							| 
									
										
										
										
											2014-08-30 16:14:56 -04:00
										 |  |  | inline T* AtomicExchangeAcquire(T* volatile& loc, U newval) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return (T*)_InterlockedExchangePointer_acq((void* volatile*)&loc, (void*)newval); | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | } | 
					
						
							|  |  |  | } |