diff options
author | Ewout van Bekkum <ewout@google.com> | 2020-12-22 12:00:18 -0800 |
---|---|---|
committer | CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2021-02-19 21:37:16 +0000 |
commit | 0f3901eda4a27acd1d175aecf1fcc80c7700c826 (patch) | |
tree | a1a4f6f975a20be128c41df4789d3198c04e7490 /pw_thread_stl/public | |
parent | ef52f3ae5fcd321879503ee4ab829ddd45969cfd (diff) | |
download | pigweed-0f3901eda4a27acd1d175aecf1fcc80c7700c826.tar.gz |
pw_thread: adds thread creation
Adds a std::thread like API through a new pw_thread facade based
on the STL's std::thread API, however with more restricted function
entry routine format support and backend specific option support
to permit non-portable configuration of thread settings/parameters
including static context allocations.
In addition this provides an initial set of backends based on using
the STL's std::thread directly.
Change-Id: Ib8c3cbdc434044e204e67e58d861a40e4acec9b4
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/30920
Reviewed-by: Wyatt Hepler <hepler@google.com>
Reviewed-by: Keir Mierle <keir@google.com>
Commit-Queue: Ewout van Bekkum <ewout@google.com>
Diffstat (limited to 'pw_thread_stl/public')
-rw-r--r-- | pw_thread_stl/public/pw_thread_stl/options.h | 26 | ||||
-rw-r--r-- | pw_thread_stl/public/pw_thread_stl/thread_inline.h | 47 | ||||
-rw-r--r-- | pw_thread_stl/public/pw_thread_stl/thread_native.h | 25 |
3 files changed, 98 insertions, 0 deletions
diff --git a/pw_thread_stl/public/pw_thread_stl/options.h b/pw_thread_stl/public/pw_thread_stl/options.h new file mode 100644 index 000000000..26f5bf578 --- /dev/null +++ b/pw_thread_stl/public/pw_thread_stl/options.h @@ -0,0 +1,26 @@ +// Copyright 2020 The Pigweed Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. +#pragma once + +#include "pw_thread/thread.h" + +namespace pw::thread::stl { + +// Unfortunately std::thread:attributes was not accepted into the C++ standard. +// Instead, users are expected to start the thread and after dynamically adjust +// the thread's attributes using std::thread::native_handle based on the native +// threading APIs. +class Options : public thread::Options {}; + +} // namespace pw::thread::stl diff --git a/pw_thread_stl/public/pw_thread_stl/thread_inline.h b/pw_thread_stl/public/pw_thread_stl/thread_inline.h new file mode 100644 index 000000000..018e80755 --- /dev/null +++ b/pw_thread_stl/public/pw_thread_stl/thread_inline.h @@ -0,0 +1,47 @@ +// Copyright 2020 The Pigweed Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. +#pragma once + +#include <thread> + +namespace pw::thread { + +inline Thread::Thread() : native_type_() {} + +inline Thread::Thread(const Options&, ThreadRoutine entry, void* arg) { + native_type_ = std::thread(entry, arg); +} + +inline Thread& Thread::operator=(Thread&& other) { + native_type_ = std::move(other.native_type_); + return *this; +} + +inline Thread::~Thread() = default; + +inline Id Thread::get_id() const { return native_type_.get_id(); } + +inline void Thread::join() { native_type_.join(); } + +inline void Thread::detach() { native_type_.detach(); } + +inline void Thread::swap(Thread& other) { + native_type_.swap(other.native_handle()); +} + +inline Thread::native_handle_type Thread::native_handle() { + return native_type_; +} + +} // namespace pw::thread diff --git a/pw_thread_stl/public/pw_thread_stl/thread_native.h b/pw_thread_stl/public/pw_thread_stl/thread_native.h new file mode 100644 index 000000000..d3592de1b --- /dev/null +++ b/pw_thread_stl/public/pw_thread_stl/thread_native.h @@ -0,0 +1,25 @@ +// Copyright 2020 The Pigweed Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. +#pragma once + +#include <thread> + +#define PW_THREAD_JOINING_ENABLED 1 + +namespace pw::thread::backend { + +using NativeThread = std::thread; +using NativeThreadHandle = std::thread&; + +} // namespace pw::thread::backend |