[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 |
|
---|
| 29 | #include <cstdlib>
|
---|
[9283830] | 30 | #include <exception>
|
---|
[ad403590] | 31 | #include <thread>
|
---|
| 32 | #include <utility>
|
---|
| 33 |
|
---|
| 34 | namespace std
|
---|
| 35 | {
|
---|
| 36 | thread::thread() noexcept
|
---|
| 37 | : id_{}
|
---|
| 38 | { /* DUMMY BODY */ }
|
---|
| 39 |
|
---|
| 40 | thread::~thread()
|
---|
| 41 | {
|
---|
[9283830] | 42 | // TODO: investigate joinable() in detail
|
---|
| 43 | // + std::terminate behaves weirdly on HelenOS
|
---|
| 44 | if (joinable() && false)
|
---|
| 45 | std::terminate();
|
---|
[063e0626] | 46 |
|
---|
[9283830] | 47 | // TODO: check for finished too?
|
---|
| 48 | // TODO: WAIT! if it's not detached, then
|
---|
| 49 | // we are joinable and std::terminate was called?
|
---|
| 50 | // TODO: review this entire thing
|
---|
[063e0626] | 51 | if (joinable_wrapper_ && !joinable_wrapper_->detached())
|
---|
| 52 | delete joinable_wrapper_;
|
---|
[ad403590] | 53 | }
|
---|
| 54 |
|
---|
| 55 | thread::thread(thread&& other) noexcept
|
---|
[9283830] | 56 | : id_{other.id_}, joinable_wrapper_{other.joinable_wrapper_}
|
---|
[ad403590] | 57 | {
|
---|
[9283830] | 58 | other.id_ = aux::thread_t{};
|
---|
| 59 | other.joinable_wrapper_ = nullptr;
|
---|
[ad403590] | 60 | }
|
---|
| 61 |
|
---|
| 62 | thread& thread::operator=(thread&& other) noexcept
|
---|
| 63 | {
|
---|
[063e0626] | 64 | if (joinable())
|
---|
[9283830] | 65 | std::terminate();
|
---|
[063e0626] | 66 |
|
---|
[ad403590] | 67 | id_ = other.id_;
|
---|
[9283830] | 68 | other.id_ = aux::thread_t{};
|
---|
| 69 |
|
---|
| 70 | joinable_wrapper_ = other.joinable_wrapper_;
|
---|
| 71 | other.joinable_wrapper_ = nullptr;
|
---|
[ad403590] | 72 |
|
---|
| 73 | return *this;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | void thread::swap(thread& other) noexcept
|
---|
| 77 | {
|
---|
| 78 | std::swap(id_, other.id_);
|
---|
[9283830] | 79 | std::swap(joinable_wrapper_, other.joinable_wrapper_);
|
---|
[ad403590] | 80 | }
|
---|
| 81 |
|
---|
| 82 | bool thread::joinable() const noexcept
|
---|
| 83 | {
|
---|
[9283830] | 84 | return id_ != aux::thread_t{};
|
---|
[ad403590] | 85 | }
|
---|
| 86 |
|
---|
| 87 | void thread::join()
|
---|
| 88 | {
|
---|
[063e0626] | 89 | if (joinable() && joinable_wrapper_)
|
---|
[48d9187] | 90 | joinable_wrapper_->join();
|
---|
[ad403590] | 91 | }
|
---|
| 92 |
|
---|
| 93 | void thread::detach()
|
---|
| 94 | {
|
---|
[9283830] | 95 | id_ = aux::thread_t{};
|
---|
[48d9187] | 96 |
|
---|
| 97 | if (joinable_wrapper_)
|
---|
| 98 | {
|
---|
| 99 | joinable_wrapper_->detach();
|
---|
| 100 | joinable_wrapper_ = nullptr;
|
---|
| 101 | }
|
---|
[ad403590] | 102 | }
|
---|
| 103 |
|
---|
| 104 | thread::id thread::get_id() const noexcept
|
---|
| 105 | {
|
---|
| 106 | return id{id_};
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | thread::native_handle_type thread::native_handle()
|
---|
| 110 | {
|
---|
| 111 | /**
|
---|
| 112 | * For fibrils the fid_t returned from fibril_create
|
---|
| 113 | * is a fibril_t* casted to fid_t, native handles
|
---|
| 114 | * are implementation defined so we just recast back.
|
---|
| 115 | */
|
---|
| 116 | return (native_handle_type)id_;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | unsigned thread::hardware_concurrency() noexcept
|
---|
| 120 | {
|
---|
| 121 | // TODO:
|
---|
| 122 | return 0;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | void swap(thread& x, thread& y) noexcept
|
---|
| 126 | {
|
---|
| 127 | x.swap(y);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | namespace this_thread
|
---|
| 131 | {
|
---|
| 132 | thread::id get_id() noexcept
|
---|
| 133 | {
|
---|
[9283830] | 134 | return thread::id{aux::threading::thread::this_thread()};
|
---|
[ad403590] | 135 | }
|
---|
| 136 |
|
---|
| 137 | void yield() noexcept
|
---|
| 138 | {
|
---|
[9283830] | 139 | aux::threading::thread::yield();
|
---|
[ad403590] | 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | bool operator==(thread::id lhs, thread::id rhs) noexcept
|
---|
| 144 | {
|
---|
| 145 | return lhs.id_ == rhs.id_;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | bool operator!=(thread::id lhs, thread::id rhs) noexcept
|
---|
| 149 | {
|
---|
| 150 | return !(lhs == rhs);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | bool operator<(thread::id lhs, thread::id rhs) noexcept
|
---|
| 154 | {
|
---|
| 155 | return lhs.id_ < rhs.id_;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | bool operator<=(thread::id lhs, thread::id rhs) noexcept
|
---|
| 159 | {
|
---|
| 160 | return !(rhs < lhs);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | bool operator>(thread::id lhs, thread::id rhs) noexcept
|
---|
| 164 | {
|
---|
| 165 | return rhs < lhs;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | bool operator>=(thread::id lhs, thread::id rhs) noexcept
|
---|
| 169 | {
|
---|
| 170 | return !(lhs < rhs);
|
---|
| 171 | }
|
---|
| 172 | }
|
---|