Module: Concurrent::Obligation

Includes:
Dereferenceable
Included in:
Delay, IVar, Promise
Defined in:
lib/concurrent/obligation.rb

Instance Method Summary collapse

Instance Method Details

#completed?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/concurrent/obligation.rb', line 37

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

#exception(*args) ⇒ Object

Examples:

allows Obligation to be risen

rejected_ivar = Ivar.new.fail
raise rejected_ivar


95
96
97
98
# File 'lib/concurrent/obligation.rb', line 95

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

#fulfilled?Boolean Also known as: realized?

Has the obligation been fulfilled?

Returns:

  • (Boolean)


14
15
16
# File 'lib/concurrent/obligation.rb', line 14

def fulfilled?
  state == :fulfilled
end

#incomplete?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/concurrent/obligation.rb', line 41

def incomplete?
  [:unscheduled, :pending].include? state
end

#no_error!(timeout = nil) ⇒ Obligation

wait until Obligation is #complete?

Parameters:

  • timeout (Numeric) (defaults to: nil)

    the maximum time in second to wait.

Returns:

Raises:

  • (Exception)

    when #rejected? it raises #reason



63
64
65
# File 'lib/concurrent/obligation.rb', line 63

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

#pending?Boolean

Is obligation completion still pending?

Returns:

  • (Boolean)


27
28
29
# File 'lib/concurrent/obligation.rb', line 27

def pending?
  state == :pending
end

#reasonObject



85
86
87
88
89
90
# File 'lib/concurrent/obligation.rb', line 85

def reason
  mutex.lock
  @reason
ensure
  mutex.unlock
end

#rejected?Boolean

Has the obligation been rejected?

Returns:

  • (Boolean)


21
22
23
# File 'lib/concurrent/obligation.rb', line 21

def rejected?
  state == :rejected
end

#stateObject



78
79
80
81
82
83
# File 'lib/concurrent/obligation.rb', line 78

def state
  mutex.lock
  @state
ensure
  mutex.unlock
end

#unscheduled?Boolean

Is the obligation still unscheduled?

Returns:

  • (Boolean)


33
34
35
# File 'lib/concurrent/obligation.rb', line 33

def unscheduled?
  state == :unscheduled
end

#value(timeout = nil) ⇒ Object

Returns see Dereferenceable#deref.

Returns:

  • (Object)

    see Dereferenceable#deref



46
47
48
49
# File 'lib/concurrent/obligation.rb', line 46

def value(timeout = nil)
  wait timeout
  deref
end

#value!(timeout = nil) ⇒ Object

Returns see Dereferenceable#deref.

Returns:

  • (Object)

    see Dereferenceable#deref

Raises:

  • (Exception)

    when #rejected? it raises #reason



69
70
71
72
73
74
75
76
# File 'lib/concurrent/obligation.rb', line 69

def value!(timeout = nil)
  wait(timeout)
  if rejected?
    raise self
  else
    deref
  end
end

#wait(timeout = nil) ⇒ Obligation

wait until Obligation is #complete?

Parameters:

  • timeout (Numeric) (defaults to: nil)

    the maximum time in second to wait.

Returns:



54
55
56
57
# File 'lib/concurrent/obligation.rb', line 54

def wait(timeout = nil)
  event.wait(timeout) if timeout != 0 && incomplete?
  self
end