Skip to content

Commit

Permalink
semaphore: make named_semaphore_exception_factory noexcept
Browse files Browse the repository at this point in the history
Catch exceptions when formatting named_semaphore_timed_out
or broken_named_semaphore exceptions and convert them
into a static message.

Signed-off-by: Benny Halevy <[email protected]>
  • Loading branch information
bhalevy committed Oct 25, 2020
1 parent 1bd1733 commit a5cff7b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
8 changes: 4 additions & 4 deletions include/seastar/core/semaphore.hh
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,23 @@ struct semaphore_default_exception_factory {
class named_semaphore_timed_out : public semaphore_timed_out {
sstring _msg;
public:
named_semaphore_timed_out(std::string_view msg);
named_semaphore_timed_out(std::string_view msg) noexcept;
virtual const char* what() const noexcept;
};

class broken_named_semaphore : public broken_semaphore {
sstring _msg;
public:
broken_named_semaphore(std::string_view msg);
broken_named_semaphore(std::string_view msg) noexcept;
virtual const char* what() const noexcept;
};

// A factory of semaphore exceptions that contain additional context: the semaphore name
// auto sem = named_semaphore(0, named_semaphore_exception_factory{"file_opening_limit_semaphore"});
struct named_semaphore_exception_factory {
sstring name;
named_semaphore_timed_out timeout();
broken_named_semaphore broken();
named_semaphore_timed_out timeout() const noexcept;
broken_named_semaphore broken() const noexcept;
};

/// \brief Counted resource guard.
Expand Down
29 changes: 23 additions & 6 deletions src/core/semaphore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,42 @@ broken_semaphore semaphore_default_exception_factory::broken() noexcept {
// A factory of semaphore exceptions that contain additional context: the semaphore name
// auto sem = named_semaphore(0, named_semaphore_exception_factory{"file_opening_limit_semaphore"});

named_semaphore_timed_out::named_semaphore_timed_out(std::string_view msg) : _msg(format("Semaphore timed out: {}", msg)) {
static_assert(std::is_nothrow_default_constructible_v<named_semaphore_exception_factory>);
static_assert(std::is_nothrow_move_constructible_v<named_semaphore_exception_factory>);

named_semaphore_timed_out::named_semaphore_timed_out(std::string_view msg) noexcept : _msg() {
try {
memory::disable_failure_guard dfg;
_msg = format("Semaphore timed out: {}", msg);
} catch (...) {
// ignore, empty _msg will generate a static message in what().
}
}

broken_named_semaphore::broken_named_semaphore(std::string_view msg) : _msg(format("Semaphore broken: {}", msg)) {
broken_named_semaphore::broken_named_semaphore(std::string_view msg) noexcept : _msg() {
try {
memory::disable_failure_guard dfg;
_msg = format("Semaphore broken: {}", msg);
} catch (...) {
// ignore, empty _msg will generate a static message in what().
}
}

const char* named_semaphore_timed_out::what() const noexcept {
return _msg.c_str();
// return a static message if generating the dynamic message failed.
return _msg.empty() ? "Named semaphore timed out" : _msg.c_str();
}

const char* broken_named_semaphore::what() const noexcept {
return _msg.c_str();
// return a static message if generating the dynamic message failed.
return _msg.empty() ? "Broken named semaphore" : _msg.c_str();
}

named_semaphore_timed_out named_semaphore_exception_factory::timeout() {
named_semaphore_timed_out named_semaphore_exception_factory::timeout() const noexcept {
return named_semaphore_timed_out(name);
}

broken_named_semaphore named_semaphore_exception_factory::broken() {
broken_named_semaphore named_semaphore_exception_factory::broken() const noexcept {
return broken_named_semaphore(name);
}

Expand Down

0 comments on commit a5cff7b

Please sign in to comment.