Skip to content

Commit

Permalink
Use atomic tag counter
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano committed Apr 2, 2022
1 parent ac1279f commit 19a2a85
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Measurements.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ end
@generated empty_der1(x::Measurement{T}) where {T<:AbstractFloat} = Derivatives{T}()
@generated empty_der2(x::T) where {T<:AbstractFloat} = Derivatives{x}()

function __init__()
task_local_storage("measurementsjl-tag", 1)
# Start from 1, 0 is reserved to derived quantities
const tag_counter = Threads.Atomic{UInt64}(1)

function __init__()
@require Unitful="1986cc42-f94f-5a68-af5c-568840ba703d" include("unitful.jl")
@require QuadGK="1fd47b50-473d-5c70-9696-f719f8f3bcdc" include("quadgk.jl")
@require SpecialFunctions="276daf66-3868-5448-9aa4-cd146d93841b" include("special-functions.jl")
Expand All @@ -93,7 +94,7 @@ function measurement(val::T, err::T) where {T<:AbstractFloat}
if iszero(err)
Measurement{T}(val, err, UInt64(0), newder)
else
@inbounds tag = task_local_storage("measurementsjl-tag", task_local_storage("measurementsjl-tag") + 1)
tag = Threads.atomic_add!(tag_counter, UInt64(1))
return Measurement{T}(val, err, tag, Derivatives(newder, (val, err, tag)=>one(T)))
end
end
Expand Down

0 comments on commit 19a2a85

Please sign in to comment.