summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkleines Filmröllchen <filmroellchen@serenityos.org>2022-11-12 14:12:25 +0100
committerAndrew Kaster <andrewdkaster@gmail.com>2022-12-11 19:07:20 -0700
commit601ede331bc3388b760604ce0578fc3b351695a2 (patch)
tree621c9082bdf6bd4e7d39055ab886e30c44e4de36
parentfe004d3389b58ba53c339dc0527d92e165d559cc (diff)
downloadserenity-601ede331bc3388b760604ce0578fc3b351695a2.zip
LibThreading: Add a thread state enum
This will later be used by Thread to keep track of its state more simply and obviously.
-rw-r--r--Userland/Libraries/LibThreading/Thread.h53
1 files changed, 53 insertions, 0 deletions
diff --git a/Userland/Libraries/LibThreading/Thread.h b/Userland/Libraries/LibThreading/Thread.h
index 3a620f6969..bb06473a6e 100644
--- a/Userland/Libraries/LibThreading/Thread.h
+++ b/Userland/Libraries/LibThreading/Thread.h
@@ -18,6 +18,28 @@ namespace Threading {
AK_TYPEDEF_DISTINCT_ORDERED_ID(intptr_t, ThreadError);
+// States of userspace threads are simplified over actual kernel states (and possibly POSIX states).
+// There are only a couple of well-defined transitions between these states, and any attempt to call a function in a state where this is not allowed will crash the program.
+enum class ThreadState : u8 {
+ // Thread has been constructed but not started.
+ // Transitions to Running via start().
+ Startable,
+ // Thread has been started, might be running, and can be joined.
+ // Note that join() (valid to call in this state) only changes the thread state after the thread has exited, so it only ever transitions from Exited to Joined.
+ // Transitions to Detached via detach(), transitions to Exited when the thread finishes its action function.
+ Running,
+ // Thread has not been detached and exited, and has to still be joined.
+ // Transitions to Joined via join().
+ Exited,
+ // Thread has been started but also detached, meaning it cannot be joined.
+ // Transitions to DetachedExited when the thread finishes its action function.
+ Detached,
+ // Thread has exited but was detached, meaning it cannot be joined.
+ DetachedExited,
+ // Thread has exited and been joined.
+ Joined,
+};
+
class Thread final : public Core::Object {
C_OBJECT(Thread);
@@ -71,3 +93,34 @@ struct AK::Formatter<Threading::Thread> : AK::Formatter<FormatString> {
return Formatter<FormatString>::format(builder, "Thread \"{}\"({})"sv, thread.thread_name(), thread.tid());
}
};
+
+template<>
+struct AK::Formatter<Threading::ThreadState> : AK::Formatter<FormatString> {
+ ErrorOr<void> format(FormatBuilder& builder, Threading::ThreadState state)
+ {
+ String name = "";
+ switch (state) {
+ case Threading::ThreadState::Detached:
+ name = "Detached";
+ break;
+ case Threading::ThreadState::DetachedExited:
+ name = "DetachedExited";
+ break;
+ case Threading::ThreadState::Exited:
+ name = "Exited";
+ break;
+ case Threading::ThreadState::Joined:
+ name = "Joined";
+ break;
+ case Threading::ThreadState::Running:
+ name = "Running";
+ break;
+ case Threading::ThreadState::Startable:
+ name = "Startable";
+ break;
+ default:
+ VERIFY_NOT_REACHED();
+ }
+ return Formatter<FormatString>::format(builder, "{}"sv, name);
+ }
+};