-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
330ebf3
commit 507ad06
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ | |
|
||
export module concurrent; | ||
|
||
export import concurrent.locked_access; | ||
export import concurrent.queue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright David Stone 2023. | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
export module concurrent.locked_access; | ||
|
||
import std_module; | ||
|
||
namespace concurrent { | ||
|
||
template<typename T, typename Mutex> | ||
struct locked_t { | ||
static_assert(std::is_reference_v<T>); | ||
constexpr locked_t(T value_, Mutex & mutex): | ||
m_value(value_), | ||
m_lock(mutex) | ||
{ | ||
} | ||
constexpr auto value() const & -> T { | ||
return m_value; | ||
} | ||
auto value() && = delete; | ||
private: | ||
T m_value; | ||
std::unique_lock<Mutex> m_lock; | ||
}; | ||
template<typename T, typename Mutex> | ||
locked_t(T &, Mutex &) -> locked_t<T &, Mutex>; | ||
|
||
export template<typename T, typename Mutex = std::mutex> | ||
struct locked_access { | ||
constexpr auto locked() & { | ||
return locked_t(m_value, m_mutex); | ||
} | ||
constexpr auto locked() const & { | ||
return locked_t(m_value, m_mutex); | ||
} | ||
constexpr auto unlocked() const -> T const & { | ||
return m_value; | ||
} | ||
|
||
private: | ||
mutable Mutex m_mutex; | ||
[[no_unique_address]] T m_value; | ||
}; | ||
|
||
} // namespace concurrent |