syncthrd.h
Go to the documentation of this file.
/*
* syncthrd.h
*
* Various thread synchronisation classes.
*
* Portable Tools Library
*
* Copyright (c) 1993-1998 Equivalence Pty. Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
*
* Portions are Copyright (C) 1993 Free Software Foundation, Inc.
* All Rights Reserved.
*
* Contributor(s): ______________________________________.
*
* $Revision: 26203 $
* $Author: rjongbloed $
* $Date: 2011-07-14 23:36:07 -0500 (Thu, 14 Jul 2011) $
*/
#ifndef PTLIB_SYNCTHRD_H
#define PTLIB_SYNCTHRD_H
#ifdef P_USE_PRAGMA
#pragma interface
#endif
#include <ptlib/mutex.h>
#include <ptlib/syncpoint.h>
#include <map>
class PSyncPointAck : public PSyncPoint
{
PCLASSINFO(PSyncPointAck, PSyncPoint);
public:
virtual void Signal();
void Signal(const PTimeInterval & waitTime);
void Acknowledge();
protected:
PSyncPoint ack;
};
class PCondMutex : public PMutex
{
PCLASSINFO(PCondMutex, PMutex);
public:
virtual void WaitCondition();
virtual void Signal();
virtual PBoolean Condition() = 0;
virtual void OnWait();
protected:
PSyncPoint syncPoint;
};
class PIntCondMutex : public PCondMutex
{
PCLASSINFO(PIntCondMutex, PCondMutex);
public:
enum Operation {
LT,
LE,
EQ,
GE,
GT
};
PIntCondMutex(
int value = 0,
int target = 0,
Operation operation = LE
);
void PrintOn(ostream & strm) const;
virtual PBoolean Condition();
operator int() const { return value; }
PIntCondMutex & operator=(int newval);
PIntCondMutex & operator++();
PIntCondMutex & operator+=(int inc);
PIntCondMutex & operator--();
PIntCondMutex & operator-=(int dec);
protected:
int value, target;
Operation operation;
};
class PReadWriteMutex : public PObject
{
PCLASSINFO(PReadWriteMutex, PObject);
public:
PReadWriteMutex();
~PReadWriteMutex();
void StartRead();
void EndRead();
void StartWrite();
void EndWrite();
protected:
PSemaphore readerSemaphore;
PMutex readerMutex;
unsigned readerCount;
PMutex starvationPreventer;
PSemaphore writerSemaphore;
PMutex writerMutex;
unsigned writerCount;
class Nest : public PObject
{
PCLASSINFO(Nest, PObject);
Nest() { readerCount = writerCount = 0; }
unsigned readerCount;
unsigned writerCount;
};
typedef std::map<PThreadIdentifier, Nest> NestMap;
NestMap m_nestedThreads;
PMutex m_nestingMutex;
Nest * GetNest();
Nest & StartNest();
void EndNest();
void InternalStartRead();
void InternalEndRead();
void InternalWait(PSemaphore & semaphore) const;
};
class PReadWaitAndSignal {
public:
PReadWaitAndSignal(
const PReadWriteMutex & rw,
PBoolean start = true
);
~PReadWaitAndSignal();
protected:
PReadWriteMutex & mutex;
};
class PWriteWaitAndSignal {
public:
PWriteWaitAndSignal(
const PReadWriteMutex & rw,
PBoolean start = true
);
~PWriteWaitAndSignal();
protected:
PReadWriteMutex & mutex;
};
#endif // PTLIB_SYNCTHRD_H
// End Of File ///////////////////////////////////////////////////////////////
PReadWriteMutex::EndRead
void EndRead()
This function attempts to release the mutex for reading.
PSemaphore
This class defines a thread synchronisation object.
Definition: semaphor.h:78
PIntCondMutex::GT
Greater than.
Definition: syncthrd.h:156
PIntCondMutex::operator=
PIntCondMutex & operator=(int newval)
Assign new condition value.
PReadWriteMutex::InternalWait
void InternalWait(PSemaphore &semaphore) const
PReadWriteMutex::PReadWriteMutex
PReadWriteMutex()
PTimeInterval
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:55
PIntCondMutex
This is a PCondMutex for which the conditional is the value of an integer.
Definition: syncthrd.h:138
PReadWriteMutex::~PReadWriteMutex
~PReadWriteMutex()
PIntCondMutex::operator--
PIntCondMutex & operator--()
Decrement condition value.
PReadWriteMutex::NestMap
std::map< PThreadIdentifier, Nest > NestMap
Definition: syncthrd.h:321
PReadWriteMutex::writerSemaphore
PSemaphore writerSemaphore
Definition: syncthrd.h:310
PSyncPointAck::Signal
virtual void Signal()
If there are waiting (blocked) threads then unblock the first one that was blocked.
PReadWriteMutex::GetNest
Nest * GetNest()
PReadWriteMutex::StartWrite
void StartWrite()
This function attempts to acquire the mutex for writing.
PIntCondMutex::PrintOn
void PrintOn(ostream &strm) const
Print the object on the stream.
PReadWriteMutex::StartRead
void StartRead()
This function attempts to acquire the mutex for reading.
PReadWriteMutex::Nest
Definition: syncthrd.h:314
PWriteWaitAndSignal::~PWriteWaitAndSignal
~PWriteWaitAndSignal()
End write operation on the PReadWriteMutex.
PCondMutex::syncPoint
PSyncPoint syncPoint
Definition: syncthrd.h:132
PWriteWaitAndSignal::PWriteWaitAndSignal
PWriteWaitAndSignal(const PReadWriteMutex &rw, PBoolean start=true)
Create the PWriteWaitAndSignal wait instance.
PIntCondMutex::value
int value
Definition: syncthrd.h:239
PIntCondMutex::operator+=
PIntCondMutex & operator+=(int inc)
Add to condition value.
PIntCondMutex::operator++
PIntCondMutex & operator++()
Increment condition value.
PCondMutex::WaitCondition
virtual void WaitCondition()
This function attempts to acquire the mutex, but will block not only until the mutex is free...
PReadWriteMutex::m_nestedThreads
NestMap m_nestedThreads
Definition: syncthrd.h:322
PBoolean
BOOL PBoolean
Definition: object.h:102
PCondMutex
This class defines a thread synchronisation object.
Definition: syncthrd.h:103
PIntCondMutex::Operation
Operation
defines possible operators on current value and target value
Definition: syncthrd.h:146
PCondMutex::OnWait
virtual void OnWait()
This function is called immediately before blocking on the condition in the WaitCondition() function...
PReadWriteMutex::readerCount
unsigned readerCount
Definition: syncthrd.h:307
PReadWaitAndSignal::mutex
PReadWriteMutex & mutex
Definition: syncthrd.h:368
PWriteWaitAndSignal::mutex
PReadWriteMutex & mutex
Definition: syncthrd.h:406
PSyncPointAck::ack
PSyncPoint ack
Definition: syncthrd.h:94
PReadWaitAndSignal
This class starts a read operation for the PReadWriteMutex on construction and automatically ends the...
Definition: syncthrd.h:351
PReadWriteMutex::StartNest
Nest & StartNest()
PReadWriteMutex::InternalStartRead
void InternalStartRead()
PIntCondMutex::operation
Operation operation
Definition: syncthrd.h:240
PReadWriteMutex::writerCount
unsigned writerCount
Definition: syncthrd.h:312
PIntCondMutex::target
int target
Definition: syncthrd.h:239
PReadWriteMutex::m_nestingMutex
PMutex m_nestingMutex
Definition: syncthrd.h:323
PIntCondMutex::LE
Less than or equal to.
Definition: syncthrd.h:150
PIntCondMutex::EQ
Equal to.
Definition: syncthrd.h:152
PReadWaitAndSignal::PReadWaitAndSignal
PReadWaitAndSignal(const PReadWriteMutex &rw, PBoolean start=true)
Create the PReadWaitAndSignal wait instance.
PReadWriteMutex::InternalEndRead
void InternalEndRead()
PIntCondMutex::GE
Greater than or equal to.
Definition: syncthrd.h:154
PWriteWaitAndSignal
This class starts a write operation for the PReadWriteMutex on construction and automatically ends th...
Definition: syncthrd.h:389
mutex.h
PCondMutex::Condition
virtual PBoolean Condition()=0
This is the condition that must be met for the WaitCondition() function to acquire the mutex...
PIntCondMutex::PIntCondMutex
PIntCondMutex(int value=0, int target=0, Operation operation=LE)
Create a cond mutex using an integer.
syncpoint.h
PReadWriteMutex::EndWrite
void EndWrite()
This function attempts to release the mutex for writing.
PIntCondMutex::operator-=
PIntCondMutex & operator-=(int dec)
Subtract from condition value.
PReadWriteMutex::EndNest
void EndNest()
PReadWriteMutex::readerMutex
PMutex readerMutex
Definition: syncthrd.h:306
PMutex
Synonym for PTimedMutex.
PCondMutex::Signal
virtual void Signal()
If there are waiting (blocked) threads then unblock the first one that was blocked.
PReadWriteMutex
This class defines a thread synchronisation object.
Definition: syncthrd.h:251
PIntCondMutex::LT
Less than.
Definition: syncthrd.h:148
PSyncPointAck::Acknowledge
void Acknowledge()
This indicates that the thread that was blocked in a Wait() on this synchronisation object has comple...
PReadWriteMutex::writerMutex
PMutex writerMutex
Definition: syncthrd.h:311
PSyncPointAck
This class defines a thread synchronisation object.
Definition: syncthrd.h:67
PReadWaitAndSignal::~PReadWaitAndSignal
~PReadWaitAndSignal()
End read operation on the PReadWriteMutex.
PObject
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
PReadWriteMutex::readerSemaphore
PSemaphore readerSemaphore
Definition: syncthrd.h:305
PSyncPoint
This class defines a thread synchronisation object.
Definition: syncpoint.h:67
PReadWriteMutex::starvationPreventer
PMutex starvationPreventer
Definition: syncthrd.h:308
PIntCondMutex::Condition
virtual PBoolean Condition()
This is the condition that must be met for the WaitCondition() function to acquire the mutex...