Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

syncthrd.h

Go to the documentation of this file.
00001 /*
00002  * syncthrd.h
00003  *
00004  * Various thread synchronisation classes.
00005  *
00006  * Portable Windows 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  * $Log: syncthrd.h,v $
00030  * Revision 1.13  2004/03/22 10:15:27  rjongbloed
00031  * Added classes similar to PWaitAndSignal to automatically unlock a PReadWriteMutex
00032  *   when goes out of scope.
00033  *
00034  * Revision 1.12  2002/12/11 03:21:28  robertj
00035  * Updated documentation for read/write mutex.
00036  *
00037  * Revision 1.11  2002/10/04 08:20:44  robertj
00038  * Changed read/write mutex so can be called by same thread without deadlock.
00039  *
00040  * Revision 1.10  2002/09/16 01:08:59  robertj
00041  * Added #define so can select if #pragma interface/implementation is used on
00042  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00043  *
00044  * Revision 1.9  2002/05/01 03:45:31  robertj
00045  * Added initialisation of PreadWriteMutex and changed slightly to agree
00046  *   with the text book definition of a semaphore for one of the mutexes.
00047  *
00048  * Revision 1.8  2002/04/30 06:21:54  robertj
00049  * Fixed PReadWriteMutex class to implement text book algorithm!
00050  *
00051  * Revision 1.7  2001/05/22 12:49:32  robertj
00052  * Did some seriously wierd rewrite of platform headers to eliminate the
00053  *   stupid GNU compiler warning about braces not matching.
00054  *
00055  * Revision 1.6  1999/03/09 02:59:51  robertj
00056  * Changed comments to doc++ compatible documentation.
00057  *
00058  * Revision 1.5  1999/02/16 08:11:17  robertj
00059  * MSVC 6.0 compatibility changes.
00060  *
00061  * Revision 1.4  1998/11/30 02:52:01  robertj
00062  * New directory structure
00063  *
00064  * Revision 1.3  1998/10/31 12:46:45  robertj
00065  * Renamed file for having general thread synchronisation objects.
00066  * Added conditional mutex and read/write mutex thread synchronisation objects.
00067  *
00068  * Revision 1.2  1998/09/23 06:21:35  robertj
00069  * Added open source copyright license.
00070  *
00071  * Revision 1.1  1998/05/30 13:26:15  robertj
00072  * Initial revision
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 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Feb 21 20:43:10 2005 for PWLib by  doxygen 1.4.1