/usr/include/ptlib/syncthrd.h is in libpt-1.10.10-dev 1.10.10-3.1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | /*
* syncthrd.h
*
* Various thread synchronisation classes.
*
* Portable Windows Library
*
* Copyright (c) 1993-1998 Equivalence Pty. Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
*
* Portions are Copyright (C) 1993 Free Software Foundation, Inc.
* All Rights Reserved.
*
* Contributor(s): ______________________________________.
*
* $Log: syncthrd.h,v $
* Revision 1.14 2005/11/25 03:43:47 csoutheren
* Fixed function argument comments to be compatible with Doxygen
*
* Revision 1.13 2004/03/22 10:15:27 rjongbloed
* Added classes similar to PWaitAndSignal to automatically unlock a PReadWriteMutex
* when goes out of scope.
*
* Revision 1.12 2002/12/11 03:21:28 robertj
* Updated documentation for read/write mutex.
*
* Revision 1.11 2002/10/04 08:20:44 robertj
* Changed read/write mutex so can be called by same thread without deadlock.
*
* Revision 1.10 2002/09/16 01:08:59 robertj
* Added #define so can select if #pragma interface/implementation is used on
* platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
*
* Revision 1.9 2002/05/01 03:45:31 robertj
* Added initialisation of PreadWriteMutex and changed slightly to agree
* with the text book definition of a semaphore for one of the mutexes.
*
* Revision 1.8 2002/04/30 06:21:54 robertj
* Fixed PReadWriteMutex class to implement text book algorithm!
*
* Revision 1.7 2001/05/22 12:49:32 robertj
* Did some seriously wierd rewrite of platform headers to eliminate the
* stupid GNU compiler warning about braces not matching.
*
* Revision 1.6 1999/03/09 02:59:51 robertj
* Changed comments to doc++ compatible documentation.
*
* Revision 1.5 1999/02/16 08:11:17 robertj
* MSVC 6.0 compatibility changes.
*
* Revision 1.4 1998/11/30 02:52:01 robertj
* New directory structure
*
* Revision 1.3 1998/10/31 12:46:45 robertj
* Renamed file for having general thread synchronisation objects.
* Added conditional mutex and read/write mutex thread synchronisation objects.
*
* Revision 1.2 1998/09/23 06:21:35 robertj
* Added open source copyright license.
*
* Revision 1.1 1998/05/30 13:26:15 robertj
* Initial revision
*
*/
#define _PSYNCPOINTACK
#ifdef P_USE_PRAGMA
#pragma interface
#endif
#include <ptlib/mutex.h>
#include <ptlib/syncpoint.h>
/** This class defines a thread synchonisation object.
This may be used to send signals to a thread and await an acknowldegement
that the signal was processed. This can be be used to initate an action in
another thread and wait for the action to be completed.
\begin{verbatim}
... thread one
while (condition) {
sync.Wait();
do_something();
sync.Acknowledge();
}
... thread 2
do_something_else();
sync.Signal(); // At this point thread 1 wake up and does something.
do_yet_more(); // However, this does not get done until Acknowldege()
// is called in the other thread.
\end{verbatim}
*/
class PSyncPointAck : public PSyncPoint
{
PCLASSINFO(PSyncPointAck, PSyncPoint);
public:
/** If there are waiting (blocked) threads then unblock the first one that
was blocked. If no waiting threads and the count is less than the
maximum then increment the semaphore.
Unlike the PSyncPoint::Signal() this function will block until the
target thread that was blocked by the Wait() function has resumed
execution and called the Acknowledge() function.
The #waitTime# parameter is used as a maximum amount of time
to wait for the achnowledgement to be returned from the other thread.
*/
virtual void Signal();
void Signal(const PTimeInterval & waitTime);
/** This indicates that the thread that was blocked in a Wait() on this
synchonrisation object has completed the operation the signal was
intended to initiate. This unblocks the thread that had called the
Signal() function to initiate the action.
*/
void Acknowledge();
protected:
PSyncPoint ack;
};
/**This class defines a thread synchonisation object.
This is a special type of mutual exclusion, where a thread wishes to get
exlusive use of a resource but only if a certain other condition is met.
*/
class PCondMutex : public PMutex
{
PCLASSINFO(PCondMutex, PMutex);
public:
/** This function attempts to acquire the mutex, but will block not only
until the mutex is free, but also that the condition returned by the
Condition() function is also met.
*/
virtual void WaitCondition();
/** If there are waiting (blocked) threads then unblock the first one that
was blocked. If no waiting threads and the count is less than the
maximum then increment the semaphore.
*/
virtual void Signal();
/** This is the condition that must be met for the WaitCondition() function
to acquire the mutex.
*/
virtual BOOL Condition() = 0;
/** This function is called immediately before blocking on the condition in
the WaitCondition() function. This could get called multiple times
before the condition is met and the WaitCondition() function returns.
*/
virtual void OnWait();
protected:
PSyncPoint syncPoint;
};
/** This is a PCondMutex for which the conditional is the value of an integer.
*/
class PIntCondMutex : public PCondMutex
{
PCLASSINFO(PIntCondMutex, PCondMutex);
public:
/**@name Construction */
//@{
/// defines possible operators on current value and target value
enum Operation {
/// Less than
LT,
/// Less than or equal to
LE,
/// Equal to
EQ,
/// Greater than or equal to
GE,
/// Greater than
GT
};
/**
Create a cond mutex using an integer
*/
PIntCondMutex(
int value = 0, ///< initial value if the integer
int target = 0, ///< target vaue which causes the mutex to unlock
Operation operation = LE ///< comparison operator
);
//@}
/**@name Overrides from class PObject */
//@{
/** Print the object on the stream. This will be of the form
#"(value < target)"#.
*/
void PrintOn(ostream & strm) const;
//@}
/**@name Condition access functions */
//@{
/** This is the condition that must be met for the WaitCondition() function
to acquire the mutex.
@return TRUE if condition is met.
*/
virtual BOOL Condition();
/**Get the current value of the condition variable.
@return Current condition variable value.
*/
operator int() const { return value; }
/**Assign new condition value.
Use the Wait() function to acquire the mutex, modify the value, then
release the mutex, possibly releasing the thread in the WaitCondition()
function if the condition was met by the operation.
@return The object reference for consecutive operations in the same statement.
*/
PIntCondMutex & operator=(int newval);
/**Increment condition value.
Use the Wait() function to acquire the mutex, modify the value, then
release the mutex, possibly releasing the thread in the WaitCondition()
function if the condition was met by the operation.
@return The object reference for consecutive operations in the same statement.
*/
PIntCondMutex & operator++();
/**Add to condition value.
Use the Wait() function to acquire the mutex, modify the value, then
release the mutex, possibly releasing the thread in the WaitCondition()
function if the condition was met by the operation.
@return The object reference for consecutive operations in the same statement.
*/
PIntCondMutex & operator+=(int inc);
/**Decrement condition value.
Use the Wait() function to acquire the mutex, modify the value, then
release the mutex, possibly releasing the thread in the WaitCondition()
function if the condition was met by the operation.
@return The object reference for consecutive operations in the same statement.
*/
PIntCondMutex & operator--();
/**Subtract from condition value.
Use the Wait() function to acquire the mutex, modify the value, then
release the mutex, possibly releasing the thread in the WaitCondition()
function if the condition was met by the operation.
@return The object reference for consecutive operations in the same statement.
*/
PIntCondMutex & operator-=(int dec);
//@}
protected:
int value, target;
Operation operation;
};
/** This class defines a thread synchonisation object.
This is a special type of mutual exclusion, where the excluded area may
have multiple read threads but only one write thread and the read threads
are blocked on write as well.
*/
class PReadWriteMutex : public PObject
{
PCLASSINFO(PReadWriteMutex, PObject);
public:
/**@name Construction */
//@{
PReadWriteMutex();
//@}
/**@name Operations */
//@{
/** This function attempts to acquire the mutex for reading.
This call may be nested and must have an equal number of EndRead()
calls for the mutex to be released.
*/
void StartRead();
/** This function attempts to release the mutex for reading.
*/
void EndRead();
/** This function attempts to acquire the mutex for writing.
This call may be nested and must have an equal number of EndWrite()
calls for the mutex to be released.
Note, if the same thread had a read lock previous to this call then
the read lock is automatically released and reacquired when EndWrite()
is called, unless an EndRead() is called. The EndRead() and EndWrite()
calls do not have to be strictly nested.
It should also be noted that a consequence of this is that another
thread may acquire the write lock before the thread that previously
had the read lock. Thus it is impossibly to go straight from a read
lock to write lock without the possiblility of the object being
changed and application logic should take this into account.
*/
void StartWrite();
/** This function attempts to release the mutex for writing.
Note, if the same thread had a read lock when the StartWrite() was
called which has not yet been released by an EndRead() call then this
will reacquire the read lock.
It should also be noted that a consequence of this is that another
thread may acquire the write lock before the thread that regains the
read lock. Thus it is impossibly to go straight from a write lock to
read lock without the possiblility of the object being changed and
application logic should take this into account.
*/
void EndWrite();
//@}
protected:
PSemaphore readerSemaphore;
PMutex readerMutex;
unsigned readerCount;
PMutex starvationPreventer;
PSemaphore writerSemaphore;
PMutex writerMutex;
unsigned writerCount;
class Nest : public PObject
{
PCLASSINFO(Nest, PObject);
Nest() { readerCount = writerCount = 0; }
unsigned readerCount;
unsigned writerCount;
};
PDictionary<POrdinalKey, Nest> nestedThreads;
PMutex nestingMutex;
Nest * GetNest() const;
Nest & StartNest();
void EndNest();
void InternalStartRead();
void InternalEndRead();
};
/**This class starts a read operation for the PReadWriteMutex on construction
and automatically ends the read operation on destruction.
This is very usefull for constructs such as:
\begin{verbatim}
void func()
{
PReadWaitAndSignal mutexWait(myMutex);
if (condition)
return;
do_something();
if (other_condition)
return;
do_something_else();
}
\end{verbatim}
*/
class PReadWaitAndSignal {
public:
/**Create the PReadWaitAndSignal wait instance.
This will wait on the specified PReadWriteMutex using the #StartRead()#
function before returning.
*/
PReadWaitAndSignal(
const PReadWriteMutex & rw, ///< PReadWriteMutex descendent to wait/signal.
BOOL start = TRUE ///< Start read operation on PReadWriteMutex before returning.
);
/** End read operation on the PReadWriteMutex.
This will execute the EndRead() function on the PReadWriteMutex that
was used in the construction of this instance.
*/
~PReadWaitAndSignal();
protected:
PReadWriteMutex & mutex;
};
/**This class starts a write operation for the PReadWriteMutex on construction
and automatically ends the write operation on destruction.
This is very usefull for constructs such as:
\begin{verbatim}
void func()
{
PWriteWaitAndSignal mutexWait(myMutex);
if (condition)
return;
do_something();
if (other_condition)
return;
do_something_else();
}
\end{verbatim}
*/
class PWriteWaitAndSignal {
public:
/**Create the PWriteWaitAndSignal wait instance.
This will wait on the specified PReadWriteMutex using the #StartWrite()#
function before returning.
*/
PWriteWaitAndSignal(
const PReadWriteMutex & rw, ///< PReadWriteMutex descendent to wait/signal.
BOOL start = TRUE ///< Start write operation on PReadWriteMutex before returning.
);
/** End write operation on the PReadWriteMutex.
This will execute the EndWrite() function on the PReadWriteMutex that
was used in the construction of this instance.
*/
~PWriteWaitAndSignal();
protected:
PReadWriteMutex & mutex;
};
// End Of File ///////////////////////////////////////////////////////////////
|