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 <tuple>
17
18#include "pw_function/function.h"
19#include "pw_thread/id.h"
20#include "pw_thread/thread_core.h"
21
22// clang-format off
23// The backend's thread_native header must provide PW_THREAD_JOINING_ENABLED.
24#include "pw_thread_backend/thread_native.h"
25// clang-format on
26
27namespace pw::thread {
28
51class Options {
52 protected:
53 // We can't use `= default` here, because it allows to create an Options
54 // instance in C++17 with `pw::thread::Options{}` syntax.
55 constexpr Options() {}
56};
57
75class Thread {
76 public:
79 using native_handle_type = backend::NativeThreadHandle;
80
84
121 Thread(const Options& options, Function<void()>&& entry);
122
149 Thread(const Options& options, ThreadCore& thread_core);
150
151 using ThreadRoutine = void (*)(void* arg);
152
163 Thread(const Options& options, ThreadRoutine entry, void* arg = nullptr);
164
168
171
172 Thread(const Thread&) = delete;
173 Thread(Thread&&) = delete;
174 Thread& operator=(const Thread&) = delete;
175
179 Id get_id() const;
180
189 bool joinable() const { return get_id() != Id(); }
190
191#if PW_THREAD_JOINING_ENABLED
205 void join();
206#else
207 template <typename kUnusedType = void>
208 void join() {
209 static_assert(kJoiningEnabled<kUnusedType>,
210 "The selected pw_thread_THREAD backend does not have join() "
211 "enabled (AKA PW_THREAD_JOINING_ENABLED = 1)");
212 }
213#endif // PW_THREAD_JOINING_ENABLED
214
222 void detach();
223
225 void swap(Thread& other);
226
230
231 private:
232 template <typename...>
233 static constexpr std::bool_constant<PW_THREAD_JOINING_ENABLED>
234 kJoiningEnabled = {};
235
236 // Note that just like std::thread, this is effectively just a pointer or
237 // reference to the native thread -- this does not contain any memory needed
238 // for the thread to execute.
239 //
240 // This may contain more than the native thread handle to enable functionality
241 // which is not always available such as joining, which may require a
242 // reference to a binary semaphore, or passing arguments to the thread's
243 // function.
244 backend::NativeThread native_type_;
245};
246
247} // namespace pw::thread
248
249#include "pw_thread_backend/thread_inline.h"
Definition: thread.h:51
Definition: thread.h:75
Thread(const Options &options, ThreadRoutine entry, void *arg=nullptr)
native_handle_type native_handle()
Thread(const Options &options, ThreadCore &thread_core)
backend::NativeThreadHandle native_handle_type
Definition: thread.h:79
Thread & operator=(Thread &&other)
~Thread()
Precondition: The thread must have been EITHER detached or joined.
bool joinable() const
Definition: thread.h:189
void swap(Thread &other)
Exchanges the underlying handles of two thread objects.
Thread(const Options &options, Function< void()> &&entry)
fit::function_impl< inline_target_size, !function_internal::config::kEnableDynamicAllocation, FunctionType, Allocator > Function
Definition: function.h:63