Skip to content

Commit

Permalink
Make grisu thread-safe again
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbyrne committed Jun 27, 2018
1 parent 6f22d13 commit 7fcfe16
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
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

0 comments on commit 7fcfe16

Please sign in to comment.