Add a handy consume() method to LinkedList
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2022-12-19 12:35:08 +01:00
parent 92a7004c2f
commit 5b72144fac
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 20 additions and 12 deletions

View File

@ -46,18 +46,7 @@ void reap_thread()
auto dying_threads = Scheduler::check_for_dying_threads(); auto dying_threads = Scheduler::check_for_dying_threads();
CPU::enable_interrupts(); CPU::enable_interrupts();
if (dying_threads.count()) dying_threads.consume([](Thread* thread) { Scheduler::reap_thread(thread); });
{
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;
}
}
kernel_sleep(250); kernel_sleep(250);
} }

View File

@ -155,26 +155,45 @@ template <typename T> class DoublyLinkedList
return nonnull_or_error((T*)extract_node(item)->get_last()); 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 <typename Callback> void for_each(Callback callback) template <typename Callback> void for_each(Callback callback)
{ {
for (Node* node = m_start_node; node; node = node->get_next()) { callback((T*)node); } 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 <typename Callback> void for_each_reversed(Callback callback) template <typename Callback> void for_each_reversed(Callback callback)
{ {
for (Node* node = m_end_node; node; node = node->get_last()) { callback((T*)node); } 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 <typename Callback> void for_each_after(T* start, Callback callback) template <typename Callback> 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); } 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 <typename Callback> void for_each_before(T* end, Callback callback) template <typename Callback> 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); } 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 <typename Callback> 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() usize count()
{ {
return m_count; return m_count;