Skip to content

Commit a026804

Browse files
committed
Fix naming in synchronization layer
1 parent aaeaef9 commit a026804

File tree

8 files changed

+89
-73
lines changed

8 files changed

+89
-73
lines changed

lib/concurrent/synchronization.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
require 'concurrent/synchronization/mri_object'
88
require 'concurrent/synchronization/jruby_object'
99
require 'concurrent/synchronization/rbx_object'
10-
require 'concurrent/synchronization/truffle_object'
10+
require 'concurrent/synchronization/truffleruby_object'
1111
require 'concurrent/synchronization/object'
1212
require 'concurrent/synchronization/volatile'
1313

1414
require 'concurrent/synchronization/abstract_lockable_object'
15-
require 'concurrent/synchronization/mri_lockable_object'
15+
require 'concurrent/synchronization/mutex_lockable_object'
1616
require 'concurrent/synchronization/jruby_lockable_object'
1717
require 'concurrent/synchronization/rbx_lockable_object'
18-
require 'concurrent/synchronization/truffle_lockable_object'
1918

2019
require 'concurrent/synchronization/lockable_object'
2120

lib/concurrent/synchronization/lockable_object.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ module Synchronization
55
# @!macro internal_implementation_note
66
LockableObjectImplementation = case
77
when Concurrent.on_cruby? && Concurrent.ruby_version(:<=, 1, 9, 3)
8-
MriMonitorLockableObject
8+
MonitorLockableObject
99
when Concurrent.on_cruby? && Concurrent.ruby_version(:>, 1, 9, 3)
10-
MriMutexLockableObject
10+
MutexLockableObject
1111
when Concurrent.on_jruby?
1212
JRubyLockableObject
1313
when Concurrent.on_rbx?
1414
RbxLockableObject
1515
when Concurrent.on_truffleruby?
16-
MriMutexLockableObject
16+
MutexLockableObject
1717
else
1818
warn 'Possibly unsupported Ruby implementation'
19-
MriMonitorLockableObject
19+
MonitorLockableObject
2020
end
2121
private_constant :LockableObjectImplementation
2222

lib/concurrent/synchronization/mri_lockable_object.rb renamed to lib/concurrent/synchronization/mutex_lockable_object.rb

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,74 @@
11
module Concurrent
2+
# noinspection RubyInstanceVariableNamingConvention
23
module Synchronization
34

45
# @!visibility private
56
# @!macro internal_implementation_note
6-
class MriLockableObject < AbstractLockableObject
7+
module ConditionSignalling
78
protected
89

910
def ns_signal
10-
@__condition__.signal
11+
@__Condition__.signal
1112
self
1213
end
1314

1415
def ns_broadcast
15-
@__condition__.broadcast
16+
@__Condition__.broadcast
1617
self
1718
end
1819
end
1920

2021

2122
# @!visibility private
2223
# @!macro internal_implementation_note
23-
class MriMutexLockableObject < MriLockableObject
24+
class MutexLockableObject < AbstractLockableObject
25+
include ConditionSignalling
26+
2427
safe_initialization!
2528

2629
def initialize(*defaults)
2730
super(*defaults)
28-
@__lock__ = ::Mutex.new
29-
@__condition__ = ::ConditionVariable.new
31+
@__Lock__ = ::Mutex.new
32+
@__Condition__ = ::ConditionVariable.new
3033
end
3134

3235
protected
3336

3437
def synchronize
35-
if @__lock__.owned?
38+
if @__Lock__.owned?
3639
yield
3740
else
38-
@__lock__.synchronize { yield }
41+
@__Lock__.synchronize { yield }
3942
end
4043
end
4144

4245
def ns_wait(timeout = nil)
43-
@__condition__.wait @__lock__, timeout
46+
@__Condition__.wait @__Lock__, timeout
4447
self
4548
end
4649
end
4750

