syncthrd.h

Go to the documentation of this file.
00001 /*
00002  * syncthrd.h
00003  *
00004  * Various thread synchronisation classes.
00005  *
00006  * Portable Tools Library
00007  *
00008  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
00025  * All Rights Reserved.
00026  *
00027  * Contributor(s): ______________________________________.
00028  *
00029  * $Revision: 24177 $
00030  * $Author: rjongbloed $
00031  * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
00032  */
00033 
00034 #ifndef PTLIB_SYNCTHRD_H
00035 #define PTLIB_SYNCTHRD_H
00036 
00037 #ifdef P_USE_PRAGMA
00038 #pragma interface
00039 #endif
00040 
00041 #include <ptlib/mutex.h>
00042 #include <ptlib/syncpoint.h>
00043 #include <map>
00044 
00045 
00067 class PSyncPointAck : public PSyncPoint
00068 {
00069   PCLASSINFO(PSyncPointAck, PSyncPoint);
00070 
00071   public:
00083     virtual void Signal();
00084     void Signal(const PTimeInterval & waitTime);
00085 
00091     void Acknowledge();
00092 
00093   protected:
00094     PSyncPoint ack;
00095 };
00096 
00097 
00103 class PCondMutex : public PMutex
00104 {
00105   PCLASSINFO(PCondMutex, PMutex);
00106 
00107   public:
00112     virtual void WaitCondition();
00113 
00118     virtual void Signal();
00119 
00123     virtual PBoolean Condition() = 0;
00124 
00129     virtual void OnWait();
00130 
00131   protected:
00132     PSyncPoint syncPoint;
00133 };
00134 
00135 
00138 class PIntCondMutex : public PCondMutex
00139 {
00140   PCLASSINFO(PIntCondMutex, PCondMutex);
00141 
00142   public:
00145 
00146     enum Operation {
00148       LT,
00150       LE,
00152       EQ,
00154       GE,
00156       GT
00157     };
00158 
00162     PIntCondMutex(
00163       int value = 0,            
00164       int target = 0,           
00165       Operation operation = LE  
00166     );
00168 
00174     void PrintOn(ostream & strm) const;
00176 
00184     virtual PBoolean Condition();
00185 
00189     operator int() const { return value; }
00190 
00198     PIntCondMutex & operator=(int newval);
00199 
00207     PIntCondMutex & operator++();
00208 
00216     PIntCondMutex & operator+=(int inc);
00217 
00225     PIntCondMutex & operator--();
00226 
00234     PIntCondMutex & operator-=(int dec);
00236 
00237 
00238   protected:
00239     int value, target;
00240     Operation operation;
00241 };
00242 
00243 
00251 class PReadWriteMutex : public PObject
00252 {
00253   PCLASSINFO(PReadWriteMutex, PObject);
00254   public:
00257     PReadWriteMutex();
00258     ~PReadWriteMutex();
00260 
00267     void StartRead();
00268 
00271     void EndRead();
00272 
00288     void StartWrite();
00289 
00301     void EndWrite();
00303 
00304   protected:
00305     PSemaphore readerSemaphore;
00306     PMutex     readerMutex;
00307     unsigned   readerCount;
00308     PMutex     starvationPreventer;
00309 
00310     PSemaphore writerSemaphore;
00311     PMutex     writerMutex;
00312     unsigned   writerCount;
00313 
00314     class Nest : public PObject
00315     {
00316       PCLASSINFO(Nest, PObject);
00317       Nest() { readerCount = writerCount = 0; }
00318       unsigned readerCount;
00319       unsigned writerCount;
00320     };
00321     typedef std::map<PThreadIdentifier, Nest> NestMap;
00322     NestMap m_nestedThreads;
00323     PMutex  m_nestingMutex;
00324 
00325     Nest * GetNest();
00326     Nest & StartNest();
00327     void EndNest();
00328     void InternalStartRead();
00329     void InternalEndRead();
00330 };
00331 
00332 
00350 class PReadWaitAndSignal {
00351   public:
00356     PReadWaitAndSignal(
00357       const PReadWriteMutex & rw,   
00358       PBoolean start = true    
00359     );
00364     ~PReadWaitAndSignal();
00365 
00366   protected:
00367     PReadWriteMutex & mutex;
00368 };
00369 
00370 
00388 class PWriteWaitAndSignal {
00389   public:
00394     PWriteWaitAndSignal(
00395       const PReadWriteMutex & rw,   
00396       PBoolean start = true    
00397     );
00402     ~PWriteWaitAndSignal();
00403 
00404   protected:
00405     PReadWriteMutex & mutex;
00406 };
00407 
00408 
00409 #endif // PTLIB_SYNCTHRD_H
00410 
00411 
00412 // End Of File ///////////////////////////////////////////////////////////////

Generated on Fri Oct 14 01:44:10 2011 for PTLib by  doxygen 1.4.7