Class: V::Future

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

Instance Method Summary collapse

Constructor Details

#initialize(worker) ⇒ Future

Returns a new instance of Future.



5
6
7
# File 'lib/v/future.rb', line 5

def initialize(worker)
  @worker, @waiting = worker, []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*params, &block) ⇒ Object



41
42
43
# File 'lib/v/future.rb', line 41

def method_missing(*params, &block)
  value.send(*params, &block)
end

Instance Method Details

#valueObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/v/future.rb', line 9

def value
  while (Thread.critical = true; not defined? @value)
    Thread.current != @worker or
    raise ThreadError, 'waiting for a value in worker causes deadlock'
    @waiting << Thread.current
    Thread.stop
  end

  @value
ensure
  Thread.critical = false
end

#value=(value) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/v/future.rb', line 22

def value=(value)
  Thread.critical = true
  @value = value

  class << self
    def value; @value end
  end

  begin
    while thread = @waiting.shift
      thread.wakeup
    end
  rescue ThreadError
    retry
  ensure
    Thread.critical = false
  end
end