Class: Concurrent::LockFreeStack

Inherits:
Synchronization::Object show all
Includes:
Enumerable
Defined in:
lib/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.

Defined Under Namespace

Classes: Node

Constant Summary collapse

EMPTY =

The singleton for empty node

Node[nil, nil]

Instance Method Summary collapse

Constructor Details

#initialize(head = EMPTY) ⇒ LockFreeStack

Returns a new instance of LockFreeStack

Parameters:

  • head (Node) (defaults to: EMPTY)


49
50
51
52
# File 'lib/concurrent/collection/lock_free_stack.rb', line 49

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

Instance Method Details

#cleartrue, false

Returns:

  • (true, false)


116
117
118
119
120
121
122
# File 'lib/concurrent/collection/lock_free_stack.rb', line 116

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:

  • value (Object)

Returns:

  • (self)


140
141
142
143
144
145
146
147
148
149
# File 'lib/concurrent/collection/lock_free_stack.rb', line 140

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:

Returns:

  • (true, false)


126
127
128
# File 'lib/concurrent/collection/lock_free_stack.rb', line 126

def clear_if(head)
  compare_and_set_head head, EMPTY
end

#compare_and_clear(head) ⇒ true, false

Parameters:

Returns:

  • (true, false)


97
98
99
# File 'lib/concurrent/collection/lock_free_stack.rb', line 97

def compare_and_clear(head)
  compare_and_set_head head, EMPTY
end

#compare_and_pop(head) ⇒ true, false

Parameters:

Returns:

  • (true, false)


83
84
85
# File 'lib/concurrent/collection/lock_free_stack.rb', line 83

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

#compare_and_push(head, value) ⇒ true, false

Parameters:

  • head (Node)
  • value (Object)

Returns:

  • (true, false)


63
64
65
# File 'lib/concurrent/collection/lock_free_stack.rb', line 63

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

#each(head = nil) ⇒ self

Parameters:

  • head (Node) (defaults to: nil)

Returns:

  • (self)


105
106
107
108
109
110
111
112
113
# File 'lib/concurrent/collection/lock_free_stack.rb', line 105

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 = self.head) ⇒ true, false

Parameters:

  • head (Node) (defaults to: self.head)

Returns:

  • (true, false)


56
57
58
# File 'lib/concurrent/collection/lock_free_stack.rb', line 56

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

#peekNode

Returns:



77
78
79
# File 'lib/concurrent/collection/lock_free_stack.rb', line 77

def peek
  head
end

#popObject

Returns:

  • (Object)


88
89
90
91
92
93
# File 'lib/concurrent/collection/lock_free_stack.rb', line 88

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:

  • value (Object)

Returns:

  • (self)


69
70
71
72
73
74
# File 'lib/concurrent/collection/lock_free_stack.rb', line 69

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:

Returns:

  • (true, false)


133
134
135
# File 'lib/concurrent/collection/lock_free_stack.rb', line 133

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

#to_sString Also known as: inspect

Returns Short string representation.

Returns:

  • (String)

    Short string representation.



152
153
154
# File 'lib/concurrent/collection/lock_free_stack.rb', line 152

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