2018-08-08 21:54:58 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <chrono>
|
2019-04-18 15:52:00 +00:00
|
|
|
#include <condition_variable>
|
2018-08-08 21:54:58 +00:00
|
|
|
#include <ctime>
|
|
|
|
#include <functional>
|
|
|
|
#include <thread>
|
|
|
|
|
2023-06-07 08:17:42 +00:00
|
|
|
#include "prepare_for_sleep.h"
|
|
|
|
|
2018-08-08 21:54:58 +00:00
|
|
|
namespace waybar::util {
|
|
|
|
|
2021-06-05 03:22:16 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2022-04-06 06:37:19 +00:00
|
|
|
|
|
|
|
public:
|
2021-06-05 03:22:16 +00:00
|
|
|
CancellationGuard() { pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate); }
|
|
|
|
~CancellationGuard() { pthread_setcancelstate(oldstate, &oldstate); }
|
|
|
|
};
|
|
|
|
|
2018-12-26 10:13:36 +00:00
|
|
|
class SleeperThread {
|
2019-04-18 15:52:00 +00:00
|
|
|
public:
|
2018-08-18 15:54:20 +00:00
|
|
|
SleeperThread() = default;
|
2018-08-08 21:54:58 +00:00
|
|
|
|
2018-08-18 15:54:20 +00:00
|
|
|
SleeperThread(std::function<void()> func)
|
2019-04-18 15:52:00 +00:00
|
|
|
: thread_{[this, func] {
|
2019-06-11 20:09:47 +00:00
|
|
|
while (do_run_) {
|
|
|
|
signal_ = false;
|
|
|
|
func();
|
|
|
|
}
|
2023-06-07 08:17:42 +00:00
|
|
|
}} {
|
|
|
|
connection_ = prepare_for_sleep().connect([this](bool sleep) {
|
|
|
|
if (not sleep) wake_up();
|
|
|
|
});
|
|
|
|
}
|
2018-08-18 15:54:20 +00:00
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
SleeperThread& operator=(std::function<void()> func) {
|
2018-08-19 11:39:57 +00:00
|
|
|
thread_ = std::thread([this, func] {
|
2019-03-18 17:46:44 +00:00
|
|
|
while (do_run_) {
|
|
|
|
signal_ = false;
|
|
|
|
func();
|
|
|
|
}
|
2018-08-18 15:54:20 +00:00
|
|
|
});
|
2023-06-07 08:17:42 +00:00
|
|
|
if (connection_.empty()) {
|
|
|
|
connection_ = prepare_for_sleep().connect([this](bool sleep) {
|
|
|
|
if (not sleep) wake_up();
|
|
|
|
});
|
|
|
|
}
|
2018-08-18 15:54:20 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
bool isRunning() const { return do_run_; }
|
2018-08-18 15:54:20 +00:00
|
|
|
|
2023-09-18 21:55:50 +00:00
|
|
|
auto sleep() {
|
|
|
|
std::unique_lock lk(mutex_);
|
|
|
|
CancellationGuard cancel_lock;
|
2023-10-20 21:57:51 +00:00
|
|
|
return condvar_.wait(lk, [this] { return signal_ || !do_run_; });
|
2023-09-18 21:55:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
auto sleep_for(std::chrono::system_clock::duration dur) {
|
2022-04-06 06:37:19 +00:00
|
|
|
std::unique_lock lk(mutex_);
|
2021-06-05 03:22:16 +00:00
|
|
|
CancellationGuard cancel_lock;
|
2023-10-22 22:54:01 +00:00
|
|
|
using timepoint = std::chrono::system_clock::time_point;
|
|
|
|
static_assert(std::numeric_limits<timepoint::rep>::max() >=
|
|
|
|
std::numeric_limits<decltype(dur)::rep>::max());
|
|
|
|
if (std::chrono::system_clock::now() >= timepoint::max() - dur) {
|
|
|
|
dur = timepoint::max() - std::chrono::system_clock::now() - std::chrono::seconds(1);
|
|
|
|
}
|
2019-03-18 17:46:44 +00:00
|
|
|
return condvar_.wait_for(lk, dur, [this] { return signal_ || !do_run_; });
|
2018-08-18 15:54:20 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
auto sleep_until(
|
|
|
|
std::chrono::time_point<std::chrono::system_clock, std::chrono::system_clock::duration>
|
|
|
|
time_point) {
|
2022-04-06 06:37:19 +00:00
|
|
|
std::unique_lock lk(mutex_);
|
2021-06-05 03:22:16 +00:00
|
|
|
CancellationGuard cancel_lock;
|
2019-03-18 17:46:44 +00:00
|
|
|
return condvar_.wait_until(lk, time_point, [this] { return signal_ || !do_run_; });
|
2018-08-18 15:54:20 +00:00
|
|
|
}
|
|
|
|
|
2023-06-07 08:17:42 +00:00
|
|
|
void wake_up() {
|
2019-06-11 20:09:47 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lck(mutex_);
|
|
|
|
signal_ = true;
|
|
|
|
}
|
2018-08-19 11:39:57 +00:00
|
|
|
condvar_.notify_all();
|
2018-08-18 15:54:20 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
auto stop() {
|
2018-12-26 10:13:36 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lck(mutex_);
|
2019-03-18 17:46:44 +00:00
|
|
|
signal_ = true;
|
2018-12-26 10:13:36 +00:00
|
|
|
do_run_ = false;
|
|
|
|
}
|
2018-08-19 11:39:57 +00:00
|
|
|
condvar_.notify_all();
|
2020-05-27 07:10:38 +00:00
|
|
|
auto handle = thread_.native_handle();
|
|
|
|
if (handle != 0) {
|
|
|
|
// TODO: find a proper way to terminate thread...
|
|
|
|
pthread_cancel(handle);
|
|
|
|
}
|
2018-08-18 15:54:20 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
~SleeperThread() {
|
2023-07-16 21:29:28 +00:00
|
|
|
connection_.disconnect();
|
2018-09-18 21:15:37 +00:00
|
|
|
stop();
|
2018-12-18 16:30:54 +00:00
|
|
|
if (thread_.joinable()) {
|
2018-12-26 10:13:36 +00:00
|
|
|
thread_.join();
|
2018-12-18 16:30:54 +00:00
|
|
|
}
|
2018-09-18 21:15:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
private:
|
2022-04-06 06:37:19 +00:00
|
|
|
std::thread thread_;
|
2018-08-19 11:39:57 +00:00
|
|
|
std::condition_variable condvar_;
|
2022-04-06 06:37:19 +00:00
|
|
|
std::mutex mutex_;
|
|
|
|
bool do_run_ = true;
|
|
|
|
bool signal_ = false;
|
2023-06-07 08:17:42 +00:00
|
|
|
sigc::connection connection_;
|
2018-08-18 15:54:20 +00:00
|
|
|
};
|
2018-08-08 21:54:58 +00:00
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
} // namespace waybar::util
|