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