-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_object.cpp
192 lines (166 loc) · 5.65 KB
/
block_object.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "block_object.h"
#include "scheduler.h"
#include "error.h"
#include <unistd.h>
#include <mutex>
#include <limits>
namespace co
{
BlockObject::BlockObject(std::size_t init_wakeup, std::size_t max_wakeup)
: wakeup_(init_wakeup), max_wakeup_(max_wakeup)
{
DebugPrint(dbg_syncblock, "BlockObject::BlockObject this=%p", this);
}
BlockObject::~BlockObject()
{
if (!lock_.try_lock()) {
ThrowError(eCoErrorCode::ec_block_object_locked);
}
if (!wait_queue_.empty()) {
ThrowError(eCoErrorCode::ec_block_object_waiting);
}
DebugPrint(dbg_syncblock, "BlockObject::~BlockObject this=%p", this);
}
void BlockObject::CoBlockWait()
{
if (!g_Scheduler.IsCoroutine()) {
while (!TryBlockWait()) usleep(10 * 1000);
return ;
}
std::unique_lock<LFLock> lock(lock_);
if (wakeup_ > 0) {
DebugPrint(dbg_syncblock, "wait immedaitely done.");
--wakeup_;
return ;
}
lock.unlock();
Task* tk = g_Scheduler.GetCurrentTask();
tk->block_ = this;
tk->state_ = TaskState::sys_block;
tk->block_timeout_ = MininumTimeDurationType::zero();
tk->is_block_timeout_ = false;
++ tk->block_sequence_;
DebugPrint(dbg_syncblock, "wait to switch. task(%s)", tk->DebugInfo());
g_Scheduler.CoYield();
}
bool BlockObject::CoBlockWaitTimed(MininumTimeDurationType timeo)
{
auto begin = std::chrono::steady_clock::now();
if (!g_Scheduler.IsCoroutine()) {
while (!TryBlockWait() &&
std::chrono::duration_cast<MininumTimeDurationType>
(std::chrono::steady_clock::now() - begin) < timeo)
usleep(10 * 1000);
return false;
}
std::unique_lock<LFLock> lock(lock_);
if (wakeup_ > 0) {
DebugPrint(dbg_syncblock, "wait immedaitely done.");
--wakeup_;
return true;
}
lock.unlock();
Task* tk = g_Scheduler.GetCurrentTask();
tk->block_ = this;
tk->state_ = TaskState::sys_block;
++tk->block_sequence_;
tk->block_timeout_ = timeo;
tk->is_block_timeout_ = false;
DebugPrint(dbg_syncblock, "wait to switch. task(%s)", tk->DebugInfo());
g_Scheduler.CoYield();
return !tk->is_block_timeout_;
}
bool BlockObject::TryBlockWait()
{
std::unique_lock<LFLock> lock(lock_);
if (wakeup_ == 0)
return false;
--wakeup_;
DebugPrint(dbg_syncblock, "try wait success.");
return true;
}
bool BlockObject::Wakeup()
{
std::unique_lock<LFLock> lock(lock_);
Task* tk = wait_queue_.pop();
if (!tk) {
if (wakeup_ >= max_wakeup_) {
DebugPrint(dbg_syncblock, "wakeup failed.");
return false;
}
++wakeup_;
DebugPrint(dbg_syncblock, "wakeup to %lu.", (long unsigned)wakeup_);
return true;
}
lock.unlock();
tk->block_ = nullptr;
if (tk->block_timer_) { // block cancel timer蹇呴』鍦╨ock涔嬪, 鍥犱负閲岄潰浼歭ock
if (g_Scheduler.BlockCancelTimer(tk->block_timer_))
tk->DecrementRef();
tk->block_timer_.reset();
}
DebugPrint(dbg_syncblock, "wakeup task(%s).", tk->DebugInfo());
g_Scheduler.AddTaskRunnable(tk);
return true;
}
void BlockObject::CancelWait(Task* tk, uint32_t block_sequence, bool in_timer)
{
std::unique_lock<LFLock> lock(lock_);
if (tk->block_ != this) {
DebugPrint(dbg_syncblock, "cancelwait task(%s) failed. tk->block_ is not this!", tk->DebugInfo());
return;
}
if (tk->block_sequence_ != block_sequence) {
DebugPrint(dbg_syncblock, "cancelwait task(%s) failed. tk->block_sequence_ = %u, block_sequence = %u.",
tk->DebugInfo(), tk->block_sequence_, block_sequence);
return;
}
if (!wait_queue_.erase(tk)) {
DebugPrint(dbg_syncblock, "cancelwait task(%s) erase failed.", tk->DebugInfo());
return;
}
lock.unlock();
tk->block_ = nullptr;
if (!in_timer && tk->block_timer_) { // block cancel timer蹇呴』鍦╨ock涔嬪, 鍥犱负閲岄潰浼歭ock
g_Scheduler.BlockCancelTimer(tk->block_timer_);
tk->block_timer_.reset();
}
tk->is_block_timeout_ = true;
DebugPrint(dbg_syncblock, "cancelwait task(%s).", tk->DebugInfo());
g_Scheduler.AddTaskRunnable(tk);
}
bool BlockObject::IsWakeup()
{
std::unique_lock<LFLock> lock(lock_);
return wakeup_ > 0;
}
bool BlockObject::AddWaitTask(Task* tk)
{
std::unique_lock<LFLock> lock(lock_);
if (wakeup_) {
--wakeup_;
return false;
}
wait_queue_.push(tk);
DebugPrint(dbg_syncblock, "add wait task(%s). timeout=%ld", tk->DebugInfo(),
(long int)std::chrono::duration_cast<std::chrono::milliseconds>(tk->block_timeout_).count());
// 甯﹁秴鏃剁殑, 澧炲姞瀹氭椂鍣�
if (MininumTimeDurationType::zero() != tk->block_timeout_) {
uint32_t seq = tk->block_sequence_;
tk->IncrementRef();
tk->block_timer_ = g_Scheduler.ExpireAt(tk->block_timeout_, [=]{
// 姝ゅ畾鏃跺櫒瓒呰繃block_object鐢熷懡鏈熸椂浼歝rash鎴栨閿�,
// 鎵�浠akeup鎴朿ancelwait鏃朵竴瀹氳kill姝ゅ畾鏃跺櫒
if (tk->block_sequence_ == seq) {
DebugPrint(dbg_syncblock,
"wait timeout, will cancelwait task(%s). this=%p, tk->block_=%p, seq=%u, timeout=%ld",
tk->DebugInfo(), this, tk->block_, seq,
(long int)std::chrono::duration_cast<std::chrono::milliseconds>(tk->block_timeout_).count());
this->CancelWait(tk, seq, true);
}
tk->DecrementRef();
});
}
return true;
}
} //namespace co