C/C++ API Reference
Loading...
Searching...
No Matches
context.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 <limits>
18
19#include "pw_thread/attrs.h"
20#include "pw_thread/stack.h"
21#include "pw_thread_backend/context.h"
22
23namespace pw {
24
26
27inline constexpr size_t kExternallyAllocatedThreadStack =
28 std::numeric_limits<size_t>::max();
29
44template <size_t kStackSizeBytes = kExternallyAllocatedThreadStack>
46 public:
47 constexpr ThreadContext() = default;
48
49 ThreadContext(const ThreadContext&) = delete;
50 ThreadContext& operator=(const ThreadContext&) = delete;
51
52 constexpr thread::backend::NativeContextWithStack<kStackSizeBytes>& native() {
53 return native_context_;
54 }
55
56 private:
57 thread::backend::NativeContextWithStack<kStackSizeBytes> native_context_;
58};
59
60// ThreadContext with externally allocated stack.
61template <>
62class ThreadContext<kExternallyAllocatedThreadStack> {
63 public:
64 constexpr ThreadContext() = default;
65
66 ThreadContext(const ThreadContext&) = delete;
67 ThreadContext& operator=(const ThreadContext&) = delete;
68
69 constexpr thread::backend::NativeContext& native() { return native_context_; }
70
71 private:
72 thread::backend::NativeContext native_context_;
73};
74
78template <const ThreadAttrs& kAttributes>
80 public:
81 constexpr ThreadContextFor() = default;
82
83 ThreadContextFor(const ThreadContextFor&) = delete;
84 ThreadContextFor& operator=(const ThreadContextFor&) = delete;
85
86 constexpr auto& native() { return context_.native(); }
87
88 private:
89 ThreadContext<kAttributes.has_external_stack()
90 ? kExternallyAllocatedThreadStack
91 : kAttributes.stack_size_bytes()>
92 context_;
93};
94
98
99} // namespace pw
Definition: context.h:79
Definition: context.h:45
The Pigweed namespace.
Definition: alignment.h:27