123456789_123456789_123456789_123456789_123456789_

Class: Concurrent::LockFreeStack

Relationships & Source Files
Namespace Children
Classes:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Concurrent::Synchronization::Object
Defined in: lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb

Overview

Note:

**Edge Features** are under active development and may change frequently.

  • Deprecations are not added before incompatible changes.

  • Edge version: major is always 0, minor bump means incompatible change, patch bump means compatible change.

  • Edge features may also lack tests and documentation.

  • Features developed in concurrent-ruby-edge are expected to move to concurrent-ruby when finalised.

Constant Summary

Class Attribute Summary

Class Method Summary

Synchronization::Object - Inherited

.atomic_attribute?, .atomic_attributes,
.attr_atomic

Creates methods for reading and writing to a instance variable with volatile (Java) semantic as .attr_volatile does.

.attr_volatile

Creates methods for reading and writing (as attr_accessor does) to a instance variable with volatile (Java) semantic.

.ensure_safe_initialization_when_final_fields_are_present

For testing purposes, quite slow.

.new

Has to be called by children.

.safe_initialization!, .define_initialize_atomic_fields

Synchronization::AbstractObject - Inherited

Instance Method Summary

Constructor Details

.new(head = EMPTY) ⇒ LockFreeStack

Parameters:

  • head (Node) (defaults to: EMPTY)
[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 51

def initialize(head = EMPTY)
  super()
  self.head = head
end

Class Method Details

.of1(value) (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 41

def self.of1(value)
  new Node[value, EMPTY]
end

.of2(value1, value2) (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 46

def self.of2(value1, value2)
  new Node[value1, Node[value2, EMPTY]]
end

Instance Method Details

#cleartrue, false

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 118

def clear
  while true
    current_head = head
    return false if current_head == EMPTY
    return true if compare_and_set_head current_head, EMPTY
  end
end

#clear_each {|value| ... } ⇒ self

Yields:

  • over the cleared stack

Yield Parameters:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 142

def clear_each(&block)
  while true
    current_head = head
    return self if current_head == EMPTY
    if compare_and_set_head current_head, EMPTY
      each current_head, &block
      return self
    end
  end
end

#clear_if(head) ⇒ true, false

Parameters:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 128

def clear_if(head)
  compare_and_set_head head, EMPTY
end

#compare_and_clear(head) ⇒ true, false

Parameters:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 99

def compare_and_clear(head)
  compare_and_set_head head, EMPTY
end

#compare_and_pop(head) ⇒ true, false

Parameters:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 85

def compare_and_pop(head)
  compare_and_set_head head, head.next_node
end

#compare_and_push(head, value) ⇒ true, false

Parameters:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 65

def compare_and_push(head, value)
  compare_and_set_head head, Node[value, head]
end

#compare_and_set_head(expected_head, new_head) ⇒ true, false (private)

Sets the head to new_head if the current head is expected_head

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 37

attr_atomic(:head)

#each(head = nil) ⇒ self

Parameters:

  • head (Node) (defaults to: nil)
[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 107

def each(head = nil)
  return to_enum(:each, head) unless block_given?
  it = head || peek
  until it.equal?(EMPTY)
    yield it.value
    it = it.next_node
  end
  self
end

#empty?(head = head()) ⇒ true, false

Parameters:

  • head (Node) (defaults to: head())
[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 58

def empty?(head = head())
  head.equal? EMPTY
end

#headObject (private)

Returns:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 37

attr_atomic(:head)

#head=(new_head) ⇒ Object (private)

Set the head.

Returns:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 37

attr_atomic(:head)

#inspect

Alias for #to_s.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 158

alias_method :inspect, :to_s

#peekNode

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 79

def peek
  head
end

#popObject

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 90

def pop
  while true
    current_head = head
    return current_head.value if compare_and_set_head current_head, current_head.next_node
  end
end

#push(value) ⇒ self

Parameters:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 71

def push(value)
  while true
    current_head = head
    return self if compare_and_set_head current_head, Node[value, current_head]
  end
end

#replace_if(head, new_head) ⇒ true, false

Parameters:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 135

def replace_if(head, new_head)
  compare_and_set_head head, new_head
end

#swap_head(new_head) ⇒ Object (private)

Set the head to new_head and return the old head.

Returns:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 37

attr_atomic(:head)

#to_sString Also known as: #inspect

Returns:

  • (String)

    Short string representation.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 154

def to_s
  format '%s %s>', super[0..-2], to_a.to_s
end

#update_head {|Object| ... } ⇒ Object (private)

Updates the head using the block.

Yields:

  • (Object)

    Calculate a new head using given (old) head

Yield Parameters:

Returns:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb', line 37

attr_atomic(:head)