Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make grisu thread-safe again #27826

Merged
merged 1 commit into from
Jun 28, 2018
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
20 changes: 11 additions & 9 deletions base/grisu/grisu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@ const SHORTEST = 1
const FIXED = 2
const PRECISION = 3

const DIGITS = Vector{UInt8}(undef, 309+17)

# thread-safe code should use a per-thread DIGITS buffer DIGITSs[Threads.threadid()]
const DIGITSs = [DIGITS]
function __init__()
Threads.resize_nthreads!(DIGITSs)
end

include("grisu/float.jl")
include("grisu/fastshortest.jl")
include("grisu/fastprecision.jl")
include("grisu/fastfixed.jl")
include("grisu/bignums.jl")
include("grisu/bignum.jl")

const DIGITS = Vector{UInt8}(undef, 309+17)
const BIGNUMS = [Bignums.Bignum(),Bignums.Bignum(),Bignums.Bignum(),Bignums.Bignum()]

function grisu(v::AbstractFloat,mode,requested_digits,buffer=DIGITSs[Threads.threadid()],bignums=BIGNUMS)
# thread-safe code should use a per-thread DIGITS buffer DIGITSs[Threads.threadid()]
const DIGITSs = [DIGITS]
const BIGNUMSs = [BIGNUMS]
function __init__()
Threads.resize_nthreads!(DIGITSs)
Threads.resize_nthreads!(BIGNUMSs)
end


function grisu(v::AbstractFloat,mode,requested_digits,buffer=DIGITSs[Threads.threadid()],bignums=BIGNUMSs[Threads.threadid()])
if signbit(v)
neg = true
v = -v
Expand Down
4 changes: 2 additions & 2 deletions base/threads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ include("locks.jl")
resize_nthreads!(A, copyvalue=A[1])

Resize the array `A` to length [`nthreads()`](@ref). Any new
elements that are allocated are initialized to `copy(copyvalue)`,
elements that are allocated are initialized to `deepcopy(copyvalue)`,
where `copyvalue` defaults to `A[1]`.

This is typically used to allocate per-thread variables, and
Expand All @@ -24,7 +24,7 @@ function resize_nthreads!(A::AbstractVector, copyvalue=A[1])
nold = length(A)
resize!(A, nthr)
for i = nold+1:nthr
A[i] = copy(copyvalue)
A[i] = deepcopy(copyvalue)
end
return A
end
Expand Down