source: mainline/uspace/lib/cpp/src/thread.cpp@ 063e0626

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

cpp: fixed thread lifetime management as per standard

  • Property mode set to 100644
File size: 4.0 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 <thread>
31#include <utility>
32
33namespace std
34{
35 thread::thread() noexcept
36 : id_{}
37 { /* DUMMY BODY */ }
38
39 thread::~thread()
40 {
41 if (joinable())
42 {
43 // TODO: call std::terminate
44 }
45
46 if (joinable_wrapper_ && !joinable_wrapper_->detached())
47 delete joinable_wrapper_;
48 }
49
50 thread::thread(thread&& other) noexcept
51 : id_{other.id_}
52 {
53 other.id_ = fid_t{};
54 }
55
56 thread& thread::operator=(thread&& other) noexcept
57 {
58 if (joinable())
59 {
60 // TODO: call std::terminate
61 }
62
63 id_ = other.id_;
64 other.id_ = fid_t{};
65
66 return *this;
67 }
68
69 void thread::swap(thread& other) noexcept
70 {
71 std::swap(id_, other.id_);
72 }
73
74 bool thread::joinable() const noexcept
75 {
76 return id_ != fid_t{};
77 }
78
79 void thread::join()
80 {
81 if (joinable() && joinable_wrapper_)
82 joinable_wrapper_->join();
83 }
84
85 void thread::detach()
86 {
87 id_ = fid_t{};
88
89 if (joinable_wrapper_)
90 {
91 joinable_wrapper_->detach();
92 joinable_wrapper_ = nullptr;
93 }
94 }
95
96 thread::id thread::get_id() const noexcept
97 {
98 return id{id_};
99 }
100
101 thread::native_handle_type thread::native_handle()
102 {
103 /**
104 * For fibrils the fid_t returned from fibril_create
105 * is a fibril_t* casted to fid_t, native handles
106 * are implementation defined so we just recast back.
107 */
108 return (native_handle_type)id_;
109 }
110
111 unsigned thread::hardware_concurrency() noexcept
112 {
113 // TODO:
114 return 0;
115 }
116
117 void swap(thread& x, thread& y) noexcept
118 {
119 x.swap(y);
120 }
121
122 namespace this_thread
123 {
124 thread::id get_id() noexcept
125 {
126 return thread::id{fibril_get_id()};
127 }
128
129 void yield() noexcept
130 {
131 fibril_yield();
132 }
133 }
134
135 bool operator==(thread::id lhs, thread::id rhs) noexcept
136 {
137 return lhs.id_ == rhs.id_;
138 }
139
140 bool operator!=(thread::id lhs, thread::id rhs) noexcept
141 {
142 return !(lhs == rhs);
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 !(rhs < lhs);
153 }
154
155 bool operator>(thread::id lhs, thread::id rhs) noexcept
156 {
157 return rhs < lhs;
158 }
159
160 bool operator>=(thread::id lhs, thread::id rhs) noexcept
161 {
162 return !(lhs < rhs);
163 }
164}
Note: See TracBrowser for help on using the repository browser.