Revision: 3427 http://trac.macosforge.org/projects/ruby/changeset/3427 Author: ernest.prabhakar@gmail.com Date: 2010-02-03 17:43:46 -0800 (Wed, 03 Feb 2010) Log Message: ----------- Initial Futures spec Modified Paths: -------------- MacRuby/trunk/lib/dispatch/dispatch.rb MacRuby/trunk/lib/dispatch/future.rb MacRuby/trunk/spec/macruby/library/dispatch/future_spec.rb Modified: MacRuby/trunk/lib/dispatch/dispatch.rb =================================================================== --- MacRuby/trunk/lib/dispatch/dispatch.rb 2010-02-04 01:24:42 UTC (rev 3426) +++ MacRuby/trunk/lib/dispatch/dispatch.rb 2010-02-04 01:43:46 UTC (rev 3427) @@ -50,8 +50,13 @@ # Companion to +Dispatch.fork+, allowing you to +wait+ until +grp+ completes # via an API similar to that used by +Threads+ # If a block is given, instead uses +notify+ to call it asynchronously - def join(&block) - Kernel.block_given? ? self.notify(&block) : self.wait + def join(q = nil, &block) + if block.nil? + self.wait + else + q ||= Dispatch::Queue.concurrent + self.notify(q, &block) + end end end Modified: MacRuby/trunk/lib/dispatch/future.rb =================================================================== --- MacRuby/trunk/lib/dispatch/future.rb 2010-02-04 01:24:42 UTC (rev 3426) +++ MacRuby/trunk/lib/dispatch/future.rb 2010-02-04 01:43:46 UTC (rev 3427) @@ -13,12 +13,15 @@ # Waits for the computation to finish, then returns the value # Duck-typed to lambda.call(void) # If a block is passed, invoke that asynchronously with the final value - def call(&callback) + def call(q = nil, &callback) if not block_given? @group.wait return @value + else + q ||= Dispatch::Queue.concurrent + @group.notify(q) { callback.call(@value) } end - @group.notify { callback.call(@value) } end + end end Modified: MacRuby/trunk/spec/macruby/library/dispatch/future_spec.rb =================================================================== --- MacRuby/trunk/spec/macruby/library/dispatch/future_spec.rb 2010-02-04 01:24:42 UTC (rev 3426) +++ MacRuby/trunk/spec/macruby/library/dispatch/future_spec.rb 2010-02-04 01:43:46 UTC (rev 3427) @@ -3,8 +3,28 @@ if MACOSX_VERSION >= 10.6 describe "Dispatch::Future" do - it "should do something" do - true.should == true + + before :each do + @future = Dispatch::Future.new { Math.sqrt(2**10) } end + + describe :new do + it "should return an Future for tracking execution of the passed block" do + @future.should be_kind_of Dispatch::Future + end + end + + describe :call do + it "should wait Synchronously to return value" do + @future.call.should == 2**5 + end + + it "should invoke passed block Asynchronously with returned value " do + $global = 0 + @future.call {|v| $global = v} + while $global == 0 do; end + $global.should == 2**5 + end + end end end \ No newline at end of file
participants (1)
-
source_changes@macosforge.org