Skip to content

Commit b7c3ac3

Browse files
committed
fixed and improved multiple locks
1 parent 72ad6a6 commit b7c3ac3

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

libmamba/include/mamba/util/synchronized_value.hpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,31 @@ namespace mamba::util
8181
return std::shared_lock{ mutex };
8282
}
8383

84-
/** Locks a mutex object using an exclusive lock available for that mutex type.
84+
/** Locks multiple mutex objects using the most constrained sharing lock available for that
85+
mutex type.
86+
@returns A tuple of scoped locking objects, one for each mutex. The exact types depends on the
87+
mutex types.
88+
*/
89+
template <Mutex... M>
90+
requires(sizeof...(M) > 1)
91+
[[nodiscard]]
92+
auto lock_as_readonly(M&... mutex)
93+
{
94+
return std::make_tuple(lock_as_readonly(mutex)...);
95+
}
96+
97+
/** Locks multiple non-shared mutex objects using the most constrained sharing lock available
98+
for that mutex type.
99+
@returns A scoped locking object.
100+
*/
101+
template <Mutex... M>
102+
requires(sizeof...(M) > 1) and ((not SharedMutex<M>) and ...)
103+
[[nodiscard]] auto lock_as_readonly(M&... mutex)
104+
{
105+
return std::scoped_lock{ mutex... };
106+
}
107+
108+
/** Locks a mutex object using an exclusive lock.
85109
@returns A scoped locking object.
86110
*/
87111
template <Mutex M>
@@ -91,6 +115,9 @@ namespace mamba::util
91115
return std::unique_lock{ mutex };
92116
}
93117

118+
/** Locks multiple mutex objects using an exclusive lock.
119+
@returns A scoped locking object.
120+
*/
94121
template <Mutex... M>
95122
requires(sizeof...(M) > 1)
96123
[[nodiscard]]
@@ -487,7 +514,7 @@ namespace mamba::util
487514
template <std::default_initializable T, Mutex M>
488515
synchronized_value<T, M>::synchronized_value(const synchronized_value& other)
489516
{
490-
auto _ = lock_as_exclusive(other.m_mutex);
517+
auto _ = lock_as_readonly(other.m_mutex);
491518
m_value = other.m_value;
492519
}
493520

@@ -573,9 +600,7 @@ namespace mamba::util
573600
synchronized_value<T, M>::operator==(const synchronized_value<U, OtherMutex>& other_value) const
574601
-> bool
575602
{
576-
auto this_lock [[maybe_unused]] = lock_as_readonly(m_mutex);
577-
auto other_lock [[maybe_unused]] = lock_as_readonly(other_value.m_mutex);
578-
603+
auto _ = lock_as_readonly(m_mutex, other_value.m_mutex);
579604
return m_value == other_value.m_value;
580605
}
581606

0 commit comments

Comments
 (0)