MCPcopy Create free account
hub / github.com/AimRT/AimRT / TimerLoop

Method TimerLoop

src/runtime/core/executor/time_wheel_executor.cc:217–327  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

215}
216
217void TimeWheelExecutor::TimerLoop() {
218 tid_ = std::this_thread::get_id();
219
220 std::string threadname = name_;
221
222 try {
223 aimrt::runtime::core::util::SetNameForCurrentThread(threadname);
224 aimrt::runtime::core::util::BindCpuForCurrentThread(options_.thread_bind_cpu);
225 aimrt::runtime::core::util::SetCpuSchedForCurrentThread(options_.thread_sched_policy);
226 } catch (const std::exception& e) {
227 AIMRT_WARN("Set thread policy for time manipulator executor '{}' get exception, {}",
228 Name(), e.what());
229 }
230
231 auto last_loop_sys_tp = std::chrono::system_clock::now();
232 auto last_loop_std_tp = std::chrono::steady_clock::now();
233
234 // Record the initial time
235 start_time_point_ =
236 std::chrono::duration_cast<std::chrono::nanoseconds>(
237 (options_.use_system_clock ? last_loop_sys_tp.time_since_epoch() : last_loop_std_tp.time_since_epoch()))
238 .count();
239
240 start_flag_.store(true);
241 start_flag_.notify_all();
242
243 while (state_.load() != State::kShutdown) {
244 try {
245 // sleep a dt
246 auto real_dt = options_.dt;
247 do {
248 // Maximum sleep time
249 static constexpr auto kMaxSleepDt = std::chrono::seconds(1);
250
251 auto sleep_time = (real_dt > kMaxSleepDt) ? kMaxSleepDt : real_dt;
252 real_dt -= sleep_time;
253
254 // A small optimization to prevent real_dt from being too small
255 if (real_dt.count() && options_.dt < kMaxSleepDt && real_dt <= options_.dt) {
256 sleep_time += real_dt;
257 real_dt = std::chrono::nanoseconds(0);
258 }
259
260 if (!options_.use_system_clock) {
261 std::this_thread::sleep_until(
262 last_loop_std_tp +=
263 std::chrono::duration_cast<std::chrono::steady_clock::time_point::duration>(sleep_time));
264 } else {
265 std::this_thread::sleep_until(
266 last_loop_sys_tp +=
267 std::chrono::duration_cast<std::chrono::system_clock::time_point::duration>(sleep_time));
268 }
269
270 } while (state_.load() != State::kShutdown && real_dt.count());
271
272 // Perform an immediate task
273 if (!bind_executor_ref_) {
274 std::queue<aimrt::executor::Task> tmp_queue;

Callers

nothing calls this directly

Calls 6

SetNameForCurrentThreadFunction · 0.85
BindCpuForCurrentThreadFunction · 0.85
whatMethod · 0.80
TickMethod · 0.45
ExecuteMethod · 0.45

Tested by

no test coverage detected