PTLib
Version 2.10.4
|
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: 26203 $ 00030 * $Author: rjongbloed $ 00031 * $Date: 2011-07-14 23:36:07 -0500 (Thu, 14 Jul 2011) $ 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 void InternalWait(PSemaphore & semaphore) const; 00331 }; 00332 00333 00351 class PReadWaitAndSignal { 00352 public: 00357 PReadWaitAndSignal( 00358 const PReadWriteMutex & rw, 00359 PBoolean start = true 00360 ); 00365 ~PReadWaitAndSignal(); 00366 00367 protected: 00368 PReadWriteMutex & mutex; 00369 }; 00370 00371 00389 class PWriteWaitAndSignal { 00390 public: 00395 PWriteWaitAndSignal( 00396 const PReadWriteMutex & rw, 00397 PBoolean start = true 00398 ); 00403 ~PWriteWaitAndSignal(); 00404 00405 protected: 00406 PReadWriteMutex & mutex; 00407 }; 00408 00409 00410 #endif // PTLIB_SYNCTHRD_H 00411 00412 00413 // End Of File ///////////////////////////////////////////////////////////////