Skip to content
This repository was archived by the owner on Mar 15, 2022. It is now read-only.

Commit 33114e7

Browse files
committed
Get rid of the ruby-atomic dependency.
1 parent f608b70 commit 33114e7

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed
Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,45 @@
11
module ThreadSafe
22
module Util
3-
# An overhead-less atomic reference.
43
AtomicReference =
54
if defined?(Rubinius::AtomicReference)
5+
# An overhead-less atomic reference.
66
Rubinius::AtomicReference
77
else
8-
require 'atomic'
9-
defined?(Atomic::InternalReference) ? Atomic::InternalReference : Atomic
8+
begin
9+
require 'atomic'
10+
defined?(Atomic::InternalReference) ? Atomic::InternalReference : Atomic
11+
rescue NameError
12+
require 'thread' # get Mutex on 1.8
13+
class FullLockingAtomicReference
14+
def initialize(value = nil)
15+
@___mutex = Mutex.new
16+
@___value = value
17+
end
18+
19+
def get
20+
@___mutex.synchronize { @___value }
21+
end
22+
alias_method :value, :get
23+
24+
def set(new_value)
25+
@___mutex.synchronize { @___value = new_value }
26+
end
27+
alias_method :value=, :set
28+
29+
def compare_and_set(old_value, new_value)
30+
return false unless @___mutex.try_lock
31+
begin
32+
return false unless @___value.equal? old_value
33+
@___value = new_value
34+
ensure
35+
@___mutex.unlock
36+
end
37+
true
38+
end
39+
end
40+
41+
FullLockingAtomicReference
42+
end
1043
end
1144
end
1245
end

thread_safe.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ Gem::Specification.new do |gem|
1919
gem.version = ThreadSafe::VERSION
2020
gem.license = "Apache-2.0"
2121

22-
gem.add_dependency 'atomic', ['>= 1.1.7', '< 2']
22+
gem.add_development_dependency 'atomic', ['>= 1.1.7', '< 2']
2323
gem.add_development_dependency 'rake'
2424
end

0 commit comments

Comments
 (0)