From d3a22aa7b3d9afd18fd860afc4f01257e1d93c32 Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Thu, 29 Feb 2024 02:18:16 +0200 Subject: [PATCH] Add std::thread implementation --- include_std/thread | 81 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 include_std/thread diff --git a/include_std/thread b/include_std/thread new file mode 100644 index 0000000..c10cfec --- /dev/null +++ b/include_std/thread @@ -0,0 +1,81 @@ +/* + This file is part of Fennix Kernel. + + Fennix Kernel is free software: you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of + the License, or (at your option) any later version. + + Fennix Kernel is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Fennix Kernel. If not, see . +*/ + +#pragma once + +#include +#include +#include +#include + +extern Tasking::Task *TaskManager; + +namespace std +{ + class thread + { + private: + Tasking::TCB *Task = nullptr; + + public: + thread() = default; + thread(const thread &) = delete; + thread(thread &&) = delete; + thread &operator=(const thread &) = delete; + thread &operator=(thread &&) = delete; + + template + explicit thread(Function &&f, Args &&...args) + { + Task = TaskManager->CreateThread(thisProcess, (Tasking::IP)f); + Task->SYSV_ABI_Call(std::forward(args)...); + } + + ~thread() + { + if (Task) + Task->SendSignal(SIGKILL); + } + + void join() + { + TaskManager->WaitForThread(Task); + } + + bool joinable() const + { + return Task != nullptr; + } + + void detach() + { + Task = nullptr; + } + + void swap(thread &other) noexcept + { + Tasking::TCB *temp = Task; + Task = other.Task; + other.Task = temp; + } + + Tasking::TCB *native_handle() + { + return Task; + } + }; +}