summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibThreading/Thread.h
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-05-22 18:47:42 +0200
committerAndreas Kling <kling@serenityos.org>2021-05-22 18:54:22 +0200
commitb5d73c834ff0f4cb1ff6112f055bee4cd02b21ec (patch)
tree82aae5e6a9d2d798b9810770b15f2a9d3cb04161 /Userland/Libraries/LibThreading/Thread.h
parent5729b4e9a5658d8728239e3141ff270004af5d6c (diff)
downloadserenity-b5d73c834ff0f4cb1ff6112f055bee4cd02b21ec.zip
Userland: Rename LibThread => LibThreading
Also rename the "LibThread" namespace to "Threading"
Diffstat (limited to 'Userland/Libraries/LibThreading/Thread.h')
-rw-r--r--Userland/Libraries/LibThreading/Thread.h57
1 files changed, 57 insertions, 0 deletions
diff --git a/Userland/Libraries/LibThreading/Thread.h b/Userland/Libraries/LibThreading/Thread.h
new file mode 100644
index 0000000000..d04fba6a54
--- /dev/null
+++ b/Userland/Libraries/LibThreading/Thread.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/DistinctNumeric.h>
+#include <AK/Function.h>
+#include <AK/Result.h>
+#include <AK/String.h>
+#include <LibCore/Object.h>
+#include <pthread.h>
+
+namespace Threading {
+
+TYPEDEF_DISTINCT_ORDERED_ID(intptr_t, ThreadError);
+
+class Thread final : public Core::Object {
+ C_OBJECT(Thread);
+
+public:
+ virtual ~Thread();
+
+ void start();
+
+ template<typename T = void>
+ Result<T, ThreadError> join();
+
+ String thread_name() const { return m_thread_name; }
+ pthread_t tid() const { return m_tid; }
+
+private:
+ explicit Thread(Function<intptr_t()> action, StringView thread_name = nullptr);
+ Function<intptr_t()> m_action;
+ pthread_t m_tid { 0 };
+ String m_thread_name;
+};
+
+template<typename T>
+Result<T, ThreadError> Thread::join()
+{
+ void* thread_return = nullptr;
+ int rc = pthread_join(m_tid, &thread_return);
+ if (rc != 0) {
+ return ThreadError { rc };
+ }
+
+ m_tid = 0;
+ if constexpr (IsVoid<T>)
+ return {};
+ else
+ return { static_cast<T>(thread_return) };
+}
+
+}