From 89cd08fc9b4c47725d3da311f10d41c2c1db9b22 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Mon, 9 Mar 2020 15:14:50 +0100 Subject: [PATCH] Threadsafe configuration. --- src/CuArrays.jl | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/CuArrays.jl b/src/CuArrays.jl index beaafe13..5ed05137 100644 --- a/src/CuArrays.jl +++ b/src/CuArrays.jl @@ -33,15 +33,30 @@ fail to check whether CUDA is functional, actual use of functionality might warn """ function functional(show_reason::Bool=false) if configured[] === nothing - configured[] = false - if __configure__(show_reason) - configured[] = true - __runtime_init__() - end + _functional(show_reason) end configured[] end +const configure_lock = ReentrantLock() +@noinline function _functional(show_reason::Bool=false) + lock(configure_lock) do + if configured[] === nothing + if __configure__(show_reason) + configured[] = true + try + __runtime_init__() + catch + configured[] = false + rethrow() + end + else + configured[] = false + end + end + end +end + # macro to guard code that only can run after the package has successfully initialized macro after_init(ex) quote