Permalink
Browse files

Get rid of the ruby-atomic dependency.

  • Loading branch information...
1 parent f608b70 commit 33114e71ad538745800f49f445effff2e60146ac @thedarkone committed Apr 3, 2014
Showing with 37 additions and 4 deletions.
  1. +36 −3 lib/thread_safe/util/atomic_reference.rb
  2. +1 −1 thread_safe.gemspec
@@ -1,12 +1,45 @@
module ThreadSafe
module Util
- # An overhead-less atomic reference.
AtomicReference =
if defined?(Rubinius::AtomicReference)
+ # An overhead-less atomic reference.
Rubinius::AtomicReference
else
- require 'atomic'
- defined?(Atomic::InternalReference) ? Atomic::InternalReference : Atomic
+ begin
+ require 'atomic'
+ defined?(Atomic::InternalReference) ? Atomic::InternalReference : Atomic
+ rescue NameError
+ require 'thread' # get Mutex on 1.8
+ class FullLockingAtomicReference
+ def initialize(value = nil)
+ @___mutex = Mutex.new
+ @___value = value
+ end
+
+ def get
+ @___mutex.synchronize { @___value }
+ end
+ alias_method :value, :get
+
+ def set(new_value)
+ @___mutex.synchronize { @___value = new_value }
+ end
+ alias_method :value=, :set
+
+ def compare_and_set(old_value, new_value)
+ return false unless @___mutex.try_lock
+ begin
+ return false unless @___value.equal? old_value
+ @___value = new_value
+ ensure
+ @___mutex.unlock
+ end
+ true
+ end
+ end
+
+ FullLockingAtomicReference
+ end
end
end
end
View
@@ -19,6 +19,6 @@ Gem::Specification.new do |gem|
gem.version = ThreadSafe::VERSION
gem.license = "Apache-2.0"
- gem.add_dependency 'atomic', ['>= 1.1.7', '< 2']
+ gem.add_development_dependency 'atomic', ['>= 1.1.7', '< 2']
gem.add_development_dependency 'rake'
end

0 comments on commit 33114e7

Please sign in to comment.