forked from lewissbaker/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspin_mutex.hpp
More file actions
47 lines (37 loc) · 1.12 KB
/
Copy pathspin_mutex.hpp
File metadata and controls
47 lines (37 loc) · 1.12 KB
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
40
41
42
43
44
45
46
47
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#ifndef CPPCORO_SPIN_MUTEX_HPP_INCLUDED
#define CPPCORO_SPIN_MUTEX_HPP_INCLUDED
#include <atomic>
namespace cppcoro
{
class spin_mutex
{
public:
/// Initialise the mutex to the unlocked state.
spin_mutex() noexcept;
/// Attempt to lock the mutex without blocking
///
/// \return
/// true if the lock was acquired, false if the lock was already held
/// and could not be immediately acquired.
bool try_lock() noexcept;
/// Block the current thread until the lock is acquired.
///
/// This will busy-wait until it acquires the lock.
///
/// This has 'acquire' memory semantics and synchronises
/// with prior calls to unlock().
void lock() noexcept;
/// Release the lock.
///
/// This has 'release' memory semantics and synchronises with
/// lock() and try_lock().
void unlock() noexcept;
private:
std::atomic<bool> m_isLocked;
};
}
#endif