Threads

Collaboration diagram for Threads:

Classes

class  protlib::ThreadParam
class  protlib::ThreadError
class  protlib::Thread
class  protlib::ThreadStarter< T, TParam >

Namespaces

namespace  protlib

Defines

#define install_cleanup(f, m)   pthread_cleanup_push((void (*)(void *)) f, (void *) m)
 install cleanup handler
#define install_cleanup_mutex(m)   install_cleanup(pthread_mutex_unlock,m)
 install cleanup handler for mutex
#define install_cleanup_mutex_lock(m)   install_cleanup_mutex(m) pthread_mutex_lock(m)
 lock mutex and install cleanup handler
#define install_cleanup_thread_lock(ttype, tp)   install_cleanup(call_unlock<ttype>,tp) tp->lock()
 Lock thread and install cleanup handler.
#define uninstall_cleanup(exec)   pthread_cleanup_pop(exec)
 uninstall cleanup handler

Functions

template<class T >
void protlib::call_unlock (void *pobj)
 unlock template
void protlib::call_void_fun (void(*f)())
 call void function
template<class T >
void * protlib::thread_starter (void *thread_object)
 protlib::ThreadParam::ThreadParam (uint32 wait, const char *name, uint32 minc=1, uint32 maxc=(uint32)-1)
 protlib::Thread::Thread (const ThreadParam &p, bool create_queue=true, bool exp_allow=true)
virtual protlib::Thread::~Thread ()
void * protlib::Thread::start_processing ()
void protlib::Thread::stop_processing (bool do_lock=true)
void protlib::Thread::abort_processing (bool do_lock=true)
bool protlib::Thread::is_running (bool do_lock=true)
int protlib::Thread::wait_cond (int32 sec, int32 nsec=0)
state_t protlib::Thread::get_state (bool do_lock=true)
static void protlib::Thread::get_time_of_day (struct timespec &ts)
 get time of day as timespec
virtual const char * protlib::ThreadError::getstr () const
void protlib::Thread::lock ()
void protlib::Thread::unlock ()
void protlib::Thread::signal_cond ()
void protlib::Thread::broadcast_cond ()
void protlib::Thread::wait_cond ()
int protlib::Thread::wait_cond (const struct timespec &ts)
void protlib::Thread::inc_running_threads ()
void protlib::Thread::dec_running_threads ()
uint32 protlib::Thread::get_running_threads () const
void protlib::Thread::inc_started_threads ()
uint32 protlib::Thread::get_started_threads () const
 protlib::ThreadStarter::ThreadStarter (uint32 count, const TParam &param)
 protlib::ThreadStarter::~ThreadStarter ()
void protlib::ThreadStarter::start_processing ()
void protlib::ThreadStarter::stop_processing ()
bool protlib::ThreadStarter::sleepuntilstop ()
void protlib::ThreadStarter::wait_until_stopped ()
void protlib::ThreadStarter::abort_processing (bool kill=false)

Variables

static uint32 protlib::ThreadParam::default_sleep_time = 5
static const char *const protlib::ThreadError::errstr []

Define Documentation

#define install_cleanup ( f,
 )     pthread_cleanup_push((void (*)(void *)) f, (void *) m)

install cleanup handler

This macro installs a cleanup handler and does some type casting. Use uninstall_cleanup(execute) or an apropriate unlock routine to unlock the mutex and uninstall the handler.

Parameters:
f pointer to a cleanup handler routine. This is casted to void (*routine)(void *)
m cleanup handler argument, normally a pointer to the mutex. This is casted to void*.

Definition at line 65 of file cleanuphandler.h.

#define install_cleanup_mutex (  )     install_cleanup(pthread_mutex_unlock,m)

install cleanup handler for mutex

Calls install_cleanup and uses pthread_mutex_unlock as cleanup handler.

Parameters:
m pointer to the mutex.

Definition at line 71 of file cleanuphandler.h.

#define install_cleanup_mutex_lock (  )     install_cleanup_mutex(m) pthread_mutex_lock(m)

lock mutex and install cleanup handler

Parameters:
m mutex

Definition at line 76 of file cleanuphandler.h.

#define install_cleanup_thread_lock ( ttype,
tp   )     install_cleanup(call_unlock<ttype>,tp) tp->lock()

Lock thread and install cleanup handler.

Parameters:
ttype class name of thread object
tp pointer to thread object

Definition at line 82 of file cleanuphandler.h.

#define uninstall_cleanup ( exec   )     pthread_cleanup_pop(exec)

uninstall cleanup handler

This uninstalls a cleanup handler and optionally executes it.

Parameters:
exec 0 or 1

Definition at line 88 of file cleanuphandler.h.


Function Documentation

template<class T , class TParam >
void protlib::ThreadStarter< T, TParam >::abort_processing ( bool  kill = false  )  [inline, inherited]

void protlib::Thread::abort_processing ( bool  do_lock = true  )  [inherited]

This is called just before a running thread is killed.

Parameters:
do_lock if true the thread mutex is used

Definition at line 202 of file threads.cpp.

