CPN
Computational Process Networks
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Types | Static Private Member Functions | Private Attributes | List of all members
Pthread Class Referenceabstract

#include <PthreadLib.h>

+ Inheritance diagram for Pthread:
+ Collaboration diagram for Pthread:

Public Member Functions

 Pthread (void)
 
 Pthread (const PthreadAttr &attr)
 
virtual ~Pthread (void)
 
void Start (void)
 
int Running (void)
 
int Done (void)
 
void * Join (void)
 
 operator pthread_t * (void)
 
 operator pthread_t (void)
 
int operator== (pthread_t t2)
 
int Detach (void)
 
int Cancel (void)
 
int SendSignal (int sig)
 
int Kill (int sig)
 
int Error (void)
 
void Clear (void)
 

Static Public Member Functions

static pthread_t Self (void)
 
static void TestCancel (void)
 
static void Exit (void *value)
 
static int SendSignal (pthread_t thd, int sig)
 
static int Kill (pthread_t thd, int sig)
 
static int GetSignalMask (sigset_t *oldSet)
 
static int SetSignalMask (const sigset_t *set, sigset_t *oldSet=0)
 
static int BlockSignals (const sigset_t *set, sigset_t *oldSet=0)
 
static int UnblockSignals (const sigset_t *set, sigset_t *oldSet=0)
 
static int CancelEnable (void)
 
static int CancelDisable (void)
 
static int CancelDeferred (void)
 
static int CancelAsynchronous (void)
 

Protected Member Functions

virtual void * EntryPoint (void)=0
 
void TrapError (int result)
 

Protected Attributes

pthread_t theThread
 
int error
 

Private Types

enum  PthreadState {
  uninitialized = 0, created, started, running,
  done, joined, canceled
}
 

Static Private Member Functions

static void * PthreadEntryPoint (void *arg)
 
static void PthreadCleanup (void *arg)
 

Private Attributes

PthreadMutex mutex
 
PthreadCondition cond
 
void * returnResult
 
bool inJoin
 
PthreadState state
 

Detailed Description

Definition at line 56 of file PthreadLib.h.

Member Enumeration Documentation

enum Pthread::PthreadState
private
Enumerator
uninitialized 
created 
started 
running 
done 
joined 
canceled 

Definition at line 89 of file PthreadLib.h.

Constructor & Destructor Documentation

Pthread::Pthread ( void  )

Definition at line 32 of file PthreadLib.cc.

References PthreadCondition::Broadcast(), cond, created, inJoin, mutex, PthreadEntryPoint(), returnResult, state, PthreadBase::theThread, PthreadErrorHandler::TrapError(), and uninitialized.

34 {
36  returnResult = 0;
37  inJoin = false;
39  TrapError( pthread_create( &theThread, 0, PthreadEntryPoint, this) );
40  state = created;
41  cond.Broadcast();
42 }
static void * PthreadEntryPoint(void *arg)
Definition: PthreadLib.cc:131
PthreadCondition & Broadcast(void)
PthreadCondition cond
Definition: PthreadLib.h:85
PthreadState state
Definition: PthreadLib.h:90
PthreadMutex mutex
Definition: PthreadLib.h:84
void * returnResult
Definition: PthreadLib.h:86
bool inJoin
Definition: PthreadLib.h:87
void TrapError(int result)
pthread_t theThread
Definition: PthreadBase.h:126

+ Here is the call graph for this function:

Pthread::Pthread ( const PthreadAttr attr)

Definition at line 46 of file PthreadLib.cc.

References PthreadCondition::Broadcast(), cond, created, inJoin, mutex, PthreadEntryPoint(), returnResult, state, PthreadBase::theThread, PthreadErrorHandler::TrapError(), and uninitialized.

