From 0a59e69fd922b02d9e3b5b043ee7f891061df7be Mon Sep 17 00:00:00 2001 From: Matthew Bentham Date: Wed, 19 Jul 2023 15:01:00 +0000 Subject: Fix problem with exception handling in CPPScheduler If an exception was thrown in the main thread, the child threads were not being synchronised, leading to undefined behaviour (and probably the program crashing instead of correctly propagating the exception). Add support to the build system for enabling ThreadSanitizer. This needs a build system option rather than simply passing extra_cxx/link_flags because the sanitizer options are incompatible with -Wl,-undefined,error. Add a unit test for throwing an exception within a CPPScheduler workload. Signed-off-by: Matthew Bentham Change-Id: I7638272a5d43a24a861f3e6d63f3ee7b099483b5 Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/c/VisualCompute/ComputeLibrary/+/538048 Comments-Addressed: bsgcomp Tested-by: bsgcomp Reviewed-by: Viet-Hoa Do Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/9957 Tested-by: Arm Jenkins Comments-Addressed: Arm Jenkins Benchmark: Arm Jenkins --- src/runtime/CPP/CPPScheduler.cpp | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'src/runtime') diff --git a/src/runtime/CPP/CPPScheduler.cpp b/src/runtime/CPP/CPPScheduler.cpp index 39811ec156..45e872428f 100644 --- a/src/runtime/CPP/CPPScheduler.cpp +++ b/src/runtime/CPP/CPPScheduler.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2022 Arm Limited. + * Copyright (c) 2016-2023 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -172,7 +172,7 @@ public: void start(); /** Wait for the current kernel execution to complete. */ - void wait(); + std::exception_ptr wait(); /** Function ran by the worker thread. */ void worker_thread(); @@ -244,17 +244,13 @@ void Thread::start() _cv.notify_one(); } -void Thread::wait() +std::exception_ptr Thread::wait() { { std::unique_lock lock(_m); _cv.wait(lock, [&] { return _job_complete; }); } - - if(_current_exception) - { - std::rethrow_exception(_current_exception); - } + return _current_exception; } void Thread::worker_thread() @@ -509,15 +505,34 @@ void CPPScheduler::run_workloads(std::vector &workloads) thread_it->start(); } info.thread_id = t; // Set main thread's thread_id - process_workloads(workloads, feeder, info); // Main thread processes workloads + std::exception_ptr last_exception = nullptr; #ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED + try + { +#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */ + process_workloads(workloads, feeder, info); // Main thread processes workloads +#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED + } + catch (...) + { + last_exception = std::current_exception(); + } + try { #endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */ thread_it = _impl->_threads.begin(); for(unsigned int i = 0; i < num_threads_to_use - 1; ++i, ++thread_it) { - thread_it->wait(); + std::exception_ptr current_exception = thread_it->wait(); + if (current_exception) + { + last_exception = current_exception; + } + } + if (last_exception) + { + std::rethrow_exception(last_exception); } #ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED } -- cgit v1.2.1