Skip to content

Commit 8c85c4f

Browse files
authored
Use UnsafeAtomics for real FAA (#2)
1 parent fc03fc0 commit 8c85c4f

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@ uuid = "78a604cf-3861-458a-9dc0-49592e8d97d7"
33
authors = ["Takafumi Arakaki <[email protected]> and contributors"]
44
version = "0.1.0-DEV"
55

6+
[deps]
7+
UnsafeAtomics = "013be700-e6cd-48c3-b4a1-df204f14c38f"
8+
69
[compat]
10+
UnsafeAtomics = "0.1"
711
julia = "1.7"

src/LeftRight.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ function guarding end
88

99
module Internal
1010

11+
using UnsafeAtomics: UnsafeAtomics, acq_rel, seq_cst
12+
1113
using ..LeftRight: LeftRight
1214

1315
include("utils.jl")

src/core.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,31 @@ end
1212

1313
ReadIndicator() = ReadIndicator(0, pad7(), nothing)
1414

15+
const OFFSET_STATE =
16+
fieldoffset(ReadIndicator, findfirst(==(:state), fieldnames(ReadIndicator)))
17+
1518
function arrive!(ind::ReadIndicator)
19+
#=
1620
@atomic ind.state += UInt(1) # [^seq_cst_state_leftright]
21+
=#
22+
ptr = Ptr{UInt}(pointer_from_objref(ind) + OFFSET_STATE)
23+
GC.@preserve ind begin
24+
UnsafeAtomics.modify!(ptr, +, UInt(1), seq_cst)
25+
end
1726
return ind
1827
end
1928

2029
function depart!(ind::ReadIndicator)
30+
#=
2131
state = @atomic(
2232
:acquire_release, # [^acq_rel_ind_state] [^acq_rel_ind_waiter]
2333
ind.state -= UInt(1)
2434
)
35+
=#
36+
ptr = Ptr{UInt}(pointer_from_objref(ind) + OFFSET_STATE)
37+
GC.@preserve ind begin
38+
_old, state = UnsafeAtomics.modify!(ptr, -, UInt(1), acq_rel)
39+
end
2540
if state == HAS_WAITER_MASK # no readers, one waiter
2641
# Choosing at most one reader task that wakes up the waiter using CAS [^cas_waker].
2742
_, ok = @atomicreplace(

0 commit comments

Comments
 (0)