48 {
50  returnResult = 0;
51  inJoin = false;
53 
54  // It should simply be:
55 // TrapError( pthread_create(&theThread, attr, PthreadEntryPoint, this) );
56 
57  // This is necessary because
58  // SGI's pthread_create is not const correct!!!
59  pthread_attr_t* myAttr = (PthreadAttr&)attr;
60  TrapError( pthread_create(&theThread, myAttr, PthreadEntryPoint, this) );
61  state = created;
62  cond.Broadcast();
63 }
static void * PthreadEntryPoint(void *arg)
Definition: PthreadLib.cc:131
PthreadCondition & Broadcast(void)
PthreadCondition cond
Definition: PthreadLib.h:85
PthreadState state
Definition: PthreadLib.h:90
PthreadMutex mutex
Definition: PthreadLib.h:84
void * returnResult
Definition: PthreadLib.h:86
bool inJoin
Definition: PthreadLib.h:87
void TrapError(int result)
pthread_t theThread
Definition: PthreadBase.h:126

+ Here is the call graph for this function:

Pthread::~Pthread ( void  )
virtual

Definition at line 67 of file PthreadLib.cc.

References PthreadCondition::Broadcast(), canceled, cond, created, Join(), mutex, running, Start(), started, state, and uninitialized.

69 {
70  try {
71  // Force the thread to terminate if it has not already done so.
72  // Is it safe to do this to a thread that has already terminated?
73 
74  bool need_start = false;
75  {
77  switch (state) {
78  case uninitialized:
79  // should be impossible
80  return;
81  case created:
82  need_start = true;
83  case started:
84  case running:
85  state = canceled;
86  cond.Broadcast();
87  default:
88  break;
89  }
90  }
91 
92  if (need_start) { Start(); }
93  // Now wait.
94  Join();
95  } catch (...) {
96  std::terminate();
97  }
98 }
PthreadCondition & Broadcast(void)
PthreadCondition cond
Definition: PthreadLib.h:85
PthreadState state
Definition: PthreadLib.h:90
PthreadMutex mutex
Definition: PthreadLib.h:84
void * Join(void)
Definition: PthreadLib.cc:108
void Start(void)
Definition: PthreadLib.cc:100

+ Here is the call graph for this function:

Member Function Documentation

static int PthreadBase::BlockSignals ( const sigset_t *  set,
sigset_t *  oldSet = 0 
)
inlinestaticinherited

Definition at line 77 of file PthreadBase.h.

78  { return pthread_sigmask(SIG_BLOCK, set, oldSet); }
int PthreadBase::Cancel ( void  )
inlineinherited

Definition at line 59 of file PthreadBase.h.

References PthreadBase::theThread.

59 { return pthread_cancel(theThread); }
pthread_t theThread
Definition: PthreadBase.h:126
int PthreadBase::CancelAsynchronous ( void  )
staticinherited

Definition at line 105 of file PthreadBase.cc.

107 {
108  int oldState;
109  pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldState);
110  return oldState;
111 }
int PthreadBase::CancelDeferred ( void  )
staticinherited

Definition at line 95 of file PthreadBase.cc.

97 {
98  int oldState;
99  pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldState);
100  return oldState;
101 }
int PthreadBase::CancelDisable ( void  )
staticinherited

Definition at line 86 of file PthreadBase.cc.

88 {
89  int oldState;
90  pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldState);
91  return oldState;
92 }
int PthreadBase::CancelEnable ( void  )
staticinherited

Definition at line 76 of file PthreadBase.cc.

78 {
79  int oldState;
80  pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldState);
81  return oldState;
82 }
void PthreadErrorHandler::Clear ( void  )
inlineinherited

Definition at line 40 of file PthreadErrorHandler.h.

References PthreadErrorHandler::error.

40 { error = 0; }
int PthreadBase::Detach ( void  )
inlineinherited

Definition at line 58 of file PthreadBase.h.

References PthreadBase::theThread.

58 { return pthread_detach(theThread); }
pthread_t theThread
Definition: PthreadBase.h:126
int Pthread::Done ( void  )
inline
Returns
true if Joining would not block.

Definition at line 72 of file PthreadLib.h.

References done, joined, mutex, and state.

72  {
74  return state == done || state == joined;
75  }
PthreadState state
Definition: PthreadLib.h:90
PthreadMutex mutex
Definition: PthreadLib.h:84
virtual void* Pthread::EntryPoint ( void  )
protectedpure virtual

Implemented in PthreadFunctionalTemplate< T >, and PthreadFunctional.

Referenced by PthreadEntryPoint().

+ Here is the caller graph for this function:

int PthreadErrorHandler::Error ( void  )
inlineinherited

Definition at line 39 of file PthreadErrorHandler.h.

