summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibThreading/Thread.h
blob: ab9c18b365030a54fae288996fb02fcbf8eee2cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
 * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
 * Copyright (c) 2021, Spencer Dixon <spencercdixon@gmail.com>
 *
 * 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();
    void detach();

    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;
    bool m_detached { false };
};

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) };
}

}