Skip to content

Commit cdbd38c

Browse files
vtjnashHans-Peter Suter
authored andcommitted
remove gc-token from WeakKeyDict (#33825)
This was a hold-over from the old iteration protocol, which needed to maintain state between `done` and `next`. The `iteration` function of `Dict` has since been re-written to be safe for concurrent deletions. Replaces: #33756 Co-Authored-By: Hans-Peter Suter <[email protected]> (cherry picked from commit 1731d0a)
1 parent 3d45ff8 commit cdbd38c

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

base/weakkeydict.jl

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
WeakKeyDict([itr])
77
88
`WeakKeyDict()` constructs a hash table where the keys are weak
9-
references to objects, and thus may be garbage collected even when
9+
references to objects which may be garbage collected even when
1010
referenced in a hash table.
1111
1212
See [`Dict`](@ref) for further help. Note, unlike [`Dict`](@ref),
@@ -112,24 +112,12 @@ getindex(wkh::WeakKeyDict{K}, key) where {K} = lock(() -> getindex(wkh.ht, key),
112112
isempty(wkh::WeakKeyDict) = isempty(wkh.ht)
113113
length(t::WeakKeyDict) = length(t.ht)
114114

115-
function iterate(t::WeakKeyDict{K,V}) where V where K
116-
gc_token = Ref{Bool}(false) # no keys will be deleted via finalizers until this token is gc'd
117-
finalizer(gc_token) do r
118-
if r[]
119-
r[] = false
120-
unlock(t.lock)
121-
end
122-
end
123-
s = lock(t.lock)
124-
iterate(t, (gc_token,))
125-
end
126-
function iterate(t::WeakKeyDict{K,V}, state) where V where K
127-
gc_token = first(state)
128-
y = iterate(t.ht, tail(state)...)
115+
function iterate(t::WeakKeyDict{K,V}, state...) where {K, V}
116+
y = lock(() -> iterate(t.ht, state...), t)
129117
y === nothing && return nothing
130-
wkv, i = y
118+
wkv, newstate = y
131119
kv = Pair{K,V}(wkv[1].value::K, wkv[2])
132-
return (kv, (gc_token, i))
120+
return (kv, newstate)
133121
end
134122

135123
filter!(f, d::WeakKeyDict) = filter_in_one_pass!(f, d)

0 commit comments

Comments
 (0)