Class: ThreadSafe::AtomicReferenceCacheBackend::Table

Inherits:
Util::PowerOfTwoTuple show all
Defined in:
lib/thread_safe/atomic_reference_cache_backend.rb

Constant Summary

Constants inherited from Util::VolatileTuple

Util::VolatileTuple::Tuple

Instance Method Summary collapse

Methods inherited from Util::PowerOfTwoTuple

#hash_to_index, #initialize, #next_in_size_table, #volatile_get_by_hash, #volatile_set_by_hash

Methods inherited from Util::VolatileTuple

#compare_and_set, #each, #initialize, #size, #volatile_get, #volatile_set

Constructor Details

This class inherits a constructor from ThreadSafe::Util::PowerOfTwoTuple

Instance Method Details

#cas_new_node(i, hash, key, value) ⇒ Object



193
194
195
# File 'lib/thread_safe/atomic_reference_cache_backend.rb', line 193

def cas_new_node(i, hash, key, value)
  cas(i, nil, Node.new(hash, key, value))
end

#delete_node_at(i, node, predecessor_node) ⇒ Object



223
224
225
226
227
228
229
# File 'lib/thread_safe/atomic_reference_cache_backend.rb', line 223

def delete_node_at(i, node, predecessor_node)
  if predecessor_node
    predecessor_node.next = node.next
  else
    volatile_set(i, node.next)
  end
end

#try_lock_via_hash(i, node, node_hash) ⇒ Object



217
218
219
220
221
# File 'lib/thread_safe/atomic_reference_cache_backend.rb', line 217

def try_lock_via_hash(i, node, node_hash)
  node.try_lock_via_hash(node_hash) do
    yield if volatile_get(i) == node
  end
end

#try_to_cas_in_computed(i, hash, key) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/thread_safe/atomic_reference_cache_backend.rb', line 197

def try_to_cas_in_computed(i, hash, key)
  succeeded = false
  new_value = nil
  new_node  = Node.new(locked_hash = hash | LOCKED, key, NULL)
  if cas(i, nil, new_node)
    begin
      if NULL == (new_value = yield(NULL))
        was_null = true
      else
        new_node.value = new_value
      end
      succeeded = true
    ensure
      volatile_set(i, nil) if !succeeded || was_null
      new_node.unlock_via_hash(locked_hash, hash)
    end
  end
  return succeeded, new_value
end