[macruby-changes] [3536] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Mon Feb 15 17:28:23 PST 2010


Revision: 3536
          http://trac.macosforge.org/projects/ruby/changeset/3536
Author:   ernest.prabhakar at gmail.com
Date:     2010-02-15 17:28:21 -0800 (Mon, 15 Feb 2010)
Log Message:
-----------
Dispatch.queue_for -> queue, plus default

Modified Paths:
--------------
    MacRuby/trunk/lib/dispatch/dispatch.rb
    MacRuby/trunk/lib/dispatch/enumerable.rb
    MacRuby/trunk/sample-macruby/Scripts/gcd/dispatch_methods.rb
    MacRuby/trunk/sample-macruby/Scripts/gcd/ring_buffer.rb
    MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb
    MacRuby/trunk/spec/macruby/library/dispatch/enumerable_spec.rb

Modified: MacRuby/trunk/lib/dispatch/dispatch.rb
===================================================================
--- MacRuby/trunk/lib/dispatch/dispatch.rb	2010-02-16 01:03:53 UTC (rev 3535)
+++ MacRuby/trunk/lib/dispatch/dispatch.rb	2010-02-16 01:28:21 UTC (rev 3536)
@@ -33,31 +33,34 @@
   # 
   def group(grp=nil, priority=nil, &block)
     grp ||= Dispatch::Group.new
-    Dispatch::Queue.concurrent(priority).async(grp) { block.call }
+    Dispatch::Queue.concurrent(priority).async(grp) { block.call } if not block.nil?
     grp
   end
 
   # Returns a mostly unique reverse-DNS-style label based on
   # the ancestor chain and ID of +obj+ plus the current time
   # 
-  #   Dispatch.label_for(Array.new)
+  #   Dispatch.labelize(Array.new)
   #   => Dispatch.enumerable.array.0x2000cc2c0.1265915278.97557
   #
-  def label_for(obj)
+  def labelize(obj)
     names = obj.class.ancestors[0...-2].map {|a| a.to_s.downcase}
     label = names.uniq.reverse.join(".")
     "#{self}.#{label}.%p.#{Time.now.to_f}" % obj.object_id
   end
 
   # Returns a new serial queue with a unique label based on +obj+
+  # (or self, if no object is specified)
   # used to serialize access to objects called from multiple threads
   #
   #   a = Array.new
-  #   q = Dispatch.queue_for(a)
+  #   q = Dispatch.queue(a)
   #   q.async {a << 2 }
   #
-  def queue_for(obj)
-    Dispatch::Queue.new Dispatch.label_for(obj)
+  def queue(obj=self, &block)
+    q = Dispatch::Queue.new(Dispatch.labelize(obj))
+    q.async { block.call } if not block.nil?
+    q
   end
 
   # Wrap the passed +obj+ (or its instance, if a Class) inside an Actor
@@ -72,6 +75,6 @@
     Dispatch::Actor.new( (obj.is_a? Class) ? obj.new : obj)
   end
 
-  module_function :async, :fork, :group, :label_for, :queue_for, :wrap
+  module_function :async, :fork, :group, :queue, :wrap, :labelize
 
 end

Modified: MacRuby/trunk/lib/dispatch/enumerable.rb
===================================================================
--- MacRuby/trunk/lib/dispatch/enumerable.rb	2010-02-16 01:03:53 UTC (rev 3535)
+++ MacRuby/trunk/lib/dispatch/enumerable.rb	2010-02-16 01:28:21 UTC (rev 3536)
@@ -39,7 +39,7 @@
     # Ideally should run from within a Dispatch.once to avoid race
     @mapreduce_q.sync do 
       @mapreduce_result = initial
-      q = Dispatch.queue_for(@mapreduce_result)
+      q = Dispatch.queue(@mapreduce_result)
       self.p_each do |obj|
         val = block.call(obj)
         q.async { @mapreduce_result = @mapreduce_result.send(op, val) }
@@ -66,7 +66,7 @@
     @find_q ||= Dispatch::Queue.new("enumerable.p_find.#{object_id}")
     @find_q.sync do 
       @find_result = nil
-      q = Dispatch.queue_for(@find_result)
+      q = Dispatch.queue(@find_result)
       self.p_each do |obj|
         if @find_result.nil?
           found = block.call(obj)

Modified: MacRuby/trunk/sample-macruby/Scripts/gcd/dispatch_methods.rb
===================================================================
--- MacRuby/trunk/sample-macruby/Scripts/gcd/dispatch_methods.rb	2010-02-16 01:03:53 UTC (rev 3535)
+++ MacRuby/trunk/sample-macruby/Scripts/gcd/dispatch_methods.rb	2010-02-16 01:28:21 UTC (rev 3536)
@@ -1,27 +1,27 @@
 #!/usr/local/bin/macruby
 require 'dispatch'
 
