Skip to content

Commit

Permalink
More "thread-safe" LazyString
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Apr 11, 2022
1 parent 0deb326 commit 309f536
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions base/strings/lazy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ See also [`lazy"str"`](@ref).
`LazyString` requires Julia 1.8 or later.
"""
mutable struct LazyString <: AbstractString
parts::Tuple
const parts::Tuple
# Created on first access
str::String
LazyString(args...) = new(args)
@atomic str::Union{String,Nothing}
LazyString(args...) = new(args, nothing)
end

"""
Expand Down Expand Up @@ -63,14 +63,15 @@ macro lazy_str(text)
end

function String(l::LazyString)
if !isdefined(l, :str)
l.str = sprint() do io
for p in l.parts
print(io, p)
end
old = @atomic :acquire l.str
old === nothing || return old
str = sprint() do io
for p in l.parts
print(io, p)
end
end
return l.str
old = @atomicswap :acquire_release l.str = str
return something(old, str)
end

hash(s::LazyString, h::UInt64) = hash(String(s), h)
Expand Down

0 comments on commit 309f536

Please sign in to comment.