| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | #ifndef INCLUDE_SPINLOCK_H
 | 
					
						
							|  |  |  | #define INCLUDE_SPINLOCK_H
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QAtomicInt>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-20 13:49:21 +01:00
										 |  |  | #include "export.h"
 | 
					
						
							| 
									
										
										
										
											2018-03-03 20:23:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | class SDRBASE_API Spinlock { | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | public: | 
					
						
							|  |  |  | 	void lock() | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		while(!m_atomic.testAndSetAcquire(0, 1)) ; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void unlock() | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		while(!m_atomic.testAndSetRelease(1, 0)) ; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  | 	QAtomicInt m_atomic; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SpinlockHolder { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 	SpinlockHolder(Spinlock* spinlock) : | 
					
						
							|  |  |  | 		m_spinlock(spinlock) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		m_spinlock->lock(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	~SpinlockHolder() | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		m_spinlock->unlock(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  | 	Spinlock* m_spinlock; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif // INCLUDE_SPINLOCK_H
 |