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_freertos/util.h"
15
16 #include "FreeRTOS.h"
17 #include "list.h"
18 #include "pw_function/function.h"
19 #include "pw_log/log.h"
20 #include "pw_status/status.h"
21 #include "pw_status/try.h"
22 #include "task.h"
23
24 // The externed symbols below are all internal FreeRTOS kernel variables from
25 // FreeRTOS/Source/tasks.c needed in order to iterate through all of the threads
26 // from interrupts which the native APIs do not permit.
27
28 extern "C" PRIVILEGED_DATA volatile BaseType_t xSchedulerRunning;
29
30 extern "C" PRIVILEGED_DATA TaskHandle_t volatile pxCurrentTCB;
31
32 // Prioritised ready tasks.
33 extern "C" PRIVILEGED_DATA List_t pxReadyTasksLists[configMAX_PRIORITIES];
34
35 // Points to the delayed task list currently being used.
36 extern "C" PRIVILEGED_DATA List_t* volatile pxDelayedTaskList;
37
38 // Points to the delayed task list currently being used to hold tasks that have
39 // overflowed the current tick count.
40 extern "C" PRIVILEGED_DATA List_t* volatile pxOverflowDelayedTaskList;
41
42 #if INCLUDE_vTaskDelete == 1
43 // Tasks that have been deleted - but their memory not yet freed.
44 extern "C" PRIVILEGED_DATA List_t xTasksWaitingTermination;
45 #endif // INCLUDE_vTaskDelete == 1
46
47 #if INCLUDE_vTaskSuspend == 1
48 // Tasks that are currently suspended.
49 extern "C" PRIVILEGED_DATA List_t xSuspendedTaskList;
50 #endif // INCLUDE_vTaskSuspend == 1
51
52 namespace pw::thread::freertos {
53 namespace {
54
ForEachThreadInList(List_t * list,const eTaskState default_list_state,const ThreadCallback & cb)55 Status ForEachThreadInList(List_t* list,
56 const eTaskState default_list_state,
57 const ThreadCallback& cb) {
58 if (listCURRENT_LIST_LENGTH(list) == 0) {
59 return OkStatus();
60 }
61
62 Status status = OkStatus();
63 // Note that these are pointers to the thread control blocks, however the
64 // list macros from FreeRTOS do not cast the types and ergo we use void *.
65 void* current_thread;
66 void* first_thread_in_list;
67 listGET_OWNER_OF_NEXT_ENTRY(first_thread_in_list, list);
68 do {
69 listGET_OWNER_OF_NEXT_ENTRY(current_thread, list);
70 // We must finish the list iteration to restore the list state, but
71 // we want to stop invoking callbacks upon the first failure.
72 if (status.ok()) {
73 // Note that the lists do not contain the running state, so instead
74 // check for each thread whether it is currently running.
75 const TaskHandle_t current_thread_handle =
76 reinterpret_cast<TaskHandle_t>(current_thread);
77 if (!cb(current_thread_handle,
78 current_thread_handle == pxCurrentTCB ? eRunning
79 : default_list_state)) {
80 status = Status::Aborted();
81 }
82 }
83 } while (current_thread != first_thread_in_list);
84 return status;
85 }
86
87 } // namespace
88
ForEachThread(const ThreadCallback & cb)89 Status ForEachThread(const ThreadCallback& cb) {
90 if (xSchedulerRunning == pdFALSE) {
91 return Status::FailedPrecondition();
92 }
93
94 for (size_t i = 0; i < configMAX_PRIORITIES; ++i) {
95 PW_TRY(ForEachThreadInList(&pxReadyTasksLists[i], eReady, cb));
96 }
97 PW_TRY(ForEachThreadInList(pxDelayedTaskList, eBlocked, cb));
98 PW_TRY(ForEachThreadInList(pxOverflowDelayedTaskList, eBlocked, cb));
99 #if INCLUDE_vTaskDelete == 1
100 PW_TRY(ForEachThreadInList(&xTasksWaitingTermination, eDeleted, cb));
101 #endif // INCLUDE_vTaskDelete == 1
102 #if INCLUDE_vTaskSuspend == 1
103 PW_TRY(ForEachThreadInList(&xSuspendedTaskList, eSuspended, cb));
104 #endif // INCLUDE_vTaskSuspend == 1
105 return OkStatus();
106 }
107
108 } // namespace pw::thread::freertos
109