Class: Atomic
- Inherits:
-
Rubinius::AtomicReference
- Object
- Rubinius::AtomicReference
- Atomic
show all
- Defined in:
- lib/atomic/rbx.rb,
lib/atomic/fallback.rb,
lib/atomic/direct_update.rb,
lib/atomic/delegated_update.rb,
lib/atomic/numeric_cas_wrapper.rb,
lib/atomic/concurrent_update_error.rb
Overview
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Defined Under Namespace
Classes: ConcurrentUpdateError
Instance Method Summary
collapse
Constructor Details
#initialize(*args) ⇒ Atomic
18
19
20
21
|
# File 'lib/atomic/fallback.rb', line 18
def initialize(value = nil)
@mutex = Mutex.new
@value = value
end
|
Instance Method Details
#_compare_and_set ⇒ Object
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/atomic/numeric_cas_wrapper.rb', line 14
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
|
#compare_and_set(expect_value, new_value) ⇒ Object
Also known as:
compare_and_swap
48
49
50
51
52
53
54
55
56
57
|
# File 'ext/atomic_reference.c', line 48
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
|
#get ⇒ Object
Also known as:
value
32
33
34
|
# File 'ext/atomic_reference.c', line 32
def get
@mutex.synchronize { @value }
end
|
#get_and_set(new_value) ⇒ Object
Also known as:
swap
41
42
43
44
45
46
47
|
# File 'ext/atomic_reference.c', line 41
def get_and_set(new_value)
@mutex.synchronize do
old_value = @value
@value = new_value
old_value
end
end
|
#set(new_value) ⇒ Object
Also known as:
value=
36
37
38
|
# File 'ext/atomic_reference.c', line 36
def set(new_value)
@mutex.synchronize { @value = new_value }
end
|
#update ⇒ Object
Pass the current value to the given block, replacing it with the block’s result. May retry if the value changes during the block’s execution.
20
21
22
23
|
# File 'lib/atomic/direct_update.rb', line 20
def update
true until compare_and_set(old_value = get, new_value = yield(old_value))
new_value
end
|