From 2e7a30e1210879f8e9cbb890016ada2528b77797 Mon Sep 17 00:00:00 2001 From: Eran Date: Sat, 12 Jun 2021 18:10:02 +0300 Subject: [PATCH] CR review (mine) --- common/utilities/time/waiting-on.h | 9 +++++ src/dispatcher.cpp | 34 +------------------ unit-tests/utilities/time/test-waiting-on.cpp | 4 ++- 3 files changed, 13 insertions(+), 34 deletions(-) diff --git a/common/utilities/time/waiting-on.h b/common/utilities/time/waiting-on.h index 886cf2b7d2..b34a52f9a7 100644 --- a/common/utilities/time/waiting-on.h +++ b/common/utilities/time/waiting-on.h @@ -1,3 +1,6 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2021 Intel Corporation. All Rights Reserved. + #pragma once #include @@ -5,6 +8,10 @@ #include #include + +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... // @@ -123,3 +130,5 @@ class waiting_on }; +} // namespace time +} // namespace utilities diff --git a/src/dispatcher.cpp b/src/dispatcher.cpp index bffeaaefc0..b466c5de9f 100644 --- a/src/dispatcher.cpp +++ b/src/dispatcher.cpp @@ -104,8 +104,7 @@ 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 ); } ); @@ -113,35 +112,4 @@ bool dispatcher::flush() 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 } diff --git a/unit-tests/utilities/time/test-waiting-on.cpp b/unit-tests/utilities/time/test-waiting-on.cpp index 2a8e7ee478..dd9dbe48d8 100644 --- a/unit-tests/utilities/time/test-waiting-on.cpp +++ b/unit-tests/utilities/time/test-waiting-on.cpp @@ -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 #ifdef BUILD_SHARED_LIBS @@ -12,6 +12,8 @@ INITIALIZE_EASYLOGGINGPP #include #include +using utilities::time::waiting_on; + bool invoke( size_t delay_in_thread, size_t timeout ) { waiting_on< bool > invoked( false );