From d21008edfa8016597c884bb00fe3254e636f17d2 Mon Sep 17 00:00:00 2001 From: Julien Portalier Date: Wed, 25 Dec 2024 23:19:02 +0100 Subject: [PATCH] Add `Thread::LinkedList#each` to safely iterate lists (#15300) Sometimes we'd like to be able to safely iterate the lists. Also adds `Thread.each(&)` and `Fiber.each(&)` as convenience accessors. This will be used in RFC 2 to safely iterate execution contexts for example. Also adds `Thread.each(&)` and `Fiber.each(&)`. --- src/crystal/system/thread.cr | 4 ++++ src/crystal/system/thread_linked_list.cr | 7 +++++++ src/fiber.cr | 5 +++++ 3 files changed, 16 insertions(+) diff --git a/src/crystal/system/thread.cr b/src/crystal/system/thread.cr index fec4a989f10a..92136d1f3989 100644 --- a/src/crystal/system/thread.cr +++ b/src/crystal/system/thread.cr @@ -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 threads.@mutex.lock end diff --git a/src/crystal/system/thread_linked_list.cr b/src/crystal/system/thread_linked_list.cr index c18c62afbde8..f43dd04fedc2 100644 --- a/src/crystal/system/thread_linked_list.cr +++ b/src/crystal/system/thread_linked_list.cr @@ -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 diff --git a/src/fiber.cr b/src/fiber.cr index 2b596a16017c..55745666c66d 100644 --- a/src/fiber.cr +++ b/src/fiber.cr @@ -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.