fix(workspaces): check thread is running

This commit is contained in:
Alexis 2018-12-09 10:49:28 +01:00
parent 0acc50264e
commit c7b0639f32
5 changed files with 15 additions and 28 deletions

View File

@ -23,35 +23,24 @@ struct SleeperThread {
SleeperThread() = default; SleeperThread() = default;
SleeperThread(std::function<void()> func) SleeperThread(std::function<void()> func)
: thread_{[this, func] { : do_run_(true), thread_{[this, func] {
while(true) { while (do_run_) func();
{
std::lock_guard<std::mutex> lock(mutex_);
if (!do_run_) {
break;
}
}
func();
}
}} }}
{} {}
SleeperThread& operator=(std::function<void()> func) SleeperThread& operator=(std::function<void()> func)
{ {
do_run_ = true;
thread_ = std::thread([this, func] { thread_ = std::thread([this, func] {
while(true) { while (do_run_) func();
{
std::lock_guard<std::mutex> lock(mutex_);
if (!do_run_) {
break;
}
}
func();
}
}); });
return *this; return *this;
} }
bool isRunnging() const
{
return do_run_;
}
auto sleep_for(chrono::duration dur) auto sleep_for(chrono::duration dur)
{ {
@ -85,10 +74,10 @@ struct SleeperThread {
} }
private: private:
bool do_run_ = false;
std::thread thread_; std::thread thread_;
std::condition_variable condvar_; std::condition_variable condvar_;
std::mutex mutex_; std::mutex mutex_;
bool do_run_ = true;
}; };
} }

View File

@ -69,8 +69,7 @@ struct waybar::modules::sway::Ipc::ipc_response
size_t total = 0; size_t total = 0;
while (total < ipc_header_size_) { while (total < ipc_header_size_) {
ssize_t res = auto res = ::recv(fd, header.data() + total, ipc_header_size_ - total, 0);
::recv(fd, header.data() + total, ipc_header_size_ - total, 0);
if (res <= 0) { if (res <= 0) {
throw std::runtime_error("Unable to receive IPC response"); throw std::runtime_error("Unable to receive IPC response");
} }
@ -81,8 +80,7 @@ struct waybar::modules::sway::Ipc::ipc_response
std::string payload; std::string payload;
payload.reserve(data32[0] + 1); payload.reserve(data32[0] + 1);
while (total < data32[0]) { while (total < data32[0]) {
ssize_t res = auto res = ::recv(fd, payload.data() + total, data32[0] - total, 0);
::recv(fd, payload.data() + total, data32[0] - total, 0);
if (res < 0) { if (res < 0) {
throw std::runtime_error("Unable to receive IPC response"); throw std::runtime_error("Unable to receive IPC response");
} }

View File

@ -24,7 +24,7 @@ void waybar::modules::sway::Mode::worker()
dp.emit(); dp.emit();
} }
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << "Mode: " << e.what() << std::endl;
} }
}; };
} }

View File

@ -36,7 +36,7 @@ void waybar::modules::sway::Window::worker()
dp.emit(); dp.emit();
} }
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << "Window: " << e.what() << std::endl;
} }
}; };
} }

View File

@ -20,7 +20,7 @@ void waybar::modules::sway::Workspaces::worker()
while (bar_.output_name.empty()) { while (bar_.output_name.empty()) {
thread_.sleep_for(chrono::milliseconds(150)); thread_.sleep_for(chrono::milliseconds(150));
} }
} else if (!workspaces_.empty()) { } else if (thread_.isRunnging() && !workspaces_.empty()) {
ipc_.handleEvent(); ipc_.handleEvent();
} }
{ {
@ -30,7 +30,7 @@ void waybar::modules::sway::Workspaces::worker()
} }
dp.emit(); dp.emit();
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << "Workspaces: " << e.what() << std::endl;
} }
}; };
} }