Skip to content

Commit 4ab0cc1

Browse files
committed
Add clear_each to LockFreeStack
1 parent 9c7d28f commit 4ab0cc1

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

lib/concurrent/edge/lock_free_stack.rb

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ def compare_and_clear(head)
6464
compare_and_set_head head, EMPTY
6565
end
6666

67+
include Enumerable
68+
69+
def each(head = nil)
70+
return to_enum(:each, head) unless block_given?
71+
it = head || peek
72+
until it.equal?(EMPTY)
73+
yield it.value
74+
it = it.next_node
75+
end
76+
self
77+
end
78+
6779
def clear
6880
while true
6981
current_head = head
@@ -72,16 +84,15 @@ def clear
7284
end
7385
end
7486

75-
include Enumerable
76-
77-
def each
78-
return to_enum unless block_given?
79-
it = peek
80-
until it.equal?(EMPTY)
81-
yield it.value
82-
it = it.next_node
87+
def clear_each(&block)
88+
while true
89+
current_head = head
90+
return self if current_head == EMPTY
91+
if compare_and_set_head current_head, EMPTY
92+
each current_head, &block
93+
return self
94+
end
8395
end
84-
self
8596
end
8697

8798
end

0 commit comments

Comments
 (0)