123456789_123456789_123456789_123456789_123456789_

Module: Concurrent::Concern::Obligation

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Defined in: lib/concurrent-ruby/concurrent/concern/obligation.rb

Instance Attribute Summary

Dereferenceable - Included

#value

Return the value this object represents after applying the options specified by the #set_deref_options method.

Instance Method Summary

Dereferenceable - Included

#deref
#apply_deref_options,
#ns_set_deref_options

Set the options which define the operations #value performs before returning data to the caller (dereferencing).

Instance Attribute Details

#complete?Boolean (readonly)

Has the obligation completed processing?

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/concern/obligation.rb', line 49

def complete?
  [:fulfilled, :rejected].include? state
end

#fulfilled?Boolean (readonly) Also known as: #realized?

Has the obligation been fulfilled?

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/concern/obligation.rb', line 20

def fulfilled?
  state == :fulfilled
end

#incomplete?Boolean (readonly)

Is the obligation still awaiting completion of processing?

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/concern/obligation.rb', line 56

def incomplete?
  ! complete?
end

#pending?Boolean (readonly)

Is obligation completion still pending?

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/concern/obligation.rb', line 35

def pending?
  state == :pending
end

#realized? (readonly)

Alias for #fulfilled?.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/concern/obligation.rb', line 23

alias_method :realized?, :fulfilled?

#rejected?Boolean (readonly)

Has the obligation been rejected?

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/concern/obligation.rb', line 28

def rejected?
  state == :rejected
end

#stateSymbol (rw)

The current state of the obligation.

Returns:

  • (Symbol)

    the current state

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/concern/obligation.rb', line 110

def state
  synchronize { @state }
end

#state=(value) (rw, private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/concern/obligation.rb', line 161

def state=(value)
  synchronize { ns_set_state(value) }
end

#unscheduled?Boolean (readonly)

Is the obligation still unscheduled?

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/concern/obligation.rb', line 42

def unscheduled?
  state == :unscheduled
end

Instance Method Details

#compare_and_set_state(next_state, *expected_current) ⇒ Boolean (private)

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

Parameters:

  • next_state (Symbol)
  • expected_current (Symbol)

Returns:

  • (Boolean)

    true is state is changed, false otherwise

[ GitHub ]

  
# 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 (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/concern/obligation.rb', line 145

def event
  @event
end

#exception(*args)

Examples:

allows Obligation to be risen

rejected_ivar = Ivar.new.fail
raise rejected_ivar
[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/concern/obligation.rb', line 126

def exception(*args)
  raise 'obligation is not rejected' unless rejected?
  reason.exception(*args)
end

#get_arguments_from(opts = {}) (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/concern/obligation.rb', line 134

def get_arguments_from(opts = {})
  [*opts.fetch(:args, [])]
end

#if_state(*expected_states) ⇒ Object (private)

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

Returns:

  • block value if executed, false otherwise

[ GitHub ]

  
# 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 (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/concern/obligation.rb', line 139

def init_obligation
  @event = Event.new
  @value = @reason = nil
end

#no_error!(timeout = nil)

Alias for #wait!.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/concern/obligation.rb', line 89

alias_method :no_error!, :wait!

#ns_check_state?(expected) ⇒ Boolean (private)

Am I in the current state?

Parameters:

  • expected (Symbol)

    The state to check against

Returns:

  • (Boolean)

    true if in the expected state else false

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/concern/obligation.rb', line 210

def ns_check_state?(expected)
  @state == expected
end

#ns_set_state(value) (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/concern/obligation.rb', line 215

def ns_set_state(value)
  @state = value
end

#reasonException

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.

Returns:

  • (Exception)

    the exception raised during processing or nil

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/concern/obligation.rb', line 119

def reason
  synchronize { @reason }
end

#set_state(success, value, reason) (private)

[ GitHub ]

  
# 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

#value(timeout = nil) ⇒ Object

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

Parameters:

  • timeout (Numeric) (defaults to: nil)

    the maximum time in seconds to wait.

Returns:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/concern/obligation.rb', line 65

def value(timeout = nil)
  wait timeout
  deref
end

#value!(timeout = nil) ⇒ Object

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).

Parameters:

  • timeout (Numeric) (defaults to: nil)

    the maximum time in seconds to wait.

Returns:

Raises:

  • (Exception)

    raises the reason when rejected

[ GitHub ]

  
# 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) ⇒ Obligation

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

Parameters:

  • timeout (Numeric) (defaults to: nil)

    the maximum time in seconds to wait.

Returns:

  • (Obligation)

    self

[ GitHub ]

  
# 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) ⇒ Obligation Also known as: #no_error!

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).

Parameters:

  • timeout (Numeric) (defaults to: nil)

    the maximum time in seconds to wait.

Returns:

  • (Obligation)

    self

Raises:

  • (Exception)

    raises the reason when rejected

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/concern/obligation.rb', line 86

def wait!(timeout = nil)
  wait(timeout).tap { raise self if rejected? }
end