References protlib::Thread::lock(), protlib::Thread::signal_cond(), protlib::Thread::state, protlib::Thread::STATE_ABORT, protlib::Thread::STATE_RUN, protlib::Thread::STATE_STOP, and protlib::Thread::unlock().

void protlib::Thread::broadcast_cond (  )  [inline, inherited]

Definition at line 225 of file threads.h.

References protlib::Thread::cond.

template<class T >
void protlib::call_unlock ( void *  pobj  )  [inline]

unlock template

This function calls the unlock method of an object.

Parameters:
pobj pointer to the locked object.

Definition at line 94 of file cleanuphandler.h.

void protlib::call_void_fun ( void(*)()  f  )  [inline]

call void function

This function calls a function of type void f(void).

Definition at line 102 of file cleanuphandler.h.

void protlib::Thread::dec_running_threads (  )  [inline, private, inherited]

Definition at line 247 of file threads.h.

References protlib::Thread::running_threads.

Referenced by protlib::Thread::start_processing().

uint32 protlib::Thread::get_running_threads (  )  const [inline, private, inherited]

Definition at line 252 of file threads.h.

References protlib::Thread::running_threads.

Referenced by protlib::Thread::is_running(), and protlib::Thread::~Thread().

uint32 protlib::Thread::get_started_threads (  )  const [inline, private, inherited]

Definition at line 260 of file threads.h.

References protlib::Thread::started_threads.

Referenced by protlib::Thread::start_processing().

Thread::state_t protlib::Thread::get_state ( bool  do_lock = true  )  [inherited]

Returns the thread's state.

Parameters:
do_lock if true the thread mutex is used
Returns:
the thread's current state
See also:
enum state_t for more information on what a Threads state is.

Definition at line 278 of file threads.cpp.

References protlib::Thread::lock(), protlib::Thread::state, and protlib::Thread::unlock().

Referenced by protlib::TPoverUDP::listener_thread(), protlib::TPoverUDP::main_loop(), protlib::TPoverTCP::main_loop(), protlib::TPoverTCP::master_listener_thread(), protlib::TimerModule::process_elapsed_timers(), and protlib::TimerModule::process_queue().

void protlib::Thread::get_time_of_day ( struct timespec &  ts  )  [static, inherited]

get time of day as timespec

Definition at line 291 of file threads.cpp.

Referenced by protlib::TPoverTCP::get_connection_to().

const char * protlib::ThreadError::getstr (  )  const [virtual, inherited]

Deprecated: Use what() instead.

Reimplemented from protlib::ProtLibException.

Definition at line 298 of file threads.cpp.

References protlib::ThreadError::err, and protlib::ThreadError::errstr.

Referenced by protlib::ThreadError::what().

void protlib::Thread::inc_running_threads (  )  [inline, private, inherited]

Definition at line 243 of file threads.h.

References protlib::Thread::running_threads.

Referenced by protlib::Thread::start_processing().

void protlib::Thread::inc_started_threads (  )  [inline, private, inherited]

Definition at line 256 of file threads.h.

References protlib::Thread::started_threads.

Referenced by protlib::Thread::start_processing().

bool protlib::Thread::is_running ( bool  do_lock = true  )  [inherited]

Checks whether there is still a running thread.

Parameters:
do_lock if true the thread mutex is used

Definition at line 221 of file threads.cpp.

References protlib::Thread::get_running_threads(), protlib::Thread::lock(), and protlib::Thread::unlock().

void protlib::Thread::lock (  )  [inline, inherited]

void protlib::Thread::signal_cond (  )  [inline, inherited]

template<class T , class TParam >
bool protlib::ThreadStarter< T, TParam >::sleepuntilstop (  )  [inline, inherited]

Wait for the thread to stop running (DEPRECATED).

Sleeps until all threads have stopped running but not longer than sleep_time seconds.

This method is deprecated because it suffers from a race condition: If none of the pthreads created in start_processing() has been run yet, then this method returns immediately. Use wait_until_stopped() instead.

Returns:
true if the threads have stopped
See also:
ThreadParam

Definition at line 453 of file threads.h.

References ariba::utility::Helper::sleep(), protlib::ThreadStarter< T, TParam >::thread_object, and protlib::ThreadStarter< T, TParam >::thread_param.

Referenced by protlib::ThreadStarter< T, TParam >::abort_processing(), and protlib::ThreadStarter< T, TParam >::~ThreadStarter().

template<class T , class TParam >
void protlib::ThreadStarter< T, TParam >::start_processing (  )  [inline, inherited]

void * protlib::Thread::start_processing (  )  [inherited]

Called for each thread when processing is started.

The thread must not be locked because this is done inside this method. Cancellation is enabled and set to synchronous mode. So you only need to install cleanup handlers when there is a cancellation point between calls to lock() and unlock().

Definition at line 120 of file threads.cpp.

References protlib::Thread::dec_running_threads(), ERRLog, protlib::Thread::get_started_threads(), protlib::ProtLibException::getstr(), protlib::Thread::inc_running_threads(), protlib::Thread::inc_started_threads(), protlib::Thread::lock(), protlib::Thread::main_loop(), protlib::ThreadParam::name, protlib::Thread::state, protlib::Thread::STATE_ABORT, protlib::Thread::STATE_INIT, protlib::Thread::STATE_RUN, protlib::Thread::STATE_STOP, protlib::Thread::tparam, and protlib::Thread::unlock().

