Repository navigation
Expand file tree
/
Copy paththread_worker.cpp
More file actions
260 lines (222 loc) · 7.68 KB
/
Copy paththread_worker.cpp
File metadata and controls
260 lines (222 loc) · 7.68 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
Copyright (c) 2009-2026 WangTingMan <75142601@qq.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "auto_guard.h"
#include "thread_worker.h"
#include "log_util.h"
#include "thread_manager.h"
#include "timer_module.h"
#include "framework_manager.h"
#include "internal/platform.h"
#include "executable_task.h"
#include "abstract_module.h"
#include <iterator>
constexpr std::chrono::seconds s_idle_max_long_time( 10 );
namespace framework
{
thread_worker::thread_worker()
{
m_last_executing_time = std::chrono::steady_clock::now();
m_is_running = true;
}
thread_worker::~thread_worker()
{
if( m_thread.joinable() )
{
m_thread.detach();
}
}
void thread_worker::run
(
std::shared_ptr<abstract_worker> a_current,
bool a_accupy_cureent_thread
)
{
if( a_accupy_cureent_thread )
{
run_impl( a_current );
}
else
{
if( !m_thread.joinable() )
{
m_thread = std::thread( &thread_worker::run_impl, this, a_current );
}
else
{
LogUtilWarning() << "Current thread is running?";
}
}
}
void thread_worker::post_task( std::shared_ptr<abstract_task> a_task )
{
std::string const& debug_info = a_task->get_debug_info();
if( !debug_info.empty() )
{
LogUtilInfo() << "post task. debug info: " << debug_info;
}
std::unique_lock<std::mutex> locker( m_mutex );
m_tasks.emplace_back( std::move( a_task ) );
locker.unlock();
m_condition_variable.notify_all();
}
void thread_worker::post_task( std::vector<std::shared_ptr<abstract_task>> a_tasks )
{
using iter_t = std::vector<std::shared_ptr<abstract_task>>::iterator;
std::unique_lock<std::mutex> locker( m_mutex );
m_tasks.insert( m_tasks.end(),
std::move_iterator<iter_t>( a_tasks.begin() ),
std::move_iterator<iter_t>( a_tasks.end() )
);
locker.unlock();
m_condition_variable.notify_all();
}
bool thread_worker::is_idle_for_long_time()
{
bool idle_long_time = false;
auto now_ = std::chrono::steady_clock::now();
std::lock_guard<std::mutex> locker( m_mutex );
auto duration_ = now_ - m_last_executing_time;
if( duration_ > s_idle_max_long_time )
{
idle_long_time = true;
}
return idle_long_time;
}
void thread_worker::exit_later()
{
auto fun = []()
{
return false;
};
m_is_running.exchange( false );
auto tsk = std::make_shared<executable_task>( fun );
tsk->set_position( source_here );
post_task( tsk );
}
uint64_t thread_worker::work_thread_id()
{
return m_thread_id;
}
void thread_worker::set_worker_name( std::string a_name )
{
auto tsk = std::make_shared<executable_task>();
tsk->set_fun( std::bind( &framework::set_thread_name, a_name ), abstract_module::s_task_runner_module_name );
post_task( tsk );
}
void thread_worker::run_impl( std::shared_ptr<abstract_worker> a_current )
{
LogUtilDebug() << "thread work started.";
m_thread_id = framework::get_current_thread_id();
std::vector<std::shared_ptr<abstract_task>> tasks;
std::unique_lock<std::mutex> locker( m_mutex, std::defer_lock );
bool ret = false;
framework::set_thread_name( "worker" );
int64_t task_started_time = 0;
while( true )
{
if( !m_is_running )
{
break;
}
locker.lock();
if( m_tasks.empty() )
{
locker.unlock();
framework_manager::get_instance().get_thread_manager().push_idle_worker( a_current );
locker.lock();
m_condition_variable.wait( locker, [this]()
{
return !m_tasks.empty();
}
);
}
tasks = std::move( m_tasks );
locker.unlock();
for( auto it = tasks.begin(); it != tasks.end(); ++it )
{
auto& the_task = ( *it );
auto& watchdog = framework_manager::get_instance().get_thread_manager().get_watchdog();
watchdog.task_started( m_thread_id );
task_started_time = timer_module::get_system_booting_time();
auto_guard watchdog_guard( [&watchdog, this]() { watchdog.task_finished( m_thread_id ); } );
thread_manager::set_current_thread_module_owner( the_task->get_target_module() );
auto_guard guard( []() { thread_manager::set_current_thread_module_owner( "" ); } );
bool exit = handle_task( the_task );
if( exit || (!m_is_running) )
{
framework_manager::get_instance().get_thread_manager().remove_worker( a_current );
std::vector<std::shared_ptr<abstract_task>> unhandled_task;
unhandled_task.assign( std::next( it ), tasks.end() );
if( !unhandled_task.empty() )
{
framework_manager::get_instance().get_instance()
.get_thread_manager().post_task( unhandled_task, source_here );
}
break;
}
std::lock_guard<std::mutex> locker( m_mutex );
m_last_executing_time = std::chrono::steady_clock::now();
}
}
locker.lock();
if( !m_tasks.empty() )
{
LogUtilDebug() << "schedule unhandled task to thread pool";
framework_manager::get_instance().get_instance()
.get_thread_manager().post_task( std::move( m_tasks ), source_here );
}
LogUtilDebug() << "thread work ended.";
}
bool thread_worker::handle_task( std::shared_ptr<abstract_task> const& a_task )
{
std::string const& debug_info = a_task->get_debug_info();
if( !debug_info.empty() )
{
LogUtilInfo() << "handle task. task debug info: " << debug_info;
}
bool handled = false;
bool ret = false;
if( a_task->get_target_module() == abstract_module::s_task_runner_module_name ||
a_task->get_target_module().empty() )
{
std::shared_ptr<executable_task> runnable_task;
if( a_task->get_task_type() == task_type::executable_task )
{
runnable_task = std::static_pointer_cast<executable_task>( a_task );
}
if( runnable_task )
{
ret = runnable_task->run_task();
if( ret )
{
auto postion_ = runnable_task->get_position();
LogUtilInfo() << "From " << runnable_task->get_source_module()
<< " to : " << runnable_task->get_target_module()
<< ". Require quit current thread. postion: " << postion_.to_string();
}
handled = true;
}
}
if( !handled )
{
framework_manager::get_instance().get_module_manager().schedule_task( a_task );
}
return ret;
}
}