module Concurrent::Concern::Obligation
Public Instance Methods
Has the obligation completed processing?
@return [Boolean]
# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 49 def complete? [:fulfilled, :rejected].include? state end
@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
Has the obligation been fulfilled?
@return [Boolean]
# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 20 def fulfilled? state == :fulfilled end
Is the obligation still awaiting completion of processing?
@return [Boolean]
# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 56 def incomplete? ! complete? end
Is obligation completion still pending?
@return [Boolean]
# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 35 def pending? state == :pending end
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
Has the obligation been rejected?
@return [Boolean]
# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 28 def rejected? state == :rejected end
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
Is the obligation still unscheduled?
@return [Boolean]
# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 42 def unscheduled? state == :unscheduled end
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
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 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 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
Protected Instance Methods
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
@!visibility private
# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 145 def event @event end
@!visibility private
# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 134 def get_arguments_from(opts = {}) [*opts.fetch(:args, [])] end
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
@!visibility private
# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 139 def init_obligation @event = Event.new @value = @reason = nil end
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
@!visibility private
# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 215 def ns_set_state(value) @state = value end
@!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
@!visibility private
# File lib/concurrent-ruby/concurrent/concern/obligation.rb, line 161 def state=(value) synchronize { ns_set_state(value) } end