class Concurrent::RubyThreadLocalVar

def self.threadlocal_finalizer(index)

@!visibility private
def self.threadlocal_finalizer(index)
  proc do
    Thread.new do # 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
        # But that is natural! More threads means more storage is used per TLV
        # So naturally more CPU time is required to free more storage
        ARRAYS.each_value do |array|
          array[index] = nil
        end
      end
    end
  end
end