Skip to content

Commit

Permalink
semaphore: use functor as wait list expiry_handler
Browse files Browse the repository at this point in the history
This allows us to instantiate a single ExceptionFactory -
the semaphore's base-class, rather than two of them,
one for the semaphore and one for the expiry_handler.

With that. constructing the wait list becomes noexcept
(based on std::function being nothrow move constructible)

Note that theoretically, exception_factory::timeout() may throw.
To simplify error handling in the expiry handler function,
catch this exception and turn the error into a generic, non-throwing one
and with that mark the returned function noexcept.

Signed-off-by: Benny Halevy <[email protected]>
  • Loading branch information
bhalevy committed Oct 25, 2020
1 parent a5cff7b commit 7cb8b49
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
31 changes: 22 additions & 9 deletions include/seastar/core/semaphore.hh
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,17 @@ private:
size_t nr;
entry(promise<>&& pr_, size_t nr_) noexcept : pr(std::move(pr_)), nr(nr_) {}
};
struct expiry_handler : public exception_factory {
expiry_handler() = default;
expiry_handler(exception_factory&& f) : exception_factory(std::move(f)) { }
void operator()(entry& e) noexcept {
e.pr.set_exception(exception_factory::timeout());
}
};
using expiry_handler = std::function<void (entry&)>;
expiring_fifo<entry, expiry_handler, clock> _wait_list;
expiry_handler make_expiry_handler() noexcept {
return [this] (entry& e) noexcept {
try {
e.pr.set_exception(exception_factory::timeout());
} catch (...) {
e.pr.set_exception(semaphore_timed_out());
}
};
}
bool has_available_units(size_t nr) const noexcept {
return _count >= 0 && (static_cast<size_t>(_count) >= nr);
}
Expand All @@ -140,8 +143,18 @@ public:
/// an unlocked mutex.
///
/// \param count number of initial units present in the counter.
basic_semaphore(size_t count) : _count(count) {}
basic_semaphore(size_t count, exception_factory&& factory) : exception_factory(factory), _count(count), _wait_list(expiry_handler(std::move(factory))) {}
basic_semaphore(size_t count) noexcept(std::is_nothrow_default_constructible_v<exception_factory>)
: exception_factory()
, _count(count),
_wait_list(make_expiry_handler())
{}
basic_semaphore(size_t count, exception_factory&& factory) noexcept(std::is_nothrow_move_constructible_v<exception_factory>)
: exception_factory(std::move(factory))
, _count(count)
, _wait_list(make_expiry_handler())
{
static_assert(std::is_nothrow_move_constructible_v<expiry_handler>);
}
/// Waits until at least a specific number of units are available in the
/// counter, and reduces the counter by that amount of units.
///
Expand Down
9 changes: 9 additions & 0 deletions src/core/semaphore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ namespace seastar {
static_assert(std::is_nothrow_default_constructible_v<semaphore_default_exception_factory>);
static_assert(std::is_nothrow_move_constructible_v<semaphore_default_exception_factory>);

static_assert(std::is_nothrow_constructible_v<semaphore, size_t>);
static_assert(std::is_nothrow_constructible_v<semaphore, size_t, semaphore_default_exception_factory&&>);
static_assert(std::is_nothrow_move_constructible_v<semaphore>);


const char* broken_semaphore::what() const noexcept {
return "Semaphore broken";
}
Expand All @@ -56,6 +61,10 @@ broken_semaphore semaphore_default_exception_factory::broken() noexcept {
static_assert(std::is_nothrow_default_constructible_v<named_semaphore_exception_factory>);
static_assert(std::is_nothrow_move_constructible_v<named_semaphore_exception_factory>);

static_assert(std::is_nothrow_constructible_v<named_semaphore, size_t>);
static_assert(std::is_nothrow_constructible_v<named_semaphore, size_t, named_semaphore_exception_factory&&>);
static_assert(std::is_nothrow_move_constructible_v<named_semaphore>);

named_semaphore_timed_out::named_semaphore_timed_out(std::string_view msg) noexcept : _msg() {
try {
memory::disable_failure_guard dfg;
Expand Down

0 comments on commit 7cb8b49

Please sign in to comment.