1*61c4878aSAndroid Build Coastguard Worker // Copyright 2024 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker //
3*61c4878aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker // the License at
6*61c4878aSAndroid Build Coastguard Worker //
7*61c4878aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker //
9*61c4878aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker // the License.
14*61c4878aSAndroid Build Coastguard Worker
15*61c4878aSAndroid Build Coastguard Worker #include "pw_system/config.h"
16*61c4878aSAndroid Build Coastguard Worker #include "pw_thread/thread.h"
17*61c4878aSAndroid Build Coastguard Worker
18*61c4878aSAndroid Build Coastguard Worker // For now, pw_system:async only supports FreeRTOS or standard library threads.
19*61c4878aSAndroid Build Coastguard Worker //
20*61c4878aSAndroid Build Coastguard Worker // This file will be rewritten once the SEED-0128 generic thread creation APIs
21*61c4878aSAndroid Build Coastguard Worker // are available. Details of the threads owned by pw_system should be an
22*61c4878aSAndroid Build Coastguard Worker // internal implementation detail. If configuration is necessary, it can be
23*61c4878aSAndroid Build Coastguard Worker // exposed through regular config options, rather than requiring users to
24*61c4878aSAndroid Build Coastguard Worker // implement functions.
25*61c4878aSAndroid Build Coastguard Worker
26*61c4878aSAndroid Build Coastguard Worker #if __has_include("FreeRTOS.h")
27*61c4878aSAndroid Build Coastguard Worker
28*61c4878aSAndroid Build Coastguard Worker #include "FreeRTOS.h"
29*61c4878aSAndroid Build Coastguard Worker #include "pw_thread_freertos/options.h"
30*61c4878aSAndroid Build Coastguard Worker #include "task.h"
31*61c4878aSAndroid Build Coastguard Worker
32*61c4878aSAndroid Build Coastguard Worker namespace pw::system {
33*61c4878aSAndroid Build Coastguard Worker namespace {
34*61c4878aSAndroid Build Coastguard Worker
ToWords(size_t bytes)35*61c4878aSAndroid Build Coastguard Worker constexpr size_t ToWords(size_t bytes) {
36*61c4878aSAndroid Build Coastguard Worker return (bytes + sizeof(StackType_t) - 1) / sizeof(StackType_t);
37*61c4878aSAndroid Build Coastguard Worker }
38*61c4878aSAndroid Build Coastguard Worker
39*61c4878aSAndroid Build Coastguard Worker } // namespace
40*61c4878aSAndroid Build Coastguard Worker
StartScheduler()41*61c4878aSAndroid Build Coastguard Worker [[noreturn]] void StartScheduler() {
42*61c4878aSAndroid Build Coastguard Worker vTaskStartScheduler();
43*61c4878aSAndroid Build Coastguard Worker PW_UNREACHABLE;
44*61c4878aSAndroid Build Coastguard Worker }
45*61c4878aSAndroid Build Coastguard Worker
46*61c4878aSAndroid Build Coastguard Worker // Low to high priorities.
47*61c4878aSAndroid Build Coastguard Worker enum class ThreadPriority : UBaseType_t {
48*61c4878aSAndroid Build Coastguard Worker kDispatcher = tskIDLE_PRIORITY + 1,
49*61c4878aSAndroid Build Coastguard Worker // TODO(amontanez): These should ideally be at different priority levels, but
50*61c4878aSAndroid Build Coastguard Worker // there's synchronization issues when they are.
51*61c4878aSAndroid Build Coastguard Worker kWorkQueue = kDispatcher,
52*61c4878aSAndroid Build Coastguard Worker kLog = kDispatcher,
53*61c4878aSAndroid Build Coastguard Worker kRpc = kDispatcher,
54*61c4878aSAndroid Build Coastguard Worker kTransfer = kDispatcher,
55*61c4878aSAndroid Build Coastguard Worker kNumPriorities,
56*61c4878aSAndroid Build Coastguard Worker };
57*61c4878aSAndroid Build Coastguard Worker
58*61c4878aSAndroid Build Coastguard Worker static_assert(static_cast<UBaseType_t>(ThreadPriority::kNumPriorities) <=
59*61c4878aSAndroid Build Coastguard Worker configMAX_PRIORITIES);
60*61c4878aSAndroid Build Coastguard Worker
LogThreadOptions()61*61c4878aSAndroid Build Coastguard Worker const thread::Options& LogThreadOptions() {
62*61c4878aSAndroid Build Coastguard Worker static thread::freertos::StaticContextWithStack<ToWords(
63*61c4878aSAndroid Build Coastguard Worker kLogThreadStackSizeBytes)>
64*61c4878aSAndroid Build Coastguard Worker context;
65*61c4878aSAndroid Build Coastguard Worker static constexpr auto options =
66*61c4878aSAndroid Build Coastguard Worker pw::thread::freertos::Options()
67*61c4878aSAndroid Build Coastguard Worker .set_name("LogThread")
68*61c4878aSAndroid Build Coastguard Worker .set_static_context(context)
69*61c4878aSAndroid Build Coastguard Worker .set_priority(static_cast<UBaseType_t>(ThreadPriority::kLog));
70*61c4878aSAndroid Build Coastguard Worker return options;
71*61c4878aSAndroid Build Coastguard Worker }
72*61c4878aSAndroid Build Coastguard Worker
RpcThreadOptions()73*61c4878aSAndroid Build Coastguard Worker const thread::Options& RpcThreadOptions() {
74*61c4878aSAndroid Build Coastguard Worker static thread::freertos::StaticContextWithStack<ToWords(
75*61c4878aSAndroid Build Coastguard Worker kRpcThreadStackSizeBytes)>
76*61c4878aSAndroid Build Coastguard Worker context;
77*61c4878aSAndroid Build Coastguard Worker static constexpr auto options =
78*61c4878aSAndroid Build Coastguard Worker pw::thread::freertos::Options()
79*61c4878aSAndroid Build Coastguard Worker .set_name("RpcThread")
80*61c4878aSAndroid Build Coastguard Worker .set_static_context(context)
81*61c4878aSAndroid Build Coastguard Worker .set_priority(static_cast<UBaseType_t>(ThreadPriority::kRpc));
82*61c4878aSAndroid Build Coastguard Worker return options;
83*61c4878aSAndroid Build Coastguard Worker }
84*61c4878aSAndroid Build Coastguard Worker
TransferThreadOptions()85*61c4878aSAndroid Build Coastguard Worker const thread::Options& TransferThreadOptions() {
86*61c4878aSAndroid Build Coastguard Worker static thread::freertos::StaticContextWithStack<ToWords(
87*61c4878aSAndroid Build Coastguard Worker kTransferThreadStackSizeBytes)>
88*61c4878aSAndroid Build Coastguard Worker context;
89*61c4878aSAndroid Build Coastguard Worker static constexpr auto options =
90*61c4878aSAndroid Build Coastguard Worker pw::thread::freertos::Options()
91*61c4878aSAndroid Build Coastguard Worker .set_name("TransferThread")
92*61c4878aSAndroid Build Coastguard Worker .set_static_context(context)
93*61c4878aSAndroid Build Coastguard Worker .set_priority(static_cast<UBaseType_t>(ThreadPriority::kTransfer));
94*61c4878aSAndroid Build Coastguard Worker return options;
95*61c4878aSAndroid Build Coastguard Worker }
96*61c4878aSAndroid Build Coastguard Worker
DispatcherThreadOptions()97*61c4878aSAndroid Build Coastguard Worker const thread::Options& DispatcherThreadOptions() {
98*61c4878aSAndroid Build Coastguard Worker static thread::freertos::StaticContextWithStack<ToWords(
99*61c4878aSAndroid Build Coastguard Worker kDispatcherThreadStackSizeBytes)>
100*61c4878aSAndroid Build Coastguard Worker context;
101*61c4878aSAndroid Build Coastguard Worker static constexpr auto options =
102*61c4878aSAndroid Build Coastguard Worker pw::thread::freertos::Options()
103*61c4878aSAndroid Build Coastguard Worker .set_name("DispatcherThread")
104*61c4878aSAndroid Build Coastguard Worker .set_static_context(context)
105*61c4878aSAndroid Build Coastguard Worker .set_priority(static_cast<UBaseType_t>(ThreadPriority::kDispatcher));
106*61c4878aSAndroid Build Coastguard Worker return options;
107*61c4878aSAndroid Build Coastguard Worker }
108*61c4878aSAndroid Build Coastguard Worker
WorkQueueThreadOptions()109*61c4878aSAndroid Build Coastguard Worker const thread::Options& WorkQueueThreadOptions() {
110*61c4878aSAndroid Build Coastguard Worker static thread::freertos::StaticContextWithStack<ToWords(
111*61c4878aSAndroid Build Coastguard Worker kWorkQueueThreadStackSizeBytes)>
112*61c4878aSAndroid Build Coastguard Worker context;
113*61c4878aSAndroid Build Coastguard Worker static constexpr auto options =
114*61c4878aSAndroid Build Coastguard Worker pw::thread::freertos::Options()
115*61c4878aSAndroid Build Coastguard Worker .set_name("WorkQueueThread")
116*61c4878aSAndroid Build Coastguard Worker .set_static_context(context)
117*61c4878aSAndroid Build Coastguard Worker .set_priority(static_cast<UBaseType_t>(ThreadPriority::kWorkQueue));
118*61c4878aSAndroid Build Coastguard Worker return options;
119*61c4878aSAndroid Build Coastguard Worker }
120*61c4878aSAndroid Build Coastguard Worker
121*61c4878aSAndroid Build Coastguard Worker } // namespace pw::system
122*61c4878aSAndroid Build Coastguard Worker
123*61c4878aSAndroid Build Coastguard Worker #else // STL
124*61c4878aSAndroid Build Coastguard Worker
125*61c4878aSAndroid Build Coastguard Worker #include <chrono>
126*61c4878aSAndroid Build Coastguard Worker #include <thread>
127*61c4878aSAndroid Build Coastguard Worker
128*61c4878aSAndroid Build Coastguard Worker #include "pw_thread_stl/options.h"
129*61c4878aSAndroid Build Coastguard Worker
130*61c4878aSAndroid Build Coastguard Worker namespace pw::system {
131*61c4878aSAndroid Build Coastguard Worker namespace {
132*61c4878aSAndroid Build Coastguard Worker
133*61c4878aSAndroid Build Coastguard Worker thread::stl::Options stl_thread_options;
134*61c4878aSAndroid Build Coastguard Worker
135*61c4878aSAndroid Build Coastguard Worker } // namespace
136*61c4878aSAndroid Build Coastguard Worker
StartScheduler()137*61c4878aSAndroid Build Coastguard Worker [[noreturn]] void StartScheduler() {
138*61c4878aSAndroid Build Coastguard Worker while (true) {
139*61c4878aSAndroid Build Coastguard Worker std::this_thread::sleep_for(std::chrono::system_clock::duration::max());
140*61c4878aSAndroid Build Coastguard Worker }
141*61c4878aSAndroid Build Coastguard Worker }
142*61c4878aSAndroid Build Coastguard Worker
LogThreadOptions()143*61c4878aSAndroid Build Coastguard Worker const thread::Options& LogThreadOptions() { return stl_thread_options; }
144*61c4878aSAndroid Build Coastguard Worker
RpcThreadOptions()145*61c4878aSAndroid Build Coastguard Worker const thread::Options& RpcThreadOptions() { return stl_thread_options; }
146*61c4878aSAndroid Build Coastguard Worker
TransferThreadOptions()147*61c4878aSAndroid Build Coastguard Worker const thread::Options& TransferThreadOptions() { return stl_thread_options; }
148*61c4878aSAndroid Build Coastguard Worker
DispatcherThreadOptions()149*61c4878aSAndroid Build Coastguard Worker const thread::Options& DispatcherThreadOptions() { return stl_thread_options; }
150*61c4878aSAndroid Build Coastguard Worker
WorkQueueThreadOptions()151*61c4878aSAndroid Build Coastguard Worker const thread::Options& WorkQueueThreadOptions() { return stl_thread_options; }
152*61c4878aSAndroid Build Coastguard Worker
153*61c4878aSAndroid Build Coastguard Worker } // namespace pw::system
154*61c4878aSAndroid Build Coastguard Worker
155*61c4878aSAndroid Build Coastguard Worker #endif // __has_include("FreeRTOS.h")
156