Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #16 from toolmantim/move-atomic-into-scrolls-module

Move Atomic into the Scrolls module
  • Loading branch information...
commit 741a206b8cee6c4ea98d32f9855e3f9deab2e25a 2 parents 6a19130 + a68dff7
@asenchi authored
Showing with 52 additions and 51 deletions.
  1. +47 −46 lib/scrolls/atomic.rb
  2. +5 −5 test/test_atomic.rb
View
93 lib/scrolls/atomic.rb
@@ -1,58 +1,59 @@
-# The result of issues with an update I made to Scrolls. After talking with
-# Fabio Kung about a fix I started work on an atomic object, but he added some
-# fixes to #context without it and then used Headius' atomic gem.
-#
-# The code below is the start and cleanup of my atomic object. It's slim on
-# details and eventually cleaned up around inspiration from Headius' code.
-#
-# LICENSE: Apache 2.0
-#
-# See Headius' atomic gem here:
-# https://github.com/headius/ruby-atomic
-
require 'thread'
-class AtomicObject
- def initialize(o)
- @mtx = Mutex.new
- @o = o
- end
+module Scrolls
+ # The result of issues with an update I made to Scrolls. After talking with
+ # Fabio Kung about a fix I started work on an atomic object, but he added some
+ # fixes to #context without it and then used Headius' atomic gem.
+ #
+ # The code below is the start and cleanup of my atomic object. It's slim on
+ # details and eventually cleaned up around inspiration from Headius' code.
+ #
+ # LICENSE: Apache 2.0
+ #
+ # See Headius' atomic gem here:
+ # https://github.com/headius/ruby-atomic
+ class AtomicObject
+ def initialize(o)
+ @mtx = Mutex.new
+ @o = o
+ end
- def get
- @mtx.synchronize { @o }
- end
+ def get
+ @mtx.synchronize { @o }
+ end
- def set(n)
- @mtx.synchronize { @o = n }
- end
+ def set(n)
+ @mtx.synchronize { @o = n }
+ end
- def verify_set(o, n)
- return false unless @mtx.try_lock
- begin
- return false unless @o.equal? o
- @o = n
- ensure
- @mtx.unlock
+ def verify_set(o, n)
+ return false unless @mtx.try_lock
+ begin
+ return false unless @o.equal? o
+ @o = n
+ ensure
+ @mtx.unlock
+ end
end
end
-end
-class Atomic < AtomicObject
- def initialize(v=nil)
- super(v)
- end
+ class Atomic < AtomicObject
+ def initialize(v=nil)
+ super(v)
+ end
- def value
- self.get
- end
+ def value
+ self.get
+ end
- def value=(v)
- self.set(v)
- v
- end
+ def value=(v)
+ self.set(v)
+ v
+ end
- def update
- true until self.verify_set(o = self.get, n = yield(o))
- n
+ def update
+ true until self.verify_set(o = self.get, n = yield(o))
+ n
+ end
end
-end
+end
View
10 test/test_atomic.rb
@@ -2,22 +2,22 @@
class TestAtomic < Test::Unit::TestCase
def test_construct
- atomic = Atomic.new
+ atomic = Scrolls::Atomic.new
assert_equal nil, atomic.value
- atomic = Atomic.new(0)
+ atomic = Scrolls::Atomic.new(0)
assert_equal 0, atomic.value
end
def test_value
- atomic = Atomic.new(0)
+ atomic = Scrolls::Atomic.new(0)
atomic.value = 1
assert_equal 1, atomic.value
end
def test_update
- atomic = Atomic.new(1000)
+ atomic = Scrolls::Atomic.new(1000)
res = atomic.update {|v| v + 1}
assert_equal 1001, atomic.value
@@ -26,7 +26,7 @@ def test_update
def test_update_retries
tries = 0
- atomic = Atomic.new(1000)
+ atomic = Scrolls::Atomic.new(1000)
atomic.update{|v| tries += 1 ; atomic.value = 1001 ; v + 1}
assert_equal 2, tries
end

1 comment on commit 741a206

@eric

Would you mind releasing a gem with this?

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