PTLib Version 2.12.9

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

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:
94 PSyncPoint ack;
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:
132 PSyncPoint syncPoint;
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,
156 GT
157 };
158
162 PIntCondMutex(
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;
240 Operation operation;
241 };
242
243
251 class PReadWriteMutex : public PObject
252 {
253 PCLASSINFO(PReadWriteMutex, PObject);
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:
305 PSemaphore m_readerSemaphore;
306 PTimedMutex m_readerMutex;
307 unsigned m_readerCount;
308 PTimedMutex m_starvationPreventer;
309
310 PSemaphore m_writerSemaphore;
311 PTimedMutex m_writerMutex;
312 unsigned m_writerCount;
313
314 struct Nest
315 {
316 unsigned m_readerCount;
317 unsigned m_writerCount;
318 bool m_waiting;
319
320 Nest()
321 : m_readerCount(0)
322 , m_writerCount(0)
323 , m_waiting(false)
324 { }
325 };
326 typedef std::map<PThreadIdentifier, Nest> NestMap;
327 NestMap m_nestedThreads;
328 PMutex m_nestingMutex;
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
356 class PReadWaitAndSignal {
357 public:
362 PReadWaitAndSignal(
363 const PReadWriteMutex & rw,
364 PBoolean start = true
365 );
370 ~PReadWaitAndSignal();
371
372 protected:
373 PReadWriteMutex & mutex;
374 };
375
376
394 class PWriteWaitAndSignal {
395 public:
400 PWriteWaitAndSignal(
401 const PReadWriteMutex & rw,
402 PBoolean start = true
403 );
408 ~PWriteWaitAndSignal();
409
410 protected:
411 PReadWriteMutex & mutex;
412 };
413
414
415 #endif // PTLIB_SYNCTHRD_H
416
417
418 // End Of File ///////////////////////////////////////////////////////////////