10 #include <boost/date_time/posix_time/posix_time_duration.hpp>
11 #include <pion/PionScheduler.hpp>
29 boost::mutex::scoped_lock scheduler_lock(
m_mutex);
33 PION_LOG_INFO(
m_logger,
"Shutting down the thread scheduler");
48 PION_LOG_INFO(
m_logger,
"The thread scheduler has shutdown");
69 boost::mutex::scoped_lock scheduler_lock(
m_mutex);
77 boost::asio::deadline_timer& my_timer)
83 boost::ref(my_service), boost::ref(my_timer)));
90 boost::mutex::scoped_lock scheduler_lock(
m_mutex);
96 boost::mutex::scoped_lock scheduler_lock(
m_mutex);
102 boost::uint32_t sleep_nsec)
104 boost::xtime wakeup_time;
106 boost::xtime_get(&wakeup_time, TIME_UTC);
108 boost::xtime_get(&wakeup_time, boost::TIME_UTC);
110 wakeup_time.sec += sleep_sec;
111 wakeup_time.nsec += sleep_nsec;
112 if (static_cast<boost::uint32_t>(wakeup_time.nsec) >=
NSEC_IN_SECOND) {
123 }
catch (std::exception& e) {
126 PION_LOG_ERROR(
m_logger,
"caught unrecognized exception");
137 boost::mutex::scoped_lock scheduler_lock(
m_mutex);
140 PION_LOG_INFO(
m_logger,
"Starting thread scheduler");
162 boost::mutex::scoped_lock scheduler_lock(
m_mutex);
165 PION_LOG_INFO(
m_logger,
"Starting thread scheduler");
170 boost::shared_ptr<ServicePair> service_ptr(
new ServicePair());
static const boost::uint32_t NSEC_IN_SECOND
number of nanoseconds in one full second (10 ^ 9)
boost::asio::io_service m_service
service used to manage async I/O events
virtual void startup(void)
Starts the thread scheduler (this is called automatically when necessary)
void keepRunning(boost::asio::io_service &my_service, boost::asio::deadline_timer &my_timer)
boost::condition m_no_more_active_users
condition triggered when there are no more active users
static boost::xtime getWakeupTime(boost::uint32_t sleep_sec, boost::uint32_t sleep_nsec)
void join(void)
the calling thread will sleep until the scheduler has stopped
PionLogger m_logger
primary logging interface used by this class
bool m_is_running
true if the thread scheduler is running
ServicePool m_service_pool
pool of IO services used to schedule work
virtual void finishServices(void)
finishes all services used to schedule work
virtual void stopThreads(void)
stops all threads used to perform work
static const boost::uint32_t MICROSEC_IN_SECOND
number of microseconds in one full second (10 ^ 6)
typedef for a pair object where first is an IO service and second is a deadline timer ...
void processServiceWork(boost::asio::io_service &service)
processes work passed to the asio service & handles uncaught exceptions
static const boost::uint32_t DEFAULT_NUM_THREADS
default number of worker threads in the thread pool
void removeActiveUser(void)
unregisters an active user with the thread scheduler
boost::condition m_scheduler_has_stopped
condition triggered when the scheduler has stopped
boost::uint32_t m_num_threads
total number of worker threads in the pool
boost::mutex m_mutex
mutex to make class thread-safe
boost::asio::deadline_timer m_timer
timer used to periodically check for shutdown
the following enables use of the lock-free cache
virtual void shutdown(void)
Stops the thread scheduler (this is called automatically when the program exits)
virtual void stopServices(void)
stops all services used to schedule work
virtual void finishThreads(void)
finishes all threads used to perform work
virtual void startup(void)
Starts the thread scheduler (this is called automatically when necessary)
virtual void startup(void)
Starts the thread scheduler (this is called automatically when necessary)
ThreadPool m_thread_pool
pool of threads used to perform work
boost::uint32_t m_active_users
the scheduler will not shutdown until there are no more active users
static const boost::uint32_t KEEP_RUNNING_TIMER_SECONDS
number of seconds a timer should wait for to keep the IO services running