• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
Thread(const Options &,ThreadRoutine entry,void * arg)42 inline Thread::Thread(const Options&, ThreadRoutine entry, void* arg) {
43   native_type_ = std::thread(entry, arg);
44 }
45 
46 inline Thread& Thread::operator=(Thread&& other) {
47   native_type_ = std::move(other.native_type_);
48   return *this;
49 }
50 
51 inline Thread::~Thread() = default;
52 
get_id()53 inline Id Thread::get_id() const { return native_type_.get_id(); }
54 
join()55 inline void Thread::join() { native_type_.join(); }
56 
detach()57 inline void Thread::detach() {
58 #if defined(__MINGW32__) || defined(__MINGW64__)
59   internal::ErrorAttemptedToInvokeStdThreadDetachOnMinGW();
60 #endif  // defined(__MINGW32__) || defined(__MINGW64__)
61   native_type_.detach();
62 }
63 
swap(Thread & other)64 inline void Thread::swap(Thread& other) {
65   native_type_.swap(other.native_handle());
66 }
67 
native_handle()68 inline Thread::native_handle_type Thread::native_handle() {
69   return native_type_;
70 }
71 
72 }  // namespace pw::thread
73