xref: /aosp_15_r20/external/pigweed/pw_thread_stl/public/pw_thread_stl/thread_inline.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15 
16 #include <thread>
17 
18 #include "pw_thread/thread.h"
19 
20 namespace pw::thread {
21 namespace internal {
22 
23 // When compiling with GCC and MinGW-w64 on Windows, std::thread::detach() can
24 // cause indefinite hangs due to issues with thread cleanup. This undefined
25 // symbol prevents binaries from linking if detach() is ever called on a thread
26 // in the final binary.
27 //
28 // It's not clear yet whether this goes away when using the official Windows
29 // SDK. For more information, see b/317922402.
30 #if defined(__MINGW32__) || defined(__MINGW64__)
31 [[noreturn]] void ErrorAttemptedToInvokeStdThreadDetachOnMinGW();
32 #endif  // defined(__MINGW32__) || defined(__MINGW64__)
33 
34 }  // namespace internal
35 
Thread()36 inline Thread::Thread() : native_type_() {}
37 
Thread(const Options &,Function<void ()> && entry)38 inline Thread::Thread(const Options&, Function<void()>&& entry) {
39   native_type_ = std::thread(std::move(entry));
40 }
41 
42 inline Thread& Thread::operator=(Thread&& other) {
43   native_type_ = std::move(other.native_type_);
44   return *this;
45 }
46 
47 inline Thread::~Thread() = default;
48 
get_id()49 inline Thread::id Thread::get_id() const { return native_type_.get_id(); }
50 
join()51 inline void Thread::join() { native_type_.join(); }
52 
detach()53 inline void Thread::detach() {
54 #if defined(__MINGW32__) || defined(__MINGW64__)
55   internal::ErrorAttemptedToInvokeStdThreadDetachOnMinGW();
56 #endif  // defined(__MINGW32__) || defined(__MINGW64__)
57   native_type_.detach();
58 }
59 
swap(Thread & other)60 inline void Thread::swap(Thread& other) {
61   native_type_.swap(*other.native_handle());
62 }
63 
native_handle()64 inline Thread::native_handle_type Thread::native_handle() {
65   return &native_type_;
66 }
67 
68 }  // namespace pw::thread
69