1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:27:35 +00:00

AK+Kernel+LibELF: Remove the need for IteratorDecision::Continue

By constraining two implementations, the compiler will select the best
fitting one. All this will require is duplicating the implementation and
simplifying for the `void` case.

This constraining also informs both the caller and compiler by passing
the callback parameter types as part of the constraint
(e.g.: `IterationFunction<int>`).

Some `for_each` functions in LibELF only take functions which return
`void`. This is a minimal correctness check, as it removes one way for a
function to incompletely do something.

There seems to be a possible idiom where inside a lambda, a `return;` is
the same as `continue;` in a for-loop.
This commit is contained in:
Nicholas Baron 2021-05-16 02:36:52 -07:00 committed by GitHub
parent bbaa463032
commit aa4d41fe2c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 311 additions and 127 deletions

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/Concepts.h>
#include <AK/EnumBits.h>
#include <AK/Function.h>
#include <AK/HashMap.h>
@ -1044,9 +1045,14 @@ public:
RefPtr<Thread> clone(Process&);
template<typename Callback>
template<IteratorFunction<Thread&> Callback>
static IterationDecision for_each_in_state(State, Callback);
template<typename Callback>
template<IteratorFunction<Thread&> Callback>
static IterationDecision for_each(Callback);
template<VoidFunction<Thread&> Callback>
static IterationDecision for_each_in_state(State, Callback);
template<VoidFunction<Thread&> Callback>
static IterationDecision for_each(Callback);
static constexpr u32 default_kernel_stack_size = 65536;
@ -1260,7 +1266,7 @@ private:
AK_ENUM_BITWISE_OPERATORS(Thread::FileBlocker::BlockFlags);
template<typename Callback>
template<IteratorFunction<Thread&> Callback>
inline IterationDecision Thread::for_each(Callback callback)
{
ScopedSpinLock lock(g_tid_map_lock);
@ -1272,7 +1278,7 @@ inline IterationDecision Thread::for_each(Callback callback)
return IterationDecision::Continue;
}
template<typename Callback>
template<IteratorFunction<Thread&> Callback>
inline IterationDecision Thread::for_each_in_state(State state, Callback callback)
{
ScopedSpinLock lock(g_tid_map_lock);
@ -1287,6 +1293,24 @@ inline IterationDecision Thread::for_each_in_state(State state, Callback callbac
return IterationDecision::Continue;
}
template<VoidFunction<Thread&> Callback>
inline IterationDecision Thread::for_each(Callback callback)
{
ScopedSpinLock lock(g_tid_map_lock);
for (auto& it : *g_tid_map)
callback(*it.value);
return IterationDecision::Continue;
}
template<VoidFunction<Thread&> Callback>
inline IterationDecision Thread::for_each_in_state(State state, Callback callback)
{
return for_each_in_state(state, [&](auto& thread) {
callback(thread);
return IterationDecision::Continue;
});
}
}
template<>