xref: /aosp_15_r20/external/pigweed/pw_thread_zephyr/public/pw_thread_zephyr/options.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2023 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 "pw_assert/assert.h"
17 #include "pw_thread/thread.h"
18 #include "pw_thread_zephyr/config.h"
19 #include "pw_thread_zephyr/context.h"
20 
21 namespace pw::thread::zephyr {
22 
23 class Context;
24 
25 // pw::thread::Options for Zephyr RTOS.
26 //
27 // Example usage:
28 //
29 //   pw::Thread example_thread(
30 //     pw::thread::zephyr::Options(static_example_thread_context)
31 //         .set_priority(kFooPriority),
32 //     example_thread_function, example_arg);
33 //
34 // TODO(aeremin): Add support for time slice configuration
35 //     (k_thread_time_slice_set when CONFIG_TIMESLICE_PER_THREAD=y).
36 // TODO(aeremin): Add support for thread name
37 //     (k_thread_name_set when CONFIG_THREAD_MONITOR is enabled).
38 class Options : public thread::Options {
39  public:
Options(StaticContext & context)40   constexpr Options(StaticContext& context) : context_(&context) {}
41   constexpr Options(const Options&) = default;
42   constexpr Options(Options&&) = default;
43 
44   // Sets the priority for the Zephyr RTOS thread.
45   // Lower priority values have a higher scheduling priority.
set_priority(int priority)46   constexpr Options& set_priority(int priority) {
47     PW_DASSERT(priority <= config::kLowestSchedulerPriority);
48     PW_DASSERT(priority >= config::kHighestSchedulerPriority);
49     priority_ = priority;
50     return *this;
51   }
52 
53   // Sets the Zephyr RTOS native options
54   // (https://docs.zephyrproject.org/latest/kernel/services/threads/index.html#thread-options)
set_native_options(uint32_t native_options)55   constexpr Options& set_native_options(uint32_t native_options) {
56     native_options_ = native_options;
57     return *this;
58   }
59 
60  private:
61   friend Thread;
62   friend Context;
63 
priority()64   int priority() const { return priority_; }
native_options()65   uint32_t native_options() const { return native_options_; }
static_context()66   StaticContext* static_context() const { return context_; }
67 
68   int priority_ = config::kDefaultPriority;
69   uint32_t native_options_ = 0;
70   StaticContext* context_ = nullptr;
71 };
72 
73 }  // namespace pw::thread::zephyr
74