Waybar/include/util/chrono.hpp

93 lines
1.8 KiB
C++
Raw Normal View History

2018-08-08 21:54:58 +00:00
#pragma once
#include <chrono>
#include <ctime>
#include <functional>
#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>;
inline struct timespec to_timespec(time_point t) noexcept
{
long secs = duration_cast<seconds>(t.time_since_epoch()).count();
long nsc = duration_cast<nanoseconds>(t.time_since_epoch() % seconds(1)).count();
return {secs, nsc};
}
inline time_point to_time_point(struct timespec t) noexcept
{
return time_point(duration_cast<duration>(seconds(t.tv_sec) + nanoseconds(t.tv_nsec)));
}
}
namespace waybar::util {
struct SleeperThread {
SleeperThread() = default;
SleeperThread(std::function<void()> func)
: thread{[this, func] {
do {
func();
} while (do_run);
}}
2018-08-13 12:05:13 +00:00
{
defined = true;
}
2018-08-08 21:54:58 +00:00
SleeperThread& operator=(std::function<void()> func)
{
thread = std::thread([this, func] {
do {
func();
} while (do_run);
});
2018-08-13 12:05:13 +00:00
defined = true;
2018-08-08 21:54:58 +00:00
return *this;
}
auto sleep_for(chrono::duration dur)
{
auto lock = std::unique_lock(mutex);
return condvar.wait_for(lock, dur);
}
auto sleep_until(chrono::time_point time)
{
auto lock = std::unique_lock(mutex);
return condvar.wait_until(lock, time);
}
auto wake_up()
{
condvar.notify_all();
}
~SleeperThread()
{
do_run = false;
2018-08-13 12:05:13 +00:00
if (defined) {
condvar.notify_all();
thread.join();
}
2018-08-08 21:54:58 +00:00
}
private:
std::thread thread;
std::condition_variable condvar;
std::mutex mutex;
2018-08-13 12:05:13 +00:00
bool defined = false;
2018-08-08 21:54:58 +00:00
bool do_run = true;
};
}