Skip to content
This repository was archived by the owner on Mar 15, 2022. It is now read-only.

Commit c6d5fac

Browse files
committed
Ensure thread-safety
1 parent 3a5012e commit c6d5fac

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

‎lib/ref/abstract_reference_key_map.rb‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ def initialize
2828
# Get a value from the map by key. If the value has been reclaimed by the garbage
2929
# collector, this will return nil.
3030
def [](key)
31-
rkey=ref_key(key)
32-
@values[rkey]ifrkey
31+
@lock.synchronizedo
32+
rkey=ref_key(key)
33+
@values[rkey]ifrkey
34+
end
3335
end
3436

3537
alias_method:get,:[]
@@ -47,12 +49,14 @@ def []=(key, value)
4749

4850
# Remove the value associated with the key from the map.
4951
defdelete(key)
50-
rkey=ref_key(key)
51-
ifrkey
52-
@references_to_keys_map.delete(rkey)
53-
@values.delete(rkey)
54-
else
55-
nil
52+
@lock.synchronizedo
53+
rkey=ref_key(key)
54+
ifrkey
55+
@references_to_keys_map.delete(rkey)
56+
@values.delete(rkey)
57+
else
58+
nil
59+
end
5660
end
5761
end
5862

@@ -87,8 +91,8 @@ def clear
8791

8892
# Merge the values from another hash into this map.
8993
defmerge!(other_hash)
90-
other_hash.eachdo |key,value|
91-
self[key]=value
94+
@lock.synchronizedo
95+
other_hash.each{ |key,value| self[key]=value}
9296
end
9397
end
9498

@@ -106,7 +110,6 @@ def empty?
106110
@references_to_keys_map.eachdo |_,ref|
107111
returnfalseifref.object
108112
end
109-
110113
true
111114
end
112115

‎lib/ref/abstract_reference_value_map.rb‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ def initialize
2828
# Get a value from the map by key. If the value has been reclaimed by the garbage
2929
# collector, this will return nil.
3030
def [](key)
31-
ref=@references[key]
32-
value=ref.objectifref
33-
value
31+
@lock.synchronizedo
32+
ref=@references[key]
33+
value=ref.objectifref
34+
value
35+
end
3436
end
3537

3638
alias_method:get,:[]
@@ -101,8 +103,8 @@ def clear
101103

102104
# Merge the values from another hash into this map.
103105
defmerge!(other_hash)
104-
other_hash.eachdo |key,value|
105-
self[key]=value
106+
@lock.synchronizedo
107+
other_hash.each{ |key,value| self[key]=value}
106108
end
107109
end
108110

0 commit comments

Comments
(0)