class Concurrent::MutexAtomicFixnum

@!macro internal_implementation_note
@!visibility private
@!macro atomic_fixnum

def compare_and_set(expect, update)

@!macro atomic_fixnum_method_compare_and_set
def compare_and_set(expect, update)
  synchronize do
    if @value == expect.to_i
      @value = update.to_i
      true
    else
      false
    end
  end
end

def decrement(delta = 1)

@!macro atomic_fixnum_method_decrement
def decrement(delta = 1)
  synchronize { ns_set(@value - delta.to_i) }
end

def increment(delta = 1)

@!macro atomic_fixnum_method_increment
def increment(delta = 1)
  synchronize { ns_set(@value + delta.to_i) }
end

def initialize(initial = 0)

@!macro atomic_fixnum_method_initialize
def initialize(initial = 0)
  super()
  @Lock = ::Mutex.new
  ns_set(initial)
end

def ns_set(value)

@!visibility private
def ns_set(value)
  Utility::NativeInteger.ensure_integer_and_bounds value
  @value = value
end

def synchronize

@!visibility private
def synchronize
  if @Lock.owned?
    yield
  else
    @Lock.synchronize { yield }
  end
end

def update

@!macro atomic_fixnum_method_update
def update
  synchronize do
    @value = yield @value
  end
end

def value

@!macro atomic_fixnum_method_value_get
def value
  synchronize { @value }
end

def value=(value)

@!macro atomic_fixnum_method_value_set
def value=(value)
  synchronize { ns_set(value) }
end