waybar/include/util/chrono.hpp

93 lines
1.7 KiB
C++
Raw Normal View History

2018-08-08 23:54:58 +02:00
#pragma once
#include <chrono>
#include <ctime>
#include <functional>
#include <condition_variable>
#include <thread>
namespace waybar::chrono {
using namespace std::chrono;
2018-08-08 23:54:58 +02:00
using clock = std::chrono::system_clock;
using duration = clock::duration;
using time_point = std::chrono::time_point<clock, duration>;
2018-08-08 23:54:58 +02:00
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};
}
2018-08-08 23:54:58 +02:00
inline time_point to_time_point(struct timespec t) noexcept
{
return time_point(duration_cast<duration>(seconds(t.tv_sec) + nanoseconds(t.tv_nsec)));
}
2018-08-08 23:54:58 +02:00
}
namespace waybar::util {
struct SleeperThread {
SleeperThread() = default;
2018-08-08 23:54:58 +02:00
SleeperThread(std::function<void()> func)
: thread{[this, func] {
2018-08-08 23:54:58 +02:00
do {
func();
} while (do_run);
}}
{
defined = true;
}
SleeperThread& operator=(std::function<void()> func)
{
thread = std::thread([this, func] {
do {
func();
} while (do_run);
});
defined = true;
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;
if (defined) {
2018-08-08 23:54:58 +02:00
condvar.notify_all();
thread.join();
2018-08-08 23:54:58 +02:00
}
}
private:
std::thread thread;
std::condition_variable condvar;
std::mutex mutex;
bool defined = false;
bool do_run = true;
};
2018-08-08 23:54:58 +02:00
}