Revision: 3150 http://trac.macosforge.org/projects/ruby/changeset/3150 Author: pthomson@apple.com Date: 2009-12-22 15:07:23 -0800 (Tue, 22 Dec 2009) Log Message: ----------- Moved the GCD-related samples to their own directory. Added Paths: ----------- MacRuby/trunk/sample-macruby/Scripts/gcd/ MacRuby/trunk/sample-macruby/Scripts/gcd/futures.rb MacRuby/trunk/sample-macruby/Scripts/gcd/sleeping_barber.rb Removed Paths: ------------- MacRuby/trunk/sample-macruby/Scripts/futures.rb MacRuby/trunk/sample-macruby/Scripts/sleeping_barber.rb Deleted: MacRuby/trunk/sample-macruby/Scripts/futures.rb =================================================================== --- MacRuby/trunk/sample-macruby/Scripts/futures.rb 2009-12-22 22:42:22 UTC (rev 3149) +++ MacRuby/trunk/sample-macruby/Scripts/futures.rb 2009-12-22 23:07:23 UTC (rev 3150) @@ -1,34 +0,0 @@ -#!/usr/local/bin/macruby - -# An implementation of futures (delayed computations) on top of GCD. -# Original implementation written by Patrick Thomson. -# Improvements made by Ben Stiglitz. - -include Dispatch - -class Future - def initialize(&block) - # Each thread gets its own FIFO queue upon which we will dispatch - # the delayed computation passed in the &block variable. - Thread.current[:futures] ||= Queue.new("org.macruby.futures-#{Thread.current.object_id}") - # Groups are just simple layers on top of semaphores. - @group = Group.new - # Asynchronously dispatch the future to the thread-local queue. - Thread.current[:futures].async(@group) { @value = block[] } - end - - def value - # Wait fo the computation to finish. If it has already finished, then - # just return the value in question. - @group.wait - @value - end -end - - -f = Future.new do - sleep 2.5 - 'some value' -end - -p f.value \ No newline at end of file Copied: MacRuby/trunk/sample-macruby/Scripts/gcd/futures.rb (from rev 3149, MacRuby/trunk/sample-macruby/Scripts/futures.rb) =================================================================== --- MacRuby/trunk/sample-macruby/Scripts/gcd/futures.rb (rev 0) +++ MacRuby/trunk/sample-macruby/Scripts/gcd/futures.rb 2009-12-22 23:07:23 UTC (rev 3150) @@ -0,0 +1,34 @@ +#!/usr/local/bin/macruby + +# An implementation of futures (delayed computations) on top of GCD. +# Original implementation written by Patrick Thomson. +# Improvements made by Ben Stiglitz. + +include Dispatch + +class Future + def initialize(&block) + # Each thread gets its own FIFO queue upon which we will dispatch + # the delayed computation passed in the &block variable. + Thread.current[:futures] ||= Queue.new("org.macruby.futures-#{Thread.current.object_id}") + # Groups are just simple layers on top of semaphores. + @group = Group.new + # Asynchronously dispatch the future to the thread-local queue. + Thread.current[:futures].async(@group) { @value = block[] } + end + + def value + # Wait fo the computation to finish. If it has already finished, then + # just return the value in question. + @group.wait + @value + end +end + + +f = Future.new do + sleep 2.5 + 'some value' +end + +p f.value \ No newline at end of file Copied: MacRuby/trunk/sample-macruby/Scripts/gcd/sleeping_barber.rb (from rev 3149, MacRuby/trunk/sample-macruby/Scripts/sleeping_barber.rb) =================================================================== --- MacRuby/trunk/sample-macruby/Scripts/gcd/sleeping_barber.rb (rev 0) +++ MacRuby/trunk/sample-macruby/Scripts/gcd/sleeping_barber.rb 2009-12-22 23:07:23 UTC (rev 3150) @@ -0,0 +1,19 @@ +# A GCD-based implementation of the sleeping barber problem: +# http://en.wikipedia.org/wiki/Sleeping_barber_problem +# http://www.madebysofa.com/#blog/the_sleeping_barber + +waiting_chairs = Dispatch::Queue.new('com.apple.waiting_chairs') +semaphore = Dispatch::Semaphore.new(3) +index = -1 +while true + index += 1 + success = semaphore.wait(Dispatch::TIME_NOW) + if success != 0 + puts "Customer turned away #{index}" + next + end + waiting_chairs.dispatch do + semaphore.signal + puts "Shave and a haircut #{index}" + end +end Deleted: MacRuby/trunk/sample-macruby/Scripts/sleeping_barber.rb =================================================================== --- MacRuby/trunk/sample-macruby/Scripts/sleeping_barber.rb 2009-12-22 22:42:22 UTC (rev 3149) +++ MacRuby/trunk/sample-macruby/Scripts/sleeping_barber.rb 2009-12-22 23:07:23 UTC (rev 3150) @@ -1,19 +0,0 @@ -# A GCD-based implementation of the sleeping barber problem: -# http://en.wikipedia.org/wiki/Sleeping_barber_problem -# http://www.madebysofa.com/#blog/the_sleeping_barber - -waiting_chairs = Dispatch::Queue.new('com.apple.waiting_chairs') -semaphore = Dispatch::Semaphore.new(3) -index = -1 -while true - index += 1 - success = semaphore.wait(Dispatch::TIME_NOW) - if success != 0 - puts "Customer turned away #{index}" - next - end - waiting_chairs.dispatch do - semaphore.signal - puts "Shave and a haircut #{index}" - end -end
participants (1)
-
source_changes@macosforge.org