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 <Matthew.Bentham@arm.com>
Change-Id: I7638272a5d43a24a861f3e6d63f3ee7b099483b5
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/c/VisualCompute/ComputeLibrary/+/538048
Comments-Addressed: bsgcomp <bsgcomp@arm.com>
Tested-by: bsgcomp <bsgcomp@arm.com>
Reviewed-by: Viet-Hoa Do <viet-hoa.do@arm.com>
Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/9957
Tested-by: Arm Jenkins <bsgcomp@arm.com>
Comments-Addressed: Arm Jenkins <bsgcomp@arm.com>
Benchmark: Arm Jenkins <bsgcomp@arm.com>
diff --git a/SConstruct b/SConstruct
index 4480c71..68c518a 100644
--- a/SConstruct
+++ b/SConstruct
@@ -133,6 +133,7 @@
     ListVariable("data_layout_support", "Enable a list of data layout to support", "all", ["nhwc", "nchw"]),
     ("toolchain_prefix", "Override the toolchain prefix; used by all toolchain components: compilers, linker, assembler etc. If unspecified, use default(auto) prefixes; if passed an empty string '' prefixes would be disabled", "auto"),
     ("compiler_prefix", "Override the compiler prefix; used by just compilers (CC,CXX); further overrides toolchain_prefix for compilers; this is for when the compiler prefixes are different from that of the linkers, archivers etc. If unspecified, this is the same as toolchain_prefix; if passed an empty string '' prefixes would be disabled", "auto"),
+    BoolVariable("thread_sanitizer", "Enable ThreadSanitizer", False),
     ("extra_cxx_flags", "Extra CXX flags to be appended to the build command", ""),
     ("extra_link_flags", "Extra LD flags to be appended to the build command", ""),
     ("compiler_cache", "Command to prefix to the C and C++ compiler (e.g ccache)", ""),
@@ -613,6 +614,10 @@
 if env['logging']:
     env.Append(CPPDEFINES = ['ARM_COMPUTE_LOGGING_ENABLED'])
 
+if env['thread_sanitizer']:
+    env.Append(CXXFLAGS = ['-fsanitize=thread'])
+    env.Append(LINKFLAGS = ['-fsanitize=thread'])
+
 env.Append(CPPPATH = ['#/include', "#"])
 env.Append(CXXFLAGS = env['extra_cxx_flags'])
 env.Append(LINKFLAGS = env['extra_link_flags'])