aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2023-12-15 13:14:17 +0000
committersaturneric <[email protected]>2023-12-15 13:14:17 +0000
commitf9a49043c35e73fc2d4ffb3ed9b39c33849c43b3 (patch)
tree7e03b0b62119ff5d5dcd732ec1ccb7d2296df86d
parentfix: slove some issues on memory and intilizations (diff)
downloadGpgFrontend-f9a49043c35e73fc2d4ffb3ed9b39c33849c43b3.tar.gz
GpgFrontend-f9a49043c35e73fc2d4ffb3ed9b39c33849c43b3.zip
fix: slove threading and memory issues
-rw-r--r--src/core/GpgCoreInit.cpp2
-rw-r--r--src/core/function/basic/GpgFunctionObject.cpp78
-rw-r--r--src/core/function/basic/GpgFunctionObject.h86
-rw-r--r--src/core/function/basic/SingletonStorage.cpp49
-rw-r--r--src/core/function/gpg/GpgContext.cpp33
-rw-r--r--src/core/module/GlobalModuleContext.cpp61
-rw-r--r--src/core/module/GlobalModuleContext.h5
-rw-r--r--src/core/module/GlobalRegisterTable.cpp6
-rw-r--r--src/core/module/GlobalRegisterTable.h4
-rw-r--r--src/core/module/Module.cpp43
-rw-r--r--src/core/module/Module.h21
-rw-r--r--src/core/module/ModuleManager.cpp84
-rw-r--r--src/core/module/ModuleManager.h24
-rw-r--r--src/core/thread/Task.cpp77
-rw-r--r--src/core/thread/Task.h3
-rw-r--r--src/core/thread/TaskRunner.cpp15
-rw-r--r--src/core/thread/TaskRunner.h25
-rw-r--r--src/core/thread/TaskRunnerGetter.cpp32
-rw-r--r--src/core/thread/TaskRunnerGetter.h6
-rw-r--r--src/init.cpp3
-rw-r--r--src/test/GpgFrontendTest.cpp21
-rw-r--r--src/test/core/GpgCoreTest.cpp8
22 files changed, 389 insertions, 297 deletions
diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp
index 2b2bc5be..7088e0e1 100644
--- a/src/core/GpgCoreInit.cpp
+++ b/src/core/GpgCoreInit.cpp
@@ -232,7 +232,7 @@ void InitGpgFrontendCore(CoreInitArgs args) {
// start the thread to check ctx and gnupg state
// it may take a few seconds or minutes
GpgFrontend::Thread::TaskRunnerGetter::GetInstance()
- .GetTaskRunner()
+ .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Default)
->PostTask(new Thread::Task(
[args](const DataObjectPtr&) -> int {
// read settings from config file
diff --git a/src/core/function/basic/GpgFunctionObject.cpp b/src/core/function/basic/GpgFunctionObject.cpp
index 242621d2..e31ff2d6 100644
--- a/src/core/function/basic/GpgFunctionObject.cpp
+++ b/src/core/function/basic/GpgFunctionObject.cpp
@@ -26,4 +26,80 @@
*
*/
-#include "GpgFunctionObject.h" \ No newline at end of file
+#include "GpgFunctionObject.h"
+
+#include <map>
+#include <mutex>
+#include <typeinfo>
+
+#include "function/SecureMemoryAllocator.h"
+#include "function/basic/ChannelObject.h"
+
+struct FunctionObjectTypeLockInfo {
+ std::map<int, std::mutex> channel_lock_map;
+ std::mutex type_lock;
+};
+
+std::mutex g_function_object_mutex_map_lock;
+std::map<size_t, FunctionObjectTypeLockInfo> g_function_object_mutex_map;
+
+namespace GpgFrontend {
+auto GetGlobalFunctionObjectChannelLock(const std::type_info& type, int channel)
+ -> std::mutex& {
+ std::lock_guard<std::mutex> lock_guard(g_function_object_mutex_map_lock);
+ auto& channel_map = g_function_object_mutex_map[type.hash_code()];
+ return channel_map.channel_lock_map[channel];
+}
+
+auto GetGlobalFunctionObjectTypeLock(const std::type_info& type)
+ -> std::mutex& {
+ std::lock_guard<std::mutex> lock_guard(g_function_object_mutex_map_lock);
+ auto& channel_map = g_function_object_mutex_map[type.hash_code()];
+ return channel_map.type_lock;
+}
+
+/**
+ * @brief Get the Instance object
+ *
+ * @param channel
+ * @return T&
+ */
+auto GetChannelObjectInstance(const std::type_info& type, int channel)
+ -> ChannelObject* {
+ SPDLOG_TRACE("try to get instance of type: {} at channel: {}", type.name(),
+ channel);
+
+ // lock this channel
+ std::lock_guard<std::mutex> guard(
+ GetGlobalFunctionObjectChannelLock(type, channel));
+
+ auto* p_storage =
+ SingletonStorageCollection::GetInstance(false)->GetSingletonStorage(type);
+ SPDLOG_TRACE("get singleton storage result, p_storage: {}",
+ static_cast<void*>(p_storage));
+
+ auto* p_pbj =
+ static_cast<ChannelObject*>(p_storage->FindObjectInChannel(channel));
+ SPDLOG_TRACE("find channel object result, channel {}, p_pbj: {}", channel,
+ static_cast<void*>(p_pbj));
+
+ return p_pbj;
+}
+
+auto CreateChannelObjectInstance(const std::type_info& type, int channel,
+ SecureUniquePtr<ChannelObject> channel_object)
+ -> ChannelObject* {
+ // lock this channel
+ std::lock_guard<std::mutex> guard(
+ GetGlobalFunctionObjectChannelLock(type, channel));
+
+ auto* p_storage =
+ SingletonStorageCollection::GetInstance(false)->GetSingletonStorage(type);
+ SPDLOG_TRACE("create channel object, channel {}, type: {}", channel,
+ type.name());
+
+ // do create object of this channel
+ return p_storage->SetObjectInChannel(channel, std::move(channel_object));
+}
+
+} // namespace GpgFrontend \ No newline at end of file
diff --git a/src/core/function/basic/GpgFunctionObject.h b/src/core/function/basic/GpgFunctionObject.h
index afe81131..1ea352b6 100644
--- a/src/core/function/basic/GpgFunctionObject.h
+++ b/src/core/function/basic/GpgFunctionObject.h
@@ -28,14 +28,27 @@
#pragma once
+#include <mutex>
+#include <stdexcept>
+
+#include "core/GpgFrontendCoreExport.h"
#include "core/function/basic/ChannelObject.h"
#include "core/function/basic/SingletonStorage.h"
#include "core/function/basic/SingletonStorageCollection.h"
#include "core/utils/MemoryUtils.h"
-#include "spdlog/spdlog.h"
namespace GpgFrontend {
+auto GPGFRONTEND_CORE_EXPORT GetChannelObjectInstance(
+ const std::type_info& type, int channel) -> ChannelObject*;
+
+auto GPGFRONTEND_CORE_EXPORT CreateChannelObjectInstance(
+ const std::type_info& type, int channel,
+ SecureUniquePtr<ChannelObject> channel_object) -> ChannelObject*;
+
+auto GPGFRONTEND_CORE_EXPORT
+GetGlobalFunctionObjectTypeLock(const std::type_info& type) -> std::mutex&;
+
/**
* @brief
*
@@ -66,52 +79,18 @@ class SingletonFunctionObject : public ChannelObject {
*/
static auto GetInstance(int channel = GpgFrontend::kGpgFrontendDefaultChannel)
-> T& {
- static std::mutex g_channel_mutex_map_lock;
- static std::map<int, std::mutex> g_channel_mutex_map;
-
- SPDLOG_TRACE("try to get instance of type: {} at channel: {}",
- typeid(T).name(), channel);
-
- {
- std::lock_guard<std::mutex> guard(g_channel_mutex_map_lock);
- if (g_channel_mutex_map.find(channel) == g_channel_mutex_map.end()) {
- g_channel_mutex_map[channel];
- }
- }
-
static_assert(std::is_base_of_v<SingletonFunctionObject<T>, T>,
"T not derived from SingletonFunctionObject<T>");
- auto* p_storage =
- SingletonStorageCollection::GetInstance(false)->GetSingletonStorage(
- typeid(T));
- SPDLOG_TRACE("get singleton storage result, p_storage: {}",
- static_cast<void*>(p_storage));
-
- auto* p_pbj = static_cast<T*>(p_storage->FindObjectInChannel(channel));
- SPDLOG_TRACE("find channel object result, channel {}, p_pbj: {}", channel,
- static_cast<void*>(p_pbj));
-
- if (p_pbj == nullptr) {
- SPDLOG_TRACE("create channel object, channel {}, type: {}", channel,
- typeid(T).name());
-
- // lock this channel
- std::lock_guard<std::mutex> guard(g_channel_mutex_map[channel]);
-
- // double check
- if (p_pbj = static_cast<T*>(p_storage->FindObjectInChannel(channel));
- p_pbj != nullptr) {
- return *p_pbj;
- }
-
- // do create object of this channel
- auto new_obj =
- ConvertToChannelObjectPtr<>(SecureCreateUniqueObject<T>(channel));
- return *static_cast<T*>(
- p_storage->SetObjectInChannel(channel, std::move(new_obj)));
+ const auto& type = typeid(T);
+ std::lock_guard<std::mutex> guard(GetGlobalFunctionObjectTypeLock(type));
+ auto* channel_object = GetChannelObjectInstance(type, channel);
+ if (channel_object == nullptr) {
+ channel_object = CreateChannelObjectInstance(
+ type, channel,
+ ConvertToChannelObjectPtr(SecureCreateUniqueObject<T>(channel)));
}
- return *p_pbj;
+ return *static_cast<T*>(channel_object);
}
/**
@@ -126,23 +105,10 @@ class SingletonFunctionObject : public ChannelObject {
static_assert(std::is_base_of_v<SingletonFunctionObject<T>, T>,
"T not derived from SingletonFunctionObject<T>");
- auto* p_storage =
- SingletonStorageCollection::GetInstance(false)->GetSingletonStorage(
- typeid(T));
- SPDLOG_TRACE("get singleton storage result, p_storage: {}",
- static_cast<void*>(p_storage));
-
- auto p_pbj = static_cast<T*>(p_storage->FindObjectInChannel(channel));
- SPDLOG_TRACE("find channel object result, channel {}, p_pbj: {}", channel,
- static_cast<void*>(p_pbj));
-
- if (p_pbj == nullptr) {
- SPDLOG_TRACE("create channel object, channel {}, type: {}", channel,
- typeid(T).name());
- return *static_cast<T*>(
- p_storage->SetObjectInChannel(channel, factory()));
- }
- return *p_pbj;
+ const auto& type = typeid(T);
+ std::lock_guard<std::mutex> guard(GetGlobalFunctionObjectTypeLock(type));
+ return *static_cast<T*>(
+ CreateChannelObjectInstance(type, channel, factory()));
}
/**
diff --git a/src/core/function/basic/SingletonStorage.cpp b/src/core/function/basic/SingletonStorage.cpp
index 4786ad93..73e19ddd 100644
--- a/src/core/function/basic/SingletonStorage.cpp
+++ b/src/core/function/basic/SingletonStorage.cpp
@@ -52,7 +52,7 @@ class SingletonStorage::Impl {
std::shared_lock<std::shared_mutex> lock(instances_mutex_);
ins_it = instances_map_.find(channel);
if (ins_it == instances_map_.end()) {
- SPDLOG_TRACE("cannot find channel object, channel :{}", channel);
+ SPDLOG_TRACE("cannot find channel object, channel: {}", channel);
return nullptr;
}
return ins_it->second.get();
@@ -70,35 +70,32 @@ class SingletonStorage::Impl {
auto SetObjectInChannel(int channel, ChannelObjectPtr p_obj)
-> GpgFrontend::ChannelObject* {
- {
- SPDLOG_TRACE("set channel object, type: {} in channel: {}, address: {}",
- typeid(p_obj.get()).name(), channel,
- static_cast<void*>(p_obj.get()));
-
- assert(p_obj != nullptr);
- if (p_obj == nullptr) {
- SPDLOG_ERROR("cannot set a nullptr as a channel obejct of channel: {}",
- channel);
- return nullptr;
- }
-
- p_obj->SetChannel(channel);
- auto* raw_obj = p_obj.get();
+ SPDLOG_TRACE("set channel object, type: {} in channel: {}, address: {}",
+ typeid(p_obj.get()).name(), channel,
+ static_cast<void*>(p_obj.get()));
+
+ assert(p_obj != nullptr);
+ if (p_obj == nullptr) {
+ SPDLOG_ERROR("cannot set a nullptr as a channel obejct of channel: {}",
+ channel);
+ return nullptr;
+ }
- {
- SPDLOG_TRACE(
- "register channel object to instances map, "
- "channel: {}, address: {}",
- channel, static_cast<void*>(p_obj.get()));
- std::unique_lock<std::shared_mutex> lock(instances_mutex_);
- instances_map_[channel] = std::move(p_obj);
- }
+ p_obj->SetChannel(channel);
+ auto* raw_obj = p_obj.get();
+ {
SPDLOG_TRACE(
- "set channel: {} success, current channel object address: {}",
- channel, static_cast<void*>(raw_obj));
- return raw_obj;
+ "register channel object to instances map, "
+ "channel: {}, address: {}",
+ channel, static_cast<void*>(p_obj.get()));
+ std::unique_lock<std::shared_mutex> lock(instances_mutex_);
+ instances_map_[channel] = std::move(p_obj);
}
+
+ SPDLOG_TRACE("set channel: {} success, current channel object address: {}",
+ channel, static_cast<void*>(raw_obj));
+ return raw_obj;
}
private:
diff --git a/src/core/function/gpg/GpgContext.cpp b/src/core/function/gpg/GpgContext.cpp
index 443c139b..f7223e13 100644
--- a/src/core/function/gpg/GpgContext.cpp
+++ b/src/core/function/gpg/GpgContext.cpp
@@ -46,15 +46,14 @@
namespace GpgFrontend {
-class GpgContext::Impl : public SingletonFunctionObject<GpgContext::Impl> {
+class GpgContext::Impl {
public:
/**
* Constructor
* Set up gpgme-context, set paths to app-run path
*/
- Impl(GpgContext *parent, const GpgContextInitArgs &args, int channel)
- : SingletonFunctionObject<GpgContext::Impl>(channel),
- parent_(parent),
+ Impl(GpgContext *parent, const GpgContextInitArgs &args)
+ : parent_(parent),
args_(args),
good_(default_ctx_initialize(args) && binary_ctx_initialize(args)) {}
@@ -281,14 +280,14 @@ class GpgContext::Impl : public SingletonFunctionObject<GpgContext::Impl> {
assert(p_ctx != nullptr);
binary_ctx_ref_ = p_ctx;
- if (!common_ctx_initialize(binary_ctx_ref_, args)) {
- SPDLOG_ERROR("get new ctx failed, binary");
- return false;
- }
+ // if (!common_ctx_initialize(binary_ctx_ref_, args)) {
+ // SPDLOG_ERROR("get new ctx failed, binary");
+ // return false;
+ // }
- /** Setting the output type must be done at the beginning */
- /** think this means ascii-armor --> ? */
- gpgme_set_armor(binary_ctx_ref_, 0);
+ // /** Setting the output type must be done at the beginning */
+ // /** think this means ascii-armor --> ? */
+ // gpgme_set_armor(binary_ctx_ref_, 0);
return true;
}
@@ -301,22 +300,22 @@ class GpgContext::Impl : public SingletonFunctionObject<GpgContext::Impl> {
assert(p_ctx != nullptr);
ctx_ref_ = p_ctx;
- if (!common_ctx_initialize(ctx_ref_, args)) {
- return false;
- }
+ // if (!common_ctx_initialize(ctx_ref_, args)) {
+ // return false;
+ // }
- gpgme_set_armor(ctx_ref_, 1);
+ // gpgme_set_armor(ctx_ref_, 1);
return true;
}
};
GpgContext::GpgContext(int channel)
: SingletonFunctionObject<GpgContext>(channel),
- p_(SecureCreateUniqueObject<Impl>(this, GpgContextInitArgs{}, channel)) {}
+ p_(SecureCreateUniqueObject<Impl>(this, GpgContextInitArgs{})) {}
GpgContext::GpgContext(const GpgContextInitArgs &args, int channel)
: SingletonFunctionObject<GpgContext>(channel),
- p_(SecureCreateUniqueObject<Impl>(this, args, channel)) {}
+ p_(SecureCreateUniqueObject<Impl>(this, args)) {}
auto GpgContext::Good() const -> bool { return p_->Good(); }
diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp
index e900535c..e2f58c92 100644
--- a/src/core/module/GlobalModuleContext.cpp
+++ b/src/core/module/GlobalModuleContext.cpp
@@ -40,17 +40,18 @@
#include "core/module/Module.h"
#include "core/thread/Task.h"
#include "model/DataObject.h"
+#include "thread/TaskRunnerGetter.h"
+#include "utils/MemoryUtils.h"
namespace GpgFrontend::Module {
class GlobalModuleContext::Impl {
public:
- explicit Impl(TaskRunnerPtr task_runner)
+ explicit Impl()
: random_gen_(
(boost::posix_time::microsec_clock::universal_time() -
boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1)))
- .total_milliseconds()),
- default_task_runner_(std::move(task_runner)) {
+ .total_milliseconds()) {
// Initialize acquired channels with default values.
acquired_channel_.insert(kGpgFrontendDefaultChannel);
acquired_channel_.insert(kGpgFrontendNonAsciiChannel);
@@ -77,23 +78,15 @@ class GlobalModuleContext::Impl {
return kGpgFrontendDefaultChannel;
}
- auto GetTaskRunner(ModuleRawPtr module) -> std::optional<TaskRunnerPtr> {
- auto opt = search_module_register_table(module->GetModuleIdentifier());
- if (!opt.has_value()) {
- return std::nullopt;
- }
- return opt.value()->task_runner;
+ auto GetTaskRunner(ModuleRawPtr /*module*/) -> std::optional<TaskRunnerPtr> {
+ return Thread::TaskRunnerGetter::GetInstance().GetTaskRunner(
+ Thread::TaskRunnerGetter::kTaskRunnerType_Module);
}
- auto GetTaskRunner(ModuleIdentifier module_id)
+ auto GetTaskRunner(ModuleIdentifier /*module_id*/)
-> std::optional<TaskRunnerPtr> {
- // Search for the module in the register table.
- auto module_info_opt = search_module_register_table(module_id);
- if (!module_info_opt.has_value()) {
- SPDLOG_ERROR("cannot find module id {} at register table", module_id);
- return std::nullopt;
- }
- return module_info_opt.value()->task_runner;
+ return Thread::TaskRunnerGetter::GetInstance().GetTaskRunner(
+ Thread::TaskRunnerGetter::kTaskRunnerType_Module);
}
auto GetGlobalTaskRunner() -> std::optional<TaskRunnerPtr> {
@@ -120,14 +113,13 @@ class GlobalModuleContext::Impl {
GpgFrontend::SecureCreateSharedObject<ModuleRegisterInfo>();
register_info->module = module;
register_info->channel = acquire_new_unique_channel();
- register_info->task_runner =
- GpgFrontend::SecureCreateSharedObject<Thread::TaskRunner>();
- register_info->task_runner->Start();
// move module to its task runner' thread
register_info->module->setParent(nullptr);
register_info->module->moveToThread(
- register_info->task_runner->GetThread());
+ Thread::TaskRunnerGetter::GetInstance()
+ .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Module)
+ ->GetThread());
// Register the module with its identifier.
module_register_table_[module->GetModuleIdentifier()] = register_info;
@@ -250,11 +242,9 @@ class GlobalModuleContext::Impl {
auto module_info = module_info_opt.value();
auto module = module_info->module;
- SPDLOG_DEBUG(
- "module {} is listening to event {}, activate state: {}, task runner "
- "running state: {}",
- module_info->module->GetModuleIdentifier(), event->GetIdentifier(),
- module_info->activate, module_info->task_runner->IsRunning());
+ SPDLOG_DEBUG("module {} is listening to event {}, activate state: {}",
+ module_info->module->GetModuleIdentifier(),
+ event->GetIdentifier(), module_info->activate);
// Check if the module is activated
if (!module_info->activate) continue;
@@ -272,12 +262,14 @@ class GlobalModuleContext::Impl {
}
};
- module_info->task_runner->PostTask(
- new Thread::Task(exec_runnerable,
- (boost::format("event/%1%/module/exec/%2%") %
- event_id % listener_module_id)
- .str(),
- nullptr, exec_callback));
+ Thread::TaskRunnerGetter::GetInstance()
+ .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Module)
+ ->PostTask(
+ new Thread::Task(exec_runnerable,
+ (boost::format("event/%1%/module/exec/%2%") %
+ event_id % listener_module_id)
+ .str(),
+ nullptr, exec_callback));
}
// Return true to indicate successful execution of all modules
@@ -292,7 +284,6 @@ class GlobalModuleContext::Impl {
private:
struct ModuleRegisterInfo {
int channel;
- TaskRunnerPtr task_runner;
ModulePtr module;
bool activate;
};
@@ -334,8 +325,8 @@ class GlobalModuleContext::Impl {
};
// Constructor for GlobalModuleContext, takes a TaskRunnerPtr as an argument.
-GlobalModuleContext::GlobalModuleContext(TaskRunnerPtr task_runner)
- : p_(std::make_unique<Impl>(std::move(task_runner))) {}
+GlobalModuleContext::GlobalModuleContext()
+ : p_(SecureCreateUniqueObject<Impl>()) {}
GlobalModuleContext::~GlobalModuleContext() = default;
diff --git a/src/core/module/GlobalModuleContext.h b/src/core/module/GlobalModuleContext.h
index 1535f2ca..48c0b7ee 100644
--- a/src/core/module/GlobalModuleContext.h
+++ b/src/core/module/GlobalModuleContext.h
@@ -32,6 +32,7 @@
#include "core/module/Event.h"
#include "core/thread/TaskRunner.h"
+#include "function/SecureMemoryAllocator.h"
#include "module/GlobalRegisterTable.h"
namespace GpgFrontend::Module {
@@ -53,7 +54,7 @@ using TaskRunnerPtr = std::shared_ptr<Thread::TaskRunner>;
class GPGFRONTEND_CORE_EXPORT GlobalModuleContext : public QObject {
Q_OBJECT
public:
- explicit GlobalModuleContext(TaskRunnerPtr);
+ explicit GlobalModuleContext();
~GlobalModuleContext() override;
@@ -81,7 +82,7 @@ class GPGFRONTEND_CORE_EXPORT GlobalModuleContext : public QObject {
private:
class Impl;
- std::unique_ptr<Impl> p_;
+ SecureUniquePtr<Impl> p_;
};
} // namespace GpgFrontend::Module \ No newline at end of file
diff --git a/src/core/module/GlobalRegisterTable.cpp b/src/core/module/GlobalRegisterTable.cpp
index 09c39dae..6f43aeb4 100644
--- a/src/core/module/GlobalRegisterTable.cpp
+++ b/src/core/module/GlobalRegisterTable.cpp
@@ -36,6 +36,9 @@
#include <utility>
#include <vector>
+#include "function/SecureMemoryAllocator.h"
+#include "utils/MemoryUtils.h"
+
namespace GpgFrontend::Module {
class GlobalRegisterTable::Impl {
@@ -141,7 +144,8 @@ class GlobalRegisterTable::Impl {
Table global_register_table_;
};
-GlobalRegisterTable::GlobalRegisterTable() : p_(std::make_unique<Impl>(this)) {}
+GlobalRegisterTable::GlobalRegisterTable()
+ : p_(SecureCreateUniqueObject<Impl>(this)) {}
GlobalRegisterTable::~GlobalRegisterTable() = default;
diff --git a/src/core/module/GlobalRegisterTable.h b/src/core/module/GlobalRegisterTable.h
index 09627841..c4685337 100644
--- a/src/core/module/GlobalRegisterTable.h
+++ b/src/core/module/GlobalRegisterTable.h
@@ -32,6 +32,8 @@
#include <functional>
#include <optional>
+#include "function/SecureMemoryAllocator.h"
+
namespace GpgFrontend::Module {
using Namespace = std::string;
@@ -58,7 +60,7 @@ class GlobalRegisterTable : public QObject {
private:
class Impl;
- std::unique_ptr<Impl> p_;
+ SecureUniquePtr<Impl> p_;
};
} // namespace GpgFrontend::Module \ No newline at end of file
diff --git a/src/core/module/Module.cpp b/src/core/module/Module.cpp
index 642aadf8..b19e36e4 100644
--- a/src/core/module/Module.cpp
+++ b/src/core/module/Module.cpp
@@ -29,6 +29,7 @@
#include "Module.h"
#include <boost/format.hpp>
+#include <utility>
#include "core/module/GlobalModuleContext.h"
@@ -43,34 +44,38 @@ class Module::Impl {
Impl(ModuleRawPtr m_ptr, ModuleIdentifier id, ModuleVersion version,
ModuleMetaData meta_data)
: m_ptr_(m_ptr),
- identifier_(id),
- version_(version),
- meta_data_(meta_data) {}
+ identifier_(std::move(id)),
+ version_(std::move(version)),
+ meta_data_(std::move(meta_data)) {}
- int GetChannel() { return get_gpc()->GetChannel(m_ptr_); }
+ auto GetChannel() -> int { return get_gpc()->GetChannel(m_ptr_); }
- int GetDefaultChannel() { return get_gpc()->GetDefaultChannel(m_ptr_); }
+ auto GetDefaultChannel() -> int {
+ return get_gpc()->GetDefaultChannel(m_ptr_);
+ }
- std::optional<TaskRunnerPtr> GetTaskRunner() {
+ auto GetTaskRunner() -> std::optional<TaskRunnerPtr> {
return get_gpc()->GetTaskRunner(m_ptr_);
}
- bool ListenEvent(EventIdentifier event) {
- return get_gpc()->ListenEvent(GetModuleIdentifier(), event);
+ auto ListenEvent(EventIdentifier event) -> bool {
+ return get_gpc()->ListenEvent(GetModuleIdentifier(), std::move(event));
}
- ModuleIdentifier GetModuleIdentifier() const { return identifier_; }
+ [[nodiscard]] auto GetModuleIdentifier() const -> ModuleIdentifier {
+ return identifier_;
+ }
- void SetGPC(GMCPtr gpc) { gpc_ = gpc; }
+ void SetGPC(GlobalModuleContext* gpc) { gpc_ = gpc; }
private:
- GMCPtr gpc_;
+ GlobalModuleContext* gpc_;
Module* m_ptr_;
const ModuleIdentifier identifier_;
const ModuleVersion version_;
const ModuleMetaData meta_data_;
- const GMCPtr get_gpc() {
+ auto get_gpc() -> GlobalModuleContext* {
if (gpc_ == nullptr) {
throw std::runtime_error("module is not registered by module manager");
}
@@ -84,21 +89,21 @@ Module::Module(ModuleIdentifier id, ModuleVersion version,
Module::~Module() = default;
-int Module::getChannel() { return p_->GetChannel(); }
+auto Module::getChannel() -> int { return p_->GetChannel(); }
-int Module::getDefaultChannel() { return p_->GetDefaultChannel(); }
+auto Module::getDefaultChannel() -> int { return p_->GetDefaultChannel(); }
-TaskRunnerPtr Module::getTaskRunner() {
+auto Module::getTaskRunner() -> TaskRunnerPtr {
return p_->GetTaskRunner().value_or(nullptr);
}
-bool Module::listenEvent(EventIdentifier event) {
- return p_->ListenEvent(event);
+auto Module::listenEvent(EventIdentifier event) -> bool {
+ return p_->ListenEvent(std::move(event));
}
-ModuleIdentifier Module::GetModuleIdentifier() const {
+auto Module::GetModuleIdentifier() const -> ModuleIdentifier {
return p_->GetModuleIdentifier();
}
-void Module::SetGPC(GMCPtr gpc) { p_->SetGPC(gpc); }
+void Module::SetGPC(GlobalModuleContext* gpc) { p_->SetGPC(gpc); }
} // namespace GpgFrontend::Module \ No newline at end of file
diff --git a/src/core/module/Module.h b/src/core/module/Module.h
index da87f88e..6cc1cc3f 100644
--- a/src/core/module/Module.h
+++ b/src/core/module/Module.h
@@ -41,7 +41,6 @@ using ModuleIdentifier = std::string;
using ModuleVersion = std::string;
using ModuleMetaData = std::map<std::string, std::string>;
using ModulePtr = std::shared_ptr<Module>;
-using GMCPtr = std::shared_ptr<GlobalModuleContext>;
using TaskRunnerPtr = std::shared_ptr<Thread::TaskRunner>;
@@ -52,26 +51,26 @@ class GPGFRONTEND_CORE_EXPORT Module : public QObject {
~Module();
- virtual bool Register() = 0;
+ virtual auto Register() -> bool = 0;
- virtual bool Active() = 0;
+ virtual auto Active() -> bool = 0;
- virtual int Exec(EventRefrernce) = 0;
+ virtual auto Exec(EventRefrernce) -> int = 0;
- virtual bool Deactive() = 0;
+ virtual auto Deactive() -> bool = 0;
- ModuleIdentifier GetModuleIdentifier() const;
+ [[nodiscard]] auto GetModuleIdentifier() const -> ModuleIdentifier;
- void SetGPC(GMCPtr);
+ void SetGPC(GlobalModuleContext*);
protected:
- int getChannel();
+ auto getChannel() -> int;
- int getDefaultChannel();
+ auto getDefaultChannel() -> int;
- TaskRunnerPtr getTaskRunner();
+ auto getTaskRunner() -> TaskRunnerPtr;
- bool listenEvent(EventIdentifier);
+ auto listenEvent(EventIdentifier) -> bool;
private:
class Impl;
diff --git a/src/core/module/ModuleManager.cpp b/src/core/module/ModuleManager.cpp
index 26974f1c..26edc43e 100644
--- a/src/core/module/ModuleManager.cpp
+++ b/src/core/module/ModuleManager.cpp
@@ -29,13 +29,18 @@
#include "ModuleManager.h"
#include <boost/format.hpp>
+#include <memory>
#include <utility>
+#include "GpgConstants.h"
#include "core/module/GlobalModuleContext.h"
#include "core/module/GlobalRegisterTable.h"
#include "core/module/Module.h"
#include "core/thread/Task.h"
#include "core/thread/TaskRunner.h"
+#include "function/SecureMemoryAllocator.h"
+#include "function/basic/GpgFunctionObject.h"
+#include "thread/TaskRunnerGetter.h"
#include "utils/MemoryUtils.h"
namespace GpgFrontend::Module {
@@ -43,40 +48,43 @@ namespace GpgFrontend::Module {
class ModuleManager::Impl {
public:
Impl()
- : task_runner_(
- GpgFrontend::SecureCreateSharedObject<Thread::TaskRunner>()),
- gmc_(GpgFrontend::SecureCreateSharedObject<GlobalModuleContext>(
- task_runner_)),
- grt_(GpgFrontend::SecureCreateSharedObject<GlobalRegisterTable>()) {
- task_runner_->Start();
- }
+ : gmc_(GpgFrontend::SecureCreateUniqueObject<GlobalModuleContext>()),
+ grt_(GpgFrontend::SecureCreateUniqueObject<GlobalRegisterTable>()) {}
+
+ ~Impl() = default;
void RegisterModule(const ModulePtr& module) {
- task_runner_->PostTask(new Thread::Task(
- [=](GpgFrontend::DataObjectPtr) -> int {
- module->SetGPC(gmc_);
- gmc_->RegisterModule(module);
- return 0;
- },
- __func__, nullptr));
+ Thread::TaskRunnerGetter::GetInstance()
+ .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Default)
+ ->PostTask(new Thread::Task(
+ [=](GpgFrontend::DataObjectPtr) -> int {
+ module->SetGPC(gmc_.get());
+ gmc_->RegisterModule(module);
+ return 0;
+ },
+ __func__, nullptr));
}
void TriggerEvent(const EventRefrernce& event) {
- task_runner_->PostTask(new Thread::Task(
- [=](const GpgFrontend::DataObjectPtr&) -> int {
- gmc_->TriggerEvent(event);
- return 0;
- },
- __func__, nullptr));
+ Thread::TaskRunnerGetter::GetInstance()
+ .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Default)
+ ->PostTask(new Thread::Task(
+ [=](const GpgFrontend::DataObjectPtr&) -> int {
+ gmc_->TriggerEvent(event);
+ return 0;
+ },
+ __func__, nullptr));
}
void ActiveModule(const ModuleIdentifier& identifier) {
- task_runner_->PostTask(new Thread::Task(
- [=](const GpgFrontend::DataObjectPtr&) -> int {
- gmc_->ActiveModule(identifier);
- return 0;
- },
- __func__, nullptr));
+ Thread::TaskRunnerGetter::GetInstance()
+ .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Default)
+ ->PostTask(new Thread::Task(
+ [=](const GpgFrontend::DataObjectPtr&) -> int {
+ gmc_->ActiveModule(identifier);
+ return 0;
+ },
+ __func__, nullptr));
}
auto GetTaskRunner(ModuleIdentifier module_id)
@@ -107,40 +115,36 @@ class ModuleManager::Impl {
private:
static ModuleMangerPtr global_module_manager;
- TaskRunnerPtr task_runner_;
- GMCPtr gmc_;
- GRTPtr grt_;
+ SecureUniquePtr<GlobalModuleContext> gmc_;
+ SecureUniquePtr<GlobalRegisterTable> grt_;
};
auto IsModuleAcivate(ModuleIdentifier id) -> bool {
- return ModuleManager::GetInstance()->IsModuleActivated(id);
+ return ModuleManager::GetInstance().IsModuleActivated(id);
}
auto UpsertRTValue(const std::string& namespace_, const std::string& key,
const std::any& value) -> bool {
- return ModuleManager::GetInstance()->UpsertRTValue(namespace_, key,
- std::any(value));
+ return ModuleManager::GetInstance().UpsertRTValue(namespace_, key,
+ std::any(value));
}
auto ListenRTPublishEvent(QObject* o, Namespace n, Key k, LPCallback c)
-> bool {
- return ModuleManager::GetInstance()->ListenRTPublish(o, n, k, c);
+ return ModuleManager::GetInstance().ListenRTPublish(o, n, k, c);
}
auto ListRTChildKeys(const std::string& namespace_, const std::string& key)
-> std::vector<Key> {
- return ModuleManager::GetInstance()->ListRTChildKeys(namespace_, key);
+ return ModuleManager::GetInstance().ListRTChildKeys(namespace_, key);
}
-ModuleManager::ModuleManager() : p_(std::make_unique<Impl>()) {}
+ModuleManager::ModuleManager(int channel)
+ : SingletonFunctionObject<ModuleManager>(channel),
+ p_(std::make_unique<Impl>()) {}
ModuleManager::~ModuleManager() = default;
-auto ModuleManager::GetInstance() -> ModuleMangerPtr {
- static ModuleMangerPtr g = SecureCreateSharedObject<ModuleManager>();
- return g;
-}
-
void ModuleManager::RegisterModule(ModulePtr module) {
return p_->RegisterModule(module);
}
diff --git a/src/core/module/ModuleManager.h b/src/core/module/ModuleManager.h
index 1b1133da..ace71c13 100644
--- a/src/core/module/ModuleManager.h
+++ b/src/core/module/ModuleManager.h
@@ -28,8 +28,10 @@
#pragma once
+#include <mutex>
#include <vector>
+#include "core/function/basic/GpgFunctionObject.h"
#include "core/module/Event.h"
namespace GpgFrontend::Thread {
@@ -54,15 +56,13 @@ using Namespace = std::string;
using Key = std::string;
using LPCallback = std::function<void(Namespace, Key, int, std::any)>;
-class GPGFRONTEND_CORE_EXPORT ModuleManager : public QObject {
- Q_OBJECT
+class GPGFRONTEND_CORE_EXPORT ModuleManager
+ : public SingletonFunctionObject<ModuleManager> {
public:
- ModuleManager();
+ explicit ModuleManager(int channel);
virtual ~ModuleManager() override;
- static auto GetInstance() -> ModuleMangerPtr;
-
void RegisterModule(ModulePtr);
auto IsModuleActivated(ModuleIdentifier) -> bool;
@@ -91,23 +91,23 @@ class GPGFRONTEND_CORE_EXPORT ModuleManager : public QObject {
template <typename T, typename... Args>
void RegisterModule(Args&&... args) {
- ModuleManager::GetInstance()->RegisterModule(
+ ModuleManager::GetInstance().RegisterModule(
GpgFrontend::SecureCreateSharedObject<T>(std::forward<Args>(args)...));
}
template <typename T, typename... Args>
void RegisterAndActivateModule(Args&&... args) {
- auto manager = ModuleManager::GetInstance();
+ auto& manager = ModuleManager::GetInstance();
auto module =
GpgFrontend::SecureCreateSharedObject<T>(std::forward<Args>(args)...);
- manager->RegisterModule(module);
- manager->ActiveModule(module->GetModuleIdentifier());
+ manager.RegisterModule(module);
+ manager.ActiveModule(module->GetModuleIdentifier());
}
template <typename... Args>
void TriggerEvent(const EventIdentifier& event_id, Args&&... args,
Event::EventCallback e_cb = nullptr) {
- ModuleManager::GetInstance()->TriggerEvent(
+ ModuleManager::GetInstance().TriggerEvent(
std::move(MakeEvent(event_id, std::forward<Args>(args)..., e_cb)));
}
@@ -156,7 +156,7 @@ template <typename T>
auto RetrieveRTValueTyped(const std::string& namespace_, const std::string& key)
-> std::optional<T> {
auto any_value =
- ModuleManager::GetInstance()->RetrieveRTValue(namespace_, key);
+ ModuleManager::GetInstance().RetrieveRTValue(namespace_, key);
if (any_value && any_value->type() == typeid(T)) {
return std::any_cast<T>(*any_value);
}
@@ -168,7 +168,7 @@ auto RetrieveRTValueTypedOrDefault(const std::string& namespace_,
const std::string& key,
const T& defaultValue) -> T {
auto any_value =
- ModuleManager::GetInstance()->RetrieveRTValue(namespace_, key);
+ ModuleManager::GetInstance().RetrieveRTValue(namespace_, key);
if (any_value && any_value->type() == typeid(T)) {
return std::any_cast<T>(*any_value);
}
diff --git a/src/core/thread/Task.cpp b/src/core/thread/Task.cpp
index 92592bf4..a5d718ee 100644
--- a/src/core/thread/Task.cpp
+++ b/src/core/thread/Task.cpp
@@ -28,28 +28,29 @@
#include "core/thread/Task.h"
+#include <qscopedpointer.h>
+
#include <boost/stacktrace.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <utility>
-namespace GpgFrontend::Thread {
+#include "utils/MemoryUtils.h"
-class Task::Impl : public QObject {
- Q_OBJECT
+namespace GpgFrontend::Thread {
+class Task::Impl {
public:
Impl(Task *parent, std::string name)
- : QObject(parent), parent_(parent), uuid_(generate_uuid()), name_(name) {
+ : parent_(parent), uuid_(generate_uuid()), name_(name) {
SPDLOG_TRACE("task {} created", GetFullID());
init();
}
Impl(Task *parent, TaskRunnable runnable, std::string name,
DataObjectPtr data_object)
- : QObject(parent),
- parent_(parent),
+ : parent_(parent),
uuid_(generate_uuid()),
name_(std::move(name)),
runnable_(std::move(runnable)),
@@ -63,8 +64,7 @@ class Task::Impl : public QObject {
Impl(Task *parent, TaskRunnable runnable, std::string name,
DataObjectPtr data_object, TaskCallback callback)
- : QObject(parent),
- parent_(parent),
+ : parent_(parent),
uuid_(generate_uuid()),
name_(std::move(name)),
runnable_(std::move(runnable)),
@@ -112,28 +112,6 @@ class Task::Impl : public QObject {
*/
void SetRTN(int rtn) { this->rtn_ = rtn; }
- private slots:
-
- /**
- * @brief
- *
- */
- void slot_run() {
- try {
- SPDLOG_TRACE("task {} is starting...", GetFullID());
- // Run() will set rtn by itself
- parent_->Run();
- SPDLOG_TRACE("task {} was end.", GetFullID());
- } catch (std::exception &e) {
- SPDLOG_ERROR("exception was caught at task: {}", e.what());
- SPDLOG_ERROR(
- "stacktrace of the exception: {}",
- boost::stacktrace::to_string(boost::stacktrace::stacktrace()));
- }
- // raise signal to anounce after runnable returned
- if (parent_->autoDelete()) emit parent_->SignalTaskShouldEnd(rtn_);
- }
-
private:
Task *const parent_;
const std::string uuid_;
@@ -152,11 +130,11 @@ class Task::Impl : public QObject {
HoldOnLifeCycle(false);
//
- connect(parent_, &Task::SignalRun, this, &Task::Impl::slot_run);
+ connect(parent_, &Task::SignalRun, [=]() { inner_run(); });
//
- connect(parent_, &Task::SignalTaskShouldEnd, this,
- &Impl::slot_task_should_end);
+ connect(parent_, &Task::SignalTaskShouldEnd,
+ [=](int rtn) { slot_task_should_end(rtn); });
//
connect(parent_, &Task::SignalTaskEnd, parent_, &Task::deleteLater);
@@ -165,14 +143,32 @@ class Task::Impl : public QObject {
/**
* @brief
*
+ */
+ void inner_run() {
+ try {
+ SPDLOG_TRACE("task {} is starting...", GetFullID());
+ // Run() will set rtn by itself
+ parent_->Run();
+ SPDLOG_TRACE("task {} was end.", GetFullID());
+ } catch (std::exception &e) {
+ SPDLOG_ERROR("exception was caught at task: {}", e.what());
+ SPDLOG_ERROR(
+ "stacktrace of the exception: {}",
+ boost::stacktrace::to_string(boost::stacktrace::stacktrace()));
+ }
+ // raise signal to anounce after runnable returned
+ if (parent_->autoDelete()) emit parent_->SignalTaskShouldEnd(rtn_);
+ }
+
+ /**
+ * @brief
+ *
* @return std::string
*/
- std::string generate_uuid() {
+ static auto generate_uuid() -> std::string {
return boost::uuids::to_string(boost::uuids::random_generator()());
}
- private slots:
-
/**
* @brief
*
@@ -265,11 +261,12 @@ class Task::Impl : public QObject {
Task::Task(std::string name) : p_(new Impl(this, name)) {}
Task::Task(TaskRunnable runnable, std::string name, DataObjectPtr data_object)
- : p_(new Impl(this, runnable, name, data_object)) {}
+ : p_(SecureCreateUniqueObject<Impl>(this, runnable, name, data_object)) {}
Task::Task(TaskRunnable runnable, std::string name, DataObjectPtr data_object,
TaskCallback callback)
- : p_(new Impl(this, runnable, name, data_object, callback)) {}
+ : p_(SecureCreateUniqueObject<Impl>(this, runnable, name, data_object,
+ callback)) {}
Task::~Task() = default;
@@ -296,6 +293,4 @@ void Task::run() {
this->SafelyRun();
}
-} // namespace GpgFrontend::Thread
-
-#include "Task.moc" \ No newline at end of file
+} // namespace GpgFrontend::Thread \ No newline at end of file
diff --git a/src/core/thread/Task.h b/src/core/thread/Task.h
index da74005f..8f157917 100644
--- a/src/core/thread/Task.h
+++ b/src/core/thread/Task.h
@@ -29,6 +29,7 @@
#pragma once
#include "core/GpgFrontendCore.h"
+#include "core/function/SecureMemoryAllocator.h"
#include "core/model/DataObject.h"
namespace GpgFrontend::Thread {
@@ -90,7 +91,7 @@ class GPGFRONTEND_CORE_EXPORT Task : public QObject, public QRunnable {
private:
class Impl;
- Impl* p_;
+ SecureUniquePtr<Impl> p_;
void run() override;
};
diff --git a/src/core/thread/TaskRunner.cpp b/src/core/thread/TaskRunner.cpp
index 86e06dfd..88913bc4 100644
--- a/src/core/thread/TaskRunner.cpp
+++ b/src/core/thread/TaskRunner.cpp
@@ -86,7 +86,11 @@ class TaskRunner::Impl : public QThread {
GpgFrontend::Thread::TaskRunner::TaskRunner() : p_(std::make_unique<Impl>()) {}
-GpgFrontend::Thread::TaskRunner::~TaskRunner() = default;
+GpgFrontend::Thread::TaskRunner::~TaskRunner() {
+ if (p_->isRunning()) {
+ Stop();
+ }
+}
void GpgFrontend::Thread::TaskRunner::PostTask(Task* task) {
p_->PostTask(task);
@@ -102,8 +106,13 @@ void TaskRunner::PostScheduleTask(Task* task, size_t seconds) {
void TaskRunner::Start() { p_->start(); }
-QThread* TaskRunner::GetThread() { return p_.get(); }
+void TaskRunner::Stop() {
+ p_->quit();
+ p_->wait();
+}
+
+auto TaskRunner::GetThread() -> QThread* { return p_.get(); }
-bool TaskRunner::IsRunning() { return p_->isRunning(); }
+auto TaskRunner::IsRunning() -> bool { return p_->isRunning(); }
} // namespace GpgFrontend::Thread
diff --git a/src/core/thread/TaskRunner.h b/src/core/thread/TaskRunner.h
index 4df1d465..8661dbd4 100644
--- a/src/core/thread/TaskRunner.h
+++ b/src/core/thread/TaskRunner.h
@@ -49,11 +49,32 @@ class GPGFRONTEND_CORE_EXPORT TaskRunner : public QObject {
*/
virtual ~TaskRunner() override;
+ /**
+ * @brief
+ *
+ */
void Start();
- QThread* GetThread();
+ /**
+ * @brief
+ *
+ */
+ void Stop();
+
+ /**
+ * @brief Get the Thread object
+ *
+ * @return QThread*
+ */
+ auto GetThread() -> QThread*;
- bool IsRunning();
+ /**
+ * @brief
+ *
+ * @return true
+ * @return false
+ */
+ auto IsRunning() -> bool;
public slots:
diff --git a/src/core/thread/TaskRunnerGetter.cpp b/src/core/thread/TaskRunnerGetter.cpp
index 639a142c..bdcd89d0 100644
--- a/src/core/thread/TaskRunnerGetter.cpp
+++ b/src/core/thread/TaskRunnerGetter.cpp
@@ -28,25 +28,37 @@
#include "core/thread/TaskRunnerGetter.h"
-#include "thread/TaskRunner.h"
+#include <mutex>
+
+#include "core/GpgConstants.h"
+#include "core/thread/TaskRunner.h"
namespace GpgFrontend::Thread {
-TaskRunnerGetter::TaskRunnerGetter(int channel)
- : SingletonFunctionObject<TaskRunnerGetter>(channel) {}
+TaskRunnerGetter::TaskRunnerGetter(int)
+ : SingletonFunctionObject<TaskRunnerGetter>(kGpgFrontendDefaultChannel) {}
-TaskRunnerPtr GpgFrontend::Thread::TaskRunnerGetter::GetTaskRunner(
- TaskRunnerType runner_type) {
+auto TaskRunnerGetter::GetTaskRunner(TaskRunnerType runner_type)
+ -> TaskRunnerPtr {
+ std::lock_guard<std::mutex> lock_guard(task_runners_map_lock_);
while (true) {
auto it = task_runners_.find(runner_type);
if (it != task_runners_.end()) {
return it->second;
- } else {
- auto runner = GpgFrontend::SecureCreateSharedObject<TaskRunner>();
- task_runners_[runner_type] = runner;
- runner->Start();
- continue;
}
+
+ auto runner = GpgFrontend::SecureCreateSharedObject<TaskRunner>();
+ task_runners_[runner_type] = runner;
+ runner->Start();
}
}
+
+void TaskRunnerGetter::StopAllTeakRunner() {
+ for (const auto& [key, value] : task_runners_) {
+ if (value->IsRunning()) {
+ value->Stop();
+ }
+ }
+}
+
} // namespace GpgFrontend::Thread \ No newline at end of file
diff --git a/src/core/thread/TaskRunnerGetter.h b/src/core/thread/TaskRunnerGetter.h
index f44861bb..49ed25c0 100644
--- a/src/core/thread/TaskRunnerGetter.h
+++ b/src/core/thread/TaskRunnerGetter.h
@@ -28,6 +28,8 @@
#pragma once
+#include <mutex>
+
#include "core/GpgFrontendCore.h"
#include "core/function/basic/GpgFunctionObject.h"
#include "core/thread/TaskRunner.h"
@@ -44,6 +46,7 @@ class GPGFRONTEND_CORE_EXPORT TaskRunnerGetter
kTaskRunnerType_GPG,
kTaskRunnerType_IO,
kTaskRunnerType_Network,
+ kTaskRunnerType_Module,
kTaskRunnerType_External_Process,
};
@@ -53,8 +56,11 @@ class GPGFRONTEND_CORE_EXPORT TaskRunnerGetter
auto GetTaskRunner(TaskRunnerType runner_type = kTaskRunnerType_Default)
-> TaskRunnerPtr;
+ void StopAllTeakRunner();
+
private:
std::map<TaskRunnerType, TaskRunnerPtr> task_runners_;
+ std::mutex task_runners_map_lock_;
};
} // namespace GpgFrontend::Thread
diff --git a/src/init.cpp b/src/init.cpp
index 780e9770..50fb5a87 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -40,6 +40,7 @@
#include "core/GpgCoreInit.h"
#include "core/function/GlobalSettingStation.h"
+#include "core/thread/TaskRunnerGetter.h"
#include "core/utils/MemoryUtils.h"
#include "module/GpgFrontendModuleInit.h"
#include "module/sdk/Log.h"
@@ -196,6 +197,8 @@ void ShutdownGlobalBasicalEnv(const GFCxtWPtr &p_ctx) {
return;
}
+ Thread::TaskRunnerGetter::GetInstance().StopAllTeakRunner();
+
ShutdownLoggingSystem(ctx);
}
diff --git a/src/test/GpgFrontendTest.cpp b/src/test/GpgFrontendTest.cpp
index 0075c113..7b318aec 100644
--- a/src/test/GpgFrontendTest.cpp
+++ b/src/test/GpgFrontendTest.cpp
@@ -107,16 +107,17 @@ void ConfigureGpgContext() {
std::filesystem::create_directory(db_path);
}
- GpgContext::CreateInstance(
- kGpgFrontendDefaultChannel, [&]() -> ChannelObjectPtr {
- GpgContextInitArgs args;
- args.test_mode = true;
- args.offline_mode = true;
- args.db_path = db_path.string();
-
- return ConvertToChannelObjectPtr<>(SecureCreateUniqueObject<GpgContext>(
- args, kGpgFrontendDefaultChannel));
- });
+ // GpgContext::CreateInstance(
+ // kGpgFrontendDefaultChannel, [&]() -> ChannelObjectPtr {
+ // GpgContextInitArgs args;
+ // args.test_mode = true;
+ // args.offline_mode = true;
+ // args.db_path = db_path.string();
+
+ // return
+ // ConvertToChannelObjectPtr<>(SecureCreateUniqueObject<GpgContext>(
+ // args, kGpgFrontendDefaultChannel));
+ // });
}
auto ExecuteAllTestCase(GpgFrontendContext args) -> int {
diff --git a/src/test/core/GpgCoreTest.cpp b/src/test/core/GpgCoreTest.cpp
index ff51ae3d..b00e5c0e 100644
--- a/src/test/core/GpgCoreTest.cpp
+++ b/src/test/core/GpgCoreTest.cpp
@@ -55,9 +55,9 @@ void GpgCoreTest::import_private_keys(const libconfig::Setting& root) {
void GpgCoreTest::TearDown() {}
void GpgCoreTest::SetUp() {
- libconfig::Config cfg;
- ASSERT_NO_THROW(cfg.readFile(config_path_.string()));
- auto& root = cfg.getRoot();
- import_private_keys(root);
+ // libconfig::Config cfg;
+ // ASSERT_NO_THROW(cfg.readFile(config_path_.string()));
+ // auto& root = cfg.getRoot();
+ // import_private_keys(root);
}
} // namespace GpgFrontend::Test