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.



24
25
26
27
# File 'lib/concurrent/edge/lock_free_stack.rb', line 24

def initialize
  @Head = AtomicReference.new EMPTY
  ensure_ivar_visibility!
end

Instance Method Details

#clearObject



63
64
65
66
67
68
69
# File 'lib/concurrent/edge/lock_free_stack.rb', line 63

def clear
  while true
    head = @Head.get
    return false if head == EMPTY
    return true if @Head.compare_and_set head, EMPTY
  end
end

#compare_and_clear(head) ⇒ Object



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

def compare_and_clear(head)
  @Head.compare_and_set head, EMPTY
end

#compare_and_pop(head) ⇒ Object



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

def compare_and_pop(head)
  @Head.compare_and_set head, head.next_node
end

#compare_and_push(head, value) ⇒ Object



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

def compare_and_push(head, value)
  @Head.compare_and_set head, Node[value, head]
end

#eachObject



73
74
75
76
77
78
79
80
81
# File 'lib/concurrent/edge/lock_free_stack.rb', line 73

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @Head.get.equal? EMPTY
end

#peekObject



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

def peek
  @Head.get
end

#popObject



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

def pop
  while true
    head = @Head.get
    return head.value if @Head.compare_and_set head, head.next_node
  end
end

#push(value) ⇒ Object



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

def push(value)
  while true
    head = @Head.get
    return self if @Head.compare_and_set head, Node[value, head]
  end
end