Class: Concurrent::Edge::LockFreeStack

Inherits:
Synchronization::Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/concurrent/edge/lock_free_stack.rb

Defined Under Namespace

Classes: Empty, Node

Constant Summary collapse

EMPTY =
Empty[nil, nil]

Instance Method Summary collapse

Constructor Details

#initializeLockFreeStack

Returns a new instance of LockFreeStack.



28
29
30
31
# File 'lib/concurrent/edge/lock_free_stack.rb', line 28

def initialize
  super()
  self.head = EMPTY
end

Instance Method Details

#clearObject



79
80
81
82
83
84
85
# File 'lib/concurrent/edge/lock_free_stack.rb', line 79

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(&block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/concurrent/edge/lock_free_stack.rb', line 87

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

#compare_and_clear(head) ⇒ Object



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

def compare_and_clear(head)
  compare_and_set_head head, EMPTY
end

#compare_and_pop(head) ⇒ Object



52
53
54
# File 'lib/concurrent/edge/lock_free_stack.rb', line 52

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

#compare_and_push(head, value) ⇒ Object



37
38
39
# File 'lib/concurrent/edge/lock_free_stack.rb', line 37

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

#each(head = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/concurrent/edge/lock_free_stack.rb', line 69

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?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/concurrent/edge/lock_free_stack.rb', line 33

def empty?
  head.equal? EMPTY
end

#peekObject



48
49
50
# File 'lib/concurrent/edge/lock_free_stack.rb', line 48

def peek
  head
end

#popObject



56
57
58
59
60
61
# File 'lib/concurrent/edge/lock_free_stack.rb', line 56

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) ⇒ Object



41
42
43
44
45
46
# File 'lib/concurrent/edge/lock_free_stack.rb', line 41

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