mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
Merge pull request #1132 from alebastr/fix-noexcept-condvar-crash
fix(util): protect std::condition_variable methods from pthread_cancel
This commit is contained in:
commit
d08fbb2ef2
@ -8,6 +8,20 @@
|
||||
|
||||
namespace waybar::util {
|
||||
|
||||
/**
|
||||
* Defer pthread_cancel until the end of a current scope.
|
||||
*
|
||||
* Required to protect a scope where it's unsafe to raise `__forced_unwind` exception.
|
||||
* An example of these is a call of a method marked as `noexcept`; an attempt to cancel within such
|
||||
* a method may result in a `std::terminate` call.
|
||||
*/
|
||||
class CancellationGuard {
|
||||
int oldstate;
|
||||
public:
|
||||
CancellationGuard() { pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate); }
|
||||
~CancellationGuard() { pthread_setcancelstate(oldstate, &oldstate); }
|
||||
};
|
||||
|
||||
class SleeperThread {
|
||||
public:
|
||||
SleeperThread() = default;
|
||||
@ -34,6 +48,7 @@ class SleeperThread {
|
||||
|
||||
auto sleep_for(std::chrono::system_clock::duration dur) {
|
||||
std::unique_lock lk(mutex_);
|
||||
CancellationGuard cancel_lock;
|
||||
return condvar_.wait_for(lk, dur, [this] { return signal_ || !do_run_; });
|
||||
}
|
||||
|
||||
@ -41,6 +56,7 @@ class SleeperThread {
|
||||
std::chrono::time_point<std::chrono::system_clock, std::chrono::system_clock::duration>
|
||||
time_point) {
|
||||
std::unique_lock lk(mutex_);
|
||||
CancellationGuard cancel_lock;
|
||||
return condvar_.wait_until(lk, time_point, [this] { return signal_ || !do_run_; });
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user