forked from andreasbuhr/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspin_mutex.cpp
More file actions
37 lines (32 loc) · 767 Bytes
/
Copy pathspin_mutex.cpp
File metadata and controls
37 lines (32 loc) · 767 Bytes
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
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include "spin_mutex.hpp"
#include "spin_wait.hpp"
namespace cppcoro
{
spin_mutex::spin_mutex() noexcept
: m_isLocked(false)
{
}
bool spin_mutex::try_lock() noexcept
{
return !m_isLocked.exchange(true, std::memory_order_acquire);
}
void spin_mutex::lock() noexcept
{
spin_wait wait;
while (!try_lock())
{
while (m_isLocked.load(std::memory_order_relaxed))
{
wait.spin_one();
}
}
}
void spin_mutex::unlock() noexcept
{
m_isLocked.store(false, std::memory_order_release);
}
}