S3FC project page | S3FC home page |
#include <s3_thread_base.h>
Inheritance diagram for s3_thread_base:
Public Types | |
typedef pthread_t | s3_thread_id |
Public Methods | |
s3_thread_base () | |
Default constructor. More... | |
virtual | ~s3_thread_base () |
Destructor will terminate the thread if it is running. More... | |
virtual void | main_loop ()=0 |
Function that is invoked as the main loop in its own thread. More... | |
s3_thread_id | get_thread_id () const |
Return the thread_id associated with this thread. More... | |
void | set_detached (bool n_detach) |
Set the detached flag, determining in what state the thread would be started. More... | |
bool | is_detached () const |
Report the detached state of this thread. More... | |
void | set_sched_round_robin (unsigned int priority) |
Set the scehule policy for this thread to be a round robin. More... | |
void | set_sched_fifo (unsigned int priority) |
Set the scehule policy for this thread to be first in first out. More... | |
void | set_sched_normal (unsigned int priority) |
Set the scehule policy to the default for the system. More... | |
bool | is_running () const |
Report the running state of this thread. More... | |
void | start () |
Start the execution of the main loop. More... | |
void | stop () |
Stop the excution of the main loop by sending a cancellation request to the running thread. More... | |
void | wait_on_exit () |
Call pthread_join() on this thread and wait for it to terminate. More... | |
void | lock_state () const |
Acquire the state lock (in other words, lock the state for the caller's exclusive use). More... | |
void | unlock_state () const |
Relinquish the state lock (in other words, unlock the state). More... | |
bool | test_state () const |
Test and set the state lock without blocking. More... | |
void | signal_state_changed () const |
Signal a state change. More... | |
void | request_terminate () |
Request soft termination of a thread (by setting a flag that is tested with test_terminate()) and signal a state change (. More... | |
Static Public Methods | |
void | test_cancel () |
Introduce a cancellation point at the point of invocation. More... | |
Static Public Attributes | |
const pthread_t | invalid_thread_id = (pthread_t)(-1) |
Protected Methods | |
void | wait_on_state_changed () const |
Wait for a state-change. More... | |
virtual void | cleanup () |
Cleanup handler. More... | |
bool | test_terminate () |
Test if soft thread termination has been requested. More... | |
Static Protected Methods | |
void * | pthread_create_ccpp_wrapper (void *obj) |
Static member function used to bridge the C/C++ interface between this class and the POSIX threads interface. More... | |
void | pthread_cleanup_ccpp_wrapper (void *obj) |
Static member function used to bridge the C/C++ interface between this class and the POSIX threads interface. More... | |
Protected Attributes | |
bool | started |
bool | detached |
pthread_t | thread_id |
pthread_attr_t | thread_attr |
s3_mutex | state_lock |
State lock mutex. More... | |
s3_semaphore | state_changed |
State changed semaphore. More... | |
bool | term_request |
s3_mutex | term_request_lock |
The class contains a single s3_mutex
and an associated s3_semaphore
, that is used to lock the global state and allow synchronisation between threads. Methods are provided to lock and unlock the state of an instance, using the mutex:
Methods are provided to synchronise threads, using the semaphore:
A thread has two state variables associated with it:
true
value indicates that the thread is busy executing its main loop (even though it might be blocking or sitting idle in the loop). A false
value indicates that it is stopped and is unable to perform any computations.true
value indicates that the thread is in the detachable state, while a false
value indicates that it is in the joinable state. If a thread in the joinable state terminates, another thread must join to it, by calling wait_on_exit()
on the terminating thread, otherwise resources associated with the thread is not freed. A thread that runs in the detached state automatically deallocates its allocated resources, but it is impossible to synchronise on thread termination.main_loop
is entered and the thread executes until the main loop terminates normally or it is explicitly terminated by a stop()
. A single thread instance can only be started once. Attempting to start()
a thread more than once, results in an exception being thrown. The running state of a thread is true
after this call. The detached state is also controlled by a parameter that is passed to this call.test_cancel()
call introduces an explicit non-blocking cancellation point.true
, the thread is started in the detached state, else in the joinable state. Default behaviour is for it to be started in the joinable state.false
after this call.
Definition at line 122 of file s3_thread_base.h.
|
Definition at line 125 of file s3_thread_base.h. Referenced by get_thread_id. |
|
Default constructor. Zero and default init. Definition at line 44 of file s3_thread_base.cc. References thread_attr. |
|
Destructor will terminate the thread if it is running.
Note that this may mean that the destructor will block on a pthread_join() call if the thread is not detatched and is blocked but not at a valid cancellation point (condition wait or semaphore). It is the programmers responsibility to ensure that the derived thread will not block in this way. If the thread is detatched, it is simply killed and control is returned immidiately. Remember to stick to Definition at line 53 of file s3_thread_base.cc. References is_running, and thread_attr. |
|
Cleanup handler.
This is called before killing the thread, as part of the termination process, once a cancellation request has been received - from Reimplemented in s3_txport_local. Definition at line 328 of file s3_thread_base.cc. Referenced by pthread_cleanup_ccpp_wrapper. |
|
Return the thread_id associated with this thread. Zero (0) is returned if this thread is not running.
Definition at line 139 of file s3_thread_base.cc. References s3_thread_id, and thread_id. Referenced by s3_txport_tcp::stop. |
|
Report the detached state of this thread. The behaviour is undefined if the thread is not running.
Definition at line 159 of file s3_thread_base.cc. References detached. |
|
Report the running state of this thread.
Definition at line 258 of file s3_thread_base.cc. References started. Referenced by s3_txport_tcp::listen, s3_txport_tcp< s3_message >::listening, s3_txport_local::listening, set_detached, s3_txport_tcp::stop, s3_post_office_switch::~s3_post_office_switch, ~s3_thread_base, and s3_txport_local::~s3_txport_local. |
|
Acquire the state lock (in other words, lock the state for the caller's exclusive use). This should be called prior to entering a critical section. This should never be called by a thread that already holds the lock. Definition at line 284 of file s3_thread_base.cc. References s3_mutex::lock, and state_lock. Referenced by s3_post_office::add_dst, s3_txport_tcp_rx_task::cleanup, s3_post_office_switch::main_loop, s3_post_office::main_loop, pthread_create_ccpp_wrapper, s3_post_office::remove_dst, and start. |
|
Function that is invoked as the main loop in its own thread. Override this in derived class.
Implemented in s3_msgb_log_rx_thread. Referenced by pthread_create_ccpp_wrapper. |
|
Static member function used to bridge the C/C++ interface between this class and the POSIX threads interface.
This calls
Definition at line 374 of file s3_thread_base.cc. References cleanup, and started. Referenced by pthread_create_ccpp_wrapper. |
|
Static member function used to bridge the C/C++ interface between this class and the POSIX threads interface.
This calls the
Definition at line 337 of file s3_thread_base.cc. References lock_state, main_loop, pthread_cleanup_ccpp_wrapper, and unlock_state. Referenced by start. |
|
Request soft termination of a thread (by setting a flag that is tested with
Definition at line 314 of file s3_thread_base.cc. References s3_mutex::lock, signal_state_changed, term_request, term_request_lock, and s3_mutex::unlock. Referenced by s3_message_box::de_init, s3_periodic_notifier::event_loop::~event_loop, s3_post_office_switch::~s3_post_office_switch, and s3_rpc_server::~s3_rpc_server. |
|
Set the detached flag, determining in what state the thread would be started. This may only be called on a thread that is not running.
Definition at line 145 of file s3_thread_base.cc. References detached, and is_running. |
|
Set the scehule policy for this thread to be first in first out. Higher priority processes get priority over lower priorities and processes at the same priorities are run on first come, first served.
Definition at line 197 of file s3_thread_base.cc. References invalid_thread_id, and thread_id. |
|
Set the scehule policy to the default for the system.
Definition at line 229 of file s3_thread_base.cc. References invalid_thread_id, and thread_id. |
|
Set the scehule policy for this thread to be a round robin. Higher priority processes get priority over lower priorities and processes at the same priorities are timesliced.
Definition at line 165 of file s3_thread_base.cc. References invalid_thread_id, and thread_id. |
|
Signal a state change.
If a thread is waiting on a state change, through Definition at line 308 of file s3_thread_base.cc. References s3_semaphore::post, and state_changed. Referenced by request_terminate. |
|
Start the execution of the main loop.
This spawns a thread and returns immediatly. If the thread creation fails, an
Definition at line 65 of file s3_thread_base.cc. References invalid_thread_id, lock_state, pthread_create_ccpp_wrapper, started, term_request, thread_attr, thread_id, and unlock_state. Referenced by s3_post_office::connect, s3_periodic_notifier::event_loop::event_loop, s3_txport_tcp::listen, s3_post_office_switch::s3_post_office_switch, s3_rpc_server::s3_rpc_server, and s3_txport_local::s3_txport_local. |
|
Stop the excution of the main loop by sending a cancellation request to the running thread.
This call returns immediately. The cleanup handler, Reimplemented in s3_txport_tcp. Definition at line 122 of file s3_thread_base.cc. |
|
Introduce a cancellation point at the point of invocation.
Definition at line 323 of file s3_thread_base.cc. Referenced by s3_msgb_log_rx_thread::main_loop, s3_socket_tcp_ssl::read, s3_socket_tcp::read, s3_socket_tcp_ssl::write, and s3_socket_tcp::write. |
|
Test and set the state lock without blocking. This effectively calls s3_mutex::try_wait() (see s3_mutex) on the state lock.
Definition at line 296 of file s3_thread_base.cc. References state_lock, and s3_mutex::try_lock. |
|
Test if soft thread termination has been requested. In main(), the application can test if it has been requested to terminate by calling this function. If true, the application must call return to terminate the thread. In this way, destructors of automatic (stack) objects declared in main() will be properly called. Note that the cleanup handlers are still called and that the thread will still obey a cancellation request via the stop() method at standard cancellation points. This call is not a cancellation point. Definition at line 382 of file s3_thread_base.cc. References s3_mutex::lock, term_request, term_request_lock, and s3_mutex::unlock. Referenced by s3_rpc_server::main_loop, s3_periodic_notifier::event_loop::main_loop, s3_post_office_switch::main_loop, and s3_post_office::main_loop. |
|
Relinquish the state lock (in other words, unlock the state). This should be called as soon as possible after leaving a critical section. This should only be called by the thread currently holding the lock. Definition at line 290 of file s3_thread_base.cc. References state_lock, and s3_mutex::unlock. Referenced by s3_post_office::add_dst, s3_txport_tcp_rx_task::cleanup, s3_post_office_switch::main_loop, s3_post_office::main_loop, pthread_create_ccpp_wrapper, s3_post_office::remove_dst, and start. |
|
Call
Definition at line 264 of file s3_thread_base.cc. References detached, invalid_thread_id, and thread_id. Referenced by s3_message_box::de_init, s3_post_office::disconnect, s3_txport_tcp::stop, s3_periodic_notifier::event_loop::~event_loop, s3_post_office_switch::~s3_post_office_switch, s3_rpc_server::~s3_rpc_server, and s3_txport_local::~s3_txport_local. |
|
Wait for a state-change.
The caller is suspended until a state-change is signalled, by calling Definition at line 302 of file s3_thread_base.cc. References state_changed, and s3_semaphore::wait. Referenced by s3_rpc_server::main_loop, and s3_msgb_log_rx_thread::main_loop. |
|
Definition at line 132 of file s3_thread_base.h. Referenced by is_detached, set_detached, and wait_on_exit. |
|
Definition at line 41 of file s3_thread_base.cc. Referenced by set_sched_fifo, set_sched_normal, set_sched_round_robin, start, and wait_on_exit. |
|
Reimplemented in s3_post_office. Definition at line 130 of file s3_thread_base.h. Referenced by is_running, pthread_cleanup_ccpp_wrapper, start, and stop. |
|
State changed semaphore.
Definition at line 144 of file s3_thread_base.h. Referenced by s3_rpc_server::main_loop, s3_msgb_log_rx_thread::s3_msgb_log_rx_thread, signal_state_changed, and wait_on_state_changed. |
|
State lock mutex.
Reimplemented in s3_periodic_notifier::event_loop. Definition at line 140 of file s3_thread_base.h. Referenced by lock_state, test_state, and unlock_state. |
|
Definition at line 146 of file s3_thread_base.h. Referenced by request_terminate, start, and test_terminate. |
|
Definition at line 147 of file s3_thread_base.h. Referenced by request_terminate, and test_terminate. |
|
Definition at line 135 of file s3_thread_base.h. Referenced by s3_thread_base, start, and ~s3_thread_base. |
|
Definition at line 134 of file s3_thread_base.h. Referenced by get_thread_id, set_sched_fifo, set_sched_normal, set_sched_round_robin, start, stop, and wait_on_exit. |
Send comments to: s3fc@stonethree.com |
|