4851
# @!visibility private
4952
# @!macro internal_implementation_note
50-
class MriMonitorLockableObject < MriLockableObject
53+
class MonitorLockableObject < AbstractLockableObject
54+
include ConditionSignalling
55+
5156
safe_initialization!
5257

5358
def initialize(*defaults)
5459
super(*defaults)
55-
@__lock__ = ::Monitor.new
56-
@__condition__ = @__lock__.new_cond
60+
@__Lock__ = ::Monitor.new
61+
@__Condition__ = @__Lock__.new_cond
5762
end
5863

5964
protected
6065

6166
def synchronize # TODO may be a problem with lock.synchronize { lock.wait }
62-
@__lock__.synchronize { yield }
67+
@__Lock__.synchronize { yield }
6368
end
6469

6570
def ns_wait(timeout = nil)
66-
@__condition__.wait timeout
71+
@__Condition__.wait timeout
6772
self
6873
end
6974
end

lib/concurrent/synchronization/object.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ module Synchronization
1111
when Concurrent.on_rbx?
1212
RbxObject
1313
when Concurrent.on_truffleruby?
14-
TruffleObject
14+
TruffleRubyObject
1515
else
16+
warn 'Possibly unsupported Ruby implementation'
1617
MriObject
1718
end
1819
private_constant :ObjectImplementation
@@ -134,8 +135,11 @@ def self.volatile_cas_fields(inherited = true)
134135
private
135136

136137
def self.define_initialize_volatile_with_cas
137-
assignments = @volatile_cas_fields.map { |name| "@Atomic#{name.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }} = AtomicReference.new(nil)" }.join("\n")
138-
class_eval <<-RUBY
138+
assignments = @volatile_cas_fields.map do |name|
139+
"@Atomic#{name.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }} = Concurrent::AtomicReference.new(nil)"
140+
end.join("\n")
141+
142+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
139143
def initialize_volatile_with_cas
140144
super
141145
#{assignments}

lib/concurrent/synchronization/truffle_lockable_object.rb

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

lib/concurrent/synchronization/truffle_object.rb

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module Concurrent
2+
module Synchronization
3+
4+
module TruffleRubyAttrVolatile
5+
def self.included(base)
6+
base.extend(ClassMethods)
7+
end
8+
9+
module ClassMethods
10+
def attr_volatile(*names)
11+
names.each do |name|
12+
ivar = :"@volatile_#{name}"
13+
14+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
15+
def #{name}
16+
full_memory_barrier
17+
#{ivar}
18+
end
19+
20+
def #{name}=(value)
21+
#{ivar} = value
22+
full_memory_barrier
23+
end
24+
RUBY
25+
end
26+
27+
names.map { |n| [n, :"#{n}="] }.flatten
28+
end
29+
end
30+
31+
def full_memory_barrier
32+
TruffleRuby.full_memory_barrier
33+
end
34+
end
35+
36+
# @!visibility private
37+
# @!macro internal_implementation_note
38+
class TruffleRubyObject < AbstractObject
39+
include TruffleRubyAttrVolatile
40+
41+
def initialize
42+
# nothing to do
43+
end
44+
end
45+
end
46+
end

lib/concurrent/synchronization/volatile.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ module Synchronization
2121
# => 2
2222

2323
Volatile = case
24-
when Concurrent.on_cruby?
25-
MriAttrVolatile
26-
when Concurrent.on_jruby?
27-
JRubyAttrVolatile
28-
when Concurrent.on_rbx? || Concurrent.on_truffleruby?
29-
RbxAttrVolatile
30-
else
31-
MriAttrVolatile
32-
end
24+
when Concurrent.on_cruby?
25+
MriAttrVolatile
26+
when Concurrent.on_jruby?
27+
JRubyAttrVolatile
28+
when Concurrent.on_rbx?
29+
RbxAttrVolatile
30+
when Concurrent.on_truffleruby?
31+
TruffleRubyAttrVolatile
32+
else
33+
MriAttrVolatile
34+
end
3335
end
3436
end

0 commit comments

Comments
 (0)