This repository was archived by the owner on Mar 15, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +21
-16
lines changed Expand file tree Collapse file tree 2 files changed +21
-16
lines changed Original file line number Diff line number Diff 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 ] if rkey
31+ @lock . synchronize do
32+ rkey = ref_key ( key )
33+ @values [ rkey ] if rkey
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 def delete ( key )
50- rkey = ref_key ( key )
51- if rkey
52- @references_to_keys_map . delete ( rkey )
53- @values . delete ( rkey )
54- else
55- nil
52+ @lock . synchronize do
53+ rkey = ref_key ( key )
54+ if rkey
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 def merge! ( other_hash )
90- other_hash . each do | key , value |
91- self [ key ] = value
94+ @lock . synchronize do
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 . each do |_ , ref |
107111 return false if ref . object
108112 end
109-
110113 true
111114 end
112115
Original file line number Diff line number Diff 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 . object if ref
33- value
31+ @lock . synchronize do
32+ ref = @references [ key ]
33+ value = ref . object if ref
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 def merge! ( other_hash )
104- other_hash . each do | key , value |
105- self [ key ] = value
106+ @lock . synchronize do
107+ other_hash . each { | key , value | self [ key ] = value }
106108 end
107109 end
108110
You can’t perform that action at this time.
0 commit comments