Class: Atomic

Inherits:
Rubinius::AtomicReference
  • Object
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

:nodoc: all



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_setObject



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



55
56
57
58
59
60
61
62
63
64
# File 'ext/atomic_reference.c', line 55

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

#getObject Also known as: value



39
40
41
# File 'ext/atomic_reference.c', line 39

def get
  @mutex.synchronize { @value }
end

#get_and_set(new_value) ⇒ Object Also known as: swap



48
49
50
51
52
53
54
# File 'ext/atomic_reference.c', line 48

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=



43
44
45
# File 'ext/atomic_reference.c', line 43

def set(new_value)
  @mutex.synchronize { @value = new_value }
end

#try_updateObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/atomic/direct_update.rb', line 25

def try_update
  old_value = get
  new_value = yield old_value
  unless compare_and_set(old_value, new_value)
    if $VERBOSE
      raise ConcurrentUpdateError, "Update failed"
    else
      raise ConcurrentUpdateError, "Update failed", ConcurrentUpdateError::CONC_UP_ERR_BACKTRACE
    end
  end
  new_value
end

#updateObject

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