refactor: proper modules destruction

This commit is contained in:
Alexis
2018-12-26 11:13:36 +01:00
parent 8af813c2f1
commit 399f61df98
25 changed files with 284 additions and 216 deletions

View File

@ -2,6 +2,7 @@
#include <sys/wait.h>
#include <giomm.h>
#include <unistd.h>
namespace waybar::util::command {

View File

@ -10,7 +10,7 @@ struct JsonParser {
: reader_(builder_.newCharReader())
{}
const Json::Value parse(const std::string data) const
const Json::Value parse(const std::string& data) const
{
Json::Value root;
std::string err;

View File

@ -6,30 +6,20 @@
#include <condition_variable>
#include <thread>
namespace waybar::chrono {
using namespace std::chrono;
using clock = std::chrono::system_clock;
using duration = clock::duration;
using time_point = std::chrono::time_point<clock, duration>;
}
namespace waybar::util {
struct SleeperThread {
class SleeperThread {
public:
SleeperThread() = default;
SleeperThread(std::function<void()> func)
: do_run_(true), thread_{[this, func] {
: thread_{[this, func] {
while (do_run_) func();
}}
{}
SleeperThread& operator=(std::function<void()> func)
{
do_run_ = true;
thread_ = std::thread([this, func] {
while (do_run_) func();
});
@ -41,16 +31,17 @@ struct SleeperThread {
return do_run_;
}
auto sleep_for(chrono::duration dur)
auto sleep_for(std::chrono::system_clock::duration dur)
{
auto lock = std::unique_lock(mutex_);
return condvar_.wait_for(lock, dur);
std::unique_lock lk(mutex_);
return condvar_.wait_for(lk, dur, [this] { return !do_run_; });
}
auto sleep_until(chrono::time_point time)
auto sleep_until(std::chrono::time_point<std::chrono::system_clock,
std::chrono::system_clock::duration> time_point)
{
auto lock = std::unique_lock(mutex_);
return condvar_.wait_until(lock, time);
std::unique_lock lk(mutex_);
return condvar_.wait_until(lk, time_point, [this] { return !do_run_; });
}
auto wake_up()
@ -60,7 +51,10 @@ struct SleeperThread {
auto stop()
{
do_run_ = false;
{
std::lock_guard<std::mutex> lck(mutex_);
do_run_ = false;
}
condvar_.notify_all();
}
@ -68,15 +62,15 @@ struct SleeperThread {
{
stop();
if (thread_.joinable()) {
thread_.detach();
thread_.join();
}
}
private:
bool do_run_ = false;
std::thread thread_;
std::condition_variable condvar_;
std::mutex mutex_;
bool do_run_ = true;
};
}