[macruby-changes] [3454] MacRuby/trunk/lib/dispatch/enumerable.rb
source_changes at macosforge.org
source_changes at macosforge.org
Mon Feb 8 15:05:29 PST 2010
Revision: 3454
http://trac.macosforge.org/projects/ruby/changeset/3454
Author: ernest.prabhakar at gmail.com
Date: 2010-02-08 15:05:29 -0800 (Mon, 08 Feb 2010)
Log Message:
-----------
wrap Enumerable#p_mapreduce in serial queue to protect @result
Modified Paths:
--------------
MacRuby/trunk/lib/dispatch/enumerable.rb
Modified: MacRuby/trunk/lib/dispatch/enumerable.rb
===================================================================
--- MacRuby/trunk/lib/dispatch/enumerable.rb 2010-02-08 23:05:16 UTC (rev 3453)
+++ MacRuby/trunk/lib/dispatch/enumerable.rb 2010-02-08 23:05:29 UTC (rev 3454)
@@ -31,19 +31,21 @@
# Parallel +collect+ plus +inject+
# Accumulates from +initial+ via +op+ (default = '+')
- def p_mapreduce(result, op=:+, &block)
- raise ArgumentError if not result.respond_to? op
+ def p_mapreduce(initial, op=:+, &block)
+ @mapreduce_q ||= Dispatch::Queue.new("enumerable.p_mapreduce.#{object_id}")
+ # Ideally should set from within a Dispatch.once to avoid race
+ raise ArgumentError if not initial.respond_to? op
# Since exceptions from a Dispatch block act funky
- @result = result
- q = Dispatch.queue_for(@result)
- self.p_each do |obj|
- val = block.call(obj)
- q.async do
- @result = @result.send(op, val)
+ @mapreduce_q.sync do
+ @result = initial
+ q = Dispatch.queue_for(@result)
+ p_each do |obj|
+ val = block.call(obj)
+ q.async { @result = @result.send(op, val) }
end
+ q.sync {}
+ return @result # can return from inside the block
end
- q.sync {}
- @result
end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100208/b048f8d7/attachment-0001.html>
More information about the macruby-changes
mailing list