Class: Concurrent::LockFreeStack
- Inherits:
-
Synchronization::Object
- Object
- Synchronization::Object
- Concurrent::LockFreeStack
- Includes:
- Enumerable
- Defined in:
- lib/concurrent/edge/lock_free_stack.rb
Defined Under Namespace
Constant Summary collapse
- EMPTY =
Empty[nil, nil]
Class Method Summary collapse
Instance Method Summary collapse
- #clear ⇒ Object
- #clear_each(&block) ⇒ Object
- #clear_if(head) ⇒ Object
- #compare_and_clear(head) ⇒ Object
- #compare_and_pop(head) ⇒ Object
- #compare_and_push(head, value) ⇒ Object
- #each(head = nil) ⇒ Object
- #empty?(head = self.head) ⇒ Boolean
-
#initialize(head = EMPTY) ⇒ LockFreeStack
constructor
A new instance of LockFreeStack.
- #peek ⇒ Object
- #pop ⇒ Object
- #push(value) ⇒ Object
- #replace_if(head, new_head) ⇒ Object
-
#to_s ⇒ String
Short string representation.
Constructor Details
#initialize(head = EMPTY) ⇒ LockFreeStack
Returns a new instance of LockFreeStack.
39 40 41 42 |
# File 'lib/concurrent/edge/lock_free_stack.rb', line 39 def initialize(head = EMPTY) super() self.head = head end |
Class Method Details
Instance Method Details
#clear ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/concurrent/edge/lock_free_stack.rb', line 90 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
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/concurrent/edge/lock_free_stack.rb', line 106 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) ⇒ Object
98 99 100 |
# File 'lib/concurrent/edge/lock_free_stack.rb', line 98 def clear_if(head) compare_and_set_head head, EMPTY end |
#compare_and_clear(head) ⇒ Object
74 75 76 |
# File 'lib/concurrent/edge/lock_free_stack.rb', line 74 def compare_and_clear(head) compare_and_set_head head, EMPTY end |
#compare_and_pop(head) ⇒ Object
63 64 65 |
# File 'lib/concurrent/edge/lock_free_stack.rb', line 63 def compare_and_pop(head) compare_and_set_head head, head.next_node end |
#compare_and_push(head, value) ⇒ Object
48 49 50 |
# File 'lib/concurrent/edge/lock_free_stack.rb', line 48 def compare_and_push(head, value) compare_and_set_head head, Node[value, head] end |
#each(head = nil) ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/concurrent/edge/lock_free_stack.rb', line 80 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) ⇒ Boolean
44 45 46 |
# File 'lib/concurrent/edge/lock_free_stack.rb', line 44 def empty?(head = self.head) head.equal? EMPTY end |
#peek ⇒ Object
59 60 61 |
# File 'lib/concurrent/edge/lock_free_stack.rb', line 59 def peek head end |
#pop ⇒ Object
67 68 69 70 71 72 |
# File 'lib/concurrent/edge/lock_free_stack.rb', line 67 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
52 53 54 55 56 57 |
# File 'lib/concurrent/edge/lock_free_stack.rb', line 52 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) ⇒ Object
102 103 104 |
# File 'lib/concurrent/edge/lock_free_stack.rb', line 102 def replace_if(head, new_head) compare_and_set_head head, new_head end |
#to_s ⇒ String
Returns Short string representation.
118 119 120 |
# File 'lib/concurrent/edge/lock_free_stack.rb', line 118 def to_s format '<#%s:0x%x %s>', self.class, object_id << 1, to_a.to_s end |