Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions base/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,10 @@ function __init__()
nothing
end

# enable threads support
@eval PCRE PCRE_COMPILE_LOCK = Threads.SpinLock()

end


end # baremodule Base
4 changes: 0 additions & 4 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -514,10 +514,6 @@ MainInclude.include
function _start()
empty!(ARGS)
append!(ARGS, Core.ARGS)
if ccall(:jl_generating_output, Cint, ()) != 0 && JLOptions().incremental == 0
# clear old invalid pointers
PCRE.__init__()
end
try
exec_options(JLOptions())
catch
Expand Down
39 changes: 29 additions & 10 deletions base/pcre.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,47 @@ function create_match_context()
return ctx
end

const THREAD_MATCH_CONTEXTS = Ptr{Cvoid}[C_NULL]
THREAD_MATCH_CONTEXTS::Vector{Ptr{Cvoid}} = [C_NULL]

PCRE_COMPILE_LOCK = nothing

_tid() = Int(ccall(:jl_threadid, Int16, ())+1)
_tid() = Int(ccall(:jl_threadid, Int16, ())) + 1
_nth() = Int(unsafe_load(cglobal(:jl_n_threads, Cint)))

function get_local_match_context()
global THREAD_MATCH_CONTEXTS
tid = _tid()
ctx = @inbounds THREAD_MATCH_CONTEXTS[tid]
ctxs = THREAD_MATCH_CONTEXTS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it scary that we rely on "implicit" atomic ordering of global for code like this. It looks so innocent and the nuance can get lost during refactoring (even though I understand it is unlikely to happen for this part of code). Rather, I think it'd be easier to encode the atomic ordering by doing something like const THREAD_MATCH_CONTEXTS = Threads.Atomic(Ptr{Cvoid}[C_NULL]).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I am just waiting on #44231 to be able to specify it explicitly. For now, I am okay with relying on the specified behavior implicitly.

if length(ctxs) < tid
# slow path to allocate it
l = PCRE_COMPILE_LOCK::Threads.SpinLock
lock(l)
try
THREAD_MATCH_CONTEXTS = ctxs = copyto!(fill(C_NULL, _nth()), THREAD_MATCH_CONTEXTS)
finally
unlock(l)
end
end
ctx = @inbounds ctxs[tid]
if ctx == C_NULL
@inbounds THREAD_MATCH_CONTEXTS[tid] = ctx = create_match_context()
# slow path to allocate it
ctx = create_match_context()
l = PCRE_COMPILE_LOCK
if l === nothing
THREAD_MATCH_CONTEXTS[tid] = ctx
else
l = l::Threads.SpinLock
lock(l)
try
THREAD_MATCH_CONTEXTS[tid] = ctx
finally
unlock(l)
end
end
end
return ctx
end

function __init__()
resize!(THREAD_MATCH_CONTEXTS, _nth())
fill!(THREAD_MATCH_CONTEXTS, C_NULL)
global PCRE_COMPILE_LOCK = Threads.SpinLock()
end

# supported options for different use cases

# arguments to pcre2_compile
Expand Down