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