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