Class: Concur::StandardFuture

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Future

#future?

Constructor Details

#initialize(runnable = nil, channel = nil, &block) ⇒ StandardFuture

Returns a new instance of StandardFuture.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/future.rb', line 19

def initialize(runnable=nil, channel=nil, &block)

  @mutex = Mutex.new
  @cv = ConditionVariable.new
  @callable = runnable
  @channel = channel
  if block_given?
    @callable = block
  end

end

Instance Attribute Details

#exObject

Returns the value of attribute ex.



17
18
19
# File 'lib/future.rb', line 17

def ex
  @ex
end

#threadObject

Returns the value of attribute thread.



17
18
19
# File 'lib/future.rb', line 17

def thread
  @thread
end

Instance Method Details

#call(channel = nil) ⇒ Object



51
52
53
# File 'lib/future.rb', line 51

def call(channel=nil)
  run
end

#complete?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/future.rb', line 55

def complete?
  @complete
end

#getObject

Returns results. Will wait for thread to complete execution if not already complete.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/future.rb', line 60

def get
#      @thread.value
  unless @complete
    @mutex.synchronize do
      unless @complete
        @cv.wait(@mutex)
      end
    end
  end
  if @ex
    raise @ex
  end
  @result
end

#run(channel = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/future.rb', line 31

def run(channel=nil)
  #Concur.logger.debug 'running StandardFuture'
  begin
    @result = @callable.call(@channel)
    Concur.logger.debug 'callable result: ' + @result.inspect
  rescue Exception => ex
    Concur.logger.info "Error occurred! #{ex.class.name}: #{ex.message}: " + ex.backtrace.inspect
    @ex = ex
  end
  if @channel
    r = (@result || @ex)
    puts "pusing #{r} to channel"
    @channel << r
  end
  @complete = true
  @cv.broadcast


end