source: mainline/uspace/lib/cpp/src/thread.cpp@ 48d9187

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

cpp: added generic joinable threads

  • Property mode set to 100644
File size: 4.2 KB
RevLine 
[ad403590]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 {
[48d9187]44 if (joinable_wrapper_)
45 {
46 joinable_wrapper_->join();
47 delete joinable_wrapper_;
48 }
49
[ad403590]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 {
[48d9187]77 return id_ != fid_t{};
[ad403590]78 }
79
80 void thread::join()
81 {
[48d9187]82 if (joinable_wrapper_)
83 {
84 printf("JOINING\n");
85 joinable_wrapper_->join();
86 printf("JOIN ENDED\n");
87 }
[ad403590]88 }
89
90 void thread::detach()
91 {
92 id_ = fid_t{};
[48d9187]93
94 if (joinable_wrapper_)
95 {
96 joinable_wrapper_->detach();
97 joinable_wrapper_ = nullptr;
98 }
[ad403590]99 }
100
101 thread::id thread::get_id() const noexcept
102 {
103 return id{id_};
104 }
105
106 thread::native_handle_type thread::native_handle()
107 {
108 /**
109 * For fibrils the fid_t returned from fibril_create
110 * is a fibril_t* casted to fid_t, native handles
111 * are implementation defined so we just recast back.
112 */
113 return (native_handle_type)id_;
114 }
115
116 unsigned thread::hardware_concurrency() noexcept
117 {
118 // TODO:
119 return 0;
120 }
121
122 void swap(thread& x, thread& y) noexcept
123 {
124 x.swap(y);
125 }
126
127 namespace this_thread
128 {
129 thread::id get_id() noexcept
130 {
131 return thread::id{fibril_get_id()};
132 }
133
134 void yield() noexcept
135 {
136 fibril_yield();
137 }
138 }
139
140 bool operator==(thread::id lhs, thread::id rhs) noexcept
141 {
142 return lhs.id_ == rhs.id_;
143 }
144
145 bool operator!=(thread::id lhs, thread::id rhs) noexcept
146 {
147 return !(lhs == rhs);
148 }
149
150 bool operator<(thread::id lhs, thread::id rhs) noexcept
151 {
152 return lhs.id_ < rhs.id_;
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 rhs < lhs;
163 }
164
165 bool operator>=(thread::id lhs, thread::id rhs) noexcept
166 {
167 return !(lhs < rhs);
168 }
169}
Note: See TracBrowser for help on using the repository browser.