// CAUTION: THIS IS NOT A FULLY GENERAL BARRIER API. Some names are unconventional.
// It may either be used as a "latch" or single-use barrier, or it may be reused under // very limited conditions, e.g. if only Pass(), but not Wait() is called. Unlike a standard // latch API, it is possible to initialize the latch to a count of zero, repeatedly call // Pass() or Wait(), and only then set the count using the Increment() method. Threads at // a Wait() are only awoken if the count reaches zero AFTER the decrement is applied. // This works because, also unlike most latch APIs, there is no way to Wait() without // decrementing the count, and thus nobody can spuriously wake up on the initial zero.
// TODO: Maybe give this a better name. class Barrier { public: enum EXPORT LockHandling {
kAllowHoldingLocks,
kDisallowHoldingLocks,
};
// If verify_count_on_shutdown is true, the destructor verifies that the count is zero in the // destructor. This means that all expected threads went through the barrier.
EXPORT explicit Barrier(int count, bool verify_count_on_shutdown = true);
EXPORT virtual ~Barrier();
// Pass through the barrier, decrement the count but do not block.
EXPORT void Pass(Thread* self) REQUIRES(!GetLock()); // Increment the barrier but do not block. The caller should ensure that it // decrements/passes it eventually. void IncrementNoWait(Thread* self) REQUIRES(!GetLock());
// Decrement the count, then wait until the count is zero. void Wait(Thread* self) REQUIRES(!GetLock());
// The following three calls are only safe if we somehow know that no other thread both // - has been woken up, and // - has not left the Wait() or Increment() call. // If these calls are made in that situation, the offending thread is likely to go back // to sleep, resulting in a deadlock.
// Increment the count by delta, wait on condition while count is non zero. If LockHandling is // kAllowHoldingLocks we will not check that all locks are released when waiting. template <Barrier::LockHandling locks = kDisallowHoldingLocks>
EXPORT void Increment(Thread* self, int delta) REQUIRES(!GetLock());
// Increment the count by delta, wait on condition while count is non zero, with a timeout. // Returns true if time out occurred. bool Increment(Thread* self, int delta, uint32_t timeout_ms) REQUIRES(!GetLock());
// Set the count to a new value. This should only be used if there is no possibility that // another thread is still in Wait(). See above. void Init(Thread* self, int count) REQUIRES(!GetLock());
int GetCount(Thread* self) REQUIRES(!GetLock());
private: void SetCountLocked(Thread* self, int count) REQUIRES(GetLock());
Mutex* GetLock() { return lock_.get();
}
// Counter, when this reaches 0 all people blocked on the barrier are signalled. int count_ GUARDED_BY(GetLock());
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.