Skip to content

Commit

Permalink
Deprecated Concurrent::Atomic in lieu of Concurrent::AtomicReference.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdantonio committed Apr 24, 2015
1 parent cd68976 commit 3354ad0
Show file tree
Hide file tree
Showing 17 changed files with 201 additions and 171 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -6,7 +6,7 @@ group :development do
gem 'rake', '~> 10.4.2'
gem 'rake-compiler', '~> 0.9.5'
gem 'gem-compiler', '~> 0.3.0'
gem 'benchmark-ips'
gem 'benchmark-ips', '~> 2.1.1'
end

group :testing do
Expand Down
2 changes: 1 addition & 1 deletion ext/com/concurrent_ruby/ext/AtomicReferenceLibrary.java
Expand Up @@ -25,7 +25,7 @@
public class AtomicReferenceLibrary implements Library {
public void load(Ruby runtime, boolean wrap) throws IOException {
RubyModule concurrentMod = runtime.defineModule("Concurrent");
RubyClass atomicCls = concurrentMod.defineClassUnder("JavaAtomic", runtime.getObject(), JRUBYREFERENCE_ALLOCATOR);
RubyClass atomicCls = concurrentMod.defineClassUnder("JavaAtomicReference", runtime.getObject(), JRUBYREFERENCE_ALLOCATOR);
try {
sun.misc.Unsafe.class.getMethod("getAndSetObject", Object.class);
atomicCls.setAllocator(JRUBYREFERENCE8_ALLOCATOR);
Expand Down
24 changes: 12 additions & 12 deletions ext/concurrent/rb_concurrent.c
Expand Up @@ -7,7 +7,7 @@
// module and class definitions

static VALUE rb_mConcurrent;
static VALUE rb_cAtomic;
static VALUE rb_cAtomicReference;
static VALUE rb_cAtomicBoolean;
static VALUE rb_cAtomicFixnum;

Expand All @@ -17,20 +17,20 @@ void Init_extension() {

// define modules and classes
rb_mConcurrent = rb_define_module("Concurrent");
rb_cAtomic = rb_define_class_under(rb_mConcurrent, "CAtomic", rb_cObject);
rb_cAtomicReference = rb_define_class_under(rb_mConcurrent, "CAtomicReference", rb_cObject);
rb_cAtomicBoolean = rb_define_class_under(rb_mConcurrent, "CAtomicBoolean", rb_cObject);
rb_cAtomicFixnum = rb_define_class_under(rb_mConcurrent, "CAtomicFixnum", rb_cObject);

// CAtomic
rb_define_alloc_func(rb_cAtomic, ir_alloc);
rb_define_method(rb_cAtomic, "initialize", ir_initialize, -1);
rb_define_method(rb_cAtomic, "get", ir_get, 0);
rb_define_method(rb_cAtomic, "set", ir_set, 1);
rb_define_method(rb_cAtomic, "get_and_set", ir_get_and_set, 1);
rb_define_method(rb_cAtomic, "_compare_and_set", ir_compare_and_set, 2);
rb_define_alias(rb_cAtomic, "value", "get");
rb_define_alias(rb_cAtomic, "value=", "set");
rb_define_alias(rb_cAtomic, "swap", "get_and_set");
// CAtomicReference
rb_define_alloc_func(rb_cAtomicReference, ir_alloc);
rb_define_method(rb_cAtomicReference, "initialize", ir_initialize, -1);
rb_define_method(rb_cAtomicReference, "get", ir_get, 0);
rb_define_method(rb_cAtomicReference, "set", ir_set, 1);
rb_define_method(rb_cAtomicReference, "get_and_set", ir_get_and_set, 1);
rb_define_method(rb_cAtomicReference, "_compare_and_set", ir_compare_and_set, 2);
rb_define_alias(rb_cAtomicReference, "value", "get");
rb_define_alias(rb_cAtomicReference, "value=", "set");
rb_define_alias(rb_cAtomicReference, "swap", "get_and_set");

// CAtomicBoolean
rb_define_alloc_func(rb_cAtomicBoolean, atomic_boolean_allocate);
Expand Down
3 changes: 1 addition & 2 deletions lib/concurrent.rb
Expand Up @@ -13,10 +13,9 @@
require 'concurrent/executors'
require 'concurrent/utilities'

require 'concurrent/atomic'
require 'concurrent/atomic/atomic_reference'
require 'concurrent/agent'
require 'concurrent/async'
require 'concurrent/atomic'
require 'concurrent/dataflow'
require 'concurrent/delay'
require 'concurrent/exchanger'
Expand Down
93 changes: 0 additions & 93 deletions lib/concurrent/atomic.rb

This file was deleted.

49 changes: 49 additions & 0 deletions lib/concurrent/atomic/atomic_reference.rb
@@ -0,0 +1,49 @@
require 'concurrent/native_extensions'
require 'concurrent/utility/engine'
require 'concurrent/atomic_reference/concurrent_update_error'
require 'concurrent/atomic_reference/mutex_atomic'

begin
# force fallback impl with FORCE_ATOMIC_FALLBACK=1
if /[^0fF]/ =~ ENV['FORCE_ATOMIC_FALLBACK']
ruby_engine = 'mutex_atomic'
else
ruby_engine = Concurrent.ruby_engine
end

require "concurrent/atomic_reference/#{ruby_engine}"
rescue LoadError
#warn 'Compiled extensions not installed, pure Ruby Atomic will be used.'
end

if defined? Concurrent::JavaAtomicReference

# @!macro atomic_reference
class Concurrent::AtomicReference < Concurrent::JavaAtomicReference
end

elsif defined? Concurrent::RbxAtomicReference

# @!macro atomic_reference
class Concurrent::AtomicReference < Concurrent::RbxAtomicReference
end

elsif defined? Concurrent::CAtomicReference

# @!macro atomic_reference
class Concurrent::AtomicReference < Concurrent::CAtomicReference
end

else

# @!macro atomic_reference
class Concurrent::AtomicReference < Concurrent::MutexAtomicReference
end
end

module Concurrent

# @see Concurrent::AtomicReference
# @deprecated Use Concurrent::AtomicReference instead.
Atomic = AtomicReference
end
12 changes: 6 additions & 6 deletions lib/concurrent/atomic/read_write_lock.rb
@@ -1,5 +1,5 @@
require 'thread'
require 'concurrent/atomic'
require 'concurrent/atomic/atomic_reference'
require 'concurrent/errors'

module Concurrent
Expand Down Expand Up @@ -53,11 +53,11 @@ class ReadWriteLock

# Create a new `ReadWriteLock` in the unlocked state.
def initialize
@counter = Atomic.new(0) # single integer which represents lock state
@reader_q = ConditionVariable.new # queue for waiting readers
@reader_mutex = Mutex.new # to protect reader queue
@writer_q = ConditionVariable.new # queue for waiting writers
@writer_mutex = Mutex.new # to protect writer queue
@counter = AtomicReference.new(0) # single integer which represents lock state
@reader_q = ConditionVariable.new # queue for waiting readers
@reader_mutex = Mutex.new # to protect reader queue
@writer_q = ConditionVariable.new # queue for waiting writers
@writer_mutex = Mutex.new # to protect writer queue
end

# Execute a block operation within a read lock.
Expand Down
2 changes: 0 additions & 2 deletions lib/concurrent/atomic/thread_local_var.rb
@@ -1,5 +1,3 @@
require 'concurrent/atomic'

module Concurrent

# @!macro [attach] abstract_thread_local_var
Expand Down
4 changes: 2 additions & 2 deletions lib/concurrent/atomic_reference/jruby.rb
@@ -1,12 +1,12 @@
require 'concurrent/native_extensions'

if defined?(Concurrent::JavaAtomic)
if defined?(Concurrent::JavaAtomicReference)
require 'concurrent/atomic_reference/direct_update'

module Concurrent

# @!macro atomic_reference
class JavaAtomic
class JavaAtomicReference
include Concurrent::AtomicDirectUpdate
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/concurrent/atomic_reference/mutex_atomic.rb
Expand Up @@ -5,7 +5,7 @@
module Concurrent

# @!macro atomic_reference
class MutexAtomic
class MutexAtomicReference
include Concurrent::AtomicDirectUpdate
include Concurrent::AtomicNumericCompareAndSetWrapper

Expand Down
2 changes: 1 addition & 1 deletion lib/concurrent/atomic_reference/rbx.rb
Expand Up @@ -7,7 +7,7 @@ module Concurrent
#
# @note Extends `Rubinius::AtomicReference` version adding aliases
# and numeric logic.
class RbxAtomic < Rubinius::AtomicReference
class RbxAtomicReference < Rubinius::AtomicReference
alias _compare_and_set compare_and_set
include Concurrent::AtomicDirectUpdate
include Concurrent::AtomicNumericCompareAndSetWrapper
Expand Down
4 changes: 2 additions & 2 deletions lib/concurrent/atomic_reference/ruby.rb
@@ -1,12 +1,12 @@
if defined? Concurrent::CAtomic
if defined? Concurrent::CAtomicReference
require 'concurrent/native_extensions'
require 'concurrent/atomic_reference/direct_update'
require 'concurrent/atomic_reference/numeric_cas_wrapper'

module Concurrent

# @!macro atomic_reference
class CAtomic
class CAtomicReference
include Concurrent::AtomicDirectUpdate
include Concurrent::AtomicNumericCompareAndSetWrapper

Expand Down
41 changes: 40 additions & 1 deletion lib/concurrent/atomics.rb
@@ -1,4 +1,43 @@
require 'concurrent/atomic'
# @!macro [new] atomic_reference
#
# An object reference that may be updated atomically.
#
# Testing with ruby 2.1.2
#
# *** Sequential updates ***
# user system total real
# no lock 0.000000 0.000000 0.000000 ( 0.005502)
# mutex 0.030000 0.000000 0.030000 ( 0.025158)
# MutexAtomicReference 0.100000 0.000000 0.100000 ( 0.103096)
# CAtomicReference 0.040000 0.000000 0.040000 ( 0.034012)
#
# *** Parallel updates ***
# user system total real
# no lock 0.010000 0.000000 0.010000 ( 0.009387)
# mutex 0.030000 0.010000 0.040000 ( 0.032545)
# MutexAtomicReference 0.830000 2.280000 3.110000 ( 2.146622)
# CAtomicReference 0.040000 0.000000 0.040000 ( 0.038332)
#
# Testing with jruby 1.9.3
#
# *** Sequential updates ***
# user system total real
# no lock 0.170000 0.000000 0.170000 ( 0.051000)
# mutex 0.370000 0.010000 0.380000 ( 0.121000)
# MutexAtomicReference 1.530000 0.020000 1.550000 ( 0.471000)
# JavaAtomicReference 0.370000 0.010000 0.380000 ( 0.112000)
#
# *** Parallel updates ***
# user system total real
# no lock 0.390000 0.000000 0.390000 ( 0.105000)
# mutex 0.480000 0.040000 0.520000 ( 0.145000)
# MutexAtomicReference 1.600000 0.180000 1.780000 ( 0.511000)
# JavaAtomicReference 0.460000 0.010000 0.470000 ( 0.131000)
#
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReference.html
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/package-summary.html

require 'concurrent/atomic/atomic_reference'
require 'concurrent/atomic/atomic_boolean'
require 'concurrent/atomic/atomic_fixnum'
require 'concurrent/atomic/condition'
Expand Down
4 changes: 2 additions & 2 deletions lib/concurrent/configuration.rb
Expand Up @@ -13,7 +13,7 @@ module Concurrent
private_constant :NULL_LOGGER

# @!visibility private
GLOBAL_LOGGER = Atomic.new(NULL_LOGGER)
GLOBAL_LOGGER = AtomicReference.new(NULL_LOGGER)
private_constant :GLOBAL_LOGGER

# @!visibility private
Expand Down Expand Up @@ -219,7 +219,7 @@ def auto_terminate
end

# create the default configuration on load
CONFIGURATION = Atomic.new(Configuration.new)
CONFIGURATION = AtomicReference.new(Configuration.new)
private_constant :CONFIGURATION

# @return [Configuration]
Expand Down
8 changes: 4 additions & 4 deletions lib/concurrent/lazy_register.rb
@@ -1,4 +1,4 @@
require 'concurrent/atomic'
require 'concurrent/atomic/atomic_reference'
require 'concurrent/delay'

module Concurrent
Expand All @@ -7,17 +7,17 @@ module Concurrent
#
# @example
# register = Concurrent::LazyRegister.new
# #=> #<Concurrent::LazyRegister:0x007fd7ecd5e230 @data=#<Concurrent::Atomic:0x007fd7ecd5e1e0>>
# #=> #<Concurrent::LazyRegister:0x007fd7ecd5e230 @data=#<Concurrent::AtomicReference:0x007fd7ecd5e1e0>>
# register[:key]
# #=> nil
# register.add(:key) { Concurrent::Actor.spawn!(Actor::AdHoc, :ping) { -> message { message } } }
# #=> #<Concurrent::LazyRegister:0x007fd7ecd5e230 @data=#<Concurrent::Atomic:0x007fd7ecd5e1e0>>
# #=> #<Concurrent::LazyRegister:0x007fd7ecd5e230 @data=#<Concurrent::AtomicReference:0x007fd7ecd5e1e0>>
# register[:key]
# #=> #<Concurrent::Actor::Reference /ping (Concurrent::Actor::AdHoc)>
class LazyRegister

def initialize
@data = Atomic.new Hash.new
@data = AtomicReference.new(Hash.new)
end

# Element reference. Retrieves the value object corresponding to the
Expand Down

0 comments on commit 3354ad0

Please sign in to comment.