template<class T , class TParam >
void protlib::ThreadStarter< T, TParam >::stop_processing (  )  [inline, inherited]

void protlib::Thread::stop_processing ( bool  do_lock = true  )  [inherited]

Called when the thread is asked to stop processing.

The thread object may do some cleanup or work on until it has completed a task.

Parameters:
do_lock if true the thread mutex is used

Definition at line 183 of file threads.cpp.

References protlib::Thread::lock(), protlib::Thread::signal_cond(), protlib::Thread::state, protlib::Thread::STATE_RUN, protlib::Thread::STATE_STOP, and protlib::Thread::unlock().

protlib::Thread::Thread ( const ThreadParam p,
bool  create_queue = true,
bool  exp_allow = true 
) [inherited]

Constructor.

Parameters:
p thread parameters
create_queue if true, create one internal queue
exp_allow if true, allow reception of expedited messages on the queue

Definition at line 75 of file threads.cpp.

References protlib::Thread::cond, protlib::Thread::mutex, PTHREAD_MUTEX_ERRORCHECK, PTHREAD_MUTEX_NORMAL, and pthread_mutexattr_settype.

template<class T >
void* protlib::thread_starter ( void *  thread_object  )  [inline]

Call the method start_processing of a Thread instance.

Parameters:
thread_object a Thread instance

Definition at line 70 of file threads.h.

protlib::ThreadParam::ThreadParam ( uint32  wait,
const char *  n,
uint32  minc = 1,
uint32  maxc = (uint32)-1 
) [inherited]

Initializes a ThreadParam object with a default wait time and a a thread (group) name string.

Parameters:
wait wait time between stopping and aborting the thread inside ThreadStarter::abort_processing
n name of the threads.
minc minimal number of threads
maxc maximal number of threads

Definition at line 59 of file threads.cpp.

template<class T , class TParam>
protlib::ThreadStarter< T, TParam >::ThreadStarter ( uint32  count,
const TParam &  param 
) [inline, inherited]

Constructor.

Parameters:
count the number of threads to start
param thread parameters

Definition at line 307 of file threads.h.

References protlib::ThreadStarter< T, TParam >::pthreads.

void protlib::Thread::unlock (  )  [inline, inherited]

int protlib::Thread::wait_cond ( const struct timespec &  ts  )  [inline, inherited]

Parameters:
ts absolute time
Returns:
0, ETIMEDOUT or EINTR.

Definition at line 238 of file threads.h.

References protlib::Thread::cond, and protlib::Thread::mutex.

void protlib::Thread::wait_cond (  )  [inline, inherited]

Definition at line 229 of file threads.h.

References protlib::Thread::cond, and protlib::Thread::mutex.

Referenced by protlib::TPoverTCP::get_connection_to().

int protlib::Thread::wait_cond ( int32  sec,
int32  nsec = 0 
) [inherited]

Wait for the condition.

Parameters:
sec relative time (seconds)
nsec relative time (nanoseconds)
Returns:
0, ETIMEDOUT or EINTR.

Definition at line 242 of file threads.cpp.

References protlib::Thread::cond, and protlib::Thread::mutex.

template<class T , class TParam >
void protlib::ThreadStarter< T, TParam >::wait_until_stopped (  )  [inline, inherited]

Wait until all threads have stopped running.

Threads that haven't been running yet (state IDLE) are not considered as stopped!

Definition at line 470 of file threads.h.

References DLog, ariba::utility::Helper::sleep(), protlib::Thread::STATE_INIT, protlib::ThreadStarter< T, TParam >::thread_object, and protlib::ThreadStarter< T, TParam >::thread_param.

Referenced by ariba::transport::tcpip::stop().

protlib::Thread::~Thread (  )  [virtual, inherited]

Destructor.

Currently throws an exception if there are still running threads.

Definition at line 101 of file threads.cpp.

References protlib::Thread::cond, protlib::ThreadError::ERROR_STILL_RUNNING, protlib::Thread::fq, protlib::Thread::get_running_threads(), and protlib::Thread::mutex.

template<class T , class TParam >
protlib::ThreadStarter< T, TParam >::~ThreadStarter (  )  [inline, inherited]


Variable Documentation

uint32 protlib::ThreadParam::default_sleep_time = 5 [static, inherited]

This is the default sleep time and can be used as default value in constructors.

Definition at line 92 of file threads.h.

const char *const protlib::ThreadError::errstr [static, protected, inherited]

Initial value:

 {
        "Cannot create POSIX Threads.",
        "Thread is running.",
        "Thread is going to stop.",
        "Thread is aborting.",
        "Still running threads left.",
        "ThreadStarter is not initialized correctly."
        "Internal ThreadStarter or Thread error.",
        "Thread has not been started yet."
}

Definition at line 122 of file threads.h.

Referenced by protlib::ThreadError::getstr().


Generated on Fri Aug 14 23:38:35 2009 for Ariba by  doxygen 1.5.8