xref: /aosp_15_r20/external/pigweed/pw_thread_zephyr/thread.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker // Copyright 2023 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 #include "pw_thread/thread.h"
15*61c4878aSAndroid Build Coastguard Worker 
16*61c4878aSAndroid Build Coastguard Worker #include <zephyr/kernel.h>
17*61c4878aSAndroid Build Coastguard Worker #include <zephyr/spinlock.h>
18*61c4878aSAndroid Build Coastguard Worker 
19*61c4878aSAndroid Build Coastguard Worker #include "pw_assert/check.h"
20*61c4878aSAndroid Build Coastguard Worker #include "pw_preprocessor/compiler.h"
21*61c4878aSAndroid Build Coastguard Worker #include "pw_thread_zephyr/config.h"
22*61c4878aSAndroid Build Coastguard Worker #include "pw_thread_zephyr/context.h"
23*61c4878aSAndroid Build Coastguard Worker #include "pw_thread_zephyr/options.h"
24*61c4878aSAndroid Build Coastguard Worker 
25*61c4878aSAndroid Build Coastguard Worker using pw::thread::zephyr::Context;
26*61c4878aSAndroid Build Coastguard Worker 
27*61c4878aSAndroid Build Coastguard Worker namespace pw::thread {
28*61c4878aSAndroid Build Coastguard Worker namespace {
29*61c4878aSAndroid Build Coastguard Worker 
30*61c4878aSAndroid Build Coastguard Worker k_spinlock global_thread_done_lock;
31*61c4878aSAndroid Build Coastguard Worker 
32*61c4878aSAndroid Build Coastguard Worker }  // namespace
33*61c4878aSAndroid Build Coastguard Worker 
ThreadEntryPoint(void * void_context_ptr,void *,void *)34*61c4878aSAndroid Build Coastguard Worker void Context::ThreadEntryPoint(void* void_context_ptr, void*, void*) {
35*61c4878aSAndroid Build Coastguard Worker   Context& context = *static_cast<Context*>(void_context_ptr);
36*61c4878aSAndroid Build Coastguard Worker 
37*61c4878aSAndroid Build Coastguard Worker   // Invoke the user's thread function. This may never return.
38*61c4878aSAndroid Build Coastguard Worker   context.fn_();
39*61c4878aSAndroid Build Coastguard Worker   context.fn_ = nullptr;
40*61c4878aSAndroid Build Coastguard Worker 
41*61c4878aSAndroid Build Coastguard Worker   k_spinlock_key_t key = k_spin_lock(&global_thread_done_lock);
42*61c4878aSAndroid Build Coastguard Worker   if (context.detached()) {
43*61c4878aSAndroid Build Coastguard Worker     context.set_task_handle(nullptr);
44*61c4878aSAndroid Build Coastguard Worker   } else {
45*61c4878aSAndroid Build Coastguard Worker     // Defer cleanup to Thread's join() or detach().
46*61c4878aSAndroid Build Coastguard Worker     context.set_thread_done();
47*61c4878aSAndroid Build Coastguard Worker   }
48*61c4878aSAndroid Build Coastguard Worker   k_spin_unlock(&global_thread_done_lock, key);
49*61c4878aSAndroid Build Coastguard Worker }
50*61c4878aSAndroid Build Coastguard Worker 
CreateThread(const zephyr::Options & options,Function<void ()> && thread_fn,Context * & native_type_out)51*61c4878aSAndroid Build Coastguard Worker void Context::CreateThread(const zephyr::Options& options,
52*61c4878aSAndroid Build Coastguard Worker                            Function<void()>&& thread_fn,
53*61c4878aSAndroid Build Coastguard Worker                            Context*& native_type_out) {
54*61c4878aSAndroid Build Coastguard Worker   PW_CHECK(options.static_context() != nullptr);
55*61c4878aSAndroid Build Coastguard Worker 
56*61c4878aSAndroid Build Coastguard Worker   // Use the statically allocated context.
57*61c4878aSAndroid Build Coastguard Worker   native_type_out = options.static_context();
58*61c4878aSAndroid Build Coastguard Worker   // Can't use a context more than once.
59*61c4878aSAndroid Build Coastguard Worker   PW_DCHECK_PTR_EQ(native_type_out->task_handle(), nullptr);
60*61c4878aSAndroid Build Coastguard Worker   // Reset the state of the static context in case it was re-used.
61*61c4878aSAndroid Build Coastguard Worker   native_type_out->set_detached(false);
62*61c4878aSAndroid Build Coastguard Worker   native_type_out->set_thread_done(false);
63*61c4878aSAndroid Build Coastguard Worker 
64*61c4878aSAndroid Build Coastguard Worker   native_type_out->set_thread_routine(std::move(thread_fn));
65*61c4878aSAndroid Build Coastguard Worker   const k_tid_t task_handle =
66*61c4878aSAndroid Build Coastguard Worker       k_thread_create(&native_type_out->thread_info(),
67*61c4878aSAndroid Build Coastguard Worker                       options.static_context()->stack(),
68*61c4878aSAndroid Build Coastguard Worker                       options.static_context()->available_stack_size(),
69*61c4878aSAndroid Build Coastguard Worker                       Context::ThreadEntryPoint,
70*61c4878aSAndroid Build Coastguard Worker                       options.static_context(),
71*61c4878aSAndroid Build Coastguard Worker                       nullptr,
72*61c4878aSAndroid Build Coastguard Worker                       nullptr,
73*61c4878aSAndroid Build Coastguard Worker                       options.priority(),
74*61c4878aSAndroid Build Coastguard Worker                       options.native_options(),
75*61c4878aSAndroid Build Coastguard Worker                       K_NO_WAIT);
76*61c4878aSAndroid Build Coastguard Worker   PW_CHECK_NOTNULL(task_handle);  // Ensure it succeeded.
77*61c4878aSAndroid Build Coastguard Worker   native_type_out->set_task_handle(task_handle);
78*61c4878aSAndroid Build Coastguard Worker }
79*61c4878aSAndroid Build Coastguard Worker 
Thread(const thread::Options & facade_options,Function<void ()> && entry)80*61c4878aSAndroid Build Coastguard Worker Thread::Thread(const thread::Options& facade_options, Function<void()>&& entry)
81*61c4878aSAndroid Build Coastguard Worker     : native_type_(nullptr) {
82*61c4878aSAndroid Build Coastguard Worker   // Cast the generic facade options to the backend specific option of which
83*61c4878aSAndroid Build Coastguard Worker   // only one type can exist at compile time.
84*61c4878aSAndroid Build Coastguard Worker   auto options = static_cast<const zephyr::Options&>(facade_options);
85*61c4878aSAndroid Build Coastguard Worker   Context::CreateThread(options, std::move(entry), native_type_);
86*61c4878aSAndroid Build Coastguard Worker }
87*61c4878aSAndroid Build Coastguard Worker 
detach()88*61c4878aSAndroid Build Coastguard Worker void Thread::detach() {
89*61c4878aSAndroid Build Coastguard Worker   PW_CHECK(joinable());
90*61c4878aSAndroid Build Coastguard Worker 
91*61c4878aSAndroid Build Coastguard Worker   k_spinlock_key_t key = k_spin_lock(&global_thread_done_lock);
92*61c4878aSAndroid Build Coastguard Worker   native_type_->set_detached();
93*61c4878aSAndroid Build Coastguard Worker   const bool thread_done = native_type_->thread_done();
94*61c4878aSAndroid Build Coastguard Worker 
95*61c4878aSAndroid Build Coastguard Worker   if (thread_done) {
96*61c4878aSAndroid Build Coastguard Worker     // The task finished (hit end of Context::ThreadEntryPoint) before we
97*61c4878aSAndroid Build Coastguard Worker     // invoked detach, clean up the task handle to allow the Context reuse.
98*61c4878aSAndroid Build Coastguard Worker     native_type_->set_task_handle(nullptr);
99*61c4878aSAndroid Build Coastguard Worker   } else {
100*61c4878aSAndroid Build Coastguard Worker     // We're detaching before the task finished, defer cleanup to the task at
101*61c4878aSAndroid Build Coastguard Worker     // the end of Context::ThreadEntryPoint.
102*61c4878aSAndroid Build Coastguard Worker   }
103*61c4878aSAndroid Build Coastguard Worker 
104*61c4878aSAndroid Build Coastguard Worker   k_spin_unlock(&global_thread_done_lock, key);
105*61c4878aSAndroid Build Coastguard Worker 
106*61c4878aSAndroid Build Coastguard Worker   // Update to no longer represent a thread of execution.
107*61c4878aSAndroid Build Coastguard Worker   native_type_ = nullptr;
108*61c4878aSAndroid Build Coastguard Worker }
109*61c4878aSAndroid Build Coastguard Worker 
join()110*61c4878aSAndroid Build Coastguard Worker void Thread::join() {
111*61c4878aSAndroid Build Coastguard Worker   PW_CHECK(joinable());
112*61c4878aSAndroid Build Coastguard Worker   PW_CHECK(this_thread::get_id() != get_id());
113*61c4878aSAndroid Build Coastguard Worker 
114*61c4878aSAndroid Build Coastguard Worker   PW_CHECK_INT_EQ(0, k_thread_join(native_type_->task_handle_, K_FOREVER));
115*61c4878aSAndroid Build Coastguard Worker 
116*61c4878aSAndroid Build Coastguard Worker   native_type_->set_task_handle(nullptr);
117*61c4878aSAndroid Build Coastguard Worker 
118*61c4878aSAndroid Build Coastguard Worker   // Update to no longer represent a thread of execution.
119*61c4878aSAndroid Build Coastguard Worker   native_type_ = nullptr;
120*61c4878aSAndroid Build Coastguard Worker }
121*61c4878aSAndroid Build Coastguard Worker 
122*61c4878aSAndroid Build Coastguard Worker }  // namespace pw::thread
123