{
/**
- * Helper class to allow conditional waiting and notifications between multiple threads
+ * Helper class to allow conditional waiting and notifications between multiple threads.
*/
class ConditionalWait
{
public:
/**
+ * @brief Allows client code to synchronize updates to its own state with the
+ * internal state of a ConditionalWait object.
+ */
+ class ScopedLock
+ {
+ public:
+ /**
+ * Constructor
+ * @brief Will acquire the internal mutex of the ConditionalWait object passed in.
+ * @param[in] wait The ConditionalWait object to lock.
+ */
+ ScopedLock( ConditionalWait& wait );
+
+ /**
+ * Destructor
+ * @brief Will release the internal mutex of the ConditionalWait object passed in.
+ */
+ ~ScopedLock();
+
+ /**
+ * Getter for the ConditionalWait locked for this instance's lifetime.
+ * @return the ConditionalWait object currently locked.
+ */
+ ConditionalWait& GetLockedWait() const { return mWait; }
+
+ private:
+
+ // Not implemented as ScopedLock cannot be copied:
+ ScopedLock( const ScopedLock& );
+ const ScopedLock& operator=( const ScopedLock& );
+
+ ConditionalWait& mWait;
+ };
+
+ /**
* @brief Constructor, creates the internal synchronization objects
*/
ConditionalWait();
void Wait();
/**
- * @brief Return true if the wait is locked, i.e. someone is waiting for it
- * @return true if this object is waiting on a thread
+ * @brief Wait for another thread to notify us when the condition is true and we can continue
+ *
+ * Will always block current thread until Notify is called.
+ * Assumes that the ScopedLock object passed in has already locked the internal state of
+ * this object. Releases the lock while waiting and re-acquires it when returning
+ * from the wait.
+ * param[in] scope A preexisting lock on the internal state of this object.
+ * @pre scope must have been passed this ConditionalWait during its construction.
+ */
+ void Wait( const ScopedLock& scope );
+
+ /**
+ * @brief Return the count of threads waiting for this conditional
+ * @return count of waits
*/
- bool IsWaiting() const;
+ unsigned int GetWaitCount() const;
private:
- /// Not implemented as ConditionalWait is not copyable
+ // Not implemented as ConditionalWait is not copyable
ConditionalWait( const ConditionalWait& );
- const ConditionalWait& operator= ( const ConditionalWait& );
+ const ConditionalWait& operator=( const ConditionalWait& );
struct ConditionalWaitImpl;
ConditionalWaitImpl* mImpl;