Class: Concurrent::Edge::LockFreeStack
- Inherits:
-
Synchronization::Object
- Object
- Synchronization::Object
- Concurrent::Edge::LockFreeStack
- Includes:
- Enumerable
- Defined in:
- lib/concurrent/edge/lock_free_stack.rb
Defined Under Namespace
Constant Summary collapse
- EMPTY =
Empty[nil, nil]
Instance Method Summary collapse
- #clear ⇒ Object
- #compare_and_clear(head) ⇒ Object
- #compare_and_pop(head) ⇒ Object
- #compare_and_push(head, value) ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ LockFreeStack
constructor
A new instance of LockFreeStack.
- #peek ⇒ Object
- #pop ⇒ Object
- #push(value) ⇒ Object
Constructor Details
#initialize ⇒ LockFreeStack
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
#clear ⇒ Object
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 |
#each ⇒ Object
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
29 30 31 |
# File 'lib/concurrent/edge/lock_free_stack.rb', line 29 def empty? @Head.get.equal? EMPTY end |
#peek ⇒ Object
44 45 46 |
# File 'lib/concurrent/edge/lock_free_stack.rb', line 44 def peek @Head.get end |
#pop ⇒ Object
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 |