Skip to content

Commit

Permalink
CR review (mine)
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Jun 12, 2021
1 parent 5b60cd8 commit 2e7a30e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 34 deletions.
9 changes: 9 additions & 0 deletions common/utilities/time/waiting-on.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2021 Intel Corporation. All Rights Reserved.

#pragma once

#include <mutex>
#include <condition_variable>
#include <thread>
#include <atomic>


namespace utilities {
namespace time {

// Helper class -- encapsulate a variable of type T that we want to wait on: another thread will set
// it and signal when we can continue...
//
Expand Down Expand Up @@ -123,3 +130,5 @@ class waiting_on
};


} // namespace time
} // namespace utilities
34 changes: 1 addition & 33 deletions src/dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,44 +104,12 @@ bool dispatcher::flush()
if( _was_stopped )
return true; // Nothing to do - so success (no timeout)

#if 1
waiting_on< bool > invoked( false );
utilities::time::waiting_on< bool > invoked( false );
invoke( [invoked = invoked.in_thread()]( cancellable_timer ) {
invoked.signal( true );
} );
invoked.wait_until( std::chrono::seconds( 10 ), [&]() {
return invoked || _was_stopped;
} );
return invoked;
#else
// We want to watch out for a timeout -- in which case this function will exit but the invoked
// block is still not dispatched so alive! I.e., we cannot access m/cv/invoked then!
struct wait_state_t
{
bool invoked = false;
std::condition_variable cv;
};
auto wait_state = std::make_shared< wait_state_t >();
// Add a function to the dispatcher that will set a flag and notify us when we get to it
invoke( [still_waiting = std::weak_ptr< wait_state_t >( wait_state )]( cancellable_timer t ) {
if( auto state = still_waiting.lock() )
{
state->invoked = true;
state->cv.notify_one();
}
} );
// Wait until 'invoked'
std::mutex m;
std::unique_lock< std::mutex > locker( m );
wait_state->cv.wait_for( locker, std::chrono::seconds( 10 ), [&]() {
return wait_state->invoked || _was_stopped;
} );
// If a timeout occurred: invoked will be false, _still_waiting will go out of scope and our
// function, when invoked, would not try to reference any of the locals here
return wait_state->invoked;
#endif
}
4 changes: 3 additions & 1 deletion unit-tests/utilities/time/test-waiting-on.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2020 Intel Corporation. All Rights Reserved.
// Copyright(c) 2021 Intel Corporation. All Rights Reserved.

#include <easylogging++.h>
#ifdef BUILD_SHARED_LIBS
Expand All @@ -12,6 +12,8 @@ INITIALIZE_EASYLOGGINGPP
#include <unit-tests/catch.h>
#include <common/utilities/time/waiting-on.h>

using utilities::time::waiting_on;

bool invoke( size_t delay_in_thread, size_t timeout )
{
waiting_on< bool > invoked( false );
Expand Down

0 comments on commit 2e7a30e

Please sign in to comment.