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