source: mainline/uspace/lib/cpp/src/thread.cpp@ 8624d1f

Last change on this file since 8624d1f was b57ba05, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Update headers in C++ files

  • Property mode set to 100644
File size: 3.3 KB
Line 
1/*
2 * SPDX-FileCopyrightText: 2019 Jaroslav Jindrak
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <cassert>
8#include <cstdlib>
9#include <exception>
10#include <thread>
11#include <utility>
12
13namespace std
14{
15 thread::thread() noexcept
16 : id_{}
17 { /* DUMMY BODY */ }
18
19 thread::~thread()
20 {
21 // TODO: investigate joinable() in detail
22 // + std::terminate behaves weirdly on HelenOS
23 if (joinable() && false)
24 std::terminate();
25
26 // TODO: check for finished too?
27 // TODO: WAIT! if it's not detached, then
28 // we are joinable and std::terminate was called?
29 // TODO: review this entire thing
30 if (joinable_wrapper_ && !joinable_wrapper_->detached())
31 delete joinable_wrapper_;
32 }
33
34 thread::thread(thread&& other) noexcept
35 : id_{other.id_}, joinable_wrapper_{other.joinable_wrapper_}
36 {
37 other.id_ = aux::thread_t{};
38 other.joinable_wrapper_ = nullptr;
39 }
40
41 thread& thread::operator=(thread&& other) noexcept
42 {
43 if (joinable())
44 std::terminate();
45
46 id_ = other.id_;
47 other.id_ = aux::thread_t{};
48
49 joinable_wrapper_ = other.joinable_wrapper_;
50 other.joinable_wrapper_ = nullptr;
51
52 return *this;
53 }
54
55 void thread::swap(thread& other) noexcept
56 {
57 std::swap(id_, other.id_);
58 std::swap(joinable_wrapper_, other.joinable_wrapper_);
59 }
60
61 bool thread::joinable() const noexcept
62 {
63 return id_ != aux::thread_t{};
64 }
65
66 void thread::join()
67 {
68 if (joinable() && joinable_wrapper_)
69 joinable_wrapper_->join();
70 }
71
72 void thread::detach()
73 {
74 id_ = aux::thread_t{};
75
76 if (joinable_wrapper_)
77 {
78 joinable_wrapper_->detach();
79 joinable_wrapper_ = nullptr;
80 }
81 }
82
83 thread::id thread::get_id() const noexcept
84 {
85 return id{id_};
86 }
87
88 thread::native_handle_type thread::native_handle()
89 {
90 /**
91 * For fibrils the fid_t returned from fibril_create
92 * is a fibril_t* casted to fid_t, native handles
93 * are implementation defined so we just recast back.
94 */
95 return (native_handle_type)id_;
96 }
97
98 unsigned thread::hardware_concurrency() noexcept
99 {
100 // TODO:
101 __unimplemented();
102 return 0;
103 }
104
105 void swap(thread& x, thread& y) noexcept
106 {
107 x.swap(y);
108 }
109
110 namespace this_thread
111 {
112 thread::id get_id() noexcept
113 {
114 return thread::id{aux::threading::thread::this_thread()};
115 }
116
117 void yield() noexcept
118 {
119 aux::threading::thread::yield();
120 }
121 }
122
123 bool operator==(thread::id lhs, thread::id rhs) noexcept
124 {
125 return lhs.id_ == rhs.id_;
126 }
127
128 bool operator!=(thread::id lhs, thread::id rhs) noexcept
129 {
130 return !(lhs == rhs);
131 }
132
133 bool operator<(thread::id lhs, thread::id rhs) noexcept
134 {
135 return lhs.id_ < rhs.id_;
136 }
137
138 bool operator<=(thread::id lhs, thread::id rhs) noexcept
139 {
140 return !(rhs < lhs);
141 }
142
143 bool operator>(thread::id lhs, thread::id rhs) noexcept
144 {
145 return rhs < lhs;
146 }
147
148 bool operator>=(thread::id lhs, thread::id rhs) noexcept
149 {
150 return !(lhs < rhs);
151 }
152}
Note: See TracBrowser for help on using the repository browser.