Skip to content

Commit

Permalink
Refactored AtomicFixnum as subclass/superclass rather than module inc…
Browse files Browse the repository at this point in the history
…lude.
  • Loading branch information
jdantonio committed Apr 14, 2014
1 parent c2e4f6b commit ddaecea
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 216 deletions.
138 changes: 76 additions & 62 deletions lib/concurrent/atomic/atomic_fixnum.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
module Concurrent

# @!visibility private
module MutexAtomicFixnum # :nodoc:
# @!macro [attach] atomic_fixnum
#
# A numeric value that can be updated atomically. Reads and writes to an atomic
# fixnum and thread-safe and guaranteed to succeed. Reads and writes may block
# briefly but no explicit locking is required.
#
# @!method value()
# Retrieves the current `Fixnum` value
# @return [Fixnum] the current value
#
# @!method value=(value)
# Explicitly sets the value
# @param [Fixnum] value the new value to be set
# @return [Fixnum] the current value
# @raise [ArgumentError] if the new value is not a `Fixnum`
#
# @!method increment()
# Increases the current value by 1
# @return [Fixnum] the current value after incrementation
#
# @!method decrement()
# Decreases the current value by 1
# @return [Fixnum] the current value after decrementation
#
# @since 0.5.0
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicLong.html java.util.concurrent.atomic.AtomicLong
class MutexAtomicFixnum

# Creates a new `AtomicFixnum` with the given initial value.
#
# @param [Fixnum] init the initial value
# @raise [ArgumentError] if the initial value is not a `Fixnum`
def initialize(init = 0)
raise ArgumentError.new('initial value must be a Fixnum') unless init.is_a?(Fixnum)
@value = init
@mutex = Mutex.new
end

def allocate_storage(init)
@value = init
Expand All @@ -26,12 +61,14 @@ def increment
@value += 1
end
end
alias_method :up, :increment

def decrement
@mutex.synchronize do
@value -= 1
end
end
alias_method :down, :decrement

def compare_and_set(expect, update)
@mutex.synchronize do
Expand All @@ -45,77 +82,54 @@ def compare_and_set(expect, update)
end
end

# @!visibility private
module JavaAtomicFixnum # :nodoc:
if RUBY_PLATFORM == 'java'

def allocate_storage(init)
@atomic = java.util.concurrent.atomic.AtomicLong.new(init)
end
class JavaAtomicFixnum

def value
@atomic.get
end
# Creates a new `AtomicFixnum` with the given initial value.
#
# @param [Fixnum] init the initial value
# @raise [ArgumentError] if the initial value is not a `Fixnum`
def initialize(init = 0)
raise ArgumentError.new('initial value must be a Fixnum') unless init.is_a?(Fixnum)
@atomic = java.util.concurrent.atomic.AtomicLong.new(init)
end

def value=(value)
raise ArgumentError.new('value must be a Fixnum') unless value.is_a?(Fixnum)
@atomic.set(value)
end
def allocate_storage(init)
end

def increment
@atomic.increment_and_get
end
def value
@atomic.get
end

def decrement
@atomic.decrement_and_get
end
def value=(value)
raise ArgumentError.new('value must be a Fixnum') unless value.is_a?(Fixnum)
@atomic.set(value)
end

def compare_and_set(expect, update)
@atomic.compare_and_set(expect, update)
end
end
def increment
@atomic.increment_and_get
end
alias_method :up, :increment

# A numeric value that can be updated atomically. Reads and writes to an atomic
# fixnum and thread-safe and guaranteed to succeed. Reads and writes may block
# briefly but no explicit locking is required.
#
# @!method value()
# Retrieves the current `Fixnum` value
# @return [Fixnum] the current value
#
# @!method value=(value)
# Explicitly sets the value
# @param [Fixnum] value the new value to be set
# @return [Fixnum] the current value
# @raise [ArgumentError] if the new value is not a `Fixnum`
#
# @!method increment()
# Increases the current value by 1
# @return [Fixnum] the current value after incrementation
#
# @!method decrement()
# Decreases the current value by 1
# @return [Fixnum] the current value after decrementation
#
# @since 0.5.0
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicLong.html java.util.concurrent.atomic.AtomicLong
class AtomicFixnum
def decrement
@atomic.decrement_and_get
end
alias_method :down, :decrement

# Creates a new `AtomicFixnum` with the given initial value.
#
# @param [Fixnum] init the initial value
# @raise [ArgumentError] if the initial value is not a `Fixnum`
def initialize(init = 0)
raise ArgumentError.new('initial value must be a Fixnum') unless init.is_a?(Fixnum)
allocate_storage(init)
def compare_and_set(expect, update)
@atomic.compare_and_set(expect, update)
end
end

if RUBY_PLATFORM == 'java'
include JavaAtomicFixnum
else
include MutexAtomicFixnum
# @!macro atomic_fixnum
class AtomicFixnum < JavaAtomicFixnum
end

alias_method :up, :increment
alias_method :down, :decrement
else

# @!macro atomic_fixnum
class AtomicFixnum < MutexAtomicFixnum
end
end
end
Loading

0 comments on commit ddaecea

Please sign in to comment.