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 "RTOS.h" 17 #include "pw_assert/assert.h" 18 #include "pw_thread/thread.h" 19 #include "pw_thread_embos/config.h" 20 #include "pw_thread_embos/context.h" 21 22 namespace pw::thread::embos { 23 24 // pw::thread::Options for FreeRTOS. 25 // 26 // Example usage: 27 // 28 // // Uses the default priority, but specifies a custom name and context. 29 // pw::thread::Thread example_thread( 30 // pw::thread::embos::Options() 31 // .set_name("example_thread"), 32 // .set_context(static_example_thread_context), 33 // example_thread_function); 34 // 35 // // Provides the name, priority, and pre-allocated context. 36 // pw::thread::Thread static_example_thread( 37 // pw::thread::embos::Options() 38 // .set_name("static_example_thread") 39 // .set_priority(kFooPriority) 40 // .set_context(static_example_thread_context), 41 // example_thread_function); 42 // 43 class Options : public thread::Options { 44 public: Options()45 constexpr Options() {} 46 constexpr Options(const Options&) = default; 47 constexpr Options(Options&&) = default; 48 49 // Sets the name for the embOS task, this is optional. 50 // Note that this will be deep copied into the context and may be truncated 51 // based on PW_THREAD_EMBOS_CONFIG_MAX_THREAD_NAME_LEN. set_name(const char * name)52 constexpr Options& set_name(const char* name) { 53 name_ = name; 54 return *this; 55 } 56 57 // Sets the priority for the embOS task. Higher values are higher priority, 58 // see embOS OS_CreateTaskEx for more detail. 59 // 60 // Precondition: This must be >= PW_THREAD_EMBOS_CONFIG_MIN_PRIORITY. set_priority(OS_PRIO priority)61 constexpr Options& set_priority(OS_PRIO priority) { 62 PW_DASSERT(priority >= config::kMinimumPriority); 63 priority_ = priority; 64 return *this; 65 } 66 67 // Sets the number of ticks this thread is allowed to run before other ready 68 // threads of the same priority are given a chance to run. 69 // 70 // A value of 0 disables time-slicing of this thread. 71 // 72 // Precondition: This must be <= 255 ticks. set_time_slice_interval(OS_UINT time_slice_interval)73 constexpr Options& set_time_slice_interval(OS_UINT time_slice_interval) { 74 PW_DASSERT(time_slice_interval <= 255); 75 time_slice_interval_ = time_slice_interval; 76 return *this; 77 } 78 79 // Set the pre-allocated context (all memory needed to run a thread), see the 80 // pw::thread::embos::Context for more detail. set_context(Context & context)81 constexpr Options& set_context(Context& context) { 82 context_ = &context; 83 return *this; 84 } 85 86 private: 87 friend thread::Thread; 88 name()89 const char* name() const { return name_; } priority()90 OS_PRIO priority() const { return priority_; } time_slice_interval()91 OS_PRIO time_slice_interval() const { return time_slice_interval_; } context()92 Context* context() const { return context_; } 93 94 const char* name_ = nullptr; 95 OS_PRIO priority_ = config::kDefaultPriority; 96 OS_PRIO time_slice_interval_ = config::kDefaultTimeSliceInterval; 97 Context* context_ = nullptr; 98 }; 99 100 } // namespace pw::thread::embos 101