/* * Copyright (c) 2019-2020, Sergey Bugaev * Copyright (c) 2021, Spencer Dixon * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include namespace Threading { TYPEDEF_DISTINCT_ORDERED_ID(intptr_t, ThreadError); class Thread final : public Core::Object { C_OBJECT(Thread); public: virtual ~Thread(); void start(); void detach(); template Result join(); String thread_name() const { return m_thread_name; } pthread_t tid() const { return m_tid; } private: explicit Thread(Function action, StringView thread_name = nullptr); Function m_action; pthread_t m_tid { 0 }; String m_thread_name; bool m_detached { false }; }; template Result 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) return {}; else return { static_cast(thread_return) }; } }