Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/GraphCtrl/GraphElement/GElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
CGRAPH_NAMESPACE_BEGIN

GElement::~GElement() {
CGRAPH_DELETE_PTR(run_task_)
CGRAPH_DELETE_PTR(perf_info_)
CGRAPH_DELETE_PTR(aspect_manager_)
for (auto& param : local_params_) {
Expand Down
7 changes: 4 additions & 3 deletions src/GraphCtrl/GraphElement/GElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,11 @@ class GElement : public GElementObject,
CBool is_marco_ { false }; // 微任务

/** 执行期间相关信息 */
GElementParamMap local_params_; // 用于记录当前element的内部参数
GElementParamMap local_params_ {}; // 用于记录当前element的内部参数
GAspectManagerPtr aspect_manager_ { nullptr }; // 整体流程的切面管理类
UThreadPoolPtr thread_pool_ { nullptr }; // 用于执行的线程池信息
GPerfInfo* perf_info_ { nullptr }; // 用于perf的信息
UTask* run_task_ { nullptr }; // 用于记录 调度执行函数
CBool is_prepared_ { false }; // 判断是否已经执行过 prepareRun() 方法

/** 图相关信息 */
Expand All @@ -469,8 +470,8 @@ class GElement : public GElementObject,
std::vector<GElement *> children_ {}; // 子节点,适用于group类型

/** 异步执行相关信息 */
std::future<CStatus> async_result_; // 用于记录当前节点的异步执行情况
UCvMutex suspend_locker_; // 控制停止执行锁信息
std::future<CStatus> async_result_ {}; // 用于记录当前节点的异步执行情况
UCvMutex suspend_locker_ {}; // 控制停止执行锁信息

friend class GNode;
friend class GGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ CVoid GDynamicEngine::analysisDagType() {
analysisParallelMatrix();
} else {
dag_type_ = internal::GEngineDagType::COMMON;
makeCommonTask();
}
}

Expand All @@ -121,6 +122,16 @@ CVoid GDynamicEngine::analysisParallelMatrix() {
}


CVoid GDynamicEngine::makeCommonTask() {
for (auto* element : total_element_arr_) {
CGRAPH_DELETE_PTR(element->run_task_)
element->run_task_ = new UTask([this, element] {
this->innerExec(element);
});
}
}


CVoid GDynamicEngine::process(GElementPtr element, const CBool affinity) {
if (unlikely(cur_status_.isErr())) {
return;
Expand All @@ -130,8 +141,7 @@ CVoid GDynamicEngine::process(GElementPtr element, const CBool affinity) {
// 如果 affinity=true,表示用当前的线程,执行这个逻辑。以便增加亲和性
innerExec(element);
} else {
thread_pool_->execute([this, element] {
this->innerExec(element); }, element->binding_index_);
thread_pool_->execute(element->run_task_, element->binding_index_);
}
}

Expand Down Expand Up @@ -234,7 +244,7 @@ CVoid GDynamicEngine::parallelRunAll() {
}
} else {
// 仅有一个任务的情况,无法使用 executeWithTid 函数,故走这边的逻辑
const auto& element = curArr.front();
auto* element = curArr.front();
thread_pool_->execute([this, element] {
parallelRunOne(element); }, element->binding_index_);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class GDynamicEngine : public GEngine {
*/
CVoid analysisParallelMatrix();

/**
* 创建 common run 的情况下,element对应的任务
* @return
*/
CVoid makeCommonTask();

/**
* 动态图运行
* @param
Expand Down
72 changes: 59 additions & 13 deletions src/UtilsCtrl/ThreadPool/Task/UTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#define CGRAPH_UTASK_H

#include <vector>
#include <memory>
#include <utility>
#include <type_traits>

Expand All @@ -36,28 +35,72 @@ class UTask : public CStruct {
public:
template<typename F,
typename std::enable_if<!std::is_same<typename std::decay<F>::type, UTask>::value, int>::type = 0>
explicit UTask(F&& func, const int priority = 0)
explicit UTask(F&& func, const CInt priority = 0, const CBool owner = true)
: impl_(new TaskDerided<F>(std::forward<F>(func)))
, priority_(priority) {}
, priority_(priority)
, owner_(owner) {
}

CVoid operator()() const {
// impl_ 理论上不可能为空
impl_->call();
if (likely(impl_)) {
impl_->call();
}
}

explicit UTask() = default;

explicit UTask(const UTask* task) {
if (likely(task)) {
impl_ = task->impl_;
priority_ = task->priority_;
owner_ = false;
}
}

explicit UTask(UTask* task) {
if (likely(task)) {
impl_ = task->impl_;
priority_ = task->priority_;
owner_ = false;
}
}

~UTask() override {
if (owner_) {
CGRAPH_DELETE_PTR(impl_);
}
}

UTask(UTask&& task) noexcept:
impl_(std::move(task.impl_)),
priority_(task.priority_) {}
impl_(task.impl_),
priority_(task.priority_),
owner_(task.owner_) {
task.impl_ = nullptr;
task.owner_ = false;
}

UTask(UTask&& task, const int priority) noexcept:
impl_(std::move(task.impl_)),
priority_(priority) {}
impl_(task.impl_),
priority_(priority),
owner_(task.owner_) {
task.impl_ = nullptr;
task.owner_ = false;
}

UTask& operator=(UTask&& task) noexcept {
if (this != &task) {
if (owner_) {
CGRAPH_DELETE_PTR(impl_);
}

impl_ = task.impl_;
priority_ = task.priority_;
owner_ = task.owner_;

task.impl_ = nullptr;
task.owner_ = false;
}

UTask &operator=(UTask&& task) noexcept {
impl_ = std::move(task.impl_);
priority_ = task.priority_;
return *this;
}

Expand All @@ -72,8 +115,11 @@ class UTask : public CStruct {
CGRAPH_NO_ALLOWED_COPY(UTask)

private:
std::unique_ptr<TaskBased> impl_ { nullptr };
friend class UThreadPool;

TaskBased* impl_ { nullptr };
CInt priority_ { 0 }; // 任务的优先级信息
CBool owner_ { true }; // impl_ 是否归属当前对象
};


Expand Down
21 changes: 21 additions & 0 deletions src/UtilsCtrl/ThreadPool/UThreadPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,27 @@ CStatus UThreadPool::submit(CGRAPH_DEFAULT_CONST_FUNCTION_REF func, const CMSec
}


CVoid UThreadPool::execute(const UTask* task, const CIndex index) {
envokeTask(UTask(task), index);
}


CVoid UThreadPool::envokeTask(UTask&& task, const CIndex index) {
const CIndex realIndex = dispatch(index);

if (likely(realIndex >= 0 && realIndex < config_.default_thread_size_)) {
primary_threads_[realIndex]->pushTask(std::move(task));
} else if (CGRAPH_LONG_TIME_TASK_STRATEGY == realIndex) {
priority_task_queue_.push(std::move(task), CGRAPH_LONG_TIME_TASK_STRATEGY);
} else if (CGRAPH_TRIGGER_ALL_THREAD_STRATEGY == realIndex) {
task_queue_.push(std::move(task));
(void)wakeupAllThread();
} else {
task_queue_.push(std::move(task));
}
}


CIndex UThreadPool::getThreadIndex(const CSize tid) {
int index = CGRAPH_SECONDARY_THREAD_COMMON_ID;
const auto result = thread_record_map_.find(tid);
Expand Down
19 changes: 18 additions & 1 deletion src/UtilsCtrl/ThreadPool/UThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,20 @@ class UThreadPool : public UThreadObject {
* @param task
* @param index
*/
template<typename FunctionType>
template<typename FunctionType,
typename std::enable_if<!std::is_same<typename std::decay<FunctionType>::type, UTask>::value, int>::type = 0>
CVoid execute(FunctionType&& task,
CIndex index = CGRAPH_DEFAULT_TASK_STRATEGY);

/**
* 异步执行已封装的任务。队列仅借用 task 中的执行内容,不接管其生命周期。
* task 必须存活到本次异步执行结束,并且可以被重复提交。
* @param task
* @param index
*/
CVoid execute(const UTask* task,
CIndex index = CGRAPH_DEFAULT_TASK_STRATEGY);

/**
* 异步写入特定thread id,执行信息
* @tparam FunctionType
Expand Down Expand Up @@ -197,6 +207,13 @@ class UThreadPool : public UThreadObject {
*/
CVoid monitor();

/**
* 将任务投递到线程池内部
* @param task
* @param index
*/
CVoid envokeTask(UTask&& task, CIndex index);

CGRAPH_NO_ALLOWED_COPY(UThreadPool)

private:
Expand Down
18 changes: 4 additions & 14 deletions src/UtilsCtrl/ThreadPool/UThreadPool.inl
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,15 @@ auto UThreadPool::commitWithPriority(const FunctionType& func, int priority)
}


template<typename FunctionType>
template<typename FunctionType,
typename std::enable_if<!std::is_same<typename std::decay<FunctionType>::type, UTask>::value, int>::type>
CVoid UThreadPool::execute(FunctionType&& task, const CIndex index) {
const CIndex realIndex = dispatch(index);

if (likely(realIndex >= 0 && realIndex < config_.default_thread_size_)) {
primary_threads_[realIndex]->pushTask(UTask(std::forward<FunctionType>(task)));
} else if (CGRAPH_LONG_TIME_TASK_STRATEGY == realIndex) {
priority_task_queue_.push(UTask(std::forward<FunctionType>(task)), CGRAPH_LONG_TIME_TASK_STRATEGY);
} else if (CGRAPH_TRIGGER_ALL_THREAD_STRATEGY == realIndex) {
task_queue_.push(UTask(std::forward<FunctionType>(task)));
(void)wakeupAllThread();
} else {
task_queue_.push(UTask(std::forward<FunctionType>(task)));
}
envokeTask(UTask(std::forward<FunctionType>(task)), index);
}


template<typename FunctionType>
CVoid UThreadPool::executeWithTid(FunctionType&& task, CIndex tid, CBool enable, CBool lockable) {
CVoid UThreadPool::executeWithTid(FunctionType&& task, const CIndex tid, const CBool enable, const CBool lockable) {
if (likely(tid >= 0 && tid < config_.default_thread_size_)) {
primary_threads_[tid]->pushTask(UTask(std::forward<FunctionType>(task)), enable, lockable);
} else {
Expand Down
Loading