PTLib  Version 2.14.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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: 28062 $
30  * $Author: rjongbloed $
31  * $Date: 2012-07-19 19:45:52 +1000 (Thu, 19 Jul 2012) $
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 {
105  PCLASSINFO(PCondMutex, PMutex);
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 {
140  PCLASSINFO(PIntCondMutex, PCondMutex);
141 
142  public:
145 
146  enum Operation {
148  LT,
150  LE,
152  EQ,
154  GE,
157  };
158 
163  int value = 0,
164  int target = 0,
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 
208 
216  PIntCondMutex & operator+=(int inc);
217 
226 
234  PIntCondMutex & operator-=(int dec);
236 
237 
238  protected:
239  int value, target;
241 };
242 
243 
251 class PReadWriteMutex : public PObject
252 {
253  PCLASSINFO(PReadWriteMutex, PObject);
254  public:
257  PReadWriteMutex();
260 
267  void StartRead();
268 
271  void EndRead();
272 
288  void StartWrite();
289 
301  void EndWrite();
303 
304  protected:
307  unsigned m_readerCount;
309 
312  unsigned m_writerCount;
313 
314  struct Nest
315  {
316  unsigned m_readerCount;
317  unsigned m_writerCount;
318  bool m_waiting;
319 
321  : m_readerCount(0)
322  , m_writerCount(0)
323  , m_waiting(false)
324  { }
325  };
326  typedef std::map<PThreadIdentifier, Nest> NestMap;
329 
330  Nest * GetNest();
331  Nest & StartNest();
332  void EndNest();
333  void InternalStartRead(Nest & nest);
334  void InternalEndRead(Nest & nest);
335  void InternalWait(Nest & nest, PSync & sync) const;
336 };
337 
338 
357  public:
363  const PReadWriteMutex & rw,
364  PBoolean start = true
365  );
371 
372  protected:
374 };
375 
376 
395  public:
401  const PReadWriteMutex & rw,
402  PBoolean start = true
403  );
409 
410  protected:
412 };
413 
414 
415 #endif // PTLIB_SYNCTHRD_H
416 
417 
418 // End Of File ///////////////////////////////////////////////////////////////