Skip to content

Commit 3046539

Browse files
ioquatixeregon
authored andcommitted
Introduce FiberLocalVar and LockLocalVar for alignment with Mutex scope.
# Conflicts: # lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb # spec/concurrent/atomic/reentrant_read_write_lock_spec.rb
1 parent d867c17 commit 3046539

File tree

8 files changed

+530
-713
lines changed

8 files changed

+530
-713
lines changed

lib/concurrent-ruby/concurrent/atomic/abstract_thread_local_var.rb renamed to lib/concurrent-ruby/concurrent/atomic/fiber_local_var.rb

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
require 'concurrent/constants'
22

33
module Concurrent
4-
5-
# @!macro thread_local_var
6-
# @!macro internal_implementation_note
7-
# @!visibility private
8-
class AbstractThreadLocalVar
9-
10-
# @!macro thread_local_var_method_initialize
4+
class FiberLocalVar
115
def initialize(default = nil, &default_block)
126
if default && block_given?
137
raise ArgumentError, "Cannot use both value and block as default value"
@@ -21,17 +15,17 @@ def initialize(default = nil, &default_block)
2115
@default = default
2216
end
2317

24-
allocate_storage
18+
@name = :"concurrent_variable_#{object_id}"
2519
end
2620

2721
# @!macro thread_local_var_method_get
2822
def value
29-
raise NotImplementedError
23+
Thread.current.fetch(@name) {default}
3024
end
3125

3226
# @!macro thread_local_var_method_set
3327
def value=(value)
34-
raise NotImplementedError
28+
Thread.current[@name] = value
3529
end
3630

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

5044
protected
5145

52-
# @!visibility private
53-
def allocate_storage
54-
raise NotImplementedError
55-
end
56-
5746
# @!visibility private
5847
def default
5948
if @default_block

lib/concurrent-ruby/concurrent/atomic/java_thread_local_var.rb

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require_relative 'fiber_local_var'
2+
require_relative 'thread_local_var'
3+
4+
module Concurrent
5+
def self.mutex_owned_per_thread?
6+
mutex = Mutex.new
7+
8+
# Lock the mutex:
9+
mutex.synchronize do
10+
# Check if the mutex is still owned in a child fiber:
11+
Fiber.new{mutex.owned?}.resume
12+
end
13+
end
14+
15+
if mutex_owned_per_thread?
16+
LockLocalVar = ThreadLocalVar
17+
else
18+
LockLocalVar = FiberLocalVar
19+
end
20+
end

lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require 'concurrent/errors'
55
require 'concurrent/synchronization/object'
66
require 'concurrent/synchronization/lock'
7-
require 'concurrent/atomic/thread_local_var'
7+
require 'concurrent/atomic/lock_local_var'
88

99
module Concurrent
1010

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

117117
# Execute a block operation within a read lock.

lib/concurrent-ruby/concurrent/atomic/ruby_thread_local_var.rb

Lines changed: 0 additions & 181 deletions
This file was deleted.

0 commit comments

Comments
 (0)