mirror of
https://github.com/polybar/polybar.git
synced 2024-11-11 13:50:56 -05:00
feat(core): Custom thread sleep
Makes it possible to manually wake up sleeping threads
This commit is contained in:
parent
d530da0d50
commit
ec92ac91ab
3 changed files with 35 additions and 9 deletions
|
@ -5,7 +5,6 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <shared_mutex>
|
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
@ -166,6 +165,9 @@ namespace modules
|
||||||
concurrency::SpinLock output_lock;
|
concurrency::SpinLock output_lock;
|
||||||
concurrency::SpinLock broadcast_lock;
|
concurrency::SpinLock broadcast_lock;
|
||||||
|
|
||||||
|
std::mutex sleep_lock;
|
||||||
|
std::condition_variable sleep_handler;
|
||||||
|
|
||||||
std::string name_;
|
std::string name_;
|
||||||
std::unique_ptr<Builder> builder;
|
std::unique_ptr<Builder> builder;
|
||||||
std::unique_ptr<ModuleFormatter> formatter;
|
std::unique_ptr<ModuleFormatter> formatter;
|
||||||
|
@ -201,8 +203,10 @@ namespace modules
|
||||||
return name_;
|
return name_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void stop() {
|
void stop()
|
||||||
|
{
|
||||||
log_trace(name());
|
log_trace(name());
|
||||||
|
this->wakeup();
|
||||||
std::lock_guard<concurrency::SpinLock> lck(this->broadcast_lock);
|
std::lock_guard<concurrency::SpinLock> lck(this->broadcast_lock);
|
||||||
this->enable(false);
|
this->enable(false);
|
||||||
}
|
}
|
||||||
|
@ -228,11 +232,32 @@ namespace modules
|
||||||
this->enabled_flag = state;
|
this->enabled_flag = state;
|
||||||
}
|
}
|
||||||
|
|
||||||
void broadcast() {
|
void broadcast()
|
||||||
|
{
|
||||||
std::lock_guard<concurrency::SpinLock> lck(this->broadcast_lock);
|
std::lock_guard<concurrency::SpinLock> lck(this->broadcast_lock);
|
||||||
broadcast_module_update(ConstCastModule(ModuleImpl).name());
|
broadcast_module_update(ConstCastModule(ModuleImpl).name());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void sleep(std::chrono::duration<double> sleep_duration)
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lck(this->sleep_lock);
|
||||||
|
std::thread sleep_thread([&]{
|
||||||
|
auto start = std::chrono::system_clock::now();
|
||||||
|
while ((std::chrono::system_clock::now() - start) < sleep_duration) {
|
||||||
|
std::this_thread::sleep_for(50ms);
|
||||||
|
}
|
||||||
|
this->wakeup();
|
||||||
|
});
|
||||||
|
sleep_thread.detach();
|
||||||
|
this->sleep_handler.wait(lck);
|
||||||
|
}
|
||||||
|
|
||||||
|
void wakeup()
|
||||||
|
{
|
||||||
|
log_trace("Releasing sleep lock for "+ this->name_);
|
||||||
|
this->sleep_handler.notify_one();
|
||||||
|
}
|
||||||
|
|
||||||
std::string get_format() {
|
std::string get_format() {
|
||||||
return DEFAULT_FORMAT;
|
return DEFAULT_FORMAT;
|
||||||
}
|
}
|
||||||
|
@ -301,10 +326,11 @@ namespace modules
|
||||||
void runner()
|
void runner()
|
||||||
{
|
{
|
||||||
while (this->enabled()) {
|
while (this->enabled()) {
|
||||||
std::lock_guard<concurrency::SpinLock> lck(this->update_lock);
|
{ std::lock_guard<concurrency::SpinLock> lck(this->update_lock);
|
||||||
if (CastModule(ModuleImpl)->update())
|
if (CastModule(ModuleImpl)->update())
|
||||||
CastModule(ModuleImpl)->broadcast();
|
CastModule(ModuleImpl)->broadcast();
|
||||||
std::this_thread::sleep_for(this->interval);
|
}
|
||||||
|
this->sleep(this->interval);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -98,7 +98,7 @@ void BatteryModule::subthread_routine()
|
||||||
if (this->state == STATE_CHARGING)
|
if (this->state == STATE_CHARGING)
|
||||||
this->broadcast();
|
this->broadcast();
|
||||||
|
|
||||||
std::this_thread::sleep_for(dur);
|
this->sleep(dur);
|
||||||
}
|
}
|
||||||
|
|
||||||
log_debug("Reached end of battery subthread");
|
log_debug("Reached end of battery subthread");
|
||||||
|
|
|
@ -104,7 +104,7 @@ bool MpdModule::has_event()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this->mpd->connected()) {
|
if (!this->mpd->connected()) {
|
||||||
std::this_thread::sleep_for(3s);
|
this->sleep(3s);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue