Revision: 3470 http://trac.macosforge.org/projects/ruby/changeset/3470 Author: ernest.prabhakar@gmail.com Date: 2010-02-09 15:25:55 -0800 (Tue, 09 Feb 2010) Log Message: ----------- Dispatch::Future has_a Group again, not is_a Modified Paths: -------------- MacRuby/trunk/lib/dispatch/dispatch.rb MacRuby/trunk/lib/dispatch/future.rb MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.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-09 23:25:29 UTC (rev 3469) +++ MacRuby/trunk/lib/dispatch/dispatch.rb 2010-02-09 23:25:55 UTC (rev 3470) @@ -43,7 +43,7 @@ # Run the +&block+ asynchronously on a concurrent queue of the given # (optional) +priority+ as part of a Future, which is returned for use with - # +join+ or +value+ -- or as a Group, of which it is a subclass + # +join+ or +value+ def fork(priority=nil, &block) Dispatch::Future.new(priority) &block Modified: MacRuby/trunk/lib/dispatch/future.rb =================================================================== --- MacRuby/trunk/lib/dispatch/future.rb 2010-02-09 23:25:29 UTC (rev 3469) +++ MacRuby/trunk/lib/dispatch/future.rb 2010-02-09 23:25:55 UTC (rev 3470) @@ -1,43 +1,37 @@ # Calculate the value of an object in the background module Dispatch - # Subclass of Dispatch::Group used to implement lazy Futures - # By returning a value and duck-typing Thread +join+ and +value+ + # Wrap Dispatch::Group to implement lazy Futures + # By duck-typing Thread +join+ and +value+ - class Future < Dispatch::Group + class Future # Create a future that asynchronously dispatches the block # to the default queue - def initialize(&block) - super + attr_accessor :group + + def initialize(priority = nil, &block) @value = nil - Dispatch.group(self, nil) { @value = block.call } + @group = Group.new + Dispatch.group(@group, priority) { @value = block.call } end # Waits for the computation to finish - alias_method :join, :wait + def join + group.wait + end # Joins, then returns the value # If a block is passed, invoke that asynchronously with the final value # on the specified +queue+ (or else the default queue). def value(queue = nil, &callback) if not block_given? - wait + group.wait return @value else queue ||= Dispatch::Queue.concurrent - notify(queue) { callback.call(@value) } + group.notify(queue) { callback.call(@value) } end end end - # Run the +&block+ asynchronously on a concurrent queue of the given - # (optional) +priority+ as part of a Future, which is returned for use with - # +join+ or +value+ -- or as a Group, of which it is a subclass - - def fork(&block) - Dispatch::Future.new &block - end - - module_function :fork - end Modified: MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb =================================================================== --- MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb 2010-02-09 23:25:29 UTC (rev 3469) +++ MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb 2010-02-09 23:25:55 UTC (rev 3470) @@ -83,15 +83,6 @@ g.join $dispatch_gval.should == 42 end - - it "should return a Group for tracking execution of the passed block" do - $dispatch_gval = 0 - g = Dispatch.fork { @actee.delay_set(42) } - $dispatch_gval.should == 0 - g.should be_kind_of Dispatch::Group - g.wait - $dispatch_gval.should == 42 - end end end Modified: MacRuby/trunk/spec/macruby/library/dispatch/future_spec.rb =================================================================== --- MacRuby/trunk/spec/macruby/library/dispatch/future_spec.rb 2010-02-09 23:25:29 UTC (rev 3469) +++ MacRuby/trunk/spec/macruby/library/dispatch/future_spec.rb 2010-02-09 23:25:55 UTC (rev 3470) @@ -13,9 +13,11 @@ it "should return a Future for tracking execution of the passed block" do @future.should be_kind_of Dispatch::Future end + end + describe :group do it "should return an instance of Dispatch::Group" do - @future.should be_kind_of Dispatch::Group + @future.group.should be_kind_of Dispatch::Group end end
participants (1)
-
source_changes@macosforge.org