xref: /aosp_15_r20/external/pigweed/pw_thread_embos/thread.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker // Copyright 2021 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 "pw_assert/check.h"
17*61c4878aSAndroid Build Coastguard Worker #include "pw_thread_embos/config.h"
18*61c4878aSAndroid Build Coastguard Worker #include "pw_thread_embos/context.h"
19*61c4878aSAndroid Build Coastguard Worker #include "pw_thread_embos/options.h"
20*61c4878aSAndroid Build Coastguard Worker 
21*61c4878aSAndroid Build Coastguard Worker using pw::thread::embos::Context;
22*61c4878aSAndroid Build Coastguard Worker 
23*61c4878aSAndroid Build Coastguard Worker namespace pw::thread {
24*61c4878aSAndroid Build Coastguard Worker 
ThreadEntryPoint(void * void_context_ptr)25*61c4878aSAndroid Build Coastguard Worker void Context::ThreadEntryPoint(void* void_context_ptr) {
26*61c4878aSAndroid Build Coastguard Worker   Context& context = *reinterpret_cast<Context*>(void_context_ptr);
27*61c4878aSAndroid Build Coastguard Worker 
28*61c4878aSAndroid Build Coastguard Worker   // Invoke the user's thread function. This may never return.
29*61c4878aSAndroid Build Coastguard Worker   context.fn_();
30*61c4878aSAndroid Build Coastguard Worker   context.fn_ = nullptr;
31*61c4878aSAndroid Build Coastguard Worker 
32*61c4878aSAndroid Build Coastguard Worker   // Use a task only critical section to guard against join() and detach().
33*61c4878aSAndroid Build Coastguard Worker   OS_SuspendAllTasks();
34*61c4878aSAndroid Build Coastguard Worker   if (context.detached()) {
35*61c4878aSAndroid Build Coastguard Worker     // There is no threadsafe way to re-use detached threads. Callbacks
36*61c4878aSAndroid Build Coastguard Worker     // registered through OS_AddOnTerminateHook CANNOT be used for this as they
37*61c4878aSAndroid Build Coastguard Worker     // are invoked before the kernel is done using the task's TCB!
38*61c4878aSAndroid Build Coastguard Worker     // However to enable unit test coverage we go ahead and clear this.
39*61c4878aSAndroid Build Coastguard Worker     context.set_in_use(false);
40*61c4878aSAndroid Build Coastguard Worker 
41*61c4878aSAndroid Build Coastguard Worker #if PW_THREAD_JOINING_ENABLED
42*61c4878aSAndroid Build Coastguard Worker     // If the thread handle was detached before the thread finished execution,
43*61c4878aSAndroid Build Coastguard Worker     // i.e. got here, then we are responsible for cleaning up the join event
44*61c4878aSAndroid Build Coastguard Worker     // object.
45*61c4878aSAndroid Build Coastguard Worker     OS_EVENT_Delete(&context.join_event_object());
46*61c4878aSAndroid Build Coastguard Worker #endif  // PW_THREAD_JOINING_ENABLED
47*61c4878aSAndroid Build Coastguard Worker 
48*61c4878aSAndroid Build Coastguard Worker     // Re-enable the scheduler before we delete this execution. Note this name
49*61c4878aSAndroid Build Coastguard Worker     // is a bit misleading as reference counting is used.
50*61c4878aSAndroid Build Coastguard Worker     OS_ResumeAllSuspendedTasks();
51*61c4878aSAndroid Build Coastguard Worker     OS_TerminateTask(nullptr);
52*61c4878aSAndroid Build Coastguard Worker     PW_UNREACHABLE;
53*61c4878aSAndroid Build Coastguard Worker   }
54*61c4878aSAndroid Build Coastguard Worker 
55*61c4878aSAndroid Build Coastguard Worker   // Otherwise the task finished before the thread was detached or joined, defer
56*61c4878aSAndroid Build Coastguard Worker   // cleanup to Thread's join() or detach().
57*61c4878aSAndroid Build Coastguard Worker   context.set_thread_done();
58*61c4878aSAndroid Build Coastguard Worker   OS_ResumeAllSuspendedTasks();
59*61c4878aSAndroid Build Coastguard Worker 
60*61c4878aSAndroid Build Coastguard Worker #if PW_THREAD_JOINING_ENABLED
61*61c4878aSAndroid Build Coastguard Worker   OS_EVENT_Set(&context.join_event_object());
62*61c4878aSAndroid Build Coastguard Worker #endif  // PW_THREAD_JOINING_ENABLED
63*61c4878aSAndroid Build Coastguard Worker 
64*61c4878aSAndroid Build Coastguard Worker   // Let the thread handle owner terminate this task when they detach or join.
65*61c4878aSAndroid Build Coastguard Worker   OS_Suspend(nullptr);
66*61c4878aSAndroid Build Coastguard Worker   PW_UNREACHABLE;
67*61c4878aSAndroid Build Coastguard Worker }
68*61c4878aSAndroid Build Coastguard Worker 
TerminateThread(Context & context)69*61c4878aSAndroid Build Coastguard Worker void Context::TerminateThread(Context& context) {
70*61c4878aSAndroid Build Coastguard Worker   // Stop the other task first.
71*61c4878aSAndroid Build Coastguard Worker   OS_TerminateTask(&context.tcb());
72*61c4878aSAndroid Build Coastguard Worker 
73*61c4878aSAndroid Build Coastguard Worker   // Mark the context as unused for potential later re-use.
74*61c4878aSAndroid Build Coastguard Worker   context.set_in_use(false);
75*61c4878aSAndroid Build Coastguard Worker 
76*61c4878aSAndroid Build Coastguard Worker #if PW_THREAD_JOINING_ENABLED
77*61c4878aSAndroid Build Coastguard Worker   // Just in case someone abused our API, ensure their use of the event group is
78*61c4878aSAndroid Build Coastguard Worker   // properly handled by the kernel regardless.
79*61c4878aSAndroid Build Coastguard Worker   OS_EVENT_Delete(&context.join_event_object());
80*61c4878aSAndroid Build Coastguard Worker #endif  // PW_THREAD_JOINING_ENABLED
81*61c4878aSAndroid Build Coastguard Worker }
82*61c4878aSAndroid Build Coastguard Worker 
CreateThread(const embos::Options & options,Function<void ()> && thread_fn)83*61c4878aSAndroid Build Coastguard Worker void Context::CreateThread(const embos::Options& options,
84*61c4878aSAndroid Build Coastguard Worker                            Function<void()>&& thread_fn) {
85*61c4878aSAndroid Build Coastguard Worker   // Can't use a context more than once.
86*61c4878aSAndroid Build Coastguard Worker   PW_DCHECK(!in_use());
87*61c4878aSAndroid Build Coastguard Worker 
88*61c4878aSAndroid Build Coastguard Worker   // Reset the state of the static context in case it was re-used.
89*61c4878aSAndroid Build Coastguard Worker   set_in_use(true);
90*61c4878aSAndroid Build Coastguard Worker   set_detached(false);
91*61c4878aSAndroid Build Coastguard Worker   set_thread_done(false);
92*61c4878aSAndroid Build Coastguard Worker #if PW_THREAD_JOINING_ENABLED
93*61c4878aSAndroid Build Coastguard Worker   OS_EVENT_CreateEx(&join_event_object(), OS_EVENT_RESET_MODE_AUTO);
94*61c4878aSAndroid Build Coastguard Worker #endif  // PW_THREAD_JOINING_ENABLED
95*61c4878aSAndroid Build Coastguard Worker 
96*61c4878aSAndroid Build Coastguard Worker   // Copy over the thread name.
97*61c4878aSAndroid Build Coastguard Worker   set_name(options.name());
98*61c4878aSAndroid Build Coastguard Worker 
99*61c4878aSAndroid Build Coastguard Worker   // In order to support functions which return and joining, a delegate is
100*61c4878aSAndroid Build Coastguard Worker   // deep copied into the context with a small wrapping function to actually
101*61c4878aSAndroid Build Coastguard Worker   // invoke the task with its arg.
102*61c4878aSAndroid Build Coastguard Worker   set_thread_routine(std::move(thread_fn));
103*61c4878aSAndroid Build Coastguard Worker 
104*61c4878aSAndroid Build Coastguard Worker   OS_CreateTaskEx(&tcb(),
105*61c4878aSAndroid Build Coastguard Worker                   name(),
106*61c4878aSAndroid Build Coastguard Worker                   options.priority(),
107*61c4878aSAndroid Build Coastguard Worker                   Context::ThreadEntryPoint,
108*61c4878aSAndroid Build Coastguard Worker                   stack().data(),
109*61c4878aSAndroid Build Coastguard Worker                   static_cast<OS_UINT>(stack().size_bytes()),
110*61c4878aSAndroid Build Coastguard Worker                   options.time_slice_interval(),
111*61c4878aSAndroid Build Coastguard Worker                   this);
112*61c4878aSAndroid Build Coastguard Worker }
113*61c4878aSAndroid Build Coastguard Worker 
Thread(const thread::Options & facade_options,Function<void ()> && entry)114*61c4878aSAndroid Build Coastguard Worker Thread::Thread(const thread::Options& facade_options, Function<void()>&& entry)
115*61c4878aSAndroid Build Coastguard Worker     : native_type_(nullptr) {
116*61c4878aSAndroid Build Coastguard Worker   // Cast the generic facade options to the backend specific option of which
117*61c4878aSAndroid Build Coastguard Worker   // only one type can exist at compile time.
118*61c4878aSAndroid Build Coastguard Worker   auto options = static_cast<const embos::Options&>(facade_options);
119*61c4878aSAndroid Build Coastguard Worker   PW_DCHECK_NOTNULL(options.context(), "The Context is not optional");
120*61c4878aSAndroid Build Coastguard Worker   native_type_ = options.context();
121*61c4878aSAndroid Build Coastguard Worker   native_type_->CreateThread(options, std::move(entry));
122*61c4878aSAndroid Build Coastguard Worker }
123*61c4878aSAndroid Build Coastguard Worker 
detach()124*61c4878aSAndroid Build Coastguard Worker void Thread::detach() {
125*61c4878aSAndroid Build Coastguard Worker   PW_CHECK(joinable());
126*61c4878aSAndroid Build Coastguard Worker 
127*61c4878aSAndroid Build Coastguard Worker   OS_Suspend(&native_type_->tcb());
128*61c4878aSAndroid Build Coastguard Worker   native_type_->set_detached();
129*61c4878aSAndroid Build Coastguard Worker   const bool thread_done = native_type_->thread_done();
130*61c4878aSAndroid Build Coastguard Worker   OS_Resume(&native_type_->tcb());
131*61c4878aSAndroid Build Coastguard Worker 
132*61c4878aSAndroid Build Coastguard Worker   if (thread_done) {
133*61c4878aSAndroid Build Coastguard Worker     // The task finished (hit end of Context::ThreadEntryPoint) before we
134*61c4878aSAndroid Build Coastguard Worker     // invoked detach, clean up the thread.
135*61c4878aSAndroid Build Coastguard Worker     Context::TerminateThread(*native_type_);
136*61c4878aSAndroid Build Coastguard Worker   } else {
137*61c4878aSAndroid Build Coastguard Worker     // We're detaching before the task finished, defer cleanup to the task at
138*61c4878aSAndroid Build Coastguard Worker     // the end of Context::ThreadEntryPoint.
139*61c4878aSAndroid Build Coastguard Worker   }
140*61c4878aSAndroid Build Coastguard Worker 
141*61c4878aSAndroid Build Coastguard Worker   // Update to no longer represent a thread of execution.
142*61c4878aSAndroid Build Coastguard Worker   native_type_ = nullptr;
143*61c4878aSAndroid Build Coastguard Worker }
144*61c4878aSAndroid Build Coastguard Worker 
145*61c4878aSAndroid Build Coastguard Worker #if PW_THREAD_JOINING_ENABLED
join()146*61c4878aSAndroid Build Coastguard Worker void Thread::join() {
147*61c4878aSAndroid Build Coastguard Worker   PW_CHECK(joinable());
148*61c4878aSAndroid Build Coastguard Worker   PW_CHECK(this_thread::get_id() != get_id());
149*61c4878aSAndroid Build Coastguard Worker 
150*61c4878aSAndroid Build Coastguard Worker   OS_EVENT_Wait(&native_type_->join_event_object());
151*61c4878aSAndroid Build Coastguard Worker 
152*61c4878aSAndroid Build Coastguard Worker   // No need for a critical section here as the thread at this point is
153*61c4878aSAndroid Build Coastguard Worker   // waiting to be deleted.
154*61c4878aSAndroid Build Coastguard Worker   Context::TerminateThread(*native_type_);
155*61c4878aSAndroid Build Coastguard Worker 
156*61c4878aSAndroid Build Coastguard Worker   // Update to no longer represent a thread of execution.
157*61c4878aSAndroid Build Coastguard Worker   native_type_ = nullptr;
158*61c4878aSAndroid Build Coastguard Worker }
159*61c4878aSAndroid Build Coastguard Worker #endif  // PW_THREAD_JOINING_ENABLED
160*61c4878aSAndroid Build Coastguard Worker 
161*61c4878aSAndroid Build Coastguard Worker }  // namespace pw::thread
162