Enable module in start funcion (#2538)

Before it was enabled by default. That means if the constructor fails,
the destructor will complain that the module was not stopped before
deconstructing.

We can't just call stop if module creation fails because the module is
only partially initialized.
This commit is contained in:
Patrick Ziegler 2021-10-15 10:33:10 +02:00 committed by GitHub
parent 76ae61f892
commit abd96eb089
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 13 additions and 1 deletions

View File

@ -158,6 +158,7 @@ namespace modules {
bool visible() const override; bool visible() const override;
void start() override;
void join() final override; void join() final override;
void stop() override; void stop() override;
void halt(string error_message) override; void halt(string error_message) override;
@ -218,7 +219,7 @@ namespace modules {
bool m_handle_events{true}; bool m_handle_events{true};
private: private:
atomic<bool> m_enabled{true}; atomic<bool> m_enabled{false};
atomic<bool> m_visible{true}; atomic<bool> m_visible{true};
atomic<bool> m_changed{true}; atomic<bool> m_changed{true};
string m_cache; string m_cache;

View File

@ -65,6 +65,11 @@ namespace modules {
return static_cast<bool>(m_enabled); return static_cast<bool>(m_enabled);
} }
template <class Impl>
void module<Impl>::start() {
m_enabled = true;
}
template <class Impl> template <class Impl>
void module<Impl>::join() { void module<Impl>::join() {
for (auto&& thread_ : m_threads) { for (auto&& thread_ : m_threads) {

View File

@ -11,6 +11,7 @@ namespace modules {
using module<Impl>::module; using module<Impl>::module;
void start() override { void start() override {
this->module<Impl>::start();
this->m_mainthread = thread(&event_module::runner, this); this->m_mainthread = thread(&event_module::runner, this);
} }

View File

@ -12,6 +12,7 @@ namespace modules {
using module<Impl>::module; using module<Impl>::module;
void start() override { void start() override {
this->module<Impl>::start();
this->m_mainthread = thread(&inotify_module::runner, this); this->m_mainthread = thread(&inotify_module::runner, this);
} }

View File

@ -11,6 +11,7 @@ namespace modules {
using module<Impl>::module; using module<Impl>::module;
void start() override { void start() override {
this->module<Impl>::start();
CAST_MOD(Impl)->update(); CAST_MOD(Impl)->update();
CAST_MOD(Impl)->broadcast(); CAST_MOD(Impl)->broadcast();
} }

View File

@ -13,6 +13,7 @@ namespace modules {
using module<Impl>::module; using module<Impl>::module;
void start() override { void start() override {
this->module<Impl>::start();
this->m_mainthread = thread(&timer_module::runner, this); this->m_mainthread = thread(&timer_module::runner, this);
} }

View File

@ -68,6 +68,7 @@ namespace modules {
* Start module and run first defined hook if configured to * Start module and run first defined hook if configured to
*/ */
void ipc_module::start() { void ipc_module::start() {
this->module::start();
m_mainthread = thread([&] { m_mainthread = thread([&] {
m_log.trace("%s: Thread id = %i", this->name(), concurrency_util::thread_id(this_thread::get_id())); m_log.trace("%s: Thread id = %i", this->name(), concurrency_util::thread_id(this_thread::get_id()));
update(); update();

View File

@ -33,6 +33,7 @@ namespace modules {
* Start the module worker * Start the module worker
*/ */
void script_module::start() { void script_module::start() {
this->module::start();
m_mainthread = thread([&] { m_mainthread = thread([&] {
try { try {
while (running()) { while (running()) {