PWLib
1.10.10
|
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.14 2005/11/25 03:43:47 csoutheren 00031 * Fixed function argument comments to be compatible with Doxygen 00032 * 00033 * Revision 1.13 2004/03/22 10:15:27 rjongbloed 00034 * Added classes similar to PWaitAndSignal to automatically unlock a PReadWriteMutex 00035 * when goes out of scope. 00036 * 00037 * Revision 1.12 2002/12/11 03:21:28 robertj 00038 * Updated documentation for read/write mutex. 00039 * 00040 * Revision 1.11 2002/10/04 08:20:44 robertj 00041 * Changed read/write mutex so can be called by same thread without deadlock. 00042 * 00043 * Revision 1.10 2002/09/16 01:08:59 robertj 00044 * Added #define so can select if #pragma interface/implementation is used on 00045 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan. 00046 * 00047 * Revision 1.9 2002/05/01 03:45:31 robertj 00048 * Added initialisation of PreadWriteMutex and changed slightly to agree 00049 * with the text book definition of a semaphore for one of the mutexes. 00050 * 00051 * Revision 1.8 2002/04/30 06:21:54 robertj 00052 * Fixed PReadWriteMutex class to implement text book algorithm! 00053 * 00054 * Revision 1.7 2001/05/22 12:49:32 robertj 00055 * Did some seriously wierd rewrite of platform headers to eliminate the 00056 * stupid GNU compiler warning about braces not matching. 00057 * 00058 * Revision 1.6 1999/03/09 02:59:51 robertj 00059 * Changed comments to doc++ compatible documentation. 00060 * 00061 * Revision 1.5 1999/02/16 08:11:17 robertj 00062 * MSVC 6.0 compatibility changes. 00063 * 00064 * Revision 1.4 1998/11/30 02:52:01 robertj 00065 * New directory structure 00066 * 00067 * Revision 1.3 1998/10/31 12:46:45 robertj 00068 * Renamed file for having general thread synchronisation objects. 00069 * Added conditional mutex and read/write mutex thread synchronisation objects. 00070 * 00071 * Revision 1.2 1998/09/23 06:21:35 robertj 00072 * Added open source copyright license. 00073 * 00074 * Revision 1.1 1998/05/30 13:26:15 robertj 00075 * Initial revision 00076 * 00077 */ 00078 00079 00080 #define _PSYNCPOINTACK 00081 00082 #ifdef P_USE_PRAGMA 00083 #pragma interface 00084 #endif 00085 00086 #include <ptlib/mutex.h> 00087 #include <ptlib/syncpoint.h> 00088 00110 class PSyncPointAck : public PSyncPoint 00111 { 00112 PCLASSINFO(PSyncPointAck, PSyncPoint); 00113 00114 public: 00126 virtual void Signal(); 00127 void Signal(const PTimeInterval & waitTime); 00128 00134 void Acknowledge(); 00135 00136 protected: 00137 PSyncPoint ack; 00138 }; 00139 00140 00146 class PCondMutex : public PMutex 00147 { 00148 PCLASSINFO(PCondMutex, PMutex); 00149 00150 public: 00155 virtual void WaitCondition(); 00156 00161 virtual void Signal(); 00162 00166 virtual BOOL Condition() = 0; 00167 00172 virtual void OnWait(); 00173 00174 protected: 00175 PSyncPoint syncPoint; 00176 }; 00177 00178 00181 class PIntCondMutex : public PCondMutex 00182 { 00183 PCLASSINFO(PIntCondMutex, PCondMutex); 00184 00185 public: 00188 00189 enum Operation { 00191 LT, 00193 LE, 00195 EQ, 00197 GE, 00199 GT 00200 }; 00201 00205 PIntCondMutex( 00206 int value = 0, 00207 int target = 0, 00208 Operation operation = LE 00209 ); 00211 00217 void PrintOn(ostream & strm) const; 00219 00227 virtual BOOL Condition(); 00228 00232 operator int() const { return value; } 00233 00241 PIntCondMutex & operator=(int newval); 00242 00250 PIntCondMutex & operator++(); 00251 00259 PIntCondMutex & operator+=(int inc); 00260 00268 PIntCondMutex & operator--(); 00269 00277 PIntCondMutex & operator-=(int dec); 00279 00280 00281 protected: 00282 int value, target; 00283 Operation operation; 00284 }; 00285 00286 00294 class PReadWriteMutex : public PObject 00295 { 00296 PCLASSINFO(PReadWriteMutex, PObject); 00297 public: 00300 PReadWriteMutex(); 00302 00309 void StartRead(); 00310 00313 void EndRead(); 00314 00330 void StartWrite(); 00331 00343 void EndWrite(); 00345 00346 protected: 00347 PSemaphore readerSemaphore; 00348 PMutex readerMutex; 00349 unsigned readerCount; 00350 PMutex starvationPreventer; 00351 00352 PSemaphore writerSemaphore; 00353 PMutex writerMutex; 00354 unsigned writerCount; 00355 00356 class Nest : public PObject 00357 { 00358 PCLASSINFO(Nest, PObject); 00359 Nest() { readerCount = writerCount = 0; } 00360 unsigned readerCount; 00361 unsigned writerCount; 00362 }; 00363 PDictionary<POrdinalKey, Nest> nestedThreads; 00364 PMutex nestingMutex; 00365 00366 Nest * GetNest() const; 00367 Nest & StartNest(); 00368 void EndNest(); 00369 void InternalStartRead(); 00370 void InternalEndRead(); 00371 }; 00372 00373 00391 class PReadWaitAndSignal { 00392 public: 00397 PReadWaitAndSignal( 00398 const PReadWriteMutex & rw, 00399 BOOL start = TRUE 00400 ); 00405 ~PReadWaitAndSignal(); 00406 00407 protected: 00408 PReadWriteMutex & mutex; 00409 }; 00410 00411 00429 class PWriteWaitAndSignal { 00430 public: 00435 PWriteWaitAndSignal( 00436 const PReadWriteMutex & rw, 00437 BOOL start = TRUE 00438 ); 00443 ~PWriteWaitAndSignal(); 00444 00445 protected: 00446 PReadWriteMutex & mutex; 00447 }; 00448 00449 00450 // End Of File ///////////////////////////////////////////////////////////////