Skip to content
Closed
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
1 change: 1 addition & 0 deletions src/TimeZones.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ end
include("compat.jl")
include("utils.jl")
include("indexable_generator.jl")
include("readers_writer_lock.jl")

include("class.jl")
include("utcoffset.jl")
Expand Down
61 changes: 61 additions & 0 deletions src/readers_writer_lock.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Base.Threads: AbstractLock, Atomic

struct StateLock{L} <: AbstractLock
state::Atomic{UInt8}
end

const ReadersLock = StateLock{:Readers}
const WriterLock = StateLock{:Writer}

function Base.lock(r::ReadersLock)
while true
x = r.state[]
if x != 0xff
y = x + 0x01
if Threads.atomic_cas!(r.state, x, y) == x
break
end
end
end
end

function Base.unlock(r::ReadersLock)
Threads.atomic_sub!(r.state, 0x01)
end

function Base.lock(w::WriterLock)
while true
x = w.state[]
if x == 0x00
if Threads.atomic_cas!(w.state, x, 0xff) == x
break
end
end
end
end

function Base.unlock(w::WriterLock)
Threads.atomic_xchg!(w.state, 0x00)
end

# https://en.wikipedia.org/wiki/Readers–writer_lock
# https://yizhang82.dev/lock-free-rw-lock

"""
ReadersWriterLock

Allow for concurrent read-only operations, while providing exclusive access for write
operations.
"""
struct ReadersWriterLock
readers::ReadersLock
writer::WriterLock

function ReadersWriterLock()
state = Threads.Atomic{UInt8}(0)
readers = ReadersLock(state)
writer = WriterLock(state)

return new(readers, writer)
end
end
44 changes: 29 additions & 15 deletions src/types/timezone.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const TIME_ZONE_CACHE = Dict{String,Tuple{TimeZone,Class}}()
const TZ_CACHE_LOCK = ReadersWriterLock()


"""
TimeZone(str::AbstractString) -> TimeZone
Expand Down Expand Up @@ -41,24 +43,36 @@ US/Pacific (UTC-8/UTC-7)
TimeZone(::AbstractString, ::Class)

function TimeZone(str::AbstractString, mask::Class=Class(:DEFAULT))
lock(TZ_CACHE_LOCK.readers)

# Note: If the class `mask` does not match the time zone we'll still load the
# information into the cache to ensure the result is consistent.
tz, class = get!(TIME_ZONE_CACHE, str) do
tz_path = joinpath(TZData.COMPILED_DIR, split(str, "/")...)

if isfile(tz_path)
open(deserialize, tz_path, "r")
elseif occursin(FIXED_TIME_ZONE_REGEX, str)
FixedTimeZone(str), Class(:FIXED)
elseif !isdir(TZData.COMPILED_DIR) || isempty(readdir(TZData.COMPILED_DIR))
# Note: Julia 1.0 supresses the build logs which can hide issues in time zone
# compliation which result in no tzdata time zones being available.
throw(ArgumentError(
"Unable to find time zone \"$str\". Try running `TimeZones.build()`."
))
else
throw(ArgumentError("Unknown time zone \"$str\""))
if haskey(TIME_ZONE_CACHE, str)
tz, class = TIME_ZONE_CACHE[str]
unlock(TZ_CACHE_LOCK.readers)
else
unlock(TZ_CACHE_LOCK.readers)
lock(TZ_CACHE_LOCK.writer)

tz, class = get!(TIME_ZONE_CACHE, str) do
tz_path = joinpath(TZData.COMPILED_DIR, split(str, "/")...)

if isfile(tz_path)
open(deserialize, tz_path, "r")
elseif occursin(FIXED_TIME_ZONE_REGEX, str)
FixedTimeZone(str), Class(:FIXED)
elseif !isdir(TZData.COMPILED_DIR) || isempty(readdir(TZData.COMPILED_DIR))
# Note: Julia 1.0 supresses the build logs which can hide issues in time zone
# compliation which result in no tzdata time zones being available.
throw(ArgumentError(
"Unable to find time zone \"$str\". Try running `TimeZones.build()`."
))
else
throw(ArgumentError("Unknown time zone \"$str\""))
end
end

unlock(TZ_CACHE_LOCK.writer)
end

if mask & class == Class(:NONE)
Expand Down