source: mainline/uspace/lib/cpp/src/thread.cpp@ 58ff673

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 58ff673 was 9283830, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: added a threading middle layer

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*
2 * Copyright (c) 2017 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 <cstdlib>
30#include <exception>
31#include <thread>
32#include <utility>
33
34#include <iostream>
35
36namespace std
37{
38 thread::thread() noexcept
39 : id_{}
40 { /* DUMMY BODY */ }
41
42 thread::~thread()
43 {
44 // TODO: investigate joinable() in detail
45 // + std::terminate behaves weirdly on HelenOS
46 if (joinable() && false)
47 std::terminate();
48
49 // TODO: check for finished too?
50 // TODO: WAIT! if it's not detached, then
51 // we are joinable and std::terminate was called?
52 // TODO: review this entire thing
53 if (joinable_wrapper_ && !joinable_wrapper_->detached())
54 delete joinable_wrapper_;
55 }
56
57 thread::thread(thread&& other) noexcept
58 : id_{other.id_}, joinable_wrapper_{other.joinable_wrapper_}
59 {
60 other.id_ = aux::thread_t{};
61 other.joinable_wrapper_ = nullptr;
62 }
63
64 thread& thread::operator=(thread&& other) noexcept
65 {
66 if (joinable())
67 std::terminate();
68
69 id_ = other.id_;
70 other.id_ = aux::thread_t{};
71
72 joinable_wrapper_ = other.joinable_wrapper_;
73 other.joinable_wrapper_ = nullptr;
74
75 return *this;
76 }
77
78 void thread::swap(thread& other) noexcept
79 {
80 std::swap(id_, other.id_);
81 std::swap(joinable_wrapper_, other.joinable_wrapper_);
82 }
83
84 bool thread::joinable() const noexcept
85 {
86 return id_ != aux::thread_t{};
87 }
88
89 void thread::join()
90 {
91 if (joinable() && joinable_wrapper_)
92 joinable_wrapper_->join();
93 }
94
95 void thread::detach()
96 {
97 id_ = aux::thread_t{};
98
99 if (joinable_wrapper_)
100 {
101 joinable_wrapper_->detach();
102 joinable_wrapper_ = nullptr;
103 }
104 }
105
106 thread::id thread::get_id() const noexcept
107 {
108 return id{id_};
109 }
110
111 thread::native_handle_type thread::native_handle()
112 {
113 /**
114 * For fibrils the fid_t returned from fibril_create
115 * is a fibril_t* casted to fid_t, native handles
116 * are implementation defined so we just recast back.
117 */
118 return (native_handle_type)id_;
119 }
120
121 unsigned thread::hardware_concurrency() noexcept
122 {
123 // TODO:
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}
Note: See TracBrowser for help on using the repository browser.