2016-12-20 11:54:17 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <chrono>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "utils/mixins.hpp"
|
|
|
|
|
|
|
|
POLYBAR_NS
|
|
|
|
|
|
|
|
namespace chrono = std::chrono;
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
|
|
|
class taskqueue : non_copyable_mixin<taskqueue> {
|
|
|
|
public:
|
|
|
|
struct deferred : non_copyable_mixin<deferred> {
|
2016-12-20 17:14:43 +00:00
|
|
|
using duration = chrono::milliseconds;
|
|
|
|
using timepoint = chrono::time_point<chrono::high_resolution_clock, duration>;
|
2016-12-20 11:54:17 +00:00
|
|
|
using callback = function<void()>;
|
|
|
|
|
2016-12-20 17:14:43 +00:00
|
|
|
explicit deferred(string&& id, timepoint&& tp, callback&& fn)
|
2016-12-20 11:54:17 +00:00
|
|
|
: id(forward<decltype(id)>(id)), when(forward<decltype(tp)>(tp)), func(forward<decltype(fn)>(fn)) {}
|
|
|
|
|
|
|
|
const string id;
|
|
|
|
const timepoint when;
|
|
|
|
const callback func;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
using make_type = unique_ptr<taskqueue>;
|
|
|
|
static make_type make();
|
|
|
|
|
|
|
|
explicit taskqueue();
|
|
|
|
~taskqueue();
|
|
|
|
|
2016-12-20 17:14:43 +00:00
|
|
|
void defer(string&& id, deferred::duration&& ms, deferred::callback&& fn);
|
|
|
|
void defer_unique(string&& id, deferred::duration&& ms, deferred::callback&& fn);
|
2016-12-20 11:54:17 +00:00
|
|
|
|
|
|
|
bool has_deferred(string&& id);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void tick();
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::thread m_thread;
|
|
|
|
std::mutex m_lock;
|
|
|
|
std::condition_variable m_hold;
|
|
|
|
std::atomic_bool m_active{true};
|
|
|
|
|
|
|
|
vector<unique_ptr<deferred>> m_deferred;
|
|
|
|
};
|
|
|
|
|
|
|
|
POLYBAR_NS_END
|