[ad403590] | 1 | /*
|
---|
[ad40b74b] | 2 | * Copyright (c) 2019 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 |
|
---|
[ad40b74b] | 29 | #include <cassert>
|
---|
[ad403590] | 30 | #include <cstdlib>
|
---|
[9283830] | 31 | #include <exception>
|
---|
[ad403590] | 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 | {
|
---|
[9283830] | 43 | // TODO: investigate joinable() in detail
|
---|
| 44 | // + std::terminate behaves weirdly on HelenOS
|
---|
| 45 | if (joinable() && false)
|
---|
| 46 | std::terminate();
|
---|
[063e0626] | 47 |
|
---|
[9283830] | 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
|
---|
[063e0626] | 52 | if (joinable_wrapper_ && !joinable_wrapper_->detached())
|
---|
| 53 | delete joinable_wrapper_;
|
---|
[ad403590] | 54 | }
|
---|
| 55 |
|
---|
| 56 | thread::thread(thread&& other) noexcept
|
---|
[9283830] | 57 | : id_{other.id_}, joinable_wrapper_{other.joinable_wrapper_}
|
---|
[ad403590] | 58 | {
|
---|
[9283830] | 59 | other.id_ = aux::thread_t{};
|
---|
| 60 | other.joinable_wrapper_ = nullptr;
|
---|
[ad403590] | 61 | }
|
---|
| 62 |
|
---|
| 63 | thread& thread::operator=(thread&& other) noexcept
|
---|
| 64 | {
|
---|
[063e0626] | 65 | if (joinable())
|
---|
[9283830] | 66 | std::terminate();
|
---|
[063e0626] | 67 |
|
---|
[ad403590] | 68 | id_ = other.id_;
|
---|
[9283830] | 69 | other.id_ = aux::thread_t{};
|
---|
| 70 |
|
---|
| 71 | joinable_wrapper_ = other.joinable_wrapper_;
|
---|
| 72 | other.joinable_wrapper_ = nullptr;
|
---|
[ad403590] | 73 |
|
---|
| 74 | return *this;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | void thread::swap(thread& other) noexcept
|
---|
| 78 | {
|
---|
| 79 | std::swap(id_, other.id_);
|
---|
[9283830] | 80 | std::swap(joinable_wrapper_, other.joinable_wrapper_);
|
---|
[ad403590] | 81 | }
|
---|
| 82 |
|
---|
| 83 | bool thread::joinable() const noexcept
|
---|
| 84 | {
|
---|
[9283830] | 85 | return id_ != aux::thread_t{};
|
---|
[ad403590] | 86 | }
|
---|
| 87 |
|
---|
| 88 | void thread::join()
|
---|
| 89 | {
|
---|
[063e0626] | 90 | if (joinable() && joinable_wrapper_)
|
---|
[48d9187] | 91 | joinable_wrapper_->join();
|
---|
[ad403590] | 92 | }
|
---|
| 93 |
|
---|
| 94 | void thread::detach()
|
---|
| 95 | {
|
---|
[9283830] | 96 | id_ = aux::thread_t{};
|
---|
[48d9187] | 97 |
|
---|
| 98 | if (joinable_wrapper_)
|
---|
| 99 | {
|
---|
| 100 | joinable_wrapper_->detach();
|
---|
| 101 | joinable_wrapper_ = nullptr;
|
---|
| 102 | }
|
---|
[ad403590] | 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:
|
---|
[ad40b74b] | 123 | __unimplemented();
|
---|
[ad403590] | 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 | {
|
---|
[9283830] | 136 | return thread::id{aux::threading::thread::this_thread()};
|
---|
[ad403590] | 137 | }
|
---|
| 138 |
|
---|
| 139 | void yield() noexcept
|
---|
| 140 | {
|
---|
[9283830] | 141 | aux::threading::thread::yield();
|
---|
[ad403590] | 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 | }
|
---|