The Pedigree Project 0.1
OwnedThread.h
1/*
2 * Copyright (c) 2026, Pedigree Developers
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted.
6 */
7
8#ifndef PEDIGREE_KERNEL_PROCESS_OWNEDTHREAD_H
9#define PEDIGREE_KERNEL_PROCESS_OWNEDTHREAD_H
10#include "pedigree/kernel/panic.h"
11#include "pedigree/kernel/process/Thread.h"
12
13#include <config.h>
14
20 public:
21 OwnedThread() : m_pThread(nullptr) {}
22
23 explicit OwnedThread(Thread* thread) : m_pThread(thread) {}
24
25 ~OwnedThread() {
26 stop();
27 }
28
29 void adopt(Thread* thread) {
30 if (m_pThread || !thread || thread->detached()) {
31 panic("OwnedThread cannot adopt this worker.");
32 }
33 m_pThread = thread;
34 }
35
36 void stop() {
37 if (!m_pThread) {
38 return;
39 }
40
41 Thread* thread = m_pThread;
42 if (thread->getStatus() != Thread::AwaitingJoin) {
44 }
45 join();
46 }
47
49 void join() {
50 if (!m_pThread) {
51 return;
52 }
53
54 Thread* thread = m_pThread;
55 if (!thread->joinForCompletion()) {
56 panic("OwnedThread could not join its worker.");
57 }
58 m_pThread = nullptr;
59 }
60
61 Thread* get() const {
62 return m_pThread;
63 }
64
65 Thread* operator->() const {
66 return m_pThread;
67 }
68
69 explicit operator bool() const {
70 return m_pThread != nullptr;
71 }
72
73 private:
74 OwnedThread(const OwnedThread&) = delete;
75 OwnedThread& operator=(const OwnedThread&) = delete;
76
77 Thread* m_pThread;
78};
79
80#endif
void join()
Definition OwnedThread.h:49
void setUnwindState(UnwindType ut)
Definition Thread.cc:3628
@ TerminateThread
Exit only this thread during Process exit.
Definition Thread.h:515
bool joinForCompletion()
Definition Thread.cc:2771
Status getStatus() const
Definition Thread.h:431
bool detached() const
Definition Thread.h:906
void EXPORTED_PUBLIC panic(const char *msg) NORETURN
Definition panic.cc:117