Class: Runivedo::Future

Inherits:
Object
  • Object
show all
Defined in:
lib/runivedo/future.rb

Instance Method Summary collapse

Constructor Details

#initializeFuture

Returns a new instance of Future.



5
6
7
8
9
10
# File 'lib/runivedo/future.rb', line 5

def initialize
  @mutex = Mutex.new
  @cond = ConditionVariable.new
  @success = nil
  @value = nil
end

Instance Method Details

#complete(value) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/runivedo/future.rb', line 31

def complete(value)
  @mutex.synchronize do
    @success = true
    @value = value
    @cond.broadcast
  end
end

#fail(exception) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/runivedo/future.rb', line 23

def fail(exception)
  @mutex.synchronize do
    @success = false
    @value = exception
    @cond.broadcast
  end
end

#getObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/runivedo/future.rb', line 12

def get
  @mutex.synchronize do
    @cond.wait(@mutex) while @success.nil?
  end
  if @success
    @value
  else
    raise @value
  end
end

#is_complete?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/runivedo/future.rb', line 39

def is_complete?
  !@success.nil?
end