module Concurrent::Concern::Obligation

Public Instance Methods

complete?() click to toggle source

Has the obligation completed processing?

@return [Boolean]

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 49
def complete?
  [:fulfilled, :rejected].include? state
end
exception(*args) click to toggle source

@example allows Obligation to be risen

rejected_ivar = Ivar.new.fail
raise rejected_ivar
# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 126
def exception(*args)
  raise 'obligation is not rejected' unless rejected?
  reason.exception(*args)
end
fulfilled?() click to toggle source

Has the obligation been fulfilled?

@return [Boolean]

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 20
def fulfilled?
  state == :fulfilled
end
Also aliased as: realized?
incomplete?() click to toggle source

Is the obligation still awaiting completion of processing?

@return [Boolean]

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 56
def incomplete?
  ! complete?
end
no_error!(timeout = nil)
Alias for: wait!
pending?() click to toggle source

Is obligation completion still pending?

@return [Boolean]

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 35
def pending?
  state == :pending
end
realized?()
Alias for: fulfilled?
reason() click to toggle source

If an exception was raised during processing this will return the exception object. Will return `nil` when the state is pending or if the obligation has been successfully fulfilled.

@return [Exception] the exception raised during processing or `nil`

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 119
def reason
  synchronize { @reason }
end
rejected?() click to toggle source

Has the obligation been rejected?

@return [Boolean]

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 28
def rejected?
  state == :rejected
end
state() click to toggle source

The current state of the obligation.

@return [Symbol] the current state

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 110
def state
  synchronize { @state }
end
unscheduled?() click to toggle source

Is the obligation still unscheduled?

@return [Boolean]

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 42
def unscheduled?
  state == :unscheduled
end
value(timeout = nil) click to toggle source

The current value of the obligation. Will be `nil` while the state is pending or the operation has been rejected.

@param [Numeric] timeout the maximum time in seconds to wait. @return [Object] see Dereferenceable#deref

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 65
def value(timeout = nil)
  wait timeout
  deref
end
value!(timeout = nil) click to toggle source

The current value of the obligation. Will be `nil` while the state is pending or the operation has been rejected. Will re-raise any exceptions raised during processing (but will not raise an exception on timeout).

@param [Numeric] timeout the maximum time in seconds to wait. @return [Object] see Dereferenceable#deref @raise [Exception] raises the reason when rejected

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 98
def value!(timeout = nil)
  wait(timeout)
  if rejected?
    raise self
  else
    deref
  end
end
wait(timeout = nil) click to toggle source

Wait until obligation is complete or the timeout has been reached.

@param [Numeric] timeout the maximum time in seconds to wait. @return [Obligation] self

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 74
def wait(timeout = nil)
  event.wait(timeout) if timeout != 0 && incomplete?
  self
end
wait!(timeout = nil) click to toggle source

Wait until obligation is complete or the timeout is reached. Will re-raise any exceptions raised during processing (but will not raise an exception on timeout).

@param [Numeric] timeout the maximum time in seconds to wait. @return [Obligation] self @raise [Exception] raises the reason when rejected

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 86
def wait!(timeout = nil)
  wait(timeout).tap { raise self if rejected? }
end
Also aliased as: no_error!

Protected Instance Methods

compare_and_set_state(next_state, *expected_current) click to toggle source

Atomic compare and set operation State is set to `next_state` only if `current state == expected_current`.

@param [Symbol] next_state @param [Symbol] expected_current

@return [Boolean] true is state is changed, false otherwise

@!visibility private

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 174
def compare_and_set_state(next_state, *expected_current)
  synchronize do
    if expected_current.include? @state
      @state = next_state
      true
    else
      false
    end
  end
end
event() click to toggle source

@!visibility private

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 145
def event
  @event
end
get_arguments_from(opts = {}) click to toggle source

@!visibility private

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 134
def get_arguments_from(opts = {})
  [*opts.fetch(:args, [])]
end
if_state(*expected_states) { || ... } click to toggle source

Executes the block within mutex if current state is included in expected_states

@return block value if executed, false otherwise

@!visibility private

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 190
def if_state(*expected_states)
  synchronize do
    raise ArgumentError.new('no block given') unless block_given?

    if expected_states.include? @state
      yield
    else
      false
    end
  end
end
init_obligation() click to toggle source

@!visibility private

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 139
def init_obligation
  @event = Event.new
  @value = @reason = nil
end
ns_check_state?(expected) click to toggle source

Am I in the current state?

@param [Symbol] expected The state to check against @return [Boolean] true if in the expected state else false

@!visibility private

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 210
def ns_check_state?(expected)
  @state == expected
end
ns_set_state(value) click to toggle source

@!visibility private

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 215
def ns_set_state(value)
  @state = value
end
set_state(success, value, reason) click to toggle source

@!visibility private

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 150
def set_state(success, value, reason)
  if success
    @value = value
    @state = :fulfilled
  else
    @reason = reason
    @state  = :rejected
  end
end
state=(value) click to toggle source

@!visibility private

# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 161
def state=(value)
  synchronize { ns_set_state(value) }
end