Pigweed
 
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
32 public:
34 constexpr ThreadAttrs()
35 : name_(""),
36 priority_(),
37 stack_(nullptr),
38 stack_size_(thread::backend::kDefaultStackSizeBytes) {}
39
41 constexpr ThreadAttrs(const ThreadAttrs&) = default;
42 constexpr ThreadAttrs& operator=(const ThreadAttrs&) = default;
43
45 constexpr const char* name() const { return name_; }
46
47 constexpr ThreadAttrs& set_name(const char* name) {
48 PW_DASSERT(name != nullptr);
49 name_ = name;
50 return *this;
51 }
52
53 constexpr ThreadAttrs& set_name(std::nullptr_t) = delete;
54
55 constexpr ThreadPriority priority() const { return priority_; }
56
59 priority_ = priority;
60 return *this;
61 }
62
68 constexpr auto native_stack() const {
69 PW_ASSERT(has_external_stack());
70 return internal::ThreadStackSpan(stack_, stack_size_);
71 }
72
76 constexpr auto native_stack_pointer() const { return stack_; }
77
82 constexpr auto native_stack_size() const {
83 PW_ASSERT(has_external_stack());
84 return stack_size_;
85 }
86
88 constexpr size_t stack_size_bytes() const {
89 if (has_external_stack()) {
90 return internal::NativeStackSizeBytes(stack_size_);
91 }
92 return stack_size_;
93 }
94
99 PW_ASSERT(!has_external_stack());
100 stack_size_ = stack_size_bytes;
101 return *this;
102 }
103
106 template <size_t kStackSizeBytes>
108 stack_ = stack.native_pointer();
109 stack_size_ = stack.native_size();
110 return *this;
111 }
112
115 stack_ = nullptr;
116 stack_size_ = thread::backend::kDefaultStackSizeBytes;
117 return *this;
118 }
119
122 [[nodiscard]] constexpr bool has_external_stack() const {
123 return stack_ != nullptr;
124 }
125
126 private:
127 const char* name_;
128 ThreadPriority priority_;
129
130 internal::ThreadStackPointer stack_;
131 size_t stack_size_;
132};
133
134} // namespace pw
Definition: attrs.h:31
constexpr ThreadAttrs()
Initializes attributes to their backend-defined defaults.
Definition: attrs.h:34
constexpr bool has_external_stack() const
Definition: attrs.h:122
constexpr auto native_stack_size() const
Definition: attrs.h:82
constexpr size_t stack_size_bytes() const
Returns the size of the stack in bytes.
Definition: attrs.h:88
constexpr auto native_stack_pointer() const
Definition: attrs.h:76
constexpr ThreadAttrs & set_stack_size_bytes(size_t stack_size_bytes)
Definition: attrs.h:98
constexpr ThreadAttrs & clear_stack()
Clears a previous call to set_stack.
Definition: attrs.h:114
constexpr ThreadAttrs & set_priority(ThreadPriority priority)
Sets a thread priority hint.
Definition: attrs.h:58
constexpr ThreadAttrs(const ThreadAttrs &)=default
Thread attributes can be copied to share properties between threads.
constexpr const char * name() const
Name hint as a null-terminated string. Never null.
Definition: attrs.h:45
constexpr ThreadAttrs & set_stack(ThreadStack< kStackSizeBytes > &stack)
Definition: attrs.h:107
constexpr auto native_stack() const
Definition: attrs.h:68
Definition: stack.h:60
constexpr native native_pointer()
Definition: stack.h:72
constexpr size_t native_size() const
Definition: stack.h:80
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27
thread::internal::Priority< thread::backend::PriorityType, thread::backend::kLowestPriority, thread::backend::kHighestPriority, thread::backend::kDefaultPriority > ThreadPriority
Definition: priority.h:37