Class: Concurrent::Futures::Promise

Inherits:
Object
  • Object
show all
Defined in:
lib/concurrent/futures.rb

Instance Method Summary collapse

Constructor Details

#initializePromise

Returns a new instance of Promise.



56
57
58
59
# File 'lib/concurrent/futures.rb', line 56

def initialize
  @lock = Mutex.new
  @ready = ConditionVariable.new
end

Instance Method Details

#fail(ex) ⇒ Object



87
88
89
90
91
92
# File 'lib/concurrent/futures.rb', line 87

def fail( ex )
  @lock.synchronize do
    @value = Thunk.new( Thread.new { raise ex } )
  end
  self
end

#fulfill(value) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/concurrent/futures.rb', line 65

def fulfill( value )
  @lock.synchronize do
    if defined? @value
      raise AlreadyFulfilledError, "promise already fulfilled"
    end
    @value = value
    @ready.broadcast
  end
  self
end

#fulfilled?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/concurrent/futures.rb', line 76

def fulfilled?
  @lock.synchronize { defined? @value }
end

#futureObject



61
62
63
# File 'lib/concurrent/futures.rb', line 61

def future
  @future ||= Thunk.new self
end

#valueObject



80
81
82
83
84
85
# File 'lib/concurrent/futures.rb', line 80

def value
  @lock.synchronize do
    @ready.wait @lock until defined? @value
    @value
  end
end