Skip to content

Commit

Permalink
pw_thread: Move pw::thread::Options to its own header
Browse files Browse the repository at this point in the history
This makes it possible to work with Options without relying on the
thread facade.

Change-Id: Ia8cd3dab9d2ba0a6d82081ffea06b00d26014a1c
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/232151
Lint: Lint 🤖 <[email protected]>
Reviewed-by: Ewout van Bekkum <[email protected]>
Commit-Queue: Wyatt Hepler <[email protected]>
Pigweed-Auto-Submit: Wyatt Hepler <[email protected]>
  • Loading branch information
255 authored and CQ Bot Account committed Aug 27, 2024
1 parent 6a1e5d0 commit 8d8bb25
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 30 deletions.
1 change: 1 addition & 0 deletions docs/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ _doxygen_input_files = [ # keep-sorted: start
"$dir_pw_system/public/pw_system/system.h",
"$dir_pw_third_party/freertos/pw_assert_stack_overflow_hook.cc",
"$dir_pw_third_party/freertos/static_task_allocation.cc",
"$dir_pw_thread/public/pw_thread/options.h",
"$dir_pw_thread/public/pw_thread/test_thread_context.h",
"$dir_pw_thread/public/pw_thread/thread.h",
"$dir_pw_tokenizer/public/pw_tokenizer/config.h",
Expand Down
7 changes: 7 additions & 0 deletions pw_thread/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,18 @@ pw_facade(
includes = ["public"],
deps = [
":id",
":options",
":thread_core",
"//pw_function",
],
)

cc_library(
name = "options",
hdrs = ["public/pw_thread/options.h"],
includes = ["public"],
)

label_flag(
name = "thread_backend",
build_setting_default = ":thread_unspecified_backend",
Expand Down
6 changes: 6 additions & 0 deletions pw_thread/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,18 @@ pw_facade("thread") {
]
public_deps = [
":id",
":options",
":thread_core",
dir_pw_function,
]
sources = [ "thread.cc" ]
}

pw_source_set("options") {
public_configs = [ ":public_include_path" ]
public = [ "public/pw_thread/options.h" ]
}

pw_source_set("deprecated_or_new_thread_function") {
public_configs = [ ":public_include_path" ]
public = [ "public/pw_thread/deprecated_or_new_thread_function.h" ]
Expand Down
8 changes: 8 additions & 0 deletions pw_thread/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,18 @@ pw_add_facade(pw_thread.thread STATIC
pw_function
pw_thread.thread_core
pw_thread.id
pw_thread.options
SOURCES
thread.cc
)

pw_add_library(pw_thread.options INTERFACE
HEADERS
public/pw_thread/options.h
PUBLIC_INCLUDES
public
)

pw_add_library(pw_thread.thread_core INTERFACE
HEADERS
public/pw_thread/thread_core.h
Expand Down
47 changes: 47 additions & 0 deletions pw_thread/public/pw_thread/options.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2024 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

namespace pw::thread {

/// The Options contains the parameters needed for a thread to start.
///
/// Options are backend specific and ergo the generic base class cannot be
/// directly instantiated.
///
/// The attributes which can be set through the options are backend specific
/// but may contain things like the thread name, priority, scheduling policy,
/// core/processor affinity, and/or an optional reference to a pre-allocated
/// Context (the collection of memory allocations needed for a thread to run).
///
/// Options shall NOT have an attribute to start threads as detached vs
/// joinable. All `pw::thread::Thread` instances must be explicitly `join()`'d
/// or `detach()`'d through the run-time Thread API.
///
/// Note that if backends set `PW_THREAD_JOINING_ENABLED` to false, backends may
/// use native OS specific APIs to create native detached threads because the
/// `join()` API would be compiled out. However, users must still explicitly
/// invoke `detach()`.
///
/// Options must not contain any memory needed for a thread to run (TCB,
/// stack, etc.). The Options may be deleted or re-used immediately after
/// starting a thread.
class Options {
protected:
// We can't use `= default` here, because it allows to create an Options
// instance in C++17 with `pw::thread::Options{}` syntax.
constexpr Options() {}
};

} // namespace pw::thread
30 changes: 1 addition & 29 deletions pw_thread/public/pw_thread/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "pw_function/function.h"
#include "pw_thread/id.h"
#include "pw_thread/options.h"
#include "pw_thread/thread_core.h"

// clang-format off
Expand All @@ -26,35 +27,6 @@

namespace pw::thread {

/// The Options contains the parameters needed for a thread to start.
///
/// Options are backend specific and ergo the generic base class cannot be
/// directly instantiated.
///
/// The attributes which can be set through the options are backend specific
/// but may contain things like the thread name, priority, scheduling policy,
/// core/processor affinity, and/or an optional reference to a pre-allocated
/// Context (the collection of memory allocations needed for a thread to run).
///
/// Options shall NOT have an attribute to start threads as detached vs
/// joinable. All `pw::thread::Thread` instances must be explicitly `join()`'d
/// or `detach()`'d through the run-time Thread API.
///
/// Note that if backends set `PW_THREAD_JOINING_ENABLED` to false, backends may
/// use native OS specific APIs to create native detached threads because the
/// `join()` API would be compiled out. However, users must still explicitly
/// invoke `detach()`.
///
/// Options must not contain any memory needed for a thread to run (TCB,
/// stack, etc.). The Options may be deleted or re-used immediately after
/// starting a thread.
class Options {
protected:
// We can't use `= default` here, because it allows to create an Options
// instance in C++17 with `pw::thread::Options{}` syntax.
constexpr Options() {}
};

/// The class Thread can represent a single thread of execution. Threads allow
/// multiple functions to execute concurrently.
///
Expand Down
3 changes: 2 additions & 1 deletion pw_thread_stl/public/pw_thread_stl/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
// the License.
#pragma once

#include "pw_thread/thread.h"
#include "pw_thread/options.h"
#include "pw_thread/thread.h" // TODO: b/362356045 - Remove unnecessary include

namespace pw::thread::stl {

Expand Down

0 comments on commit 8d8bb25

Please sign in to comment.