Class: Concurrent::LockFreeStack

Inherits:
Synchronization::Object
  • Object
show all
Includes:
Enumerable
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.

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)


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

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

Instance Method Details

#cleartrue, false

Returns:

  • (true, false)


118
119
120
121
122
123
124
# 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:

  • value (Object)

Returns:

  • (self)


142
143
144
145
146
147
148
149
150
151
# 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:

Returns:

  • (true, false)


128
129
130
# 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:

Returns:

  • (true, false)


99
100
101
# 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:

Returns:

  • (true, false)


85
86
87
# 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:

  • head (Node)
  • value (Object)

Returns:

  • (true, false)


65
66
67
# 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

#each(head = nil) ⇒ self

Parameters:

  • head (Node) (defaults to: nil)

Returns:

  • (self)


107
108
109
110
111
112
113
114
115
# 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())

Returns:

  • (true, false)


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

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

#peekNode

Returns:



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

def peek
  head
end

#popObject

Returns:

  • (Object)


90
91
92
93
94
95
# 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:

  • value (Object)

Returns:

  • (self)


71
72
73
74
75
76
# 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:

Returns:

  • (true, false)


135
136
137
# 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

#to_sString Also known as: inspect

Returns Short string representation.

Returns:

  • (String)

    Short string representation.



154
155
156
# 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