Pigweed
 
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 {
46namespace thread {
47
65class Thread {
66 public:
69 using native_handle_type = backend::NativeThreadHandle;
70
86 using id = ::pw::thread::backend::NativeId;
87
91
117 Thread(const Options& options, Function<void()>&& entry);
118
119 // Creates a thread with a ThreadContext associated with a ThreadAttrs.
120 template <const ThreadAttrs& kAttributes>
121 Thread(ThreadContextFor<kAttributes>& context, Function<void()>&& entry)
122 : Thread(GetThreadOptions(context), std::move(entry)) {}
123
124 // Creates a thread from context and attributes. Performs a runtime check
125 // that the ThreadContext's stack is large enough, which can be avoided by
126 // using one of the other constructors.
127 template <size_t kStackSizeHintBytes>
129 const ThreadAttrs& attributes,
130 Function<void()>&& entry)
131 : Thread(GetThreadOptions(context, attributes), std::move(entry)) {}
132
133 // Creates a thread with the provided context and attributes. The
134 // attributes must have a ThreadStack set.
135 Thread(ThreadContext<>& context,
136 const ThreadAttrs& attributes,
137 Function<void()>&& entry)
138 : Thread(GetThreadOptions(context, attributes), std::move(entry)) {}
139
142
145
146 Thread(const Thread&) = delete;
147 Thread(Thread&&) = delete;
148 Thread& operator=(const Thread&) = delete;
149
153 id get_id() const;
154
163 bool joinable() const { return get_id() != id(); }
164
165#if PW_THREAD_JOINING_ENABLED
179 void join();
180#else
181 template <typename..., bool kJoiningEnabled = false>
182 void join() {
183 static_assert(kJoiningEnabled,
184 "The selected pw_thread_THREAD backend does not have join() "
185 "enabled (AKA PW_THREAD_JOINING_ENABLED = 1)");
186 }
187#endif // PW_THREAD_JOINING_ENABLED
188
196 void detach();
197
199 void swap(Thread& other);
200
204
205 private:
206 // Note that just like std::thread, this is effectively just a pointer or
207 // reference to the native thread -- this does not contain any memory needed
208 // for the thread to execute.
209 //
210 // This may contain more than the native thread handle to enable functionality
211 // which is not always available such as joining, which may require a
212 // reference to a binary semaphore, or passing arguments to the thread's
213 // function.
214 backend::NativeThread native_type_;
215};
216
217} // namespace thread
218
221using Thread = ::pw::thread::Thread; // Must use `=` for Doxygen to find this.
222
223} // namespace pw
224
225#include "pw_thread_backend/thread_inline.h"
Definition: attrs.h:31
Definition: context.h:73
Definition: context.h:39
Definition: options.h:40
Definition: thread.h:65
native_handle_type native_handle()
::pw::thread::backend::NativeId id
Definition: thread.h:86
backend::NativeThreadHandle native_handle_type
Definition: thread.h:69
Thread & operator=(Thread &&other)
bool joinable() const
Definition: thread.h:163
void swap(Thread &other)
Exchanges the underlying handles of two thread objects.
Thread(const Options &options, Function< void()> &&entry)
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:74
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27