Skip to content

Commit

Permalink
Merge pull request #482 from leapmotion/feature-nowaitbarrier
Browse files Browse the repository at this point in the history
Add timeout-free Barrier variant
  • Loading branch information
gtremper committed Apr 2, 2015
2 parents 2e311b4 + cb47b58 commit 1584e54
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions autowiring/DispatchQueue.h
Expand Up @@ -200,6 +200,11 @@ class DispatchQueue {
/// </remarks>
bool Barrier(std::chrono::nanoseconds timeout);

/// <summary>
/// Identical to the timed version of Barrier, but does not time out
/// </summary>
void Barrier(void);

/// <summary>
/// Recommends a point in time to wake up to check for events
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions src/autowiring/DispatchQueue.cpp
Expand Up @@ -193,6 +193,21 @@ bool DispatchQueue::Barrier(std::chrono::nanoseconds timeout) {
return rv;
}

void DispatchQueue::Barrier(void) {
// Set up the lambda:
bool complete = false;
*this += [&] { complete = true; };

// Obtain the lock, wait until our variable is satisfied, which might be right away:
std::unique_lock<std::mutex> lk(m_dispatchLock);
m_queueUpdated.wait(lk, [&] { return m_aborted || complete; });
if (m_aborted)
// At this point, the dispatch queue MUST be completely run down. We have no outstanding references
// to our stack-allocated "complete" variable. Furthermore, after m_aborted is true, no further
// dispatchers are permitted to be run.
throw autowiring_error("Dispatch queue was aborted while a barrier was invoked");
}

std::chrono::steady_clock::time_point
DispatchQueue::SuggestSoonestWakeupTimeUnsafe(std::chrono::steady_clock::time_point latestTime) const {
return
Expand Down
2 changes: 1 addition & 1 deletion src/autowiring/test/DispatchQueueTest.cpp
Expand Up @@ -129,4 +129,4 @@ TEST_F(DispatchQueueTest, BarrierWithAbort) {
ct->Abort();
ASSERT_EQ(std::future_status::ready, f.wait_for(std::chrono::seconds(5))) << "Barrier did not abort fast enough";
ASSERT_TRUE(*exception) << "Exception should have been thrown inside the Barrier call";
}
}

0 comments on commit 1584e54

Please sign in to comment.