Class: Concur::StandardFuture
Instance Attribute Summary collapse
-
#ex ⇒ Object
Returns the value of attribute ex.
-
#thread ⇒ Object
Returns the value of attribute thread.
Instance Method Summary collapse
- #call(channel = nil) ⇒ Object
- #complete? ⇒ Boolean
-
#get ⇒ Object
Returns results.
-
#initialize(runnable = nil, channel = nil, &block) ⇒ StandardFuture
constructor
A new instance of StandardFuture.
- #run(channel = nil) ⇒ Object
Methods included from 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
#ex ⇒ Object
Returns the value of attribute ex.
17 18 19 |
# File 'lib/future.rb', line 17 def ex @ex end |
#thread ⇒ Object
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
55 56 57 |
# File 'lib/future.rb', line 55 def complete? @complete end |
#get ⇒ Object
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.}: " + ex.backtrace.inspect @ex = ex end if @channel r = (@result || @ex) puts "pusing #{r} to channel" @channel << r end @complete = true @cv.broadcast end |