Skip to content

Commit

Permalink
Fix naming in synchronization layer
Browse files Browse the repository at this point in the history
  • Loading branch information
pitr-ch committed Jul 7, 2018
1 parent aaeaef9 commit a026804
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 73 deletions.
5 changes: 2 additions & 3 deletions lib/concurrent/synchronization.rb
Expand Up @@ -7,15 +7,14 @@
require 'concurrent/synchronization/mri_object'
require 'concurrent/synchronization/jruby_object'
require 'concurrent/synchronization/rbx_object'
require 'concurrent/synchronization/truffle_object'
require 'concurrent/synchronization/truffleruby_object'
require 'concurrent/synchronization/object'
require 'concurrent/synchronization/volatile'

require 'concurrent/synchronization/abstract_lockable_object'
require 'concurrent/synchronization/mri_lockable_object'
require 'concurrent/synchronization/mutex_lockable_object'
require 'concurrent/synchronization/jruby_lockable_object'
require 'concurrent/synchronization/rbx_lockable_object'
require 'concurrent/synchronization/truffle_lockable_object'

require 'concurrent/synchronization/lockable_object'

Expand Down
8 changes: 4 additions & 4 deletions lib/concurrent/synchronization/lockable_object.rb
Expand Up @@ -5,18 +5,18 @@ module Synchronization
# @!macro internal_implementation_note
LockableObjectImplementation = case
when Concurrent.on_cruby? && Concurrent.ruby_version(:<=, 1, 9, 3)
MriMonitorLockableObject
MonitorLockableObject
when Concurrent.on_cruby? && Concurrent.ruby_version(:>, 1, 9, 3)
MriMutexLockableObject
MutexLockableObject
when Concurrent.on_jruby?
JRubyLockableObject
when Concurrent.on_rbx?
RbxLockableObject
when Concurrent.on_truffleruby?
MriMutexLockableObject
MutexLockableObject
else
warn 'Possibly unsupported Ruby implementation'
MriMonitorLockableObject
MonitorLockableObject
end
private_constant :LockableObjectImplementation

Expand Down
@@ -1,69 +1,74 @@
module Concurrent
# noinspection RubyInstanceVariableNamingConvention
module Synchronization

# @!visibility private
# @!macro internal_implementation_note
class MriLockableObject < AbstractLockableObject
module ConditionSignalling
protected

def ns_signal
@__condition__.signal
@__Condition__.signal
self
end

def ns_broadcast
@__condition__.broadcast
@__Condition__.broadcast
self
end
end


# @!visibility private
# @!macro internal_implementation_note
class MriMutexLockableObject < MriLockableObject
class MutexLockableObject < AbstractLockableObject
include ConditionSignalling

safe_initialization!

def initialize(*defaults)
super(*defaults)
@__lock__ = ::Mutex.new
@__condition__ = ::ConditionVariable.new
@__Lock__ = ::Mutex.new
@__Condition__ = ::ConditionVariable.new
end

protected

def synchronize
if @__lock__.owned?
if @__Lock__.owned?
yield
else
@__lock__.synchronize { yield }
@__Lock__.synchronize { yield }
end
end

def ns_wait(timeout = nil)
@__condition__.wait @__lock__, timeout
@__Condition__.wait @__Lock__, timeout
self
end
end

# @!visibility private
# @!macro internal_implementation_note
class MriMonitorLockableObject < MriLockableObject
class MonitorLockableObject < AbstractLockableObject
include ConditionSignalling

safe_initialization!

def initialize(*defaults)
super(*defaults)
@__lock__ = ::Monitor.new
@__condition__ = @__lock__.new_cond
@__Lock__ = ::Monitor.new
@__Condition__ = @__Lock__.new_cond
end

protected

def synchronize # TODO may be a problem with lock.synchronize { lock.wait }
@__lock__.synchronize { yield }
@__Lock__.synchronize { yield }
end

def ns_wait(timeout = nil)
@__condition__.wait timeout
@__Condition__.wait timeout
self
end
end
Expand Down
10 changes: 7 additions & 3 deletions lib/concurrent/synchronization/object.rb
Expand Up @@ -11,8 +11,9 @@ module Synchronization
when Concurrent.on_rbx?
RbxObject
when Concurrent.on_truffleruby?
TruffleObject
TruffleRubyObject
else
warn 'Possibly unsupported Ruby implementation'
MriObject
end
private_constant :ObjectImplementation
Expand Down Expand Up @@ -134,8 +135,11 @@ def self.volatile_cas_fields(inherited = true)
private

def self.define_initialize_volatile_with_cas
assignments = @volatile_cas_fields.map { |name| "@Atomic#{name.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }} = AtomicReference.new(nil)" }.join("\n")
class_eval <<-RUBY
assignments = @volatile_cas_fields.map do |name|
"@Atomic#{name.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }} = Concurrent::AtomicReference.new(nil)"
end.join("\n")

class_eval <<-RUBY, __FILE__, __LINE__ + 1
def initialize_volatile_with_cas
super
#{assignments}
Expand Down
9 changes: 0 additions & 9 deletions lib/concurrent/synchronization/truffle_lockable_object.rb

This file was deleted.

31 changes: 0 additions & 31 deletions lib/concurrent/synchronization/truffle_object.rb

This file was deleted.

46 changes: 46 additions & 0 deletions lib/concurrent/synchronization/truffleruby_object.rb
@@ -0,0 +1,46 @@
module Concurrent
module Synchronization

module TruffleRubyAttrVolatile
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods
def attr_volatile(*names)
names.each do |name|
ivar = :"@volatile_#{name}"

class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{name}
full_memory_barrier
#{ivar}
end
def #{name}=(value)
#{ivar} = value
full_memory_barrier
end
RUBY
end

names.map { |n| [n, :"#{n}="] }.flatten
end
end

def full_memory_barrier
TruffleRuby.full_memory_barrier
end
end

# @!visibility private
# @!macro internal_implementation_note
class TruffleRubyObject < AbstractObject
include TruffleRubyAttrVolatile

def initialize
# nothing to do
end
end
end
end
20 changes: 11 additions & 9 deletions lib/concurrent/synchronization/volatile.rb
Expand Up @@ -21,14 +21,16 @@ module Synchronization
# => 2

Volatile = case
when Concurrent.on_cruby?
MriAttrVolatile
when Concurrent.on_jruby?
JRubyAttrVolatile
when Concurrent.on_rbx? || Concurrent.on_truffleruby?
RbxAttrVolatile
else
MriAttrVolatile
end
when Concurrent.on_cruby?
MriAttrVolatile
when Concurrent.on_jruby?
JRubyAttrVolatile
when Concurrent.on_rbx?
RbxAttrVolatile
when Concurrent.on_truffleruby?
TruffleRubyAttrVolatile
else
MriAttrVolatile
end
end
end

0 comments on commit a026804

Please sign in to comment.