mirror of
				https://github.com/f4exb/sdrangel.git
				synced 2025-10-29 20:10:22 -04:00 
			
		
		
		
	
		
			
				
	
	
		
			42 lines
		
	
	
		
			543 B
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			42 lines
		
	
	
		
			543 B
		
	
	
	
		
			C++
		
	
	
	
	
	
| #ifndef INCLUDE_SPINLOCK_H
 | |
| #define INCLUDE_SPINLOCK_H
 | |
| 
 | |
| #include <QAtomicInt>
 | |
| 
 | |
| #include "export.h"
 | |
| 
 | |
| class SDRBASE_API Spinlock {
 | |
| 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
 |