-puts "\n Use Dispatch.async to do stuff in the background"
-Dispatch.async { p "Did this later" }
-sleep 0.1 # => "Did this later"
+puts "\n Use Dispatch.async to do stuff on another thread or core) "
+Dispatch.async { p "Did this elsewhere" }
+sleep 0.1 # => "Did this elsewhere"
 
-puts "\n Use Dispatch.group to track when stuff completes"
+puts "\n Use Dispatch.group to track when async stuff completes"
 g = Dispatch.group { p "Do this" }
 Dispatch.group(g) { p "and that" }
 g.wait # => "Do this" "and that"
 p "Done"
 
-puts "\n Use Dispatch.fork to capture return values in a Future"
+puts "\n Use Dispatch.fork to capture return values of async stuff"
 f = Dispatch.fork {  2+2  }
 p f.value # => 4
 puts "  - pass a block to return the value asynchronously"
 f.value { |v| p "Returns #{v}" }
 sleep 0.1  # => "Returns 4"
 
-puts "\n Use Dispatch.queue_for to create a private serial queue"
+puts "\n Use Dispatch.queue to create a private serial queue"
 puts "  - synchronizes access to shared data structures"
 a = Array.new
-q = Dispatch.queue_for(a)
+q = Dispatch.queue(a)
 puts "  - has a (mostly) unique name:"
 p q # => Dispatch.enumerable.array.0x2000a6920.1266002369.9854
 q.async { a << "change me"  }

Modified: MacRuby/trunk/sample-macruby/Scripts/gcd/ring_buffer.rb
===================================================================
--- MacRuby/trunk/sample-macruby/Scripts/gcd/ring_buffer.rb	2010-02-16 01:03:53 UTC (rev 3535)
+++ MacRuby/trunk/sample-macruby/Scripts/gcd/ring_buffer.rb	2010-02-16 01:28:21 UTC (rev 3536)
@@ -15,7 +15,7 @@
     attr_accessor :successor
     attr_reader :index
     def initialize(g, index, successor)
-        @queue = Dispatch.queue_for(self)
+        @queue = Dispatch.queue(self)
         @group = g
         @index = index
         @successor = successor

Modified: MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb
===================================================================
--- MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb	2010-02-16 01:03:53 UTC (rev 3535)
+++ MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb	2010-02-16 01:28:21 UTC (rev 3536)
@@ -47,20 +47,31 @@
       end
     end
 
-    describe :label_for do
+    describe :labelize do
       it "should return a unique label for any object" do
-        s1 = Dispatch.label_for(@actee)
-        s2 = Dispatch.label_for(@actee)
+        s1 = Dispatch.labelize @actee
+        s2 = Dispatch.labelize @actee
         s1.should_not == s2
       end
-    end
+  end
 
-    describe :queue_for do
-      it "should return a unique queue" do
-        q1 = Dispatch.queue_for(@actee)
-        q2 = Dispatch.queue_for(@actee)
+    describe :queue do
+      it "should return a dispatch queue" do
+        q = Dispatch.queue
+        q.should be_kind_of Dispatch::Queue
+      end
+
+      it "should return a unique queue for the same object" do
+        q1 = Dispatch.queue(@actee)
+        q2 = Dispatch.queue(@actee)
         q1.should_not == q2
       end
+
+      it "should return a unique label for each queue" do
+        q1 = Dispatch.queue(@actee)
+        q2 = Dispatch.queue(@actee)
+        q1.to_s.should_not == q2.to_s
+      end
     end
 
     describe :wrap do

Modified: MacRuby/trunk/spec/macruby/library/dispatch/enumerable_spec.rb
===================================================================
--- MacRuby/trunk/spec/macruby/library/dispatch/enumerable_spec.rb	2010-02-16 01:03:53 UTC (rev 3535)
+++ MacRuby/trunk/spec/macruby/library/dispatch/enumerable_spec.rb	2010-02-16 01:28:21 UTC (rev 3536)
@@ -16,7 +16,7 @@
         @sum1 = 0
         @ary.each {|v| @sum1 += v*v}
         @sum2 = 0
-        @q = Dispatch.queue_for(@sum2)
+        @q = Dispatch.queue(@sum2)
         @ary.p_each {|v| temp = v*v; @q.sync {@sum2 += temp} }
         @sum2.should == @sum1
       end
@@ -39,7 +39,7 @@
         @sum1 = 0
         @ary.each_with_index {|v, i| @sum1 += v**i}
         @sum2 = 0
-        @q = Dispatch.queue_for(@sum2)
+        @q = Dispatch.queue(@sum2)
         @ary.p_each_with_index {|v, i| temp = v**i; @q.sync {@sum2 += temp} }
         @sum2.should == @sum1
       end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100215/b3f4bdd0/attachment.html>


More information about the macruby-changes mailing list