Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify latch API to match std::experimental #13

Open
wants to merge 1 commit into
base: generalized_instances
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/ecst/utils/cv_operations/cv_operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ ECST_NAMESPACE
},
FWD(f));
}

void wait_counter(
mutex_type& mutex, cv_type& cv, counter_type& c) noexcept
{
unique_lock_type l(mutex);
cv.wait(l, [&c]{ return c == 0; });
}
}
}
ECST_NAMESPACE_END
4 changes: 4 additions & 0 deletions include/ecst/utils/cv_operations/latch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ ECST_NAMESPACE
/// @brief Constructs a latch with `initial_count` counter value.
latch(impl::counter_inner_type initial_count) noexcept;

void count_down() noexcept;

/// @brief Decrements the counter and notifies one thread waiting on the
/// `condition_variable`.
/// @details Asserts `_counter` to be greater than zero.
Expand All @@ -39,6 +41,8 @@ ECST_NAMESPACE
/// will decrement the counter to zero.
template <typename TF>
void execute_and_wait_until_zero(TF&& f) noexcept(noexcept(f()));

void wait() noexcept;
};
}
ECST_NAMESPACE_END
9 changes: 9 additions & 0 deletions include/ecst/utils/cv_operations/latch.inl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ ECST_NAMESPACE
{
}

void latch::count_down() noexcept
{
--_counter;
}

void latch::decrement_and_notify_one() noexcept
{
impl::decrement_cv_counter_and_notify_one(_mutex, _cv, _counter);
Expand All @@ -33,5 +38,9 @@ ECST_NAMESPACE
impl::execute_and_wait_until_counter_zero(
_mutex, _cv, _counter, FWD(f));
}

void latch::wait() noexcept {
impl::wait_counter(_mutex, _cv, _counter);
}
}
ECST_NAMESPACE_END