Thread: Normalize all for_each constructs to use IterationDecision

This way a caller can abort the for_each early if they want.
This commit is contained in:
Robin Burchell 2019-07-19 12:16:00 +02:00 committed by Andreas Kling
parent a5d80f7e3b
commit e74dce65e6
Notes: sideshowbarker 2024-07-19 13:09:03 +09:00
3 changed files with 39 additions and 24 deletions

View file

@ -291,14 +291,14 @@ bool Scheduler::pick_next()
// Dispatch any pending signals.
// FIXME: Do we really need this to be a separate pass over the process list?
Thread::for_each_living([](Thread& thread) {
Thread::for_each_living([](Thread& thread) -> IterationDecision {
if (!thread.has_unmasked_pending_signals())
return true;
return IterationDecision::Continue;
// FIXME: It would be nice if the Scheduler didn't have to worry about who is "current"
// For now, avoid dispatching signals to "current" and do it in a scheduling pass
// while some other process is interrupted. Otherwise a mess will be made.
if (&thread == current)
return true;
return IterationDecision::Continue;
// We know how to interrupt blocked processes, but if they are just executing
// at some random point in the kernel, let them continue. They'll be in userspace
// sooner or later and we can deliver the signal then.
@ -306,17 +306,17 @@ bool Scheduler::pick_next()
// signal and dispatch it then and there? Would that be doable without the
// syscall effectively being "interrupted" despite having completed?
if (thread.in_kernel() && !thread.is_blocked() && !thread.is_stopped())
return true;
return IterationDecision::Continue;
// NOTE: dispatch_one_pending_signal() may unblock the process.
bool was_blocked = thread.is_blocked();
if (thread.dispatch_one_pending_signal() == ShouldUnblockThread::No)
return true;
return IterationDecision::Continue;
if (was_blocked) {
dbgprintf("Unblock %s(%u) due to signal\n", thread.process().name().characters(), thread.pid());
thread.m_was_interrupted_while_blocked = true;
thread.unblock();
}
return true;
return IterationDecision::Continue;
});
#ifdef SCHEDULER_RUNNABLE_DEBUG

View file

@ -178,6 +178,7 @@ void Thread::finalize_dying_threads()
InterruptDisabler disabler;
for_each_in_state(Thread::State::Dying, [&](Thread& thread) {
dying_threads.append(&thread);
return IterationDecision::Continue;
});
}
for (auto* thread : dying_threads)

View file

@ -262,15 +262,15 @@ public:
void set_thread_list(InlineLinkedList<Thread>*);
template<typename Callback>
static void for_each_in_state(State, Callback);
static IterationDecision for_each_in_state(State, Callback);
template<typename Callback>
static void for_each_living(Callback);
static IterationDecision for_each_living(Callback);
template<typename Callback>
static void for_each_runnable(Callback);
static IterationDecision for_each_runnable(Callback);
template<typename Callback>
static void for_each_nonrunnable(Callback);
static IterationDecision for_each_nonrunnable(Callback);
template<typename Callback>
static void for_each(Callback);
static IterationDecision for_each(Callback);
static bool is_runnable_state(Thread::State state)
{
@ -313,63 +313,77 @@ private:
HashTable<Thread*>& thread_table();
template<typename Callback>
inline void Thread::for_each_in_state(State state, Callback callback)
inline IterationDecision Thread::for_each_in_state(State state, Callback callback)
{
ASSERT_INTERRUPTS_DISABLED();
for (auto* thread = thread_list_for_state(state)->head(); thread;) {
auto* next_thread = thread->next();
if (thread->state() == state)
callback(*thread);
if (thread->state() == state) {
if (callback(*thread) == IterationDecision::Break)
return IterationDecision::Break;
}
thread = next_thread;
}
return IterationDecision::Continue;
}
template<typename Callback>
inline void Thread::for_each_living(Callback callback)
inline IterationDecision Thread::for_each_living(Callback callback)
{
ASSERT_INTERRUPTS_DISABLED();
for (auto* thread = g_runnable_threads->head(); thread;) {
auto* next_thread = thread->next();
if (thread->state() != Thread::State::Dead && thread->state() != Thread::State::Dying)
callback(*thread);
if (callback(*thread) == IterationDecision::Break)
return IterationDecision::Break;
thread = next_thread;
}
for (auto* thread = g_nonrunnable_threads->head(); thread;) {
auto* next_thread = thread->next();
if (thread->state() != Thread::State::Dead && thread->state() != Thread::State::Dying)
callback(*thread);
if (callback(*thread) == IterationDecision::Break)
return IterationDecision::Break;
thread = next_thread;
}
return IterationDecision::Continue;
}
template<typename Callback>
inline void Thread::for_each(Callback callback)
inline IterationDecision Thread::for_each(Callback callback)
{
ASSERT_INTERRUPTS_DISABLED();
for_each_runnable(callback);
for_each_nonrunnable(callback);
auto ret = for_each_runnable(callback);
if (ret == IterationDecision::Break)
return ret;
return for_each_nonrunnable(callback);
}
template<typename Callback>
inline void Thread::for_each_runnable(Callback callback)
inline IterationDecision Thread::for_each_runnable(Callback callback)
{
ASSERT_INTERRUPTS_DISABLED();
for (auto* thread = g_runnable_threads->head(); thread;) {
auto* next_thread = thread->next();
if (callback(*thread) == IterationDecision::Break)
return;
return IterationDecision::Break;
thread = next_thread;
}
return IterationDecision::Continue;
}
template<typename Callback>
inline void Thread::for_each_nonrunnable(Callback callback)
inline IterationDecision Thread::for_each_nonrunnable(Callback callback)
{
ASSERT_INTERRUPTS_DISABLED();
for (auto* thread = g_nonrunnable_threads->head(); thread;) {
auto* next_thread = thread->next();
if (callback(*thread) == IterationDecision::Break)
return;
return IterationDecision::Break;
thread = next_thread;
}
return IterationDecision::Continue;
}