00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 #define _PSYNCPOINTACK
00078
00079
#ifdef P_USE_PRAGMA
00080
#pragma interface
00081
#endif
00082
00083
#include <ptlib/mutex.h>
00084
#include <ptlib/syncpoint.h>
00085
00107 class PSyncPointAck :
public PSyncPoint
00108 {
00109
PCLASSINFO(
PSyncPointAck,
PSyncPoint);
00110
00111
public:
00123
virtual void Signal();
00124
void Signal(
const PTimeInterval & waitTime);
00125
00131
void Acknowledge();
00132
00133
protected:
00134 PSyncPoint ack;
00135 };
00136
00137
00143 class PCondMutex :
public PMutex
00144 {
00145
PCLASSINFO(
PCondMutex,
PMutex);
00146
00147
public:
00152
virtual void WaitCondition();
00153
00158
virtual void Signal();
00159
00163
virtual BOOL
Condition() = 0;
00164
00169
virtual void OnWait();
00170
00171
protected:
00172 PSyncPoint syncPoint;
00173 };
00174
00175
00178 class PIntCondMutex :
public PCondMutex
00179 {
00180
PCLASSINFO(
PIntCondMutex,
PCondMutex);
00181
00182
public:
00185
00186 enum Operation {
00188
LT,
00190
LE,
00192
EQ,
00194
GE,
00196
GT
00197 };
00198
00202
PIntCondMutex(
00203
int value = 0,
00204
int target = 0,
00205 Operation operation = LE
00206 );
00208
00214
void PrintOn(ostream & strm)
const;
00216
00224
virtual BOOL
Condition();
00225
00229 operator int()
const {
return value; }
00230
00238
PIntCondMutex & operator=(
int newval);
00239
00247
PIntCondMutex &
operator++();
00248
00256
PIntCondMutex & operator+=(
int inc);
00257
00265
PIntCondMutex &
operator--();
00266
00274
PIntCondMutex & operator-=(
int dec);
00276
00277
00278
protected:
00279 int value,
target;
00280 Operation operation;
00281 };
00282
00283
00291 class PReadWriteMutex :
public PObject
00292 {
00293
PCLASSINFO(
PReadWriteMutex,
PObject);
00294
public:
00297
PReadWriteMutex();
00299
00306
void StartRead();
00307
00310
void EndRead();
00311
00327
void StartWrite();
00328
00340
void EndWrite();
00342
00343
protected:
00344 PSemaphore readerSemaphore;
00345 PMutex readerMutex;
00346 unsigned readerCount;
00347 PMutex starvationPreventer;
00348
00349 PSemaphore writerSemaphore;
00350 PMutex writerMutex;
00351 unsigned writerCount;
00352
00353 class Nest :
public PObject
00354 {
00355
PCLASSINFO(
Nest,
PObject);
00356
Nest() { readerCount = writerCount = 0; }
00357
unsigned readerCount;
00358
unsigned writerCount;
00359 };
00360 PDictionary<POrdinalKey, Nest>
nestedThreads;
00361 PMutex nestingMutex;
00362
00363
Nest *
GetNest() const;
00364
Nest & StartNest();
00365
void EndNest();
00366
void InternalStartRead();
00367
void InternalEndRead();
00368 };
00369
00370
00388 class
PReadWaitAndSignal {
00389
public:
00394 PReadWaitAndSignal(
00395
const PReadWriteMutex & rw,
00396 BOOL start = TRUE
00397 );
00402 ~PReadWaitAndSignal();
00403
00404
protected:
00405 PReadWriteMutex & mutex;
00406 };
00407
00408
00426 class PWriteWaitAndSignal {
00427
public:
00432
PWriteWaitAndSignal(
00433
const PReadWriteMutex & rw,
00434 BOOL start = TRUE
00435 );
00440 ~
PWriteWaitAndSignal();
00441
00442
protected:
00443 PReadWriteMutex & mutex;
00444 };
00445
00446
00447