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:
|
2016-12-21 03:50:43 +00:00
|
|
|
struct deferred {
|
|
|
|
using clock = chrono::high_resolution_clock;
|
2016-12-20 17:14:43 +00:00
|
|
|
using duration = chrono::milliseconds;
|
2016-12-21 03:50:43 +00:00
|
|
|
using timepoint = chrono::time_point<clock, duration>;
|
|
|
|
using callback = function<void(size_t remaining)>;
|
2016-12-20 11:54:17 +00:00
|
|
|
|
2016-12-21 03:50:43 +00:00
|
|
|
explicit deferred(string id, timepoint now, duration wait, callback fn, size_t count)
|
|
|
|
: id(move(id)), func(move(fn)), now(move(now)), wait(move(wait)), count(move(count)) {}
|
2016-12-20 11:54:17 +00:00
|
|
|
|
|
|
|
const string id;
|
|
|
|
const callback func;
|
2016-12-21 03:50:43 +00:00
|
|
|
timepoint now;
|
|
|
|
duration wait;
|
|
|
|
size_t count;
|
2016-12-20 11:54:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
using make_type = unique_ptr<taskqueue>;
|
|
|
|
static make_type make();
|
|
|
|
|
|
|
|
explicit taskqueue();
|
|
|
|
~taskqueue();
|
|
|
|
|
2016-12-21 03:50:43 +00:00
|
|
|
void defer(
|
|
|
|
string id, deferred::duration ms, deferred::callback fn, deferred::duration offset = 0ms, size_t count = 1);
|
|
|
|
void defer_unique(
|
|
|
|
string id, deferred::duration ms, deferred::callback fn, deferred::duration offset = 0ms, size_t count = 1);
|
2016-12-20 11:54:17 +00:00
|
|
|
|
2016-12-21 03:50:43 +00:00
|
|
|
bool exist(const string& id);
|
|
|
|
bool purge(const string& id);
|
2016-12-20 11:54:17 +00:00
|
|
|
|
|
|
|
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
|