mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
refactor: format code
This commit is contained in:
@ -1,25 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <ctime>
|
||||
#include <functional>
|
||||
#include <condition_variable>
|
||||
#include <thread>
|
||||
|
||||
namespace waybar::util {
|
||||
|
||||
class SleeperThread {
|
||||
public:
|
||||
public:
|
||||
SleeperThread() = default;
|
||||
|
||||
SleeperThread(std::function<void()> func)
|
||||
: thread_{[this, func] {
|
||||
while (do_run_) func();
|
||||
}}
|
||||
{}
|
||||
: thread_{[this, func] {
|
||||
while (do_run_) func();
|
||||
}} {}
|
||||
|
||||
SleeperThread& operator=(std::function<void()> func)
|
||||
{
|
||||
SleeperThread& operator=(std::function<void()> func) {
|
||||
thread_ = std::thread([this, func] {
|
||||
while (do_run_) {
|
||||
signal_ = false;
|
||||
@ -29,32 +27,26 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool isRunning() const
|
||||
{
|
||||
return do_run_;
|
||||
}
|
||||
bool isRunning() const { return do_run_; }
|
||||
|
||||
auto sleep_for(std::chrono::system_clock::duration dur)
|
||||
{
|
||||
auto sleep_for(std::chrono::system_clock::duration dur) {
|
||||
std::unique_lock lk(mutex_);
|
||||
return condvar_.wait_for(lk, dur, [this] { return signal_ || !do_run_; });
|
||||
}
|
||||
|
||||
auto sleep_until(std::chrono::time_point<std::chrono::system_clock,
|
||||
std::chrono::system_clock::duration> time_point)
|
||||
{
|
||||
auto sleep_until(
|
||||
std::chrono::time_point<std::chrono::system_clock, std::chrono::system_clock::duration>
|
||||
time_point) {
|
||||
std::unique_lock lk(mutex_);
|
||||
return condvar_.wait_until(lk, time_point, [this] { return signal_ || !do_run_; });
|
||||
}
|
||||
|
||||
auto wake_up()
|
||||
{
|
||||
auto wake_up() {
|
||||
signal_ = true;
|
||||
condvar_.notify_all();
|
||||
}
|
||||
|
||||
auto stop()
|
||||
{
|
||||
auto stop() {
|
||||
{
|
||||
std::lock_guard<std::mutex> lck(mutex_);
|
||||
signal_ = true;
|
||||
@ -63,20 +55,19 @@ public:
|
||||
condvar_.notify_all();
|
||||
}
|
||||
|
||||
~SleeperThread()
|
||||
{
|
||||
~SleeperThread() {
|
||||
stop();
|
||||
if (thread_.joinable()) {
|
||||
thread_.join();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::thread thread_;
|
||||
private:
|
||||
std::thread thread_;
|
||||
std::condition_variable condvar_;
|
||||
std::mutex mutex_;
|
||||
bool do_run_ = true;
|
||||
bool signal_ = false;
|
||||
std::mutex mutex_;
|
||||
bool do_run_ = true;
|
||||
bool signal_ = false;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace waybar::util
|
||||
|
Reference in New Issue
Block a user