Classes | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Protected Attributes

ibrcommon::Thread Class Reference

#include <Thread.h>

Inherited by ibrcommon::DetachedThread [protected], and ibrcommon::JoinableThread [protected].

Collaboration diagram for ibrcommon::Thread:
Collaboration graph
[legend]

List of all members.

Classes

class  CancelProtector

Public Member Functions

virtual ~Thread ()=0
virtual void setup (void)
virtual void run (void)=0
virtual void finally (void)
void detach (void)
bool operator== (const ibrcommon::Thread &other)
void interrupt ()

Static Public Member Functions

static void yield (void)
static void sleep (size_t timeout)
static void exit (void)
static void concurrency (int level)

Protected Types

enum  THREAD_STATE {
  THREAD_INITIALIZED, THREAD_PREPARE, THREAD_RUNNING, THREAD_CANCELLED,
  THREAD_FINALIZED, THREAD_JOINED, THREAD_ERROR
}

Protected Member Functions

 Thread (size_t stack=DEFAULT_STACKSIZE, bool delete_on_exit=false)
int kill (int sig)
void cancellation_point ()
void enable_interruption ()
void disable_interruption ()
void cancel () throw (ThreadException)
virtual bool __cancellation ()

Static Protected Member Functions

static bool equal (pthread_t thread1, pthread_t thread2)
static void * exec_thread (void *obj)
static void finalize_thread (void *obj)

Protected Attributes

enum
ibrcommon::Thread::THREAD_STATE 
_state
ibrcommon::Conditional _state_lock
pthread_t tid
size_t stack
int priority
pthread_attr_t attr

Detailed Description

An abstract class for defining classes that operate as a thread. A derived thread class has a run method that is invoked with the newly created thread context, and can use the derived object to store all member data that needs to be associated with that context. This means the derived object can safely hold thread-specific data that is managed with the life of the object, rather than having to use the clumsy thread-specific data management and access functions found in thread support libraries.

Author:
David Sugar <dyfet@gnutelephony.org>

Definition at line 41 of file Thread.h.


Member Enumeration Documentation

Enumerator:
THREAD_INITIALIZED 
THREAD_PREPARE 
THREAD_RUNNING 
THREAD_CANCELLED 
THREAD_FINALIZED 
THREAD_JOINED 
THREAD_ERROR 

Definition at line 44 of file Thread.h.


Constructor & Destructor Documentation

ibrcommon::Thread::Thread ( size_t  stack = DEFAULT_STACKSIZE,
bool  delete_on_exit = false 
) [protected]

Create a thread object that will have a preset stack size. If 0 is used, then the stack size is os defined/default.

Parameters:
stack size to use or 0 for default.

Definition at line 102 of file Thread.cpp.

References attr.

ibrcommon::Thread::~Thread (  )  [pure virtual]

Destroy thread object, thread-specific data, and execution context.

Definition at line 145 of file Thread.cpp.

References attr.


Member Function Documentation

virtual bool ibrcommon::Thread::__cancellation (  )  [inline, protected, virtual]
void ibrcommon::Thread::cancel (  )  throw (ThreadException) [protected]

Cancel the running thread context.

Definition at line 183 of file Thread.cpp.

References __cancellation(), _state, _state_lock, kill(), ibrcommon::Conditional::signal(), THREAD_PREPARE, THREAD_RUNNING, and tid.

Referenced by ibrcommon::DetachedThread::stop(), and ibrcommon::JoinableThread::stop().

Here is the call graph for this function:

void ibrcommon::Thread::cancellation_point (  )  [protected]

this method enables the thread to terminate at this position if a cancellation request is pending

Definition at line 119 of file Thread.cpp.

void ibrcommon::Thread::concurrency ( int  level  )  [static]

Set concurrency level of process. This is essentially a portable wrapper for pthread_setconcurrency.

Definition at line 124 of file Thread.cpp.

void ibrcommon::Thread::detach ( void   ) 

detach this thread

Definition at line 150 of file Thread.cpp.

References tid.

void ibrcommon::Thread::disable_interruption (  )  [protected]

disable interruption.

Definition at line 178 of file Thread.cpp.

void ibrcommon::Thread::enable_interruption (  )  [protected]

