1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-11 13:50:56 -05:00
polybar/include/components/taskqueue.hpp

63 lines
1.5 KiB
C++
Raw Normal View History

2016-12-20 06:54:17 -05: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-20 22:50:43 -05:00
struct deferred {
using clock = chrono::high_resolution_clock;
2016-12-20 12:14:43 -05:00
using duration = chrono::milliseconds;
2016-12-20 22:50:43 -05:00
using timepoint = chrono::time_point<clock, duration>;
using callback = function<void(size_t remaining)>;
2016-12-20 06:54:17 -05:00
2016-12-20 22:50:43 -05: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 06:54:17 -05:00
const string id;
const callback func;
2016-12-20 22:50:43 -05:00
timepoint now;
duration wait;
size_t count;
2016-12-20 06:54:17 -05:00
};
public:
using make_type = unique_ptr<taskqueue>;
static make_type make();
explicit taskqueue();
~taskqueue();
2016-12-20 22:50:43 -05: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 06:54:17 -05:00
2016-12-20 22:50:43 -05:00
bool exist(const string& id);
bool purge(const string& id);
2016-12-20 06:54:17 -05:00
protected:
void tick();
private:
std::thread m_thread;
std::mutex m_lock{};
2016-12-20 06:54:17 -05:00
std::condition_variable m_hold;
std::atomic_bool m_active{true};
vector<unique_ptr<deferred>> m_deferred;
};
POLYBAR_NS_END