1 | /*
|
---|
2 | * Copyright (c) 2019 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <cassert>
|
---|
30 | #include <cstdlib>
|
---|
31 | #include <exception>
|
---|
32 | #include <thread>
|
---|
33 | #include <utility>
|
---|
34 |
|
---|
35 | namespace std
|
---|
36 | {
|
---|
37 | thread::thread() noexcept
|
---|
38 | : id_{}
|
---|
39 | { /* DUMMY BODY */ }
|
---|
40 |
|
---|
41 | thread::~thread()
|
---|
42 | {
|
---|
43 | // TODO: investigate joinable() in detail
|
---|
44 | // + std::terminate behaves weirdly on HelenOS
|
---|
45 | if (joinable() && false)
|
---|
46 | std::terminate();
|
---|
47 |
|
---|
48 | // TODO: check for finished too?
|
---|
49 | // TODO: WAIT! if it's not detached, then
|
---|
50 | // we are joinable and std::terminate was called?
|
---|
51 | // TODO: review this entire thing
|
---|
52 | if (joinable_wrapper_ && !joinable_wrapper_->detached())
|
---|
53 | delete joinable_wrapper_;
|
---|
54 | }
|
---|
55 |
|
---|
56 | thread::thread(thread&& other) noexcept
|
---|
57 | : id_{other.id_}, joinable_wrapper_{other.joinable_wrapper_}
|
---|
58 | {
|
---|
59 | other.id_ = aux::thread_t{};
|
---|
60 | other.joinable_wrapper_ = nullptr;
|
---|
61 | }
|
---|
62 |
|
---|
63 | thread& thread::operator=(thread&& other) noexcept
|
---|
64 | {
|
---|
65 | if (joinable())
|
---|
66 | std::terminate();
|
---|
67 |
|
---|
68 | id_ = other.id_;
|
---|
69 | other.id_ = aux::thread_t{};
|
---|
70 |
|
---|
71 | joinable_wrapper_ = other.joinable_wrapper_;
|
---|
72 | other.joinable_wrapper_ = nullptr;
|
---|
73 |
|
---|
74 | return *this;
|
---|
75 | }
|
---|
76 |
|
---|
77 | void thread::swap(thread& other) noexcept
|
---|
78 | {
|
---|
79 | std::swap(id_, other.id_);
|
---|
80 | std::swap(joinable_wrapper_, other.joinable_wrapper_);
|
---|
81 | }
|
---|
82 |
|
---|
83 | bool thread::joinable() const noexcept
|
---|
84 | {
|
---|
85 | return id_ != aux::thread_t{};
|
---|
86 | }
|
---|
87 |
|
---|
88 | void thread::join()
|
---|
89 | {
|
---|
90 | if (joinable() && joinable_wrapper_)
|
---|
91 | joinable_wrapper_->join();
|
---|
92 | }
|
---|
93 |
|
---|
94 | void thread::detach()
|
---|
95 | {
|
---|
96 | id_ = aux::thread_t{};
|
---|
97 |
|
---|
98 | if (joinable_wrapper_)
|
---|
99 | {
|
---|
100 | joinable_wrapper_->detach();
|
---|
101 | joinable_wrapper_ = nullptr;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | thread::id thread::get_id() const noexcept
|
---|
106 | {
|
---|
107 | return id{id_};
|
---|
108 | }
|
---|
109 |
|
---|
110 | thread::native_handle_type thread::native_handle()
|
---|
111 | {
|
---|
112 | /**
|
---|
113 | * For fibrils the fid_t returned from fibril_create
|
---|
114 | * is a fibril_t* casted to fid_t, native handles
|
---|
115 | * are implementation defined so we just recast back.
|
---|
116 | */
|
---|
117 | return (native_handle_type)id_;
|
---|
118 | }
|
---|
119 |
|
---|
120 | unsigned thread::hardware_concurrency() noexcept
|
---|
121 | {
|
---|
122 | // TODO:
|
---|
123 | __unimplemented();
|
---|
124 | return 0;
|
---|
125 | }
|
---|
126 |
|
---|
127 | void swap(thread& x, thread& y) noexcept
|
---|
128 | {
|
---|
129 | x.swap(y);
|
---|
130 | }
|
---|
131 |
|
---|
132 | namespace this_thread
|
---|
133 | {
|
---|
134 | thread::id get_id() noexcept
|
---|
135 | {
|
---|
136 | return thread::id{aux::threading::thread::this_thread()};
|
---|
137 | }
|
---|
138 |
|
---|
139 | void yield() noexcept
|
---|
140 | {
|
---|
141 | aux::threading::thread::yield();
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | bool operator==(thread::id lhs, thread::id rhs) noexcept
|
---|
146 | {
|
---|
147 | return lhs.id_ == rhs.id_;
|
---|
148 | }
|
---|
149 |
|
---|
150 | bool operator!=(thread::id lhs, thread::id rhs) noexcept
|
---|
151 | {
|
---|
152 | return !(lhs == rhs);
|
---|
153 | }
|
---|
154 |
|
---|
155 | bool operator<(thread::id lhs, thread::id rhs) noexcept
|
---|
156 | {
|
---|
157 | return lhs.id_ < rhs.id_;
|
---|
158 | }
|
---|
159 |
|
---|
160 | bool operator<=(thread::id lhs, thread::id rhs) noexcept
|
---|
161 | {
|
---|
162 | return !(rhs < lhs);
|
---|
163 | }
|
---|
164 |
|
---|
165 | bool operator>(thread::id lhs, thread::id rhs) noexcept
|
---|
166 | {
|
---|
167 | return rhs < lhs;
|
---|
168 | }
|
---|
169 |
|
---|
170 | bool operator>=(thread::id lhs, thread::id rhs) noexcept
|
---|
171 | {
|
---|
172 | return !(lhs < rhs);
|
---|
173 | }
|
---|
174 | }
|
---|