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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
/************************************************************************
*
* Copyright (C) 2009-2025 IRCAD France
* Copyright (C) 2012-2020 IHU Strasbourg
*
* This file is part of Sight.
*
* Sight is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sight. If not, see <https://www.gnu.org/licenses/>.
*
***********************************************************************/
#include "core/thread/timer.hpp"
#include "core/thread/worker.hpp"
#include <core/time_stamp.hpp>
#include <boost/asio.hpp>
#include <boost/asio/deadline_timer.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/placeholders.hpp>
#include <boost/bind.hpp>
#include <thread>
namespace sight::core::thread
{
/**
* @brief Private implementation of core::thread::worker using boost::asio.
*/
class worker_asio final : public core::thread::worker
{
public:
worker_asio();
~worker_asio() final;
void stop() final;
void post(task_t _handler) final;
[[nodiscard]] thread_id_t get_thread_id() const final;
void set_thread_name(const std::string& _thread_name) final;
SPTR(core::thread::timer) create_timer() final;
void process_tasks() final;
void process_tasks(period_t _maxtime) final;
protected:
/// Copy constructor forbidden
worker_asio(const worker_asio&);
/// Copy operator forbidden
worker_asio& operator=(const worker_asio&);
private:
struct context
{
/// Class provides functionality to manipulate asynchronous tasks.
boost::asio::io_context m_io_context;
/// Class to inform the io_context when it has work to do.
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> m_work_guard {
m_io_context.get_executor()
};
/// Thread created and managed by the worker.
std::thread m_thread;
};
std::shared_ptr<context> m_context {std::make_shared<context>()};
};
//------------------------------------------------------------------------------
/**
* @brief Private Timer implementation using boost::asio.
*/
class timer_asio final : public core::thread::timer
{
public:
SIGHT_DECLARE_CLASS(timer_asio, core::thread::timer);
SIGHT_ALLOW_SHARED_FROM_THIS();
/**
* @brief Constructs a TimerAsio from given io_context.
*/
explicit timer_asio(boost::asio::io_context& _io_srv);
~timer_asio() final;
/// Starts or restarts the timer.
void start() final;
/// Stops the timer and cancel all pending operations.
void stop() final;
/// Sets time duration.
void set_duration(time_duration_t _duration) final;
/// Returns if the timer mode is 'one shot'.
bool is_one_shot() const final
{
core::mt::scoped_lock lock(m_mutex);
return m_one_shot;
}
/// Sets timer mode.
void set_one_shot(bool _one_shot) final
{
core::mt::scoped_lock lock(m_mutex);
m_one_shot = _one_shot;
}
/// Returns true if the timer is currently running.
bool is_running() const final
{
core::mt::scoped_lock lock(m_mutex);
return m_running;
}
protected:
void cancel_no_lock();
void rearm_no_lock(time_duration_t _duration);
/// Copy constructor forbidden.
timer_asio(const timer_asio&);
/// Copy operator forbidden.
timer_asio& operator=(const timer_asio&);
private:
friend struct timer_callback;
/// Timer object.
boost::asio::deadline_timer m_timer;
/// Time to wait until timer's expiration.
time_duration_t m_duration;
/// Timer's mode.
bool m_one_shot {false};
/// Timer's state.
bool m_running {false};
};
//------------------------------------------------------------------------------
// ---------- WorkerAsio private implementation ----------
worker_asio::worker_asio()
{
// Explicitly copy the shared context to keep it alive.
auto work =
[context = this->m_context](auto&& ...)
{
// run() is blocking while work_guard is alive.
return context->m_io_context.run();
};
std::packaged_task<core::thread::worker::exit_return_type()> task(work);
m_future = task.get_future();
m_context->m_thread = std::thread(std::move(task));
}
//------------------------------------------------------------------------------
worker_asio::~worker_asio()
{
SIGHT_ASSERT(
"Worker must be properly stopped. Try to call stop() from the caller thread before.",
!m_context->m_thread.joinable()
);
}
//------------------------------------------------------------------------------
void worker_asio::stop()
{
SIGHT_ASSERT("Thread is not joinable", m_context->m_thread.joinable());
SIGHT_ASSERT(
"Can not destroy a thread while running it. Try to call stop() from the caller thread before.",
m_context->m_thread.get_id() != core::thread::get_current_thread_id()
);
m_context->m_work_guard.reset();
m_context->m_thread.join();
}
//------------------------------------------------------------------------------
SPTR(core::thread::timer) worker_asio::create_timer()
{
return std::make_shared<timer_asio>(m_context->m_io_context);
}
//------------------------------------------------------------------------------
void worker_asio::post(task_t _handler)
{
boost::asio::post(m_context->m_io_context, _handler);
}
//------------------------------------------------------------------------------
thread_id_t worker_asio::get_thread_id() const
{
return m_context->m_thread.get_id();
}
//------------------------------------------------------------------------------
void worker_asio::set_thread_name(const std::string& _thread_name)
{
core::thread::set_thread_name(_thread_name, m_context->m_thread.native_handle());
}
//------------------------------------------------------------------------------
void worker_asio::process_tasks()
{
m_context->m_io_context.poll();
}
//------------------------------------------------------------------------------
void worker_asio::process_tasks(period_t _maxtime)
{
core::time_stamp time_stamp;
time_stamp.set_life_period(_maxtime);
time_stamp.modified();
while(time_stamp.period_expired())
{
m_context->m_io_context.poll_one();
}
}
// ---------- Worker ----------
SPTR(worker) worker::make()
{
return std::make_shared<worker_asio>();
}
// ---------- Timer private implementation ----------
timer_asio::timer_asio(boost::asio::io_context& _io_srv) :
m_timer(_io_srv),
m_duration(std::chrono::seconds(1))
{
}
timer_asio::~timer_asio() = default;
//------------------------------------------------------------------------------
void timer_asio::set_duration(time_duration_t _duration)
{
core::mt::scoped_lock lock(m_mutex);
m_duration = _duration;
}
//------------------------------------------------------------------------------
void timer_asio::start()
{
core::mt::scoped_lock lock(m_mutex);
this->rearm_no_lock(m_duration);
m_running = true;
}
//------------------------------------------------------------------------------
void timer_asio::stop()
{
core::mt::scoped_lock lock(m_mutex);
if(m_running)
{
m_running = false;
this->cancel_no_lock();
}
}
//------------------------------------------------------------------------------
struct timer_callback
{
//------------------------------------------------------------------------------
static void call(const boost::system::error_code& _error, timer_asio::sptr _timer)
{
if(!_error)
{
timer_asio::time_duration_t duration;
bool one_shot = false;
{
core::mt::scoped_lock lock(_timer->m_mutex);
one_shot = _timer->m_one_shot;
duration = _timer->m_duration;
}
if(!one_shot)
{
{
core::mt::scoped_lock lock(_timer->m_mutex);
if(_timer->m_running)
{
_timer->rearm_no_lock(duration);
}
}
_timer->m_function();
}
else
{
_timer->m_function();
core::mt::scoped_lock lock(_timer->m_mutex);
_timer->m_running = false;
}
}
}
};
//------------------------------------------------------------------------------
void timer_asio::rearm_no_lock(time_duration_t _duration)
{
this->cancel_no_lock();
boost::posix_time::time_duration d =
boost::posix_time::microseconds(std::chrono::duration_cast<std::chrono::microseconds>(_duration).count());
m_timer.expires_from_now(d);
// NOLINTNEXTLINE(modernize-avoid-bind)
m_timer.async_wait(boost::bind(timer_callback::call, boost::asio::placeholders::error, this->get_sptr()));
}
//------------------------------------------------------------------------------
void timer_asio::cancel_no_lock()
{
m_timer.cancel();
}
} //namespace sight::core::thread
|