[ad403590] | 1 | /*
|
---|
[b57a3ee] | 2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
[ad403590] | 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 |
|
---|
[b57a3ee] | 29 | #ifndef LIBCPP_BITS_THREAD
|
---|
| 30 | #define LIBCPP_BITS_THREAD
|
---|
[ad403590] | 31 |
|
---|
[b57a3ee] | 32 | #include <__bits/thread/threading.hpp>
|
---|
| 33 | #include <chrono>
|
---|
[ad403590] | 34 | #include <ostream>
|
---|
| 35 |
|
---|
| 36 | namespace std
|
---|
| 37 | {
|
---|
[48d9187] | 38 | namespace aux
|
---|
| 39 | {
|
---|
| 40 | template<class Callable>
|
---|
| 41 | int thread_main(void*);
|
---|
| 42 |
|
---|
| 43 | /**
|
---|
| 44 | * Fibrils in HelenOS are not joinable. They were
|
---|
| 45 | * in the past, but that functionality was removed from them
|
---|
| 46 | * so we created a workaround using a conditional variable that
|
---|
| 47 | * comprises the following two wrapper classes.
|
---|
| 48 | */
|
---|
| 49 | class joinable_wrapper
|
---|
| 50 | {
|
---|
| 51 | public:
|
---|
| 52 | joinable_wrapper()
|
---|
| 53 | : join_mtx_{}, join_cv_{},
|
---|
| 54 | finished_{false}, detached_{false}
|
---|
| 55 | {
|
---|
[9283830] | 56 | aux::threading::mutex::init(join_mtx_);
|
---|
| 57 | aux::threading::condvar::init(join_cv_);
|
---|
[48d9187] | 58 | }
|
---|
| 59 |
|
---|
| 60 | void join()
|
---|
| 61 | {
|
---|
[9283830] | 62 | aux::threading::mutex::lock(join_mtx_);
|
---|
[48d9187] | 63 | while (!finished_)
|
---|
[9283830] | 64 | aux::threading::condvar::wait(join_cv_, join_mtx_);
|
---|
| 65 | aux::threading::mutex::unlock(join_mtx_);
|
---|
[48d9187] | 66 | }
|
---|
| 67 |
|
---|
| 68 | bool finished() const
|
---|
| 69 | {
|
---|
| 70 | return finished_;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | void detach()
|
---|
| 74 | {
|
---|
| 75 | detached_ = true;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | bool detached() const
|
---|
| 79 | {
|
---|
| 80 | return detached_;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | protected:
|
---|
[9283830] | 84 | aux::mutex_t join_mtx_;
|
---|
| 85 | aux::condvar_t join_cv_;
|
---|
[48d9187] | 86 | bool finished_;
|
---|
| 87 | bool detached_;
|
---|
| 88 | };
|
---|
| 89 |
|
---|
| 90 | template<class Callable>
|
---|
| 91 | class callable_wrapper: public joinable_wrapper
|
---|
| 92 | {
|
---|
| 93 | public:
|
---|
| 94 | callable_wrapper(Callable&& clbl)
|
---|
| 95 | : joinable_wrapper{}, callable_{forward<Callable>(clbl)}
|
---|
| 96 | { /* DUMMY BODY */ }
|
---|
| 97 |
|
---|
| 98 | void operator()()
|
---|
| 99 | {
|
---|
| 100 | callable_();
|
---|
| 101 |
|
---|
[9283830] | 102 | aux::threading::mutex::lock(join_mtx_);
|
---|
[48d9187] | 103 | finished_ = true;
|
---|
[9283830] | 104 | aux::threading::mutex::unlock(join_mtx_);
|
---|
[48d9187] | 105 |
|
---|
[9283830] | 106 | aux::threading::condvar::broadcast(join_cv_);
|
---|
[48d9187] | 107 | }
|
---|
| 108 |
|
---|
| 109 | private:
|
---|
| 110 | Callable callable_;
|
---|
| 111 | };
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[ad403590] | 114 | /**
|
---|
| 115 | * 30.3.1, class thread:
|
---|
| 116 | */
|
---|
| 117 |
|
---|
| 118 | class thread
|
---|
| 119 | {
|
---|
| 120 | public:
|
---|
| 121 | class id;
|
---|
| 122 |
|
---|
[9283830] | 123 | using native_handle_type = aux::thread_t*;
|
---|
[ad403590] | 124 |
|
---|
| 125 | /**
|
---|
| 126 | * 30.3.1.2, thread constructors:
|
---|
| 127 | * 30.3.1.3, thread destructor:
|
---|
| 128 | * 30.3.1.4, thread assignment:
|
---|
| 129 | */
|
---|
| 130 |
|
---|
| 131 | thread() noexcept;
|
---|
| 132 |
|
---|
| 133 | ~thread();
|
---|
| 134 |
|
---|
| 135 | // TODO: check the remark in the standard
|
---|
| 136 | template<class F, class... Args>
|
---|
| 137 | explicit thread(F&& f, Args&&... args)
|
---|
[48d9187] | 138 | : id_{}
|
---|
[ad403590] | 139 | {
|
---|
[4e484b5] | 140 | auto callable = [=](){
|
---|
[48d9187] | 141 | return f(forward<Args>(args)...);
|
---|
| 142 | };
|
---|
| 143 |
|
---|
| 144 | auto callable_wrapper = new aux::callable_wrapper<decltype(callable)>{move(callable)};
|
---|
| 145 | joinable_wrapper_ = static_cast<aux::joinable_wrapper*>(callable_wrapper);
|
---|
| 146 |
|
---|
[9283830] | 147 | id_ = aux::threading::thread::create(
|
---|
| 148 | aux::thread_main<decltype(callable_wrapper)>,
|
---|
| 149 | *callable_wrapper
|
---|
[48d9187] | 150 | );
|
---|
[9283830] | 151 |
|
---|
| 152 | aux::threading::thread::start(id_);
|
---|
| 153 | // TODO: fibrils are weird here, 2 returns with same thread ids
|
---|
[ad403590] | 154 | }
|
---|
| 155 |
|
---|
| 156 | thread(const thread&) = delete;
|
---|
| 157 | thread& operator=(const thread&) = delete;
|
---|
| 158 |
|
---|
| 159 | thread(thread&& other) noexcept;
|
---|
| 160 | thread& operator=(thread&& other) noexcept;
|
---|
| 161 |
|
---|
| 162 | /**
|
---|
| 163 | * 30.3.1.5, thread members:
|
---|
| 164 | */
|
---|
| 165 |
|
---|
| 166 | void swap(thread& other) noexcept;
|
---|
| 167 |
|
---|
| 168 | bool joinable() const noexcept;
|
---|
| 169 |
|
---|
| 170 | void join();
|
---|
| 171 |
|
---|
| 172 | void detach();
|
---|
| 173 |
|
---|
| 174 | id get_id() const noexcept;
|
---|
| 175 |
|
---|
| 176 | native_handle_type native_handle();
|
---|
| 177 |
|
---|
| 178 | static unsigned hardware_concurrency() noexcept;
|
---|
| 179 |
|
---|
| 180 | private:
|
---|
[9283830] | 181 | aux::thread_t id_;
|
---|
[48d9187] | 182 | aux::joinable_wrapper* joinable_wrapper_{nullptr};
|
---|
| 183 |
|
---|
| 184 | template<class Callable>
|
---|
| 185 | friend int aux::thread_main(void*);
|
---|
[ad403590] | 186 | };
|
---|
| 187 |
|
---|
[48d9187] | 188 | namespace aux
|
---|
| 189 | {
|
---|
| 190 | template<class CallablePtr>
|
---|
| 191 | int thread_main(void* clbl)
|
---|
| 192 | {
|
---|
| 193 | if (!clbl)
|
---|
| 194 | return 1;
|
---|
| 195 |
|
---|
| 196 | auto callable = static_cast<CallablePtr>(clbl);
|
---|
| 197 | (*callable)();
|
---|
| 198 |
|
---|
[063e0626] | 199 | if (callable->detached())
|
---|
[48d9187] | 200 | delete callable;
|
---|
| 201 |
|
---|
| 202 | return 0;
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[ad403590] | 206 | void swap(thread& x, thread& y) noexcept;
|
---|
| 207 |
|
---|
| 208 | /**
|
---|
| 209 | * 30.3.2, namespace this_thread:
|
---|
| 210 | */
|
---|
| 211 |
|
---|
| 212 | namespace this_thread
|
---|
| 213 | {
|
---|
| 214 | thread::id get_id() noexcept;
|
---|
| 215 |
|
---|
| 216 | void yield() noexcept;
|
---|
| 217 |
|
---|
| 218 | template<class Clock, class Duration>
|
---|
| 219 | void sleep_until(const chrono::time_point<Clock, Duration>& abs_time)
|
---|
| 220 | {
|
---|
| 221 | auto now = Clock::now();
|
---|
| 222 |
|
---|
[9283830] | 223 | auto time = aux::threading::time::convert(abs_time - now);
|
---|
| 224 | aux::threading::time::sleep(time);
|
---|
[ad403590] | 225 | }
|
---|
| 226 |
|
---|
| 227 | template<class Rep, class Period>
|
---|
| 228 | void sleep_for(const chrono::duration<Rep, Period>& rel_time)
|
---|
| 229 | {
|
---|
| 230 | if (rel_time <= chrono::duration<Rep, Period>::zero())
|
---|
| 231 | return;
|
---|
| 232 |
|
---|
| 233 | // TODO: timeouts?
|
---|
[9283830] | 234 | auto time = aux::threading::time::convert(rel_time);
|
---|
| 235 | aux::threading::time::sleep(time);
|
---|
[ad403590] | 236 | }
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | template<class T>
|
---|
| 240 | struct hash;
|
---|
| 241 |
|
---|
| 242 | class thread::id
|
---|
| 243 | {
|
---|
| 244 | public:
|
---|
[5e5498e] | 245 | constexpr id() noexcept
|
---|
[ad403590] | 246 | : id_{}
|
---|
| 247 | { /* DUMMY BODY */ }
|
---|
| 248 |
|
---|
| 249 | private:
|
---|
[de53138] | 250 | aux::thread_t id_;
|
---|
| 251 |
|
---|
| 252 | id(aux::thread_t id)
|
---|
[ad403590] | 253 | : id_{id}
|
---|
| 254 | { /* DUMMY BODY */ }
|
---|
| 255 |
|
---|
| 256 | friend class thread;
|
---|
| 257 |
|
---|
| 258 | friend bool operator==(thread::id, thread::id) noexcept;
|
---|
| 259 | friend bool operator!=(thread::id, thread::id) noexcept;
|
---|
| 260 | friend bool operator<(thread::id, thread::id) noexcept;
|
---|
| 261 | friend bool operator<=(thread::id, thread::id) noexcept;
|
---|
| 262 | friend bool operator>(thread::id, thread::id) noexcept;
|
---|
| 263 | friend bool operator>=(thread::id, thread::id) noexcept;
|
---|
| 264 |
|
---|
| 265 | template<class Char, class Traits>
|
---|
| 266 | friend basic_ostream<Char, Traits>& operator<<(
|
---|
| 267 | basic_ostream<Char, Traits>&, thread::id);
|
---|
| 268 |
|
---|
| 269 | friend struct hash<id>;
|
---|
| 270 |
|
---|
| 271 | friend id this_thread::get_id() noexcept;
|
---|
| 272 | };
|
---|
| 273 |
|
---|
| 274 | bool operator==(thread::id lhs, thread::id rhs) noexcept;
|
---|
| 275 | bool operator!=(thread::id lhs, thread::id rhs) noexcept;
|
---|
| 276 | bool operator<(thread::id lhs, thread::id rhs) noexcept;
|
---|
| 277 | bool operator<=(thread::id lhs, thread::id rhs) noexcept;
|
---|
| 278 | bool operator>(thread::id lhs, thread::id rhs) noexcept;
|
---|
| 279 | bool operator>=(thread::id lhs, thread::id rhs) noexcept;
|
---|
| 280 |
|
---|
| 281 | template<class Char, class Traits>
|
---|
| 282 | basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& out, thread::id id)
|
---|
| 283 | {
|
---|
| 284 | out << id.id_;
|
---|
| 285 |
|
---|
| 286 | return out;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | template<> // TODO: implement
|
---|
| 290 | struct hash<thread::id>;
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | #endif
|
---|