PTLib
Version 2.18.8
|
This class defines a thread synchronisation object. More...
#include <semaphor.h>
Public Member Functions | |
Construction | |
PSemaphore (unsigned initial=0, unsigned maximum=UINT_MAX) | |
Create a new semaphore with maximum count and initial value specified. More... | |
PSemaphore (const PString &name, unsigned initial=0, unsigned maximum=UINT_MAX) | |
Create a new system global semaphore with maximum count and initial value specified. More... | |
PSemaphore (const PSemaphore &sem) | |
Create a new semaphore with the same initial and maximum values as the original. More... | |
~PSemaphore () | |
Destroy the semaphore. More... | |
Operations | |
virtual void | Wait () |
If the semaphore count is > 0, decrement the semaphore and return. More... | |
virtual PBoolean | Wait (const PTimeInterval &timeout) |
If the semaphore count is > 0, decrement the semaphore and return. More... | |
virtual void | Signal () |
If there are waiting (blocked) threads then unblock the first one that was blocked. More... | |
virtual void | Reset (unsigned initial=0, unsigned maximum=UINT_MAX) |
Reset the semaphore to the specified inital and maximum values. More... | |
unsigned | GetInitial () const |
Get the initial value semaphore was creted with. More... | |
unsigned | GetMaximum () const |
Get the initial value semaphore was creted with. More... | |
![]() | |
PSync () | |
virtual bool | InstrumentedWait (const PTimeInterval &timeout, const PDebugLocation &) |
As for Wait() but with location of call for instrumentation. Mostly used internally. More... | |
virtual void | InstrumentedSignal (const PDebugLocation &) |
As for Signal() but with location of call for instrumentation. Mostly used internally. More... | |
![]() | |
__inline unsigned | GetTraceContextIdentifier () const |
Get PTRACE context identifier. More... | |
__inline void | SetTraceContextIdentifier (unsigned id) |
__inline void | SetTraceContextIdentifier (const PObject &obj) |
__inline void | SetTraceContextIdentifier (const PObject *obj) |
__inline void | CopyTraceContextIdentifier (PObject &obj) const |
__inline void | CopyTraceContextIdentifier (PObject *obj) const |
virtual | ~PObject () |
__inline const char * | GetClass () const |
__inline bool | IsClass (const char *name) const |
__inline const PObject * | PTraceObjectInstance () const |
virtual PObject * | Clone () const |
Create a copy of the class on the heap. More... | |
template<class CLS > | |
CLS * | CloneAs () const |
As for Clone() but converts to specified type. More... | |
virtual PINDEX | HashFunction () const |
This function yields a hash value required by the PDictionary class. More... | |
virtual Comparison | Compare (const PObject &obj) const |
Compare the two objects and return their relative rank. More... | |
virtual Comparison | CompareObjectMemoryDirect (const PObject &obj) const |
Determine the byte wise comparison of two objects. More... | |
bool | operator== (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator!= (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator< (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator> (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator<= (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator>= (const PObject &obj) const |
Compare the two objects. More... | |
virtual void | PrintOn (ostream &strm) const |
Output the contents of the object to the stream. More... | |
virtual void | ReadFrom (istream &strm) |
Input the contents of the object from the stream. More... | |
Protected Member Functions | |
PQUEUE (ThreadQueue, PThread) | |
![]() | |
PObject () | |
Constructor for PObject, made protected so cannot ever create one on its own. More... | |
Protected Attributes | |
PString | m_name |
unsigned | m_maximum |
unsigned | m_initial |
ThreadQueue | waitQueue |
![]() | |
unsigned | m_traceContextIdentifier |
Additional Inherited Members | |
![]() | |
enum | Comparison { LessThan = -1, EqualTo = 0, GreaterThan = 1 } |
Result of the comparison operation performed by the Compare() function. More... | |
![]() | |
static __inline void | CopyTraceContextIdentifier (PObject &to, const PObject &from) |
static __inline void | CopyTraceContextIdentifier (PObject &to, const PObject *from) |
static __inline void | CopyTraceContextIdentifier (PObject *to, const PObject &from) |
static __inline void | CopyTraceContextIdentifier (PObject *to, const PObject *from) |
static __inline const char * | Class () |
static __inline const PObject * | PTraceObjectInstance (const char *) |
static __inline const PObject * | PTraceObjectInstance (const PObject *obj) |
template<typename T > | |
static Comparison | Compare2 (T v1, T v2) |
Compare two types, returning Comparison type. More... | |
static Comparison | InternalCompareObjectMemoryDirect (const PObject *obj1, const PObject *obj2, PINDEX size) |
Internal function caled from CompareObjectMemoryDirect() More... | |
This class defines a thread synchronisation object.
This is in the form of a integer semaphore. The semaphore has a count and a maximum value. The various combinations of count and usage of the Wait() and Signal() functions determine the type of synchronisation mechanism to be employed.
The Wait() operation is that if the semaphore count is > 0, decrement the semaphore and return. If it is = 0 then wait (block).
The Signal() operation is that if there are waiting threads then unblock the first one that was blocked. If no waiting threads and the count is less than the maximum then increment the semaphore.
The most common is to create a mutual exclusion zone. A mutex is where a piece of code or data cannot be accessed by more than one thread at a time. To prevent this the PSemaphore is used in the following manner:
PSemaphore mutex(1, 1); // Maximum value of 1 and initial value of 1.
...
mutex.Wait();
... critical section - only one thread at a time here.
mutex.Signal();
...
The first thread will pass through the Wait() function, a second thread will block on that function until the first calls the Signal() function, releasing the second thread.
|
inline |
Create a new semaphore with maximum count and initial value specified.
If the initial value is larger than the maximum value then is is set to the maximum value.
initial | Initial value for semaphore count. |
maximum | Maximum value for semaphore count. |
References Reset().
|
inline |
Create a new system global semaphore with maximum count and initial value specified.
If the initial value is larger than the maximum value then is is set to the maximum value.
initial | Initial value for semaphore count. |
maximum | Maximum value for semaphore count. |
References Reset().
|
inline |
PSemaphore::~PSemaphore | ( | ) |
Destroy the semaphore.
This will assert if there are still waiting threads on the semaphore.
|
inline |
Get the initial value semaphore was creted with.
References m_initial.
|
inline |
Get the initial value semaphore was creted with.
References m_maximum.
|
protected |
|
virtual |
Reset the semaphore to the specified inital and maximum values.
Note that the behaviour is undetermined if something is waiting on the semaphire when this is called.
initial | Initial value for semaphore count. |
maximum | Maximum value for semaphore count. |
Referenced by PSemaphore().
|
virtual |
If there are waiting (blocked) threads then unblock the first one that was blocked.
If no waiting threads and the count is less than the maximum then increment the semaphore.
Implements PSync.
Referenced by PSyncQueue< PQueuedThreadPool::QueuedWorkerThread::QueuedWork >::Close(), PSyncQueue< PQueuedThreadPool::QueuedWorkerThread::QueuedWork >::Drain(), and PSyncQueue< PQueuedThreadPool::QueuedWorkerThread::QueuedWork >::Enqueue().
|
virtual |
If the semaphore count is > 0, decrement the semaphore and return.
If if is = 0 then wait (block).
Implements PSync.
Referenced by PSyncQueue< PQueuedThreadPool::QueuedWorkerThread::QueuedWork >::Dequeue(), and PSyncQueue< PQueuedThreadPool::QueuedWorkerThread::QueuedWork >::Restart().
|
virtual |
If the semaphore count is > 0, decrement the semaphore and return.
If if is = 0 then wait (block) for the specified amount of time.
Implements PSync.
|
protected |
Referenced by GetInitial(), and PSemaphore().
|
protected |
Referenced by GetMaximum(), and PSemaphore().
|
protected |
|
protected |