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



45
46
47
# File 'lib/future.rb', line 45

def call(channel=nil)
  run
end

#complete?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/future.rb', line 49

def complete?
  @complete
end

#getObject

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/future.rb', line 54

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
# 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.debug "Error occurred! #{ex.class.name}: #{ex.message}: " + ex.backtrace.inspect
    @ex = ex
  end
  @complete = true
  @cv.broadcast

end