PionNetworkLibrary
|
#include <scheduler.hpp>
Public Member Functions | |
scheduler (void) | |
constructs a new scheduler More... | |
virtual | ~scheduler () |
virtual destructor More... | |
virtual void | startup (void) |
Starts the thread scheduler (this is called automatically when necessary) More... | |
virtual void | shutdown (void) |
Stops the thread scheduler (this is called automatically when the program exits) More... | |
void | join (void) |
the calling thread will sleep until the scheduler has stopped More... | |
void | add_active_user (void) |
void | remove_active_user (void) |
unregisters an active user with the thread scheduler More... | |
bool | is_running (void) const |
returns true if the scheduler is running More... | |
void | set_num_threads (const boost::uint32_t n) |
sets the number of threads to be used (these are shared by all servers) More... | |
boost::uint32_t | get_num_threads (void) const |
returns the number of threads currently in use More... | |
void | set_logger (logger log_ptr) |
sets the logger to be used More... | |
logger | get_logger (void) |
returns the logger currently in use More... | |
virtual boost::asio::io_service & | get_io_service (void)=0 |
returns an async I/O service used to schedule work More... | |
virtual void | post (boost::function0< void > work_func) |
void | keep_running (boost::asio::io_service &my_service, boost::asio::deadline_timer &my_timer) |
void | process_service_work (boost::asio::io_service &service) |
processes work passed to the asio service & handles uncaught exceptions More... | |
Static Public Member Functions | |
static void | sleep (boost::uint32_t sleep_sec, boost::uint32_t sleep_nsec) |
template<typename ConditionType , typename LockType > | |
static void | sleep (ConditionType &wakeup_condition, LockType &wakeup_lock, boost::uint32_t sleep_sec, boost::uint32_t sleep_nsec) |
Protected Member Functions | |
virtual void | stop_services (void) |
stops all services used to schedule work More... | |
virtual void | stop_threads (void) |
stops all threads used to perform work More... | |
virtual void | finish_services (void) |
finishes all services used to schedule work More... | |
virtual void | finish_threads (void) |
finishes all threads used to perform work More... | |
Static Protected Member Functions | |
static boost::system_time | get_wakeup_time (boost::uint32_t sleep_sec, boost::uint32_t sleep_nsec) |
Protected Attributes | |
boost::mutex | m_mutex |
mutex to make class thread-safe More... | |
logger | m_logger |
primary logging interface used by this class More... | |
boost::condition | m_no_more_active_users |
condition triggered when there are no more active users More... | |
boost::condition | m_scheduler_has_stopped |
condition triggered when the scheduler has stopped More... | |
boost::uint32_t | m_num_threads |
total number of worker threads in the pool More... | |
boost::uint32_t | m_active_users |
the scheduler will not shutdown until there are no more active users More... | |
bool | m_is_running |
true if the thread scheduler is running More... | |
Static Protected Attributes | |
static const boost::uint32_t | DEFAULT_NUM_THREADS = 8 |
default number of worker threads in the thread pool More... | |
static const boost::uint32_t | NSEC_IN_SECOND = 1000000000 |
number of nanoseconds in one full second (10 ^ 9) More... | |
static const boost::uint32_t | MICROSEC_IN_SECOND = 1000000 |
number of microseconds in one full second (10 ^ 6) More... | |
static const boost::uint32_t | KEEP_RUNNING_TIMER_SECONDS = 5 |
number of seconds a timer should wait for to keep the IO services running More... | |
scheduler: combines Boost.ASIO with a managed thread pool for scheduling
|
inline |
constructs a new scheduler
|
inlinevirtual |
virtual destructor
void pion::scheduler::add_active_user | ( | void | ) |
registers an active user with the thread scheduler. Shutdown of the scheduler is deferred until there are no more active users. This ensures that any work queued will not reference destructed objects
References m_active_users, m_is_running, m_mutex, and startup().
Referenced by pion::tcp::server::start().
|
inlineprotectedvirtual |
finishes all services used to schedule work
Reimplemented in pion::one_to_one_scheduler, and pion::single_service_scheduler.
Referenced by shutdown().
|
inlineprotectedvirtual |
finishes all threads used to perform work
Reimplemented in pion::multi_thread_scheduler.
Referenced by shutdown().
|
pure virtual |
returns an async I/O service used to schedule work
Implemented in pion::one_to_one_scheduler, and pion::single_service_scheduler.
|
inline |
returns the logger currently in use
|
inline |
returns the number of threads currently in use
|
staticprotected |
calculates a wakeup time in boost::system_time format
sleep_sec | number of seconds to sleep for |
sleep_nsec | number of nanoseconds to sleep for |
|
inline |
returns true if the scheduler is running
void pion::scheduler::join | ( | void | ) |
the calling thread will sleep until the scheduler has stopped
References m_is_running, m_mutex, and m_scheduler_has_stopped.
void pion::scheduler::keep_running | ( | boost::asio::io_service & | my_service, |
boost::asio::deadline_timer & | my_timer | ||
) |
thread function used to keep the io_service running
my_service | IO service used to re-schedule keep_running() |
my_timer | deadline timer used to keep the IO service active while running |
References KEEP_RUNNING_TIMER_SECONDS, and m_is_running.
Referenced by pion::single_service_scheduler::startup(), and pion::one_to_one_scheduler::startup().
|
inlinevirtual |
schedules work to be performed by one of the pooled threads
work_func | work function to be executed |
void pion::scheduler::process_service_work | ( | boost::asio::io_service & | service | ) |
processes work passed to the asio service & handles uncaught exceptions
References pion::diagnostic_information(), m_is_running, m_logger, and PION_LOG_ERROR.
Referenced by pion::single_service_scheduler::startup(), and pion::one_to_one_scheduler::startup().
void pion::scheduler::remove_active_user | ( | void | ) |
unregisters an active user with the thread scheduler
References m_active_users, m_mutex, and m_no_more_active_users.
Referenced by pion::tcp::server::stop().
|
inline |
sets the logger to be used
|
inline |
sets the number of threads to be used (these are shared by all servers)
|
virtual |
Stops the thread scheduler (this is called automatically when the program exits)
References finish_services(), finish_threads(), m_active_users, m_is_running, m_logger, m_mutex, m_no_more_active_users, m_scheduler_has_stopped, PION_LOG_INFO, stop_services(), and stop_threads().
|
inlinestatic |
puts the current thread to sleep for a specific period of time
sleep_sec | number of entire seconds to sleep for |
sleep_nsec | number of nanoseconds to sleep for (10^-9 in 1 second) |
Referenced by pion::tcp::server::stop().
|
inlinestatic |
puts the current thread to sleep for a specific period of time, or until a wakeup condition is signaled
wakeup_condition | if signaled, the condition will wakeup the thread early |
wakeup_lock | scoped lock protecting the wakeup condition |
sleep_sec | number of entire seconds to sleep for |
sleep_nsec | number of nanoseconds to sleep for (10^-9 in 1 second) |
|
inlinevirtual |
Starts the thread scheduler (this is called automatically when necessary)
Reimplemented in pion::one_to_one_scheduler, and pion::single_service_scheduler.
Referenced by add_active_user().
|
inlineprotectedvirtual |
stops all services used to schedule work
Reimplemented in pion::one_to_one_scheduler, and pion::single_service_scheduler.
Referenced by shutdown().
|
inlineprotectedvirtual |
stops all threads used to perform work
Reimplemented in pion::multi_thread_scheduler.
Referenced by shutdown().
|
staticprotected |
default number of worker threads in the thread pool
|
staticprotected |
number of seconds a timer should wait for to keep the IO services running
Referenced by keep_running().
|
protected |
the scheduler will not shutdown until there are no more active users
Referenced by add_active_user(), remove_active_user(), and shutdown().
|
protected |
true if the thread scheduler is running
Referenced by add_active_user(), join(), keep_running(), process_service_work(), shutdown(), pion::single_service_scheduler::startup(), and pion::one_to_one_scheduler::startup().
|
protected |
primary logging interface used by this class
Referenced by process_service_work(), shutdown(), pion::single_service_scheduler::startup(), and pion::one_to_one_scheduler::startup().
|
protected |
mutex to make class thread-safe
Referenced by add_active_user(), join(), remove_active_user(), shutdown(), pion::single_service_scheduler::startup(), and pion::one_to_one_scheduler::startup().
|
protected |
condition triggered when there are no more active users
Referenced by remove_active_user(), and shutdown().
|
protected |
total number of worker threads in the pool
Referenced by pion::single_service_scheduler::startup(), and pion::one_to_one_scheduler::startup().
|
protected |
condition triggered when the scheduler has stopped
Referenced by join(), and shutdown().
|
staticprotected |
number of microseconds in one full second (10 ^ 6)
|
staticprotected |
number of nanoseconds in one full second (10 ^ 9)