polybar/src/utils/throttle.cpp

37 lines
847 B
C++
Raw Normal View History

2016-11-20 22:04:31 +00:00
#include <thread>
2016-11-02 19:22:45 +00:00
#include "utils/throttle.hpp"
2016-11-19 05:22:44 +00:00
POLYBAR_NS
2016-11-02 19:22:45 +00:00
namespace throttle_util {
namespace strategy {
/**
* Only pass events when there are slots available
*/
2016-11-25 12:55:15 +00:00
bool try_once_or_leave_yolo::operator()(queue& q, limit l, timewindow /*unused*/) {
if (q.size() >= l) {
2016-11-02 19:22:45 +00:00
return false;
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
q.emplace_back(timepoint_clock::now());
return true;
}
/**
* If no slots are available, wait the required
* amount of time for a slot to become available
* then let the event pass
*/
2016-11-25 12:55:15 +00:00
bool wait_patiently_by_the_door::operator()(queue& q, limit l, timewindow /*unused*/) {
2016-11-02 19:22:45 +00:00
auto now = timepoint_clock::now();
q.emplace_back(now);
if (q.size() >= l) {
2016-11-20 22:04:31 +00:00
std::this_thread::sleep_for(now - q.front());
2016-11-02 19:22:45 +00:00
}
return true;
}
}
}
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END