PionNetworkLibrary
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pion::scheduler Class Referenceabstract

#include <scheduler.hpp>

Inheritance diagram for pion::scheduler:
Collaboration diagram for pion::scheduler:

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...
 

Detailed Description

scheduler: combines Boost.ASIO with a managed thread pool for scheduling

Constructor & Destructor Documentation

pion::scheduler::scheduler ( void  )
inline

constructs a new scheduler

virtual pion::scheduler::~scheduler ( )
inlinevirtual

virtual destructor

Member Function Documentation

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().

virtual void pion::scheduler::finish_services ( void  )
inlineprotectedvirtual

finishes all services used to schedule work

Reimplemented in pion::one_to_one_scheduler, and pion::single_service_scheduler.

Referenced by shutdown().

virtual void pion::scheduler::finish_threads ( void  )
inlineprotectedvirtual

finishes all threads used to perform work

Reimplemented in pion::multi_thread_scheduler.

Referenced by shutdown().

virtual boost::asio::io_service& pion::scheduler::get_io_service ( void  )
pure virtual

returns an async I/O service used to schedule work

Implemented in pion::one_to_one_scheduler, and pion::single_service_scheduler.

logger pion::scheduler::get_logger ( void  )
inline

returns the logger currently in use

boost::uint32_t pion::scheduler::get_num_threads ( void  ) const
inline

returns the number of threads currently in use

boost::system_time pion::scheduler::get_wakeup_time ( boost::uint32_t  sleep_sec,
boost::uint32_t  sleep_nsec 
)
staticprotected

calculates a wakeup time in boost::system_time format

Parameters
sleep_secnumber of seconds to sleep for
sleep_nsecnumber of nanoseconds to sleep for
Returns
boost::system_time time to wake up from sleep
bool pion::scheduler::is_running ( void  ) const
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

Parameters
my_serviceIO service used to re-schedule keep_running()
my_timerdeadline 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().

virtual void pion::scheduler::post ( boost::function0< void >  work_func)
inlinevirtual

schedules work to be performed by one of the pooled threads

Parameters
work_funcwork 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().

void pion::scheduler::set_logger ( logger  log_ptr)
inline

sets the logger to be used

void pion::scheduler::set_num_threads ( const boost::uint32_t  n)
inline

sets the number of threads to be used (these are shared by all servers)

void pion::scheduler::shutdown ( void  )
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().

static void pion::scheduler::sleep ( boost::uint32_t  sleep_sec,
boost::uint32_t  sleep_nsec 
)
inlinestatic

puts the current thread to sleep for a specific period of time

Parameters
sleep_secnumber of entire seconds to sleep for
sleep_nsecnumber of nanoseconds to sleep for (10^-9 in 1 second)

Referenced by pion::tcp::server::stop().

template<typename ConditionType , typename LockType >
static void pion::scheduler::sleep ( ConditionType &  wakeup_condition,
LockType &  wakeup_lock,
boost::uint32_t  sleep_sec,
boost::uint32_t  sleep_nsec 
)
inlinestatic

puts the current thread to sleep for a specific period of time, or until a wakeup condition is signaled

Parameters
wakeup_conditionif signaled, the condition will wakeup the thread early
wakeup_lockscoped lock protecting the wakeup condition
sleep_secnumber of entire seconds to sleep for
sleep_nsecnumber of nanoseconds to sleep for (10^-9 in 1 second)
virtual void pion::scheduler::startup ( void  )
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().

virtual void pion::scheduler::stop_services ( void  )
inlineprotectedvirtual

stops all services used to schedule work

Reimplemented in pion::one_to_one_scheduler, and pion::single_service_scheduler.

Referenced by shutdown().

virtual void pion::scheduler::stop_threads ( void  )
inlineprotectedvirtual

stops all threads used to perform work

Reimplemented in pion::multi_thread_scheduler.

Referenced by shutdown().

Member Data Documentation

const boost::uint32_t pion::scheduler::DEFAULT_NUM_THREADS = 8
staticprotected

default number of worker threads in the thread pool

const boost::uint32_t pion::scheduler::KEEP_RUNNING_TIMER_SECONDS = 5
staticprotected

number of seconds a timer should wait for to keep the IO services running

Referenced by keep_running().

boost::uint32_t pion::scheduler::m_active_users
protected

the scheduler will not shutdown until there are no more active users

Referenced by add_active_user(), remove_active_user(), and shutdown().

bool pion::scheduler::m_is_running
protected
logger pion::scheduler::m_logger
protected
boost::mutex pion::scheduler::m_mutex
protected
boost::condition pion::scheduler::m_no_more_active_users
protected

condition triggered when there are no more active users

Referenced by remove_active_user(), and shutdown().

boost::uint32_t pion::scheduler::m_num_threads
protected

total number of worker threads in the pool

Referenced by pion::single_service_scheduler::startup(), and pion::one_to_one_scheduler::startup().

boost::condition pion::scheduler::m_scheduler_has_stopped
protected

condition triggered when the scheduler has stopped

Referenced by join(), and shutdown().

const boost::uint32_t pion::scheduler::MICROSEC_IN_SECOND = 1000000
staticprotected

number of microseconds in one full second (10 ^ 6)

const boost::uint32_t pion::scheduler::NSEC_IN_SECOND = 1000000000
staticprotected

number of nanoseconds in one full second (10 ^ 9)


The documentation for this class was generated from the following files: