30 #ifndef PTLIB_SYNCTHRD_H
31 #define PTLIB_SYNCTHRD_H
172 void PrintOn(ostream & strm)
const;
187 operator int()
const {
return value; }
317 virtual void PrintOn(ostream &strm)
const;
325 #if P_READ_WRITE_ALGO2
354 typedef std::map<PThreadIdentifier, Nest>
NestMap;
379 #define PDECLARE_READ_WRITE_MUTEX_ARG_1(var) struct PReadWriteMutex_##var : PReadWriteMutex { PReadWriteMutex_##var() : PReadWriteMutex(P_DEBUG_LOCATION) { } } var
380 #define PDECLARE_READ_WRITE_MUTEX_ARG_2(var,nam) struct PReadWriteMutex_##var : PReadWriteMutex { PReadWriteMutex_##var() : PReadWriteMutex(#nam, ) { } } var
381 #define PDECLARE_READ_WRITE_MUTEX_ARG_3(var,nam,to) struct PReadWriteMutex_##var : PReadWriteMutex { PReadWriteMutex_##var() : PReadWriteMutex(#nam,to ) { } } var
382 #define PDECLARE_READ_WRITE_MUTEX_ARG_4(var,nam,to,tw) struct PReadWriteMutex_##var : PReadWriteMutex { PReadWriteMutex_##var() : PReadWriteMutex(#nam,to,tw ) { } } var
383 #define PDECLARE_READ_WRITE_MUTEX_ARG_5(var,nam,to,tw,th) struct PReadWriteMutex_##var : PReadWriteMutex { PReadWriteMutex_##var() : PReadWriteMutex(#nam,to,tw,th ) { } } var
385 #define PDECLARE_READ_WRITE_MUTEX_PART1(narg, args) PDECLARE_READ_WRITE_MUTEX_PART2(narg, args)
386 #define PDECLARE_READ_WRITE_MUTEX_PART2(narg, args) PDECLARE_READ_WRITE_MUTEX_ARG_##narg args
388 #define PDECLARE_READ_WRITE_MUTEX(...) PDECLARE_READ_WRITE_MUTEX_PART1(PARG_COUNT(__VA_ARGS__), (__VA_ARGS__))
485 PInstrumentedReadWriteMutex(
486 const char * baseName,
489 const char * waitReadOnlyName,
490 const char * heldReadOnlyName,
491 const char * waitReadWriteName,
492 const char * heldReadWriteName,
495 unsigned throttleTime = 10000,
496 unsigned throttledLogLevel = 2,
497 unsigned unthrottledLogLevel = 6,
498 unsigned thresholdPercent = 5,
499 unsigned maxHistory = 0
501 , m_timeWaitReadOnlyContext (
PDebugLocation(file, line, waitReadOnlyName ), waitTime, throttleTime, throttledLogLevel, unthrottledLogLevel, thresholdPercent, maxHistory)
502 , m_timeHeldReadOnlyContext (
PDebugLocation(file, line, heldReadOnlyName ), heldTime, throttleTime, throttledLogLevel, unthrottledLogLevel, thresholdPercent, maxHistory)
503 , m_timeWaitReadWriteContext(
PDebugLocation(file, line, waitReadWriteName), waitTime, throttleTime, throttledLogLevel, unthrottledLogLevel, thresholdPercent, maxHistory)
504 , m_timeHeldReadWriteContext(
PDebugLocation(file, line, heldReadWriteName), heldTime, throttleTime, throttledLogLevel, unthrottledLogLevel, thresholdPercent, maxHistory)
507 void SetWaitReadOnlyThrottleTime(
unsigned throttleTime) { m_timeWaitReadOnlyContext.SetThrottleTime(throttleTime); }
508 void SetWaitReadOnlyThrottledLogLevel(
unsigned throttledLogLevel) { m_timeWaitReadOnlyContext.SetThrottledLogLevel(throttledLogLevel); }
509 void SetWaitReadOnlyUnthrottledLogLevel(
unsigned unthrottledLogLevel) { m_timeWaitReadOnlyContext.SetUnthrottledLogLevel(unthrottledLogLevel); }
510 void SetWaitReadOnlyThresholdPercent(
unsigned thresholdPercent) { m_timeWaitReadOnlyContext.SetThresholdPercent(thresholdPercent); }
511 void SetWaitReadOnlyMaxHistory(
unsigned maxHistory) { m_timeWaitReadOnlyContext.SetMaxHistory(maxHistory); }
513 void SetHeldReadOnlyThrottleTime(
unsigned throttleTime) { m_timeHeldReadOnlyContext.SetThrottleTime(throttleTime); }
514 void SetHeldReadOnlyThrottledLogLevel(
unsigned throttledLogLevel) { m_timeHeldReadOnlyContext.SetThrottledLogLevel(throttledLogLevel); }
515 void SetHeldReadOnlyUnthrottledLogLevel(
unsigned unthrottledLogLevel) { m_timeHeldReadOnlyContext.SetUnthrottledLogLevel(unthrottledLogLevel); }
516 void SetHeldReadOnlyThresholdPercent(
unsigned thresholdPercent) { m_timeHeldReadOnlyContext.SetThresholdPercent(thresholdPercent); }
517 void SetHeldReadOnlyMaxHistory(
unsigned maxHistory) { m_timeHeldReadOnlyContext.SetMaxHistory(maxHistory); }
519 void SetWaitReadWriteThrottleTime(
unsigned throttleTime) { m_timeWaitReadWriteContext.SetThrottleTime(throttleTime); }
520 void SetWaitReadWriteThrottledLogLevel(
unsigned throttledLogLevel) { m_timeWaitReadWriteContext.SetThrottledLogLevel(throttledLogLevel); }
521 void SetWaitReadWriteUnthrottledLogLevel(
unsigned unthrottledLogLevel) { m_timeWaitReadWriteContext.SetUnthrottledLogLevel(unthrottledLogLevel); }
522 void SetWaitReadWriteThresholdPercent(
unsigned thresholdPercent) { m_timeWaitReadWriteContext.SetThresholdPercent(thresholdPercent); }
523 void SetWaitReadWriteMaxHistory(
unsigned maxHistory) { m_timeWaitReadWriteContext.SetMaxHistory(maxHistory); }
525 void SetHeldReadWriteThrottleTime(
unsigned throttleTime) { m_timeHeldReadWriteContext.SetThrottleTime(throttleTime); }
526 void SetHeldReadWriteThrottledLogLevel(
unsigned throttledLogLevel) { m_timeHeldReadWriteContext.SetThrottledLogLevel(throttledLogLevel); }
527 void SetHeldReadWriteUnthrottledLogLevel(
unsigned unthrottledLogLevel) { m_timeHeldReadWriteContext.SetUnthrottledLogLevel(unthrottledLogLevel); }
528 void SetHeldReadWriteThresholdPercent(
unsigned thresholdPercent) { m_timeHeldReadWriteContext.SetThresholdPercent(thresholdPercent); }
529 void SetHeldReadWriteMaxHistory(
unsigned maxHistory) { m_timeHeldReadWriteContext.SetMaxHistory(maxHistory); }
544 PInstrumentedReadWaitAndSignal(
554 PInstrumentedWriteWaitAndSignal(
561 #define PDECLARE_INSTRUMENTED_READ_WRITE_MUTEX(var, name, ...) \
562 struct PInstrumentedReadWriteMutex_##name : PInstrumentedReadWriteMutex { \
563 PInstrumentedReadWriteMutex_##name(const char * mutexName = #name) \
564 : PInstrumentedReadWriteMutex(mutexName, __FILE__, __LINE__, \
565 "Wait R/O " #name, "Held R/O " #name, \
566 "Wait R/W " #name, "Held R/W " #name, \
570 #define PDECLARE_INSTRUMENTED_READ_WRITE_MUTEX(var, name, waitTime, heldTime, ...) \
571 PDECLARE_READ_WRITE_MUTEX(var, name, MinDeadlockTime(waitTime))
622 bool dequeued =
false;
639 if (available && !
m_queue.empty()) {
766 __inline
void operator=(
const PSyncQueue &) { }
770 #endif // PTLIB_SYNCTHRD_H
std::queue< T > BaseQueue
Definition: syncthrd.h:583
Information about a source file location.
Definition: object.h:333
__inline const PMutex & GetMutex() const
Definition: syncthrd.h:754
#define PMaxTimeInterval
Definition: timeint.h:31
This class waits for the semaphore on construction and automatically signals the semaphore on destruc...
Definition: psync.h:115
Definition: syncthrd.h:342
This class defines a thread synchronisation object.
Definition: semaphor.h:74
PReadWriteMutex & m_mutex
Definition: syncthrd.h:412
Greater than.
Definition: syncthrd.h:154
void(PReadWriteMutex::* StartFn)(const PDebugLocation *location)
Definition: syncthrd.h:393
~PSyncQueue()
Destroy synchronous queue.
Definition: syncthrd.h:601
EndFn m_end
Definition: syncthrd.h:414
PIntCondMutex & operator=(int newval)
Assign new condition value.
Nest & operator=(const Nest &other)
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:51
This is a PCondMutex for which the conditional is the value of an integer.
Definition: syncthrd.h:136
PIntCondMutex & operator--()
Decrement condition value.
size_t size() const
Get the current size of the queue.
Definition: syncthrd.h:739
std::map< PThreadIdentifier, Nest > NestMap
Definition: syncthrd.h:354
void Restart()
Restart the queue after it has been closed.
Definition: syncthrd.h:724
virtual void Signal()
If there are waiting (blocked) threads then unblock the first one that was blocked.
bool Dequeue(T &value, const PTimeInterval &timeout=PMaxTimeInterval)
Dequeue an object from the synchronous queue.
Definition: syncthrd.h:620
void InternalWait(Nest &nest, PSync &sync, const PDebugLocation &location) const
#define PAssertAlways(msg)
This macro is used to assert immediately.
Definition: object.h:437
This class implements critical section mutexes using the most efficient mechanism available on the ho...
Definition: mutex.h:270
void Close(bool wait)
Close the queue and break block in Dequeue() function.
Definition: syncthrd.h:699
void PrintOn(ostream &strm) const
Print the object on the stream.
PReadWriteWaitAndSignalBase(const PReadWriteMutex &mutex, const PDebugLocation *location, StartFn start, EndFn end)
Definition: syncthrd.h:396
virtual void ReleasedLock(const PObject &mutex, uint64_t startHeldSamplePoint, bool readOnly, const PDebugLocation &location)
State
Definition: syncthrd.h:585
atomic< unsigned > m_readerCount
Definition: syncthrd.h:344
BaseQueue m_queue
Definition: syncthrd.h:758
Definition: syncthrd.h:587
__inline void StartWrite()
This function attempts to acquire the mutex for writing.
Definition: syncthrd.h:299
A synchronous queue of objects.
Definition: syncthrd.h:579
Definition: syncthrd.h:590
PSyncPoint syncPoint
Definition: syncthrd.h:130
virtual void Signal()
If there are waiting (blocked) threads then unblock the first one that was blocked.
void InternalEndWriteWithNest(Nest &nest, const PDebugLocation &location)
int value
Definition: syncthrd.h:237
PIntCondMutex & operator+=(int inc)
Add to condition value.
PIntCondMutex & operator++()
Increment condition value.
virtual void WaitCondition()
This function attempts to acquire the mutex, but will block not only until the mutex is free...
NestMap m_nestedThreads
Definition: syncthrd.h:355
virtual void PrintOn(ostream &strm) const
Output the contents of the object to the stream.
__inline void StartWrite(const PDebugLocation &location)
Definition: syncthrd.h:300
This class defines a thread synchronisation object.
Definition: syncthrd.h:101
PCriticalSection m_nestingMutex
Definition: syncthrd.h:356
Operation
defines possible operators on current value and target value
Definition: syncthrd.h:144
virtual void Wait()
If the semaphore count is > 0, decrement the semaphore and return.
PWriteWaitAndSignal(const PReadWriteMutex &mutex, PBoolean start=true)
Create the PWriteWaitAndSignal wait instance.
Definition: syncthrd.h:474
PSemaphore m_readerSemaphore
Definition: syncthrd.h:333
virtual void OnWait()
This function is called immediately before blocking on the condition in the WaitCondition() function...
unsigned m_readerCount
Definition: syncthrd.h:335
__inline void StartRead()
This function attempts to acquire the mutex for reading.
Definition: syncthrd.h:276
bool empty() const
Determine if queue is empty.
Definition: syncthrd.h:747
PSyncPoint m_closed
Definition: syncthrd.h:762
State m_state
Definition: syncthrd.h:759
atomic< PUniqueThreadIdentifier > m_uniqueId
Definition: syncthrd.h:348
unsigned m_writerCount
Definition: syncthrd.h:340
virtual void Wait()
Block until the synchronisation object is available.
PSyncPoint ack
Definition: syncthrd.h:92
This class starts a read operation for the PReadWriteMutex on construction and automatically ends the...
Definition: syncthrd.h:436
void InternalEndWrite(const PDebugLocation *location)
Operation operation
Definition: syncthrd.h:238
This class defines a thread-safe object in a collection.
Definition: safecoll.h:123
bool PBoolean
Definition: object.h:174
void(PReadWriteMutex::* EndFn)(const PDebugLocation *location)
Definition: syncthrd.h:394
friend class PInstrumentedWriteWaitAndSignal
Definition: syncthrd.h:375
int target
Definition: syncthrd.h:237
Less than or equal to.
Definition: syncthrd.h:148
void InternalStartWriteWithNest(Nest &nest, const PDebugLocation &location)
Equal to.
Definition: syncthrd.h:150
Definition: syncthrd.h:390
~PReadWriteWaitAndSignalBase()
Definition: syncthrd.h:406
This class, along with the PPROFILE_TIMESCOPE() macro, allows the measurement of the time used by a s...
Definition: object.h:1430
Greater than or equal to.
Definition: syncthrd.h:152
This class starts a write operation for the PReadWriteMutex on construction and automatically ends th...
Definition: syncthrd.h:467
Definition: syncthrd.h:589
void EndWrite(const PDebugLocation &location)
Definition: syncthrd.h:314
virtual PBoolean Condition()=0
This is the condition that must be met for the WaitCondition() function to acquire the mutex...
This class defines a thread mutual exclusion object.
Definition: mutex.h:101
void Drain(bool wait)
Begin graceful draining of the queue.
Definition: syncthrd.h:679
void InternalStartRead(const PDebugLocation *location)
friend class PInstrumentedReadWaitAndSignal
Definition: syncthrd.h:374
PIntCondMutex(int value=0, int target=0, Operation operation=LE)
Create a cond mutex using an integer.
PReadWaitAndSignal(const PReadWriteMutex &mutex, bool start=true)
Create the PReadWaitAndSignal wait instance.
Definition: syncthrd.h:443
void InternalEndRead(const PDebugLocation *location)
__inline void StartRead(const PDebugLocation &location)
Definition: syncthrd.h:277
virtual void AcquiredLock(uint64_t startWaitCycle, bool readOnly, const PDebugLocation &location)
bool Enqueue(const T &obj)
Enqueue an object to the synchronous queue.
Definition: syncthrd.h:607
atomic< bool > m_waiting
Definition: syncthrd.h:346
void EndWrite()
This function attempts to release the mutex for writing.
Definition: syncthrd.h:313
atomic< unsigned > m_writerCount
Definition: syncthrd.h:345
PIntCondMutex & operator-=(int dec)
Subtract from condition value.
PSemaphore m_writerSemaphore
Definition: syncthrd.h:338
virtual void Signal()
Signal that the synchronisation object is available.
PSemaphore m_available
Definition: syncthrd.h:760
PSyncQueue()
Construct synchronous queue.
Definition: syncthrd.h:594
virtual void Signal()
If there are waiting (blocked) threads then unblock the first one that was blocked.
bool IsOpen() const
Definition: syncthrd.h:717
This class defines a thread synchronisation object.
Definition: syncthrd.h:256
Less than.
Definition: syncthrd.h:146
Definition: object.h:1537
void Acknowledge()
This indicates that the thread that was blocked in a Wait() on this synchronisation object has comple...
__inline void EndRead()
This function attempts to release the mutex for reading.
Definition: syncthrd.h:281
void InternalEndReadWithNest(Nest &nest, const PDebugLocation &location)
void InternalStartReadWithNest(Nest &nest, const PDebugLocation &location)
__inline PMutex & GetMutex()
Definition: syncthrd.h:755
This class defines a thread synchronisation object.
Definition: syncthrd.h:65
atomic< uint64_t > m_startHeldCycle
Definition: syncthrd.h:347
void InternalStartWrite(const PDebugLocation *location)
PTimedMutex m_writerMutex
Definition: syncthrd.h:339
PTimedMutex m_readerMutex
Definition: syncthrd.h:334
Definition: syncthrd.h:588
PTimedMutex m_starvationPreventer
Definition: syncthrd.h:336
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
PDebugLocation m_location
Definition: syncthrd.h:413
This class defines a thread synchronisation object.
Definition: syncpoint.h:63
virtual PBoolean Condition()
This is the condition that must be met for the WaitCondition() function to acquire the mutex...
__inline void EndRead(const PDebugLocation &location)
Definition: syncthrd.h:282