[macruby-changes] [3540] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Mon Feb 15 17:30:04 PST 2010


Revision: 3540
          http://trac.macosforge.org/projects/ruby/changeset/3540
Author:   ernest.prabhakar at gmail.com
Date:     2010-02-15 17:30:04 -0800 (Mon, 15 Feb 2010)
Log Message:
-----------
Initial commit of Dispatch::Group extensions plus specs

Modified Paths:
--------------
    MacRuby/trunk/lib/dispatch.rb

Added Paths:
-----------
    MacRuby/trunk/lib/dispatch/group.rb
    MacRuby/trunk/spec/macruby/library/dispatch/group_spec.rb

Added: MacRuby/trunk/lib/dispatch/group.rb
===================================================================
--- MacRuby/trunk/lib/dispatch/group.rb	                        (rev 0)
+++ MacRuby/trunk/lib/dispatch/group.rb	2010-02-16 01:30:04 UTC (rev 3540)
@@ -0,0 +1,19 @@
+# Provide an easy, uniform wrapper around +wait+ and +notify+
+
+module Dispatch
+  # Extend Dispatch::Group 
+   
+  # Implements "join" in Group for compatibility with Future
+  # TODO: make Future a subclass of Group
+  #       Currently not possible: https://www.macruby.org/trac/ticket/613
+  class Group
+    # Waits for the group to complete
+    # If a block is specified, call on the specified queue or priority, if any
+    def join(queue = Dispatch::Queue.concurrent, &block)
+      return group.wait if block.nil?
+      queue = Dispatch::Queue.concurrent(queue) if not queue.is_a? Dispatch::Queue #i.e., a priority
+      group.notify(queue) { block.call }
+    end
+  end
+  
+end

Modified: MacRuby/trunk/lib/dispatch.rb
===================================================================
--- MacRuby/trunk/lib/dispatch.rb	2010-02-16 01:29:54 UTC (rev 3539)
+++ MacRuby/trunk/lib/dispatch.rb	2010-02-16 01:30:04 UTC (rev 3540)
@@ -16,5 +16,5 @@
 require 'dispatch/dispatch'
 require 'dispatch/enumerable'
 require 'dispatch/future'
-require 'dispatch/queue'
+require 'dispatch/group'
 require 'dispatch/queue_source'

Added: MacRuby/trunk/spec/macruby/library/dispatch/group_spec.rb
===================================================================
--- MacRuby/trunk/spec/macruby/library/dispatch/group_spec.rb	                        (rev 0)
+++ MacRuby/trunk/spec/macruby/library/dispatch/group_spec.rb	2010-02-16 01:30:04 UTC (rev 3540)
@@ -0,0 +1,37 @@
+require File.dirname(__FILE__) + "/../../spec_helper"
+require 'dispatch'
+
+if MACOSX_VERSION >= 10.6
+  describe "Dispatch::Group" do
+    
+    before :each do
+      @q = Dispatch::Queue.new('org.macruby.gcd_spec.group')
+      @g = Dispatch::Group.new
+      @i = 0
+    end
+    
+    describe :join do
+      it "should wait until execution is complete if no block is passed" do
+        @q.async(@g) { @i = 42 }
+        @g.join
+        @i.should == 42
+      end
+
+      it "should run a notify block on completion, if passed" do
+        @q.async(@g) { @i = 42 }
+        @g.join {@i *= 2}
+        while @i <= 42 do; end    
+        @i.should == 84
+      end
+
+      it "should run notify block on specified queue, if any" do
+        @q.async(@g) { @i = 42 }
+        @q.sync { }
+        @g.join(@q) {@i *= 2}
+        @q.sync { }
+        @i.should == 84
+      end
+    end
+
+  end
+end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100215/a64b088a/attachment.html>


More information about the macruby-changes mailing list