diff --git a/NEWS.md b/NEWS.md index 0590f4d727722..f04730df5dd9b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -43,6 +43,9 @@ New library functions * `uuid5` has been added to the `UUIDs` standard library ([#28761]). * Predicate functions `Sys.isfreebsd`, `Sys.isopenbsd`, `Sys.isnetbsd`, and `Sys.isdragonfly` for detecting BSD systems have been added ([#30249]). + * Internal `Base.disable_library_threading` that sets libraries to use one thread. + It executes function hooks that have been registered with + `Base.at_disable_library_threading` ([#30004]). Standard library changes ------------------------ diff --git a/base/initdefs.jl b/base/initdefs.jl index 164389827cf4d..60b6a07580fc1 100644 --- a/base/initdefs.jl +++ b/base/initdefs.jl @@ -236,3 +236,30 @@ function _atexit() end end end + +## hook for disabling threaded libraries ## + +library_threading_enabled = true +const disable_library_threading_hooks = [] + +function at_disable_library_threading(f) + push!(disable_library_threading_hooks, f) + if !library_threading_enabled + disable_library_threading() + end + return +end + +function disable_library_threading() + global library_threading_enabled = false + while !isempty(disable_library_threading_hooks) + f = pop!(disable_library_threading_hooks) + try + f() + catch err + @warn("a hook from a library to disable threading failed:", + exception = (err, catch_backtrace())) + end + end + return +end diff --git a/stdlib/Distributed/Project.toml b/stdlib/Distributed/Project.toml index e40ce0d9825da..af0e5ca7c5806 100644 --- a/stdlib/Distributed/Project.toml +++ b/stdlib/Distributed/Project.toml @@ -2,7 +2,6 @@ name = "Distributed" uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" [deps] -LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Sockets = "6462fe0b-24de-5631-8697-dd941f90decc" diff --git a/stdlib/Distributed/src/cluster.jl b/stdlib/Distributed/src/cluster.jl index bad20054fbf03..50f81251be71a 100644 --- a/stdlib/Distributed/src/cluster.jl +++ b/stdlib/Distributed/src/cluster.jl @@ -163,12 +163,6 @@ mutable struct LocalProcess LocalProcess() = new(1) end - -import LinearAlgebra -function disable_threaded_libs() - LinearAlgebra.BLAS.set_num_threads(1) -end - worker_timeout() = parse(Float64, get(ENV, "JULIA_WORKER_TIMEOUT", "60.0")) diff --git a/stdlib/Distributed/src/process_messages.jl b/stdlib/Distributed/src/process_messages.jl index 4ba89347f52cb..2caf423f8c646 100644 --- a/stdlib/Distributed/src/process_messages.jl +++ b/stdlib/Distributed/src/process_messages.jl @@ -307,7 +307,7 @@ function handle_msg(msg::JoinPGRPMsg, header, r_stream, w_stream, version) topology(msg.topology) if !msg.enable_threaded_blas - disable_threaded_libs() + Base.disable_library_threading() end lazy = msg.lazy diff --git a/stdlib/LinearAlgebra/src/LinearAlgebra.jl b/stdlib/LinearAlgebra/src/LinearAlgebra.jl index e4bc35dab03e2..d7b1984f7f4d1 100644 --- a/stdlib/LinearAlgebra/src/LinearAlgebra.jl +++ b/stdlib/LinearAlgebra/src/LinearAlgebra.jl @@ -437,6 +437,8 @@ function __init__() Base.showerror_nostdio(ex, "WARNING: Error during initialization of module LinearAlgebra") end + # register a hook to disable BLAS threading + Base.at_disable_library_threading(() -> BLAS.set_num_threads(1)) end end # module LinearAlgebra