Skip to content

Commit 2befe84

Browse files
committed
Make the arrays in try_lock_until_impl constexpr
This is probably unnecessary but makes it clear that is possible to create them at compile time. Signed-off-by: Ted Lyngmo <[email protected]>
1 parent c110f42 commit 2befe84

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

include/beman/timed_lock_alg/mutex.hpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,24 @@ struct result {
6868
template <class Timepoint, class Locks, class... Seqs>
6969
int try_lock_until_impl(const Timepoint& end_time, Locks locks, type_pack<Seqs...>) {
7070
// an array with one function per lockable/sequence to try:
71-
std::array<result (*)(const Timepoint&, Locks&), sizeof...(Seqs)> seqs{{+[](const Timepoint& tp, Locks& lks) {
72-
return []<class Tp, std::size_t I0, std::size_t... Is>(
73-
const Tp& itp, Locks& lcks, std::index_sequence<I0, Is...>) -> result {
74-
if (std::unique_lock first{std::get<I0>(lcks), itp}) {
75-
int res = friendly_try_lock(std::get<Is>(lcks)...);
76-
if (res == -1) {
77-
first.release();
78-
return {-1, false}; // success
71+
constexpr std::array<result (*)(const Timepoint&, Locks&), sizeof...(Seqs)> seqs{
72+
{+[](const Timepoint& tp, Locks& lks) {
73+
return []<class Tp, std::size_t I0, std::size_t... Is>(
74+
const Tp& itp, Locks& lcks, std::index_sequence<I0, Is...>) -> result {
75+
if (std::unique_lock first{std::get<I0>(lcks), itp}) {
76+
int res = friendly_try_lock(std::get<Is>(lcks)...);
77+
if (res == -1) {
78+
first.release();
79+
return {-1, false}; // success
80+
}
81+
// return the index to try_lock_until next round
82+
constexpr std::array idxs{static_cast<int>(Is)...};
83+
return {idxs[static_cast<std::size_t>(res)], true};
7984
}
80-
// return the index to try_lock_until next round
81-
return {static_cast<int>(std::array{Is...}[static_cast<std::size_t>(res)]), true};
82-
}
83-
// timeout
84-
return {static_cast<int>(I0), false};
85-
}(tp, lks, Seqs{});
86-
}...}};
85+
// timeout
86+
return {static_cast<int>(I0), false};
87+
}(tp, lks, Seqs{});
88+
}...}};
8789

8890
// try rotations in seqs while there is time to retry
8991
result ret{};

0 commit comments

Comments
 (0)