Skip to content

Commit

Permalink
Safer finalizers
Browse files Browse the repository at this point in the history
  • Loading branch information
pitr-ch committed Aug 28, 2019
1 parent bbeacbc commit 8ea74c8
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/concurrent/atomic/ruby_thread_local_var.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def value=(value)
unless array = get_threadlocal_array(me)
array = set_threadlocal_array([], me)
LOCK.synchronize { ARRAYS[array.object_id] = array }
ObjectSpace.define_finalizer(me, self.class.thread_finalizer(array))
ObjectSpace.define_finalizer(me, self.class.thread_finalizer(array.object_id))
end
array[@index] = (value.nil? ? NULL : value)
value
Expand All @@ -83,7 +83,7 @@ def allocate_storage
# @!visibility private
def self.threadlocal_finalizer(index)
proc do
Thread.new do # avoid error: can't be called from trap context
Thread.new(index) do |index| # avoid error: can't be called from trap context
LOCK.synchronize do
FREE.push(index)
# The cost of GC'ing a TLV is linear in the number of threads using TLVs
Expand All @@ -98,13 +98,13 @@ def self.threadlocal_finalizer(index)
end

# @!visibility private
def self.thread_finalizer(array)
def self.thread_finalizer(id)
proc do
Thread.new do # avoid error: can't be called from trap context
Thread.new(id) do |id| # avoid error: can't be called from trap context
LOCK.synchronize do
# The thread which used this thread-local array is now gone
# So don't hold onto a reference to the array (thus blocking GC)
ARRAYS.delete(array.object_id)
ARRAYS.delete(id)
end
end
end
Expand Down

0 comments on commit 8ea74c8

Please sign in to comment.