Skip to content

Commit

Permalink
Pass async computations promises for their own result too, and try to
Browse files Browse the repository at this point in the history
detect divergence from the thread demanding its own result.
  • Loading branch information
MenTaLguY committed Feb 18, 2006
1 parent e140338 commit a087875
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lazy/async.rb
Expand Up @@ -18,12 +18,13 @@ def initialize( &computation ) #:nodoc:

thread = Thread.new do
begin
result = computation.call
result = computation.call( self )
rescue Exception => exception
end
end

super() do
raise DivergenceError.new if Thread.current == thread
thread.join
raise exception if exception
result
Expand All @@ -36,10 +37,15 @@ def initialize( &computation ) #:nodoc:
module Kernel

# Schedules a computation to be run asynchronously in a background thread
# and returns a promise for its result. demand() will wait for the
# and returns a promise for its result. Kernel.demand will wait for the
# computation to finish.
#
def async( &block ) ; Lazy::Async.new &block ; end
# As with Kernel.promise, async passes the block a promise for its own
# result -- use wisely.
#
def async( &computation ) #:yields: result
Lazy::Async.new &computation
end

end

0 comments on commit a087875

Please sign in to comment.