enable interruption for this thread. used with pselect it is possible to break the blocking state of pselect by calling interrupt()

Definition at line 171 of file Thread.cpp.

Referenced by dtn::net::TCPConvergenceLayer::componentRun(), and ibrcommon::NetLinkManager::run().

bool ibrcommon::Thread::equal ( pthread_t  thread1,
pthread_t  thread2 
) [static, protected]

Determine if two thread identifiers refer to the same thread.

Parameters:
thread1 to test.
thread2 to test.
Returns:
true if both are the same context.

Definition at line 203 of file Thread.cpp.

Referenced by ibrcommon::JoinableThread::join(), and operator==().

void * ibrcommon::Thread::exec_thread ( void *  obj  )  [static, protected]

static execute thread method

Definition at line 53 of file Thread.cpp.

References _state, _state_lock, exit(), finalize_thread(), IBRCOMMON_LOGGER_DEBUG, setup(), ibrcommon::Conditional::signal(), and THREAD_CANCELLED.

Referenced by ibrcommon::DetachedThread::start(), and ibrcommon::JoinableThread::start().

Here is the call graph for this function:

void ibrcommon::Thread::exit ( void   )  [static]

Exit the thread context.

Definition at line 155 of file Thread.cpp.

Referenced by exec_thread(), and ibrcommon::JoinableThread::join().

void ibrcommon::Thread::finalize_thread ( void *  obj  )  [static, protected]

this method finalizes a thread. If the thread is self-deleting, the object will be invalid after exection.

Definition at line 25 of file Thread.cpp.

References _state, _state_lock, finally(), and ibrcommon::Conditional::signal().

Referenced by exec_thread().

Here is the call graph for this function:

virtual void ibrcommon::Thread::finally ( void   )  [inline, virtual]

This method is called when the run() method finishes.

Reimplemented in dtn::daemon::ClientHandler, and dtn::net::TCPConnection.

Definition at line 107 of file Thread.h.

Referenced by finalize_thread().

void ibrcommon::Thread::interrupt (  ) 

Interrupt the thread execution. Actually, this method sends a SIGINT to the thread.

Definition at line 166 of file Thread.cpp.

References kill().

Referenced by ibrcommon::NetLinkManager::__cancellation(), dtn::net::TCPConvergenceLayer::__cancellation(), dtn::net::IPNDAgent::__cancellation(), and dtn::net::TCPConvergenceLayer::componentDown().

Here is the call graph for this function:

int ibrcommon::Thread::kill ( int  sig  )  [protected]

sends a signal to this thread

Parameters:
sig 
Returns:

Definition at line 160 of file Thread.cpp.

References tid.

Referenced by cancel(), and interrupt().

bool ibrcommon::Thread::operator== ( const ibrcommon::Thread other  )  [inline]

Determine if two thread identifiers refer to the same thread.

Parameters:
other The thread to compare.
Returns:
True, if both threads are the same.

Definition at line 130 of file Thread.h.

References equal(), and tid.

Here is the call graph for this function:

virtual void ibrcommon::Thread::run ( void   )  [pure virtual]
virtual void ibrcommon::Thread::setup ( void   )  [inline, virtual]

This method is called before the run.

Reimplemented in dtn::net::TCPConnection.

Definition at line 97 of file Thread.h.

Referenced by exec_thread().

void ibrcommon::Thread::sleep ( size_t  timeout  )  [static]

Sleep current thread for a specified time period.

Parameters:
timeout to sleep for in milliseconds.

Definition at line 131 of file Thread.cpp.

void ibrcommon::Thread::yield ( void   )  [static]

Member Data Documentation

pthread_attr_t ibrcommon::Thread::attr [protected]

Definition at line 67 of file Thread.h.

Referenced by Thread(), and ~Thread().

int ibrcommon::Thread::priority [protected]

Definition at line 64 of file Thread.h.

size_t ibrcommon::Thread::stack [protected]

Definition at line 61 of file Thread.h.

pthread_t ibrcommon::Thread::tid [protected]

Definition at line 58 of file Thread.h.

Referenced by cancel(), detach(), ibrcommon::JoinableThread::join(), kill(), and operator==().


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