References PthreadErrorHandler::error.

39 { return error; }
static void PthreadBase::Exit ( void *  value)
inlinestaticinherited

Definition at line 64 of file PthreadBase.h.

64 { pthread_exit(value); }
static int PthreadBase::GetSignalMask ( sigset_t *  oldSet)
inlinestaticinherited

Definition at line 73 of file PthreadBase.h.

74  { return pthread_sigmask(SIG_SETMASK, 0, oldSet); }
void * Pthread::Join ( void  )

Definition at line 108 of file PthreadLib.cc.

References PthreadCondition::Broadcast(), cond, inJoin, joined, mutex, returnResult, state, PthreadBase::theThread, PthreadErrorHandler::TrapError(), and PthreadCondition::Wait().

Referenced by ~Pthread().

108  {
109  void *result = 0;
110  {
112  if (inJoin || state == joined) {
113  while (state != joined) {
114  cond.Wait(mutex);
115  }
116  return returnResult;
117  }
118  inJoin = true;
119  }
120  TrapError( pthread_join(theThread, &result) );
121  {
123  returnResult = result;
124  state = joined;
125  cond.Broadcast();
126  return returnResult;
127  }
128 }
PthreadCondition & Wait(PthreadMutex &mutex)
PthreadCondition & Broadcast(void)
PthreadCondition cond
Definition: PthreadLib.h:85
PthreadState state
Definition: PthreadLib.h:90
PthreadMutex mutex
Definition: PthreadLib.h:84
void * returnResult
Definition: PthreadLib.h:86
bool inJoin
Definition: PthreadLib.h:87
void TrapError(int result)
pthread_t theThread
Definition: PthreadBase.h:126

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int PthreadBase::Kill ( int  sig)
inlineinherited

Definition at line 68 of file PthreadBase.h.

References PthreadBase::theThread.

68 { return pthread_kill(theThread, sig); }
pthread_t theThread
Definition: PthreadBase.h:126
static int PthreadBase::Kill ( pthread_t  thd,
int  sig 
)
inlinestaticinherited

Definition at line 71 of file PthreadBase.h.

71 { return pthread_kill(thd, sig); }
PthreadBase::operator pthread_t ( void  )
inlineinherited

Definition at line 52 of file PthreadBase.h.

References PthreadBase::theThread.

52 { return theThread; }
pthread_t theThread
Definition: PthreadBase.h:126
PthreadBase::operator pthread_t * ( void  )
inlineinherited

Definition at line 51 of file PthreadBase.h.

References PthreadBase::theThread.

51 { return &theThread; }
pthread_t theThread
Definition: PthreadBase.h:126
int PthreadBase::operator== ( pthread_t  t2)
inlineinherited

Definition at line 54 of file PthreadBase.h.

References PthreadBase::theThread.

54 { return pthread_equal(theThread, t2); }
pthread_t theThread
Definition: PthreadBase.h:126
void Pthread::PthreadCleanup ( void *  arg)
staticprivate

Definition at line 154 of file PthreadLib.cc.

References PthreadCondition::Broadcast(), cond, done, mutex, and state.

Referenced by PthreadEntryPoint().

156 {
157  Pthread* ptr = (Pthread*) arg;
159 // ptr->Cleanup(); // the sub-classes have already been destructed
160  ptr->state = done;
161  ptr->cond.Broadcast();
162 }
PthreadCondition & Broadcast(void)
PthreadCondition cond
Definition: PthreadLib.h:85
PthreadState state
Definition: PthreadLib.h:90
PthreadMutex mutex
Definition: PthreadLib.h:84

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void * Pthread::PthreadEntryPoint ( void *  arg)
staticprivate

Definition at line 131 of file PthreadLib.cc.

References PthreadCondition::Broadcast(), cond, created, EntryPoint(), mutex, PthreadCleanup(), running, started, state, PthreadBase::TestCancel(), and PthreadCondition::Wait().

Referenced by Pthread().

