Skip to content

Commit

Permalink
Introduce FiberLocalVar and LockLocalVar for alignment with Mutex s…
Browse files Browse the repository at this point in the history
…cope.

# Conflicts:
#	lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb
#	spec/concurrent/atomic/reentrant_read_write_lock_spec.rb
  • Loading branch information
ioquatix authored and eregon committed Jan 23, 2023
1 parent d867c17 commit 3046539
Show file tree
Hide file tree
Showing 8 changed files with 530 additions and 713 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
require 'concurrent/constants'

module Concurrent

# @!macro thread_local_var
# @!macro internal_implementation_note
# @!visibility private
class AbstractThreadLocalVar

# @!macro thread_local_var_method_initialize
class FiberLocalVar
def initialize(default = nil, &default_block)
if default && block_given?
raise ArgumentError, "Cannot use both value and block as default value"
Expand All @@ -21,17 +15,17 @@ def initialize(default = nil, &default_block)
@default = default
end

allocate_storage
@name = :"concurrent_variable_#{object_id}"
end

# @!macro thread_local_var_method_get
def value
raise NotImplementedError
Thread.current.fetch(@name) {default}
end

# @!macro thread_local_var_method_set
def value=(value)
raise NotImplementedError
Thread.current[@name] = value
end

# @!macro thread_local_var_method_bind
Expand All @@ -49,11 +43,6 @@ def bind(value, &block)

protected

# @!visibility private
def allocate_storage
raise NotImplementedError
end

# @!visibility private
def default
if @default_block
Expand Down
37 changes: 0 additions & 37 deletions lib/concurrent-ruby/concurrent/atomic/java_thread_local_var.rb

This file was deleted.

20 changes: 20 additions & 0 deletions lib/concurrent-ruby/concurrent/atomic/lock_local_var.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require_relative 'fiber_local_var'
require_relative 'thread_local_var'

module Concurrent
def self.mutex_owned_per_thread?
mutex = Mutex.new

# Lock the mutex:
mutex.synchronize do
# Check if the mutex is still owned in a child fiber:
Fiber.new{mutex.owned?}.resume
end
end

if mutex_owned_per_thread?
LockLocalVar = ThreadLocalVar
else
LockLocalVar = FiberLocalVar
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require 'concurrent/errors'
require 'concurrent/synchronization/object'
require 'concurrent/synchronization/lock'
require 'concurrent/atomic/thread_local_var'
require 'concurrent/atomic/lock_local_var'

module Concurrent

Expand Down Expand Up @@ -111,7 +111,7 @@ def initialize
@Counter = AtomicFixnum.new(0) # single integer which represents lock state
@ReadQueue = Synchronization::Lock.new # used to queue waiting readers
@WriteQueue = Synchronization::Lock.new # used to queue waiting writers
@HeldCount = ThreadLocalVar.new(0) # indicates # of R & W locks held by this thread
@HeldCount = LockLocalVar.new(0) # indicates # of R & W locks held by this thread
end

# Execute a block operation within a read lock.
Expand Down
181 changes: 0 additions & 181 deletions lib/concurrent-ruby/concurrent/atomic/ruby_thread_local_var.rb

This file was deleted.

Loading

0 comments on commit 3046539

Please sign in to comment.