C/C++ API Reference
Loading...
Searching...
No Matches
thread.h
1// Copyright 2021 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 <type_traits>
17
18#include "pw_function/function.h"
19#include "pw_thread/attrs.h"
20#include "pw_thread/context.h"
21#include "pw_thread/id.h"
22#include "pw_thread/native_options.h"
23#include "pw_thread/options.h"
24
25// clang-format off
26// The backend's thread_native header must provide PW_THREAD_JOINING_ENABLED.
27#include "pw_thread_backend/thread_native.h" // IWYU pragma: export
28// clang-format on
29
30// Detect if the default "null" thread creation backend is seletecd.
31#ifdef _PW_THREAD_GENERIC_CREATION_UNSUPPORTED
32
33#define PW_THREAD_GENERIC_CREATION_IS_SUPPORTED 0
34#undef _PW_THREAD_GENERIC_CREATION_UNSUPPORTED
35
36#else
37
38// Indicates whether generic thread creation is implemented.
39// TODO: b/385440717 - Remove this flag when generic thread creation is
40// implemented for all pw_thread backends.
41#define PW_THREAD_GENERIC_CREATION_IS_SUPPORTED 1
42
43#endif // _PW_THREAD_GENERIC_CREATION_UNSUPPORTED
44
45namespace pw {
46
48
50namespace thread {
51
69class Thread {
70 public:
73 using native_handle_type = backend::NativeThreadHandle;
74
90 using id = ::pw::thread::backend::NativeId;
91
95
121 Thread(const Options& options, Function<void()>&& entry);
122
123 // Creates a thread with a ThreadContext associated with a ThreadAttrs.
124 template <const ThreadAttrs& kAttributes>
125 Thread(ThreadContextFor<kAttributes>& context, Function<void()>&& entry)
126 : Thread(GetThreadOptions(context), std::move(entry)) {}
127
128 // Creates a thread from context and attributes. Performs a runtime check
129 // that the ThreadContext's stack is large enough, which can be avoided by
130 // using one of the other constructors.
131 template <size_t kStackSizeHintBytes>
133 const ThreadAttrs& attributes,
134 Function<void()>&& entry)
135 : Thread(GetThreadOptions(context, attributes), std::move(entry)) {}
136
137 // Creates a thread with the provided context and attributes. The
138 // attributes must have a ThreadStack set.
139 Thread(ThreadContext<>& context,
140 const ThreadAttrs& attributes,
141 Function<void()>&& entry)
142 : Thread(GetThreadOptions(context, attributes), std::move(entry)) {}
143
146
149
150 Thread(const Thread&) = delete;
151 Thread(Thread&&) = delete;
152 Thread& operator=(const Thread&) = delete;
153
157 id get_id() const;
158
167 bool joinable() const { return get_id() != id(); }
168
169#if PW_THREAD_JOINING_ENABLED
183 void join();
184#else
185 template <typename..., bool kJoiningEnabled = false>
186 void join() {
187 static_assert(kJoiningEnabled,
188 "The selected pw_thread_THREAD backend does not have join() "
189 "enabled (AKA PW_THREAD_JOINING_ENABLED = 1)");
190 }
191#endif // PW_THREAD_JOINING_ENABLED
192
200 void detach();
201
203 void swap(Thread& other);
204
208
209 private:
210 // Note that just like std::thread, this is effectively just a pointer or
211 // reference to the native thread -- this does not contain any memory needed
212 // for the thread to execute.
213 //
214 // This may contain more than the native thread handle to enable functionality
215 // which is not always available such as joining, which may require a
216 // reference to a binary semaphore, or passing arguments to the thread's
217 // function.
218 backend::NativeThread native_type_;
219};
220
221} // namespace thread
222
225using Thread = ::pw::thread::Thread; // Must use `=` for Doxygen to find this.
226
227} // namespace pw
228
229#include "pw_thread_backend/thread_inline.h"
Definition: attrs.h:33
Definition: context.h:79
Definition: context.h:45
Definition: options.h:42
Definition: thread.h:69
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:73
native_handle_type native_handle()
::pw::thread::backend::NativeId id
Definition: thread.h:90
backend::NativeThreadHandle native_handle_type
Definition: thread.h:73
Thread & operator=(Thread &&other)
bool joinable() const
Definition: thread.h:167
void swap(Thread &other)
Exchanges the underlying handles of two thread objects.
Thread(const Options &options, Function< void()> &&entry)
The Pigweed namespace.
Definition: alignment.h:27