133 {
134  void* result = 0;
135  Pthread* ptr = (Pthread*) arg;
136  {
138  while (ptr->state == created) {
139  ptr->cond.Wait(ptr->mutex);
140  }
141  if (ptr->state != started) return 0;
142  ptr->state = running;
143  ptr->cond.Broadcast();
144  }
145  TestCancel();
146  pthread_cleanup_push( PthreadCleanup, ptr);
147  result = ptr->EntryPoint();
148  pthread_cleanup_pop(1);
149  return result;
150 }
PthreadCondition & Wait(PthreadMutex &mutex)
PthreadCondition & Broadcast(void)
static void PthreadCleanup(void *arg)
Definition: PthreadLib.cc:154
PthreadCondition cond
Definition: PthreadLib.h:85
PthreadState state
Definition: PthreadLib.h:90
static void TestCancel(void)
Definition: PthreadBase.h:63
PthreadMutex mutex
Definition: PthreadLib.h:84
virtual void * EntryPoint(void)=0

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int Pthread::Running ( void  )
inline

Definition at line 64 of file PthreadLib.h.

References mutex, running, and state.

64  {
66  return state == running;
67  }
PthreadState state
Definition: PthreadLib.h:90
PthreadMutex mutex
Definition: PthreadLib.h:84
static pthread_t PthreadBase::Self ( void  )
inlinestaticinherited

Definition at line 61 of file PthreadBase.h.

Referenced by PthreadBase::PthreadBase().

61 { return pthread_self(); }

+ Here is the caller graph for this function:

int PthreadBase::SendSignal ( int  sig)
inlineinherited

Definition at line 67 of file PthreadBase.h.

References PthreadBase::theThread.

67 { return pthread_kill(theThread, sig); }
pthread_t theThread
Definition: PthreadBase.h:126
static int PthreadBase::SendSignal ( pthread_t  thd,
int  sig 
)
inlinestaticinherited

Definition at line 70 of file PthreadBase.h.

70 { return pthread_kill(thd, sig); }
static int PthreadBase::SetSignalMask ( const sigset_t *  set,
sigset_t *  oldSet = 0 
)
inlinestaticinherited

Definition at line 75 of file PthreadBase.h.

76  { return pthread_sigmask(SIG_SETMASK, set, oldSet); }
void Pthread::Start ( void  )

Definition at line 100 of file PthreadLib.cc.

References PthreadCondition::Broadcast(), cond, created, mutex, started, and state.

Referenced by ~Pthread().

100  {
102  if (state == created) {
103  state = started;
104  cond.Broadcast();
105  }
106 }
PthreadCondition & Broadcast(void)
PthreadCondition cond
Definition: PthreadLib.h:85
PthreadState state
Definition: PthreadLib.h:90
PthreadMutex mutex
Definition: PthreadLib.h:84

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void PthreadBase::TestCancel ( void  )
inlinestaticinherited

Definition at line 63 of file PthreadBase.h.

Referenced by PthreadEntryPoint().

63 { pthread_testcancel(); }

+ Here is the caller graph for this function:

void PthreadErrorHandler::TrapError ( int  result)
inlineprotectedinherited
static int PthreadBase::UnblockSignals ( const sigset_t *  set,
sigset_t *  oldSet = 0 
)
inlinestaticinherited

Definition at line 79 of file PthreadBase.h.

80  { return pthread_sigmask(SIG_UNBLOCK, set, oldSet); }

Member Data Documentation

PthreadCondition Pthread::cond
private

Definition at line 85 of file PthreadLib.h.

Referenced by Join(), Pthread(), PthreadCleanup(), PthreadEntryPoint(), Start(), and ~Pthread().

int PthreadErrorHandler::error
protectedinherited
bool Pthread::inJoin
private

Definition at line 87 of file PthreadLib.h.

Referenced by Join(), and Pthread().

PthreadMutex Pthread::mutex
private

Definition at line 84 of file PthreadLib.h.

Referenced by Done(), Join(), Pthread(), PthreadCleanup(), PthreadEntryPoint(), Running(), Start(), and ~Pthread().

void* Pthread::returnResult
private

Definition at line 86 of file PthreadLib.h.

Referenced by Join(), and Pthread().

PthreadState Pthread::state
private

Definition at line 90 of file PthreadLib.h.

Referenced by Done(), Join(), Pthread(), PthreadCleanup(), PthreadEntryPoint(), Running(), Start(), and ~Pthread().

pthread_t PthreadBase::theThread
protectedinherited

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