123456789_123456789_123456789_123456789_123456789_

Class: Concurrent::ThreadLocals

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Concurrent::AbstractLocals
Defined in: lib/concurrent-ruby/concurrent/atomic/locals.rb

Overview

Note:

**Private Implementation:** This abstraction is a private, internal implementation detail. It should never be used directly.

An array-backed storage of indexed variables per thread.

Class Method Summary

AbstractLocals - Inherited

Instance Method Summary

AbstractLocals - Inherited

#fetch, #free_index, #next_index, #set, #synchronize,
#weak_synchronize
#local_finalizer

When the local goes out of scope, clean up that slot across all locals currently assigned.

#locals

Returns the locals for the current scope, or nil if none exist.

#locals!

Returns the locals for the current scope, creating them if necessary.

#thread_fiber_finalizer

When a thread/fiber goes out of scope, remove the array from @all_arrays.

Constructor Details

This class inherits a constructor from Concurrent::AbstractLocals

Instance Method Details

#locals

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/locals.rb', line 142

def locals
  Thread.current.thread_variable_get(:concurrent_thread_locals)
end

#locals!

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/locals.rb', line 146

def locals!
  thread = Thread.current
  locals = thread.thread_variable_get(:concurrent_thread_locals)

  unless locals
    locals = thread.thread_variable_set(:concurrent_thread_locals, [])
    weak_synchronize do
      @all_arrays[locals.object_id] = locals
    end
    # When the thread goes out of scope, we should delete the associated locals:
    ObjectSpace.define_finalizer(thread, thread_fiber_finalizer(locals.object_id))
  end

  locals
end