source: mainline/uspace/lib/cpp/src/thread.cpp@ 4fe4ea6

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

cpp: wrappers are not deallocated when they are detached

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