xref: /aosp_15_r20/external/pigweed/pw_thread_embos/util.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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_embos/util.h"
15 
16 #include "RTOS.h"
17 #include "pw_function/function.h"
18 #include "pw_status/status.h"
19 
20 namespace pw::thread::embos {
21 
22 namespace internal {
23 
24 // Iterates through all threads that haven't been deleted, calling the provided
25 // callback.
ForEachThread(const OS_TASK & starting_thread,const ThreadCallback & cb)26 Status ForEachThread(const OS_TASK& starting_thread, const ThreadCallback& cb) {
27   if (!OS_IsRunning()) {
28     return Status::FailedPrecondition();
29   }
30 
31   const OS_TASK* thread = &starting_thread;
32   while (thread != nullptr) {
33     if (!cb(*thread)) {
34       // Early-terminate iteration if requested by the callback.
35       return Status::Aborted();
36     }
37     thread = thread->pNext;
38   }
39 
40   return OkStatus();
41 }
42 
43 }  // namespace internal
44 
ForEachThread(const ThreadCallback & cb)45 Status ForEachThread(const ThreadCallback& cb) {
46   return internal::ForEachThread(*OS_Global.pTask, cb);
47 }
48 
49 }  // namespace pw::thread::embos
50