PTLib  Version 2.10.11
syncthrd.h
Go to the documentation of this file.
1 /*
2  * syncthrd.h
3  *
4  * Various thread synchronisation classes.
5  *
6  * Portable Tools Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25  * All Rights Reserved.
26  *
27  * Contributor(s): ______________________________________.
28  *
29  * $Revision: 26203 $
30  * $Author: rjongbloed $
31  * $Date: 2011-07-14 23:36:07 -0500 (Thu, 14 Jul 2011) $
32  */
33 
34 #ifndef PTLIB_SYNCTHRD_H
35 #define PTLIB_SYNCTHRD_H
36 
37 #ifdef P_USE_PRAGMA
38 #pragma interface
39 #endif
40 
41 #include <ptlib/mutex.h>
42 #include <ptlib/syncpoint.h>
43 #include <map>
44 
45 
67 class PSyncPointAck : public PSyncPoint
68 {
69  PCLASSINFO(PSyncPointAck, PSyncPoint);
70 
71  public:
83  virtual void Signal();
84  void Signal(const PTimeInterval & waitTime);
85 
91  void Acknowledge();
92 
93  protected:
95 };
96 
97 
103 class PCondMutex : public PMutex
104 {
106 
107  public:
112  virtual void WaitCondition();
113 
118  virtual void Signal();
119 
123  virtual PBoolean Condition() = 0;
124 
129  virtual void OnWait();
130 
131  protected:
133 };
134 
135 
138 class PIntCondMutex : public PCondMutex
139 {
141 
142  public:
145  enum Operation {
148  LT,
150  LE,
152  EQ,
154  GE,
156  GT
157  };
158 
163  int value = 0,
164  int target = 0,
165  Operation operation = LE
166  );
168 
174  void PrintOn(ostream & strm) const;
176 
184  virtual PBoolean Condition();
185 
189  operator int() const { return value; }
190 
198  PIntCondMutex & operator=(int newval);
199 
207  PIntCondMutex & operator++();
208 
216  PIntCondMutex & operator+=(int inc);
217 
225  PIntCondMutex & operator--();
226 
234  PIntCondMutex & operator-=(int dec);
236 
237 
238  protected:
239  int value, target;
241 };
242 
243 
251 class PReadWriteMutex : public PObject
252 {
254  public:
257  PReadWriteMutex();
258  ~PReadWriteMutex();
260 
267  void StartRead();
268 
271  void EndRead();
272 
288  void StartWrite();
289 
301  void EndWrite();
303 
304  protected:
307  unsigned readerCount;
309 
312  unsigned writerCount;
313 
314  class Nest : public PObject
315  {
317  Nest() { readerCount = writerCount = 0; }
318  unsigned readerCount;
319  unsigned writerCount;
320  };
321  typedef std::map<PThreadIdentifier, Nest> NestMap;
324 
325  Nest * GetNest();
326  Nest & StartNest();
327  void EndNest();
328  void InternalStartRead();
329  void InternalEndRead();
330  void InternalWait(PSemaphore & semaphore) const;
331 };
332 
333 
352  public:
358  const PReadWriteMutex & rw,
359  PBoolean start = true
360  );
366 
367  protected:
369 };
370 
371 
390  public:
396  const PReadWriteMutex & rw,
397  PBoolean start = true
398  );
404 
405  protected:
407 };
408 
409 
410 #endif // PTLIB_SYNCTHRD_H
411 
412 
413 // End Of File ///////////////////////////////////////////////////////////////
This class defines a thread synchronisation object.
Definition: semaphor.h:78
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:55
This is a PCondMutex for which the conditional is the value of an integer.
Definition: syncthrd.h:138
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
std::map< PThreadIdentifier, Nest > NestMap
Definition: syncthrd.h:321
PSemaphore writerSemaphore
Definition: syncthrd.h:310
virtual void Signal()
If there are waiting (blocked) threads then unblock the first one that was blocked.
Definition: syncthrd.h:314
PSyncPoint syncPoint
Definition: syncthrd.h:132
int value
Definition: syncthrd.h:239
virtual void PrintOn(ostream &strm) const
Output the contents of the object to the stream.
NestMap m_nestedThreads
Definition: syncthrd.h:322
BOOL PBoolean
Definition: object.h:102
This class defines a thread synchronisation object.
Definition: syncthrd.h:103
Operation
defines possible operators on current value and target value
Definition: syncthrd.h:146
unsigned readerCount
Definition: syncthrd.h:307
PReadWriteMutex & mutex
Definition: syncthrd.h:368
PReadWriteMutex & mutex
Definition: syncthrd.h:406
PSyncPoint ack
Definition: syncthrd.h:94
This class starts a read operation for the PReadWriteMutex on construction and automatically ends the...
Definition: syncthrd.h:351
Operation operation
Definition: syncthrd.h:240
unsigned writerCount
Definition: syncthrd.h:312
PMutex m_nestingMutex
Definition: syncthrd.h:323
Less than or equal to.
Definition: syncthrd.h:150
Equal to.
Definition: syncthrd.h:152
Greater than or equal to.
Definition: syncthrd.h:154
This class starts a write operation for the PReadWriteMutex on construction and automatically ends th...
Definition: syncthrd.h:389
PMutex readerMutex
Definition: syncthrd.h:306
Synonym for PTimedMutex.
This class defines a thread synchronisation object.
Definition: syncthrd.h:251
Less than.
Definition: syncthrd.h:148
void Acknowledge()
This indicates that the thread that was blocked in a Wait() on this synchronisation object has comple...
PMutex writerMutex
Definition: syncthrd.h:311
This class defines a thread synchronisation object.
Definition: syncthrd.h:67
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
PSemaphore readerSemaphore
Definition: syncthrd.h:305
This class defines a thread synchronisation object.
Definition: syncpoint.h:67
PMutex starvationPreventer
Definition: syncthrd.h:308