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
108 lines (86 loc) · 2.8 KB
/
GEvent.cpp
File metadata and controls
108 lines (86 loc) · 2.8 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
105
106
107
108
/***************************
@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::fatInit() {
CGRAPH_FUNCTION_BEGIN
async_run_finish_futures_.clear();
async_destroy_futures_.clear();
status = init();
CGRAPH_FUNCTION_END
}
CStatus GEvent::fatDestroy() {
CGRAPH_FUNCTION_BEGIN
asyncWait(GEventAsyncStrategy::PIPELINE_DESTROY);
async_run_finish_futures_.clear();
async_destroy_futures_.clear();
status = destroy();
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_)
asyncProcess(strategy);
break;
default:
CGRAPH_RETURN_ERROR_STATUS("unknown event type")
}
CGRAPH_FUNCTION_END
}
std::shared_future<CVoid> GEvent::asyncProcess(GEventAsyncStrategy strategy) {
CGRAPH_ASSERT_NOT_NULL_THROW_ERROR(thread_pool_)
std::shared_future<CVoid> future = thread_pool_->commit([this] {
this->trigger(this->param_);
}, CGRAPH_POOL_TASK_STRATEGY);
/**
* 根据具体策略,将 future信息放到对应的容器中
* 在特定的时间点,等待执行结束
*/
if (GEventAsyncStrategy::PIPELINE_RUN_FINISH == strategy) {
CGRAPH_LOCK_GUARD lock(async_run_finished_lock_);
async_run_finish_futures_.emplace_back(future);
} else if (GEventAsyncStrategy::PIPELINE_DESTROY == strategy) {
CGRAPH_LOCK_GUARD lock(async_destroy_lock_);
async_destroy_futures_.emplace_back(future);
}
return future;
}
CVoid GEvent::asyncWait(GEventAsyncStrategy strategy) {
switch (strategy) {
case GEventAsyncStrategy::PIPELINE_RUN_FINISH: {
CGRAPH_LOCK_GUARD lock(async_run_finished_lock_);
for (auto& cur : async_run_finish_futures_) {
cur.valid() ? cur.wait() : void();
}
async_run_finish_futures_.clear();
break;
}
case GEventAsyncStrategy::PIPELINE_DESTROY: {
CGRAPH_LOCK_GUARD lock(async_destroy_lock_);
for (auto& cur : async_destroy_futures_) {
cur.valid() ? cur.wait() : void();
}
async_destroy_futures_.clear();
break;
}
default:
CGRAPH_THROW_EXCEPTION("unknown event async strategy type")
}
}
CGRAPH_NAMESPACE_END