Skip to content
Browse files

Merge remote-tracking branch 'origin/master' into MagLev-1.1

  • Loading branch information...
2 parents cecccba + 583d77a commit 5b378ceeb47f4f76f11daecac5be5ceb92d51045 @timfel timfel committed Oct 23, 2012
View
4 THANKS
@@ -5,7 +5,7 @@ including:
- Avi Bryant, for a lot of inspiration and hard work from day one.
- Markus Roberts, for lots of feedback, bugfixes, and porting irb and zaml
to MagLev.
-- Michael Latta, for getting the MySQL driver and some of Rails working
+- Michael Latta, for getting the MySQL driver and some of Rails working
under MagLev, and his valuable feedback and bugfixes while suffering
through earliest Alphas
- Danie Roux and Otto Behrens, for providing rake scripts to manage multiple
@@ -19,4 +19,4 @@ including:
- Tim Felgentreff for creating an FFI'd bcrypt-ruby that works with MagLev, getting
mkmf.rb working and other help with MagLev C extensions.
- Jesse Cooke for adding MagLev support to ruby-build - https://github.com/sstephenson/ruby-build
- and exposing RCQueue, along with starting the use of MSpec for functionality like the Reduced Conflict classes.
+ and exposing some of the Reduced Conflict classes, along with starting the use of MSpec for functionality like the Reduced Conflict classes.
View
37 lib/ruby/1.8/maglev/rccounter.rb
@@ -0,0 +1,37 @@
+# Reduced Conflict Counter
+# RCCounter is identically Smalltalk RcCounter
+
+RCCounter = __resolve_smalltalk_global(:RcCounter)
+
+# RCCounter is a simple counter that allows multiple users to update it
+# without running into conflicts. It replaces the need for a mutex in
+# normal Ruby programming when implementing a counter that would be accessed
+# by multiple threads.
+#
+# Unlike another counter, RCCounter provides for concurrent handling of an individual
+# instance by multiple sessions. Any or all of those sessions can modify
+# the single instance. When that happens, RCCounter reduces (but does not
+# eliminate) the transaction conflicts that can arise among those sessions
+# when they attempt to commit the instance to GemStone.
+
+class RCCounter
+ class_primitive 'new', 'new'
+
+ primitive 'value', 'value'
+ primitive 'increment', 'increment'
+ primitive 'increment_by', 'incrementBy:'
+ primitive 'decrement', 'decrement'
+ primitive '__decrement_by', 'decrementBy:'
+ primitive 'decrement_if_negative&', 'decrementIfNegative:'
+ primitive '__decrement_by_if_less_than&', 'decrementBy:ifLessThan:thenExecute:'
+
+ def decrement_by(amount, guard=nil)
+ if guard && block_given?
+ __decrement_by_if_less_than(amount, guard) do
+ yield
+ end
+ else
+ __decrement_by(amount)
+ end
+ end
+end
View
50 spec/maglev/reducedconflict/counter/decrement_by_spec.rb
@@ -0,0 +1,50 @@
+require File.expand_path('../../../../spec_helper', __FILE__)
+require 'maglev/rccounter'
+
+describe "RCCounter#decrement_by" do
+
+ describe "with only amount" do
+ it "subtracts x from the counter" do
+ counter = RCCounter.new
+ counter.decrement_by 10
+ counter.value.should == -10
+ end
+ end
+
+ describe "with amount, guard and block" do
+ before do
+ @counter = RCCounter.new
+ end
+
+ describe "when guard is hit" do
+ it "executes the block" do
+ @counter.decrement_by(10, -1) do
+ 'block executed'
+ end.should == 'block executed'
+ end
+
+ it "doesn't decrement the value" do
+ @counter.decrement_by(10, -1) do
+ 'block executed'
+ end
+ @counter.value.should == 0
+ end
+ end
+
+ describe "when guard is missed" do
+ it "decrements the value" do
+ @counter.decrement_by(5, -7) do
+ "won't get executed"
+ end
+ @counter.value.should == -5
+ end
+
+ it "returns self" do
+ @counter.decrement_by(5, -7) do
+ "won't get executed"
+ end.should equal(@counter)
+ end
+ end
+ end
+
+end
View
23 spec/maglev/reducedconflict/counter/decrement_if_negative_spec.rb
@@ -0,0 +1,23 @@
+require File.expand_path('../../../../spec_helper', __FILE__)
+require 'maglev/rccounter'
+
+describe "RCCounter#decrement_if_negative" do
+
+ describe "when value is positive" do
+ it "subtracts 1 from the counter" do
+ counter = RCCounter.new
+ counter.increment_by 10
+ counter.decrement_if_negative { "won't get executed" }
+ counter.value.should == 9
+ end
+ end
+
+ describe "when value is negative" do
+ it "executes the block" do
+ counter = RCCounter.new
+ result = counter.decrement_if_negative { 'block executed' }
+ result.should == 'block executed'
+ end
+ end
+
+end
View
12 spec/maglev/reducedconflict/counter/decrement_spec.rb
@@ -0,0 +1,12 @@
+require File.expand_path('../../../../spec_helper', __FILE__)
+require 'maglev/rccounter'
+
+describe "RCCounter#decrement" do
+
+ it "subtracts 1 from the counter" do
+ counter = RCCounter.new
+ counter.decrement
+ counter.value.should == -1
+ end
+
+end
View
12 spec/maglev/reducedconflict/counter/increment_by_spec.rb
@@ -0,0 +1,12 @@
+require File.expand_path('../../../../spec_helper', __FILE__)
+require 'maglev/rccounter'
+
+describe "RCCounter#increment_by" do
+
+ it "adds x to the counter" do
+ counter = RCCounter.new
+ counter.increment_by 10
+ counter.value.should == 10
+ end
+
+end
View
12 spec/maglev/reducedconflict/counter/increment_spec.rb
@@ -0,0 +1,12 @@
+require File.expand_path('../../../../spec_helper', __FILE__)
+require 'maglev/rccounter'
+
+describe "RCCounter#increment" do
+
+ it "adds 1 to the counter" do
+ counter = RCCounter.new
+ counter.increment
+ counter.value.should == 1
+ end
+
+end
View
10 spec/maglev/reducedconflict/counter/new_spec.rb
@@ -0,0 +1,10 @@
+require File.expand_path('../../../../spec_helper', __FILE__)
+require 'maglev/rccounter'
+
+describe "RCCounter.new" do
+
+ it "returns an instance of RCCounter" do
+ RCCounter.new.should be_an_instance_of(RCCounter)
+ end
+
+end
View
10 spec/maglev/reducedconflict/counter/value_spec.rb
@@ -0,0 +1,10 @@
+require File.expand_path('../../../../spec_helper', __FILE__)
+require 'maglev/rccounter'
+
+describe "RCCounter#value" do
+ describe "new instance" do
+ it "returns 0" do
+ RCCounter.new.value.should == 0
+ end
+ end
+end
View
6 spec/maglev/reducedconflict/hash/each_spec.rb
@@ -4,6 +4,8 @@
require File.expand_path('../shared/iteration', __FILE__)
describe "RCHash#each" do
- it_behaves_like(:rchash_each, :each)
- it_behaves_like(:rchash_iteration_no_block, :each)
+ quarantine! do
+ it_behaves_like(:rchash_each, :each)
+ it_behaves_like(:rchash_iteration_no_block, :each)
+ end
end

0 comments on commit 5b378ce

Please sign in to comment.
Something went wrong with that request. Please try again.