File size: 691 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#pragma once
#include <atomic>

namespace fastsignals::detail
{

class spin_mutex

{
public:
	spin_mutex() = default;
	spin_mutex(const spin_mutex&) = delete;
	spin_mutex& operator=(const spin_mutex&) = delete;
	spin_mutex(spin_mutex&&) = delete;
	spin_mutex& operator=(spin_mutex&&) = delete;

	inline bool try_lock() noexcept
	{
		return !m_busy.test_and_set(std::memory_order_acquire);
	}

	inline void lock() noexcept
	{
		while (!try_lock())
		{
			/* do nothing */;
		}
	}

	inline void unlock() noexcept
	{
		m_busy.clear(std::memory_order_release);
	}

private:
	std::atomic_flag m_busy = ATOMIC_FLAG_INIT;
};

} // namespace fastsignals::detail