-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] add GSome for pipeline async run.
- Loading branch information
1 parent
6717f7d
commit 5222701
Showing
23 changed files
with
312 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,12 @@ | |
@Contact: [email protected] | ||
@File: GSingleton.cpp | ||
@Time: 2021/10/30 10:24 下午 | ||
@Desc: | ||
@Desc: | ||
***************************/ | ||
|
||
#ifndef CGRAPH_GSINGLETON_INL | ||
#define CGRAPH_GSINGLETON_INL | ||
|
||
#include "GSingleton.h" | ||
|
||
CGRAPH_NAMESPACE_BEGIN | ||
|
@@ -97,3 +100,5 @@ CStatus GSingleton<T>::setElementInfo(const std::set<GElementPtr> &dependElement | |
} | ||
|
||
CGRAPH_NAMESPACE_END | ||
|
||
#endif //CGRAPH_GSINGLETON_INL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/*************************** | ||
@Author: Chunel | ||
@Contact: [email protected] | ||
@File: GSome.h | ||
@Time: 2023/8/20 11:20 | ||
@Desc: | ||
***************************/ | ||
|
||
#ifndef CGRAPH_GSOME_H | ||
#define CGRAPH_GSOME_H | ||
|
||
#include <memory> | ||
#include <mutex> | ||
#include <condition_variable> | ||
|
||
#include "../GGroup.h" | ||
|
||
CGRAPH_NAMESPACE_BEGIN | ||
|
||
template<CSize TriggerNum = 1> | ||
class GSome : public GGroup { | ||
protected: | ||
CStatus addElement(GElementPtr element) override; | ||
|
||
explicit GSome(); | ||
|
||
/** | ||
* 异步处理 GSome中的所有信息 | ||
* @param ptr | ||
* @return | ||
*/ | ||
CVoid process(GAsyncNodePtr ptr); | ||
|
||
CStatus run() override; | ||
|
||
CBool isSerializable() override; | ||
|
||
CVoid dump(std::ostream& oss) final; | ||
|
||
CGRAPH_NO_ALLOWED_COPY(GSome) | ||
|
||
private: | ||
std::atomic<CSize> left_num_ {0}; // 还剩的触发结束的个数 | ||
CStatus cur_status_ ; // 记录异步时刻的当前状态信息 | ||
|
||
std::mutex lock_; | ||
std::condition_variable cv_; | ||
|
||
friend class GPipeline; | ||
friend class GStaticEngine; | ||
friend class GDynamicEngine; | ||
friend class UAllocator; | ||
}; | ||
|
||
CGRAPH_NAMESPACE_END | ||
|
||
#include "GSome.inl" | ||
|
||
#endif //CGRAPH_GSOME_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/*************************** | ||
@Author: Chunel | ||
@Contact: [email protected] | ||
@File: GSome.inl | ||
@Time: 2023/8/20 11:20 | ||
@Desc: | ||
***************************/ | ||
|
||
#ifndef CGRAPH_GSOME_INL | ||
#define CGRAPH_GSOME_INL | ||
|
||
#include "GSome.h" | ||
|
||
CGRAPH_NAMESPACE_BEGIN | ||
|
||
template<CSize TriggerNum> | ||
CStatus GSome<TriggerNum>::addElement(GElementPtr element) { | ||
CGRAPH_FUNCTION_BEGIN | ||
CGRAPH_ASSERT_INIT(false) | ||
|
||
if (GElementType::ASYNC_NODE != element->element_type_) { | ||
group_elements_arr_.clear(); // 如果有错误的情况,就直接清空本地的内容 | ||
CGRAPH_RETURN_ERROR_STATUS("GSome can insert async node only current.") | ||
} | ||
|
||
group_elements_arr_.template emplace_back(element); | ||
CGRAPH_FUNCTION_END | ||
} | ||
|
||
|
||
template<CSize TriggerNum> | ||
GSome<TriggerNum>::GSome() { | ||
element_type_ = GElementType::SOME; | ||
session_ = URandom<>::generateSession(CGRAPH_STR_SOME); | ||
} | ||
|
||
|
||
template<CSize TriggerNum> | ||
CStatus GSome<TriggerNum>::run() { | ||
CGRAPH_FUNCTION_BEGIN | ||
CGRAPH_ASSERT_NOT_NULL(thread_pool_) | ||
left_num_.store(TriggerNum); // 还剩n个,就完成当前GSome的执行逻辑 | ||
cur_status_ = CStatus(); | ||
|
||
for (auto* element : group_elements_arr_) { | ||
process((GAsyncNodePtr)element); | ||
} | ||
|
||
CGRAPH_UNIQUE_LOCK lock(lock_); | ||
cv_.wait(lock, [this] { | ||
return left_num_ <= 0 || cur_status_.isErr(); | ||
}); | ||
|
||
if (!cur_status_.isOK()) { | ||
status = cur_status_; // 出错的话,赋值到外部去,让上游知道。 | ||
} | ||
CGRAPH_FUNCTION_END | ||
} | ||
|
||
|
||
template<CSize TriggerNum> | ||
CVoid GSome<TriggerNum>::process(GAsyncNodePtr ptr) { | ||
// 这里的内容,仅可能为 GAsyncNode 的子类的信息 | ||
const auto& exec = [this, ptr] { | ||
{ | ||
CGRAPH_UNIQUE_LOCK lock(lock_); | ||
cur_status_ += ptr->run(); // 开始异步执行起来了 | ||
} | ||
if (cur_status_.isOK()) { | ||
cur_status_ += ptr->getResult(); | ||
} | ||
left_num_--; | ||
cv_.notify_one(); | ||
}; | ||
|
||
thread_pool_->commit(exec); | ||
} | ||
|
||
|
||
template<CSize TriggerNum> | ||
CBool GSome<TriggerNum>::isSerializable() { | ||
return false; // 情况较为复杂,默认不可以 | ||
} | ||
|
||
|
||
template<CSize TriggerNum> | ||
CVoid GSome<TriggerNum>::dump(std::ostream& oss) { | ||
dumpElement(oss); | ||
dumpGroupLabelBegin(oss); | ||
oss << 'p' << this << "[shape=point height=0];\n"; | ||
oss << "color=blue;style=dashed;\n"; // 蓝色虚线 | ||
|
||
for (const auto& element : group_elements_arr_) { | ||
element->dump(oss); | ||
} | ||
|
||
dumpGroupLabelEnd(oss); | ||
|
||
for (const auto& element : run_before_) { | ||
dumpEdge(oss, this, element); | ||
} | ||
} | ||
|
||
CGRAPH_NAMESPACE_END | ||
|
||
#endif //CGRAPH_GSOME_INL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,5 +93,4 @@ CGRAPH_NAMESPACE_END | |
|
||
#include "GParamManager.inl" | ||
|
||
|
||
#endif //CGRAPH_GPARAMMANAGER_H |
Oops, something went wrong.