C/C++ API Reference
Loading...
Searching...
No Matches
attrs.h
1// Copyright 2024 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 <cstddef>
17#include <type_traits>
18
19#include "pw_span/span.h"
20#include "pw_thread/priority.h"
21#include "pw_thread/stack.h"
22
23namespace pw {
24
26
34 public:
36 constexpr ThreadAttrs()
37 : name_(""),
38 priority_(),
39 stack_(nullptr),
40 stack_size_(thread::backend::kDefaultStackSizeBytes) {}
41
43 constexpr ThreadAttrs(const ThreadAttrs&) = default;
44 constexpr ThreadAttrs& operator=(const ThreadAttrs&) = default;
45
47 constexpr const char* name() const { return name_; }
48
49 constexpr ThreadAttrs& set_name(const char* name) {
50 PW_DASSERT(name != nullptr);
51 name_ = name;
52 return *this;
53 }
54
55 constexpr ThreadAttrs& set_name(std::nullptr_t) = delete;
56
57 constexpr ThreadPriority priority() const { return priority_; }
58
61 priority_ = priority;
62 return *this;
63 }
64
70 constexpr auto native_stack() const {
71 PW_ASSERT(has_external_stack());
72 return internal::ThreadStackSpan(stack_, stack_size_);
73 }
74
78 constexpr auto native_stack_pointer() const { return stack_; }
79
84 constexpr auto native_stack_size() const {
85 PW_ASSERT(has_external_stack());
86 return stack_size_;
87 }
88
90 constexpr size_t stack_size_bytes() const {
91 if (has_external_stack()) {
92 return internal::NativeStackSizeBytes(stack_size_);
93 }
94 return stack_size_;
95 }
96
101 PW_ASSERT(!has_external_stack());
102 stack_size_ = stack_size_bytes;
103 return *this;
104 }
105
108 template <size_t kStackSizeBytes>
110 stack_ = stack.native_pointer();
111 stack_size_ = stack.native_size();
112 return *this;
113 }
114
117 stack_ = nullptr;
118 stack_size_ = thread::backend::kDefaultStackSizeBytes;
119 return *this;
120 }
121
124 [[nodiscard]] constexpr bool has_external_stack() const {
125 return stack_ != nullptr;
126 }
127
128 private:
129 const char* name_;
130 ThreadPriority priority_;
131
132 internal::ThreadStackPointer stack_;
133 size_t stack_size_;
134};
135
136} // namespace pw
Definition: attrs.h:33
Definition: stack.h:63
constexpr ThreadAttrs()
Initializes attributes to their backend-defined defaults.
Definition: attrs.h:36
constexpr bool has_external_stack() const
Definition: attrs.h:124
constexpr auto native_stack_size() const
Definition: attrs.h:84
constexpr size_t stack_size_bytes() const
Returns the size of the stack in bytes.
Definition: attrs.h:90
constexpr auto native_stack_pointer() const
Definition: attrs.h:78
thread::internal::Priority< thread::backend::PriorityType, thread::backend::kLowestPriority, thread::backend::kHighestPriority, thread::backend::kDefaultPriority > ThreadPriority
Definition: priority.h:39
constexpr ThreadAttrs & set_stack_size_bytes(size_t stack_size_bytes)
Definition: attrs.h:100
constexpr ThreadAttrs & clear_stack()
Clears a previous call to set_stack.
Definition: attrs.h:116
constexpr ThreadAttrs & set_priority(ThreadPriority priority)
Sets a thread priority hint.
Definition: attrs.h:60
constexpr ThreadAttrs(const ThreadAttrs &)=default
Thread attributes can be copied to share properties between threads.
constexpr native native_pointer()
Definition: stack.h:75
constexpr const char * name() const
Name hint as a null-terminated string. Never null.
Definition: attrs.h:47
constexpr size_t native_size() const
Definition: stack.h:83
constexpr ThreadAttrs & set_stack(ThreadStack< kStackSizeBytes > &stack)
Definition: attrs.h:109
constexpr auto native_stack() const
Definition: attrs.h:70
The Pigweed namespace.
Definition: alignment.h:27