Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Thread::LinkedList#each to safely iterate lists #15300

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/crystal/system/thread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class Thread
@@threads.try(&.unsafe_each { |thread| yield thread })
end

def self.each(&)
threads.each { |thread| yield thread }
end

def self.lock : Nil
[email protected]
end
Expand Down
7 changes: 7 additions & 0 deletions src/crystal/system/thread_linked_list.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ class Thread
end
end

# Safely iterates the list.
def each(&) : Nil
@mutex.synchronize do
unsafe_each { |node| yield node }
end
end

# Appends a node to the tail of the list. The operation is thread-safe.
#
# There are no guarantees that a node being pushed will be iterated by
Expand Down
5 changes: 5 additions & 0 deletions src/fiber.cr
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ class Fiber
@@fibers.try(&.unsafe_each { |fiber| yield fiber })
end

# :nodoc:
def self.each(&)
fibers.each { |fiber| yield fiber }
end

# Creates a new `Fiber` instance.
#
# When the fiber is executed, it runs *proc* in its context.
Expand Down
Loading