1 // Copyright 2021 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 <cstring> 17 18 #include "RTOS.h" 19 #include "pw_function/function.h" 20 #include "pw_span/span.h" 21 #include "pw_string/util.h" 22 #include "pw_thread_embos/config.h" 23 24 namespace pw::thread { 25 26 class Thread; // Forward declare Thread which depends on Context. 27 28 } // namespace pw::thread 29 30 namespace pw::thread::embos { 31 32 class Options; 33 34 // Static thread context allocation including the TCB, an event group for 35 // joining if enabled, and an external statically allocated stack. 36 // 37 // Example usage: 38 // 39 // std::array<ULONG, kFooStackSizeWords> example_thread_stack; 40 // pw::thread::embos::Context example_thread_context(example_thread_stack); 41 // void StartExampleThread() { 42 // pw::thread::DetachedThread( 43 // pw::thread::embos::Options() 44 // .set_name("static_example_thread") 45 // .set_priority(kFooPriority) 46 // .set_context(example_thread_context), 47 // example_thread_function); 48 // } 49 class Context { 50 public: Context(span<OS_UINT> stack_span)51 explicit Context(span<OS_UINT> stack_span) 52 : tcb_{}, stack_span_(stack_span) {} 53 Context(const Context&) = delete; 54 Context& operator=(const Context&) = delete; 55 56 // Intended for unit test & Thread use only. tcb()57 OS_TASK& tcb() { return tcb_; } 58 59 private: 60 friend Thread; 61 void CreateThread(const embos::Options& options, 62 Function<void()>&& thread_fn); 63 stack()64 span<OS_UINT> stack() { return stack_span_; } 65 in_use()66 bool in_use() const { return in_use_; } 67 void set_in_use(bool in_use = true) { in_use_ = in_use; } 68 name()69 const char* name() const { return name_.data(); } set_name(const char * name)70 void set_name(const char* name) { string::Copy(name, name_); } 71 set_thread_routine(Function<void ()> && rvalue)72 void set_thread_routine(Function<void()>&& rvalue) { 73 fn_ = std::move(rvalue); 74 } 75 detached()76 bool detached() const { return detached_; } 77 void set_detached(bool value = true) { detached_ = value; } 78 thread_done()79 bool thread_done() const { return thread_done_; } 80 void set_thread_done(bool value = true) { thread_done_ = value; } 81 82 #if PW_THREAD_JOINING_ENABLED join_event_object()83 OS_EVENT& join_event_object() { return event_object_; } 84 #endif // PW_THREAD_JOINING_ENABLED 85 86 static void ThreadEntryPoint(void* void_context_ptr); 87 static void TerminateThread(Context& context); 88 89 OS_TASK tcb_; 90 span<OS_UINT> stack_span_; 91 92 Function<void()> fn_; 93 #if PW_THREAD_JOINING_ENABLED 94 // Note that the embOS life cycle of this event object is managed together 95 // with the thread life cycle, not this object's life cycle. 96 OS_EVENT event_object_; 97 #endif // PW_THREAD_JOINING_ENABLED 98 bool in_use_ = false; 99 bool detached_ = false; 100 bool thread_done_ = false; 101 102 // The TCB does not have storage for the name, ergo we provide storage for 103 // the thread's name which can be truncated down to just a null delimiter. 104 std::array<char, config::kMaximumNameLength + 1> name_; 105 }; 106 107 // Static thread context allocation including the stack along with the Context. 108 // 109 // Example usage: 110 // 111 // pw::thread::embos::ContextWithStack<kFooStackSizeWords> 112 // example_thread_context; 113 // void StartExampleThread() { 114 // pw::thread::DetachedThread( 115 // pw::thread::embos::Options() 116 // .set_name("static_example_thread") 117 // .set_priority(kFooPriority) 118 // .set_context(example_thread_context), 119 // example_thread_function); 120 // } 121 template <size_t kStackSizeWords = config::kDefaultStackSizeWords> 122 class ContextWithStack final : public Context { 123 public: ContextWithStack()124 constexpr ContextWithStack() : Context(stack_storage_) { 125 static_assert(kStackSizeWords >= config::kMinimumStackSizeWords); 126 } 127 128 private: 129 std::array<OS_UINT, kStackSizeWords> stack_storage_; 130 }; 131 132 } // namespace pw::thread::embos 133