7 #include "fwThread/Timer.hpp" 8 #include "fwThread/Worker.hpp" 10 #include <fwCore/TimeStamp.hpp> 12 #include <boost/asio/deadline_timer.hpp> 13 #include <boost/asio/io_service.hpp> 20 std::size_t WorkerThread( ::boost::asio::io_service& io_service )
23 std::size_t res = io_service.run();
34 typedef ::boost::asio::io_service IOServiceType;
35 typedef ::boost::asio::io_service::work WorkType;
36 typedef std::shared_ptr< WorkType > WorkPtrType;
37 typedef std::thread ThreadType;
45 void post(TaskType handler);
84 TimerAsio(::boost::asio::io_service& ioSrv);
95 void setDuration(TimeDurationType duration);
100 ::fwCore::mt::ScopedLock lock(m_mutex);
107 ::fwCore::mt::ScopedLock lock(m_mutex);
114 ::fwCore::mt::ScopedLock lock(m_mutex);
121 void rearmNoLock(TimeDurationType duration);
123 void call(const ::boost::system::error_code& code);
148 WorkerAsio::WorkerAsio() :
150 m_work(
std::make_shared< WorkType >(
std::ref(m_ioService)) )
152 std::packaged_task< ::fwThread::Worker::ExitReturnType() > task( std::bind(&WorkerThread, std::ref(m_ioService)) );
153 std::future< ::fwThread::Worker::ExitReturnType > ufuture = task.get_future();
155 m_thread = std::make_shared< ThreadType >( std::move( task ) );
157 m_future = std::move(ufuture);
160 WorkerAsio::~WorkerAsio()
162 if(!m_ioService.stopped())
178 return std::make_shared< TimerAsio >(std::ref(m_ioService));
185 m_ioService.post(handler);
192 return m_thread->get_id();
211 m_ioService.poll_one();
218 return std::make_shared< WorkerAsio >();
231 TimerAsio::~TimerAsio()
239 ::fwCore::mt::ScopedLock lock(m_mutex);
247 ::fwCore::mt::ScopedLock lock(m_mutex);
256 ::fwCore::mt::ScopedLock lock(m_mutex);
260 this->cancelNoLock();
266 void TimerAsio::rearmNoLock(TimeDurationType duration)
268 this->cancelNoLock();
269 ::boost::posix_time::time_duration d =
270 ::boost::posix_time::microseconds(std::chrono::duration_cast<std::chrono::microseconds>(duration).count());
272 m_timer.async_wait( std::bind(&TimerAsio::call,
this, std::placeholders::_1));
277 void TimerAsio::call(const ::boost::system::error_code& error)
283 TimerAsio::sptr deleteLater = std::dynamic_pointer_cast<
TimerAsio>(shared_from_this());
285 TimeDurationType duration;
288 ::fwCore::mt::ScopedLock lock(m_mutex);
295 this->rearmNoLock(duration);
301 ::fwCore::mt::ScopedLock lock(m_mutex);
310 void TimerAsio::cancelNoLock()
bool isOneShot() const
Returns if the timer mode is 'one shot'.
bool isRunning() const
Returns true if the timer is currently running.
FWCORE_API void modified()
Update the timestamp to the current EPOCH time.
ThreadIdType getThreadId() const
Returns the worker's thread id.
bool m_running
Timer's state.
bool m_oneShot
Timer's mode.
TimeDurationType m_duration
Time to wait until timer's expiration.
std::shared_ptr< ::fwThread::Timer > createTimer()
Creates and returns a fwThread::Timer running in this Worker.
FWTHREAD_API ThreadIdType getCurrentThreadId()
Returns the current thread id.
void setOneShot(bool oneShot)
Sets timer mode.
#define OSLM_TRACE(message)
void stop()
Stops the timer and cancel all pending operations.
virtual void processTasks()
Processes all worker pending tasks for the calling thread until there are no more tasks to process...
TimerAsio(::boost::asio::io_service &ioSrv)
Constructs a TimerAsio from given io_service.
void post(TaskType handler)
Requests invocation of the given task handler and returns immediately.
FunctionType m_function
Function object to execute each time the timer expires.
void start()
Starts or restarts the timer.
void stop()
Waits for the last task to be processed and stops the loop.
FWCORE_API void setLifePeriod(TimeStampType period)
Setter for the life period.
Provides a timestamp and a expiracy system.
IOServiceType m_ioService
Class provides functionality to manipulate asynchronous tasks.
The Timer class provides single-shot or repetitive timers. A Timer triggers a function once after a d...
FWCORE_API bool periodExpired() const
Check TimeStamp expiracy status.
WorkPtrType m_work
Class to inform the io_service when it has work to do.
::boost::asio::deadline_timer m_timer
Timer object.
Private implementation of fwThread::Worker using boost::asio.
std::shared_ptr< ThreadType > m_thread
Thread created and managed by the worker.
This class creates and manages a task loop. The default implementation create a loop in a new thread...
This namespace fwThread provides few tools to execute asynchronous tasks on different threads...
Private Timer implementation using boost::asio.
WorkerAsio & operator=(const WorkerAsio &)
Copy operator forbidden.
void setDuration(TimeDurationType duration)
Sets time duration.