CPN
Computational Process Networks
Public Member Functions | Private Attributes | List of all members
Sync::StatusHandler< Status_t > Class Template Reference

#include <ReentrantLock.h>

+ Collaboration diagram for Sync::StatusHandler< Status_t >:

Public Member Functions

 StatusHandler (Status_t initialStatus)
 
 ~StatusHandler ()
 
void Post (Status_t newStatus)
 
Status_t Get () const
 
bool CompareAndPost (Status_t oldStatus, Status_t newStatus)
 
template<class Comparator >
bool CompareAndPost (Status_t oldStatus, Status_t newStatus, Comparator comp)
 
Status_t CompareAndWait (Status_t oldStatus) const
 
template<class Comparator >
Status_t CompareAndWait (Status_t oldStatus, Comparator comp) const
 
Status_t ComparePostAndWait (Status_t oldStatus, Status_t newStatus)
 
template<class Comparator >
Status_t ComparePostAndWait (Status_t oldStatus, Status_t newStatus, Comparator comp)
 
void CompareWaitAndPost (Status_t theStatus, Status_t newStatus)
 

Private Attributes

Status_t status
 
pthread_mutex_t lock
 
pthread_cond_t cond
 

Detailed Description

template<class Status_t>
class Sync::StatusHandler< Status_t >

The StatusHandler template is meant to be used to simplify having a status variable in a multi threaded application. The StatusHandler can be used much like an atomic variable or a blocking queue with size 1.

Future improvements to this might be to use actual atomic compare and set primatives for these operations rather than a lock and condition.

The ReentrantLock passed in the constructor must be valid for the lifetime of this object and you must hold the lock when calling any of the wait functions.

Definition at line 39 of file ReentrantLock.h.

Constructor & Destructor Documentation

template<class Status_t>
Sync::StatusHandler< Status_t >::StatusHandler ( Status_t  initialStatus)
inline

Definition at line 60 of file StatusHandler.h.

61  : status(initialStatus) {
62  ENSURE(!pthread_mutex_init(&lock, 0));
63  ENSURE(!pthread_cond_init(&cond, 0));
64  }
pthread_cond_t cond
#define ENSURE(exp,...)
pthread_mutex_t lock
template<class Status_t>
Sync::StatusHandler< Status_t >::~StatusHandler ( )
inline

Definition at line 66 of file StatusHandler.h.

66  {
67  ENSURE_ABORT(!pthread_cond_destroy(&cond));
68  ENSURE_ABORT(!pthread_mutex_destroy(&lock));
69  }
#define ENSURE_ABORT(exp,...)
pthread_cond_t cond
pthread_mutex_t lock

Member Function Documentation

template<class Status_t>
bool Sync::StatusHandler< Status_t >::CompareAndPost ( Status_t  oldStatus,
Status_t  newStatus 
)
inline

Post a change in status only if the current status is oldStatus.

Parameters
oldStatusthe status to compare to
newStatusthe new status to change to
Returns
true if the status changed

Definition at line 96 of file StatusHandler.h.

Referenced by CPN::Kernel::EntryPoint(), and CPN::Kernel::NotifyTerminate().

96  {
97  Internal::ScopeMutex l(lock);
98  if (oldStatus == status) {
99  status = newStatus;
100  pthread_cond_broadcast(&cond);
101  return true;
102  }
103  return false;
104  }
pthread_cond_t cond
pthread_mutex_t lock

+ Here is the caller graph for this function:

template<class Status_t>
template<class Comparator >
bool Sync::StatusHandler< Status_t >::CompareAndPost ( Status_t  oldStatus,
Status_t  newStatus,
Comparator  comp 
)
inline

Definition at line 107 of file StatusHandler.h.

107  {
108  Internal::ScopeMutex l(lock);
109  if (comp(oldStatus, status)) {
110  status = newStatus;
111  pthread_cond_broadcast(&cond);
112  return true;
113  }
114  return false;
115  }
pthread_cond_t cond
pthread_mutex_t lock
template<class Status_t>
Status_t Sync::StatusHandler< Status_t >::CompareAndWait ( Status_t  oldStatus) const
inline

This function waits until the status is different than oldStatus.

Parameters
oldStatusthe status to compare to the current status
Returns
the new status

