forked from ChunelFeng/CGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGEvent.cpp
More file actions
104 lines (85 loc) · 2.67 KB
/
GEvent.cpp
File metadata and controls
104 lines (85 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: GEvent.cpp
@Time: 2023/1/20 23:07
@Desc:
***************************/
#include "GEvent.h"
CGRAPH_NAMESPACE_BEGIN
GEvent::GEvent() {
session_ = URandom<>::generateSession(CGRAPH_STR_EVENT);
}
GEvent::~GEvent() {
CGRAPH_DELETE_PTR(param_)
}
CStatus GEvent::init() {
CGRAPH_FUNCTION_BEGIN
CGRAPH_ASSERT_INIT(false)
async_run_finish_futures_.clear();
async_destroy_futures_.clear();
is_init_ = true;
CGRAPH_FUNCTION_END
}
CStatus GEvent::destroy() {
CGRAPH_FUNCTION_BEGIN
CGRAPH_ASSERT_INIT(true)
asyncWait(GEventAsyncStrategy::PIPELINE_DESTROY);
async_run_finish_futures_.clear();
async_destroy_futures_.clear();
is_init_ = false;
CGRAPH_FUNCTION_END
}
CStatus GEvent::process(GEventType type, GEventAsyncStrategy strategy) {
CGRAPH_FUNCTION_BEGIN
switch (type) {
case GEventType::SYNC: // 同步触发
this->trigger(this->param_);
break;
case GEventType::ASYNC: // 异步触发
CGRAPH_ASSERT_NOT_NULL(this->thread_pool_)
{
auto future = thread_pool_->commit([this] {
this->trigger(this->param_);
}, CGRAPH_POOL_TASK_STRATEGY);
/**
* 根据具体策略,将 future信息放到对应的容器中
* 在特定的时间点,等待执行结束
*/
if (GEventAsyncStrategy::PIPELINE_RUN_FINISH == strategy) {
async_run_finish_futures_.emplace_back(std::move(future));
} else if (GEventAsyncStrategy::PIPELINE_DESTROY == strategy) {
async_destroy_futures_.emplace_back(std::move(future));
}
}
break;
default:
CGRAPH_RETURN_ERROR_STATUS("unknown event type")
}
CGRAPH_FUNCTION_END
}
CVoid GEvent::asyncWait(GEventAsyncStrategy strategy) {
switch (strategy) {
case GEventAsyncStrategy::PIPELINE_RUN_FINISH: {
for (auto& cur : async_run_finish_futures_) {
if (cur.valid()) {
cur.wait();
}
}
async_run_finish_futures_.clear();
}
break;
case GEventAsyncStrategy::PIPELINE_DESTROY: {
for (auto& cur : async_destroy_futures_) {
if (cur.valid()) {
cur.wait();
}
}
async_destroy_futures_.clear();
}
break;
default:
CGRAPH_THROW_EXCEPTION("unknown event async strategy type")
}
}
CGRAPH_NAMESPACE_END