diff --git a/kernel/src/thread/Scheduler.cpp b/kernel/src/thread/Scheduler.cpp index 31e556ed..d961edf0 100644 --- a/kernel/src/thread/Scheduler.cpp +++ b/kernel/src/thread/Scheduler.cpp @@ -182,25 +182,14 @@ namespace Scheduler { DoublyLinkedList result; - Thread* thread_to_remove = nullptr; - - g_threads.for_each([&](Thread* thread) { - if (thread_to_remove) + g_threads.delayed_for_each([&](Thread* thread) { + if (thread->state == ThreadState::Dying) { - g_threads.remove(thread_to_remove); - result.append(thread_to_remove); + g_threads.remove(thread); + result.append(thread); } - - if (thread->state == ThreadState::Dying) { thread_to_remove = thread; } - else { thread_to_remove = nullptr; } }); - if (thread_to_remove) - { - g_threads.remove(thread_to_remove); - result.append(thread_to_remove); - } - return result; } } diff --git a/luna/include/luna/LinkedList.h b/luna/include/luna/LinkedList.h index 65a55b91..0ea2ecc3 100644 --- a/luna/include/luna/LinkedList.h +++ b/luna/include/luna/LinkedList.h @@ -161,6 +161,18 @@ template class DoublyLinkedList for (Node* node = m_start_node; node; node = node->get_next()) { callback((T*)node); } } + // Iterates over the elements of the DoublyLinkedList from start to end, calling callback for every element. This + // for_each is implemented in such a way that elements can be removed while iterating over it. + template void delayed_for_each(Callback callback) + { + for (Node* node = m_start_node; node;) + { + T* current = (T*)node; + node = node->get_next(); + callback(current); + } + } + // Iterates over the elements of the DoublyLinkedList from end to start, calling callback for every element. template void for_each_reversed(Callback callback) {