From 5b72144fac518345331380684f9380a007078f4b Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 19 Dec 2022 12:35:08 +0100 Subject: [PATCH] Add a handy consume() method to LinkedList --- kernel/src/main.cpp | 13 +------------ luna/include/luna/LinkedList.h | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index dc8a367b..a8e8622d 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -46,18 +46,7 @@ void reap_thread() auto dying_threads = Scheduler::check_for_dying_threads(); CPU::enable_interrupts(); - if (dying_threads.count()) - { - Thread* thread_to_reap = dying_threads.expect_first(); - Thread* next_thread = thread_to_reap; - - while (thread_to_reap) - { - next_thread = dying_threads.next(thread_to_reap).value_or(nullptr); - Scheduler::reap_thread(thread_to_reap); - thread_to_reap = next_thread; - } - } + dying_threads.consume([](Thread* thread) { Scheduler::reap_thread(thread); }); kernel_sleep(250); } diff --git a/luna/include/luna/LinkedList.h b/luna/include/luna/LinkedList.h index 50615134..65a55b91 100644 --- a/luna/include/luna/LinkedList.h +++ b/luna/include/luna/LinkedList.h @@ -155,26 +155,45 @@ template class DoublyLinkedList return nonnull_or_error((T*)extract_node(item)->get_last()); } + // Iterates over the elements of the DoublyLinkedList from start to end, calling callback for every element. template void for_each(Callback callback) { for (Node* node = m_start_node; node; node = node->get_next()) { callback((T*)node); } } + // Iterates over the elements of the DoublyLinkedList from end to start, calling callback for every element. template void for_each_reversed(Callback callback) { for (Node* node = m_end_node; node; node = node->get_last()) { callback((T*)node); } } + // Iterates over the elements of the DoublyLinkedList from the element after 'start' to end, calling callback for + // every element. template void for_each_after(T* start, Callback callback) { for (Node* node = extract_node(start)->m_next_node; node; node = node->get_next()) { callback((T*)node); } } + // Iterates over the elements of the DoublyLinkedList from the element before 'end' to start, calling callback for + // every element. template void for_each_before(T* end, Callback callback) { for (Node* node = extract_node(end)->m_last_node; node; node = node->get_last()) { callback((T*)node); } } + // Iterates over the elements of the DoublyLinkedList from start to end, removing each element before passing it to + // the callback. + template void consume(Callback callback) + { + for (Node* node = m_start_node; node;) + { + T* current = (T*)node; + node = node->get_next(); + remove(current); + callback(current); + } + } + usize count() { return m_count;