Definition at line 124 of file StatusHandler.h.

Referenced by CPN::Kernel::Kernel(), and CPN::Kernel::Wait().

124  {
125  Internal::ScopeMutex l(lock);
126  while (oldStatus == status) { pthread_cond_wait(&cond, &lock); }
127  return status;
128  }
pthread_cond_t cond
pthread_mutex_t lock

+ Here is the caller graph for this function:

template<class Status_t>
template<class Comparator >
Status_t Sync::StatusHandler< Status_t >::CompareAndWait ( Status_t  oldStatus,
Comparator  comp 
) const
inline

Definition at line 131 of file StatusHandler.h.

131  {
132  Internal::ScopeMutex l(lock);
133  while (comp(oldStatus, status)) { pthread_cond_wait(&cond, &lock); }
134  return status;
135  }
pthread_cond_t cond
pthread_mutex_t lock
template<class Status_t>
Status_t Sync::StatusHandler< Status_t >::ComparePostAndWait ( Status_t  oldStatus,
Status_t  newStatus 
)
inline

This function compares status to oldStatus and if the same sets the status to newStatus. Then waits until the status is different than newStatus.

Parameters
oldStatusthe status to compare
newStatusthe status to set the status to
Returns
the status as of the return of this function

Definition at line 145 of file StatusHandler.h.

145  {
146  Internal::ScopeMutex l(lock);
147  if (oldStatus == status) {
148  status = newStatus;
149  pthread_cond_broadcast(&cond);
150  while (status == newStatus) { pthread_cond_wait(&cond, &lock); }
151  }
152  return status;
153  }
pthread_cond_t cond
pthread_mutex_t lock
template<class Status_t>
template<class Comparator >
Status_t Sync::StatusHandler< Status_t >::ComparePostAndWait ( Status_t  oldStatus,
Status_t  newStatus,
Comparator  comp 
)
inline

Definition at line 156 of file StatusHandler.h.

156  {
157  Internal::ScopeMutex l(lock);
158  if (comp(oldStatus, status)) {
159  status = newStatus;
160  pthread_cond_broadcast(&cond);
161  while (newStatus == status) { pthread_cond_wait(&cond, &lock); }
162  }
163  return status;
164  }
pthread_cond_t cond
pthread_mutex_t lock
template<class Status_t>
void Sync::StatusHandler< Status_t >::CompareWaitAndPost ( Status_t  theStatus,
Status_t  newStatus 
)
inline

Wait for the status to become theStatus then set to newStatus.

Definition at line 169 of file StatusHandler.h.

169  {
170  Internal::ScopeMutex l(lock);
171  while (theStatus != status) { pthread_cond_wait(&cond, &lock); }
172  status = newStatus;
173  pthread_cond_broadcast(&cond);
174  }
pthread_cond_t cond
pthread_mutex_t lock
template<class Status_t>
Status_t Sync::StatusHandler< Status_t >::Get ( void  ) const
inline
Returns
the current status

Definition at line 84 of file StatusHandler.h.

Referenced by CPN::Kernel::EntryPoint(), CPN::Kernel::InternalCreateNode(), CPN::Kernel::LogState(), CPN::Kernel::NodeTerminated(), and CPN::Kernel::Wait().

84  {
85  Internal::ScopeMutex l(lock);
86  return status;
87  }
pthread_mutex_t lock

+ Here is the caller graph for this function:

template<class Status_t>
void Sync::StatusHandler< Status_t >::Post ( Status_t  newStatus)
inline

Post a change in status.

Parameters
newStatusthe new status

Definition at line 75 of file StatusHandler.h.

Referenced by CPN::Kernel::EntryPoint().

75  {
76  Internal::ScopeMutex l(lock);
77  status = newStatus;
78  pthread_cond_broadcast(&cond);
79  }
pthread_cond_t cond
pthread_mutex_t lock

+ Here is the caller graph for this function:

Member Data Documentation

template<class Status_t>
pthread_cond_t Sync::StatusHandler< Status_t >::cond
mutableprivate
template<class Status_t>
pthread_mutex_t Sync::StatusHandler< Status_t >::lock
mutableprivate
template<class Status_t>
Status_t Sync::StatusHandler< Status_t >::status
private

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