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 // NOTE: FreeRTOS declares these symbols as `static`, so they are not normally
29 // accessible outside of their translation unit. The `static` keyword must be
30 // removed by setting `pw_third_party_freertos_DISABLE_TASKS_STATICS` in the
31 // builds. See https://pigweed.dev/pw_thread_freertos/#thread-iteration-backend.
32
33 extern "C" PRIVILEGED_DATA volatile BaseType_t xSchedulerRunning;
34
35 extern "C" PRIVILEGED_DATA TaskHandle_t volatile pxCurrentTCB;
36
37 // Prioritised ready tasks.
38 extern "C" PRIVILEGED_DATA List_t pxReadyTasksLists[configMAX_PRIORITIES];
39
40 // Points to the delayed task list currently being used.
41 extern "C" PRIVILEGED_DATA List_t* volatile pxDelayedTaskList;
42
43 // Points to the delayed task list currently being used to hold tasks that have
44 // overflowed the current tick count.
45 extern "C" PRIVILEGED_DATA List_t* volatile pxOverflowDelayedTaskList;
46
47 #if INCLUDE_vTaskDelete == 1
48 // Tasks that have been deleted - but their memory not yet freed.
49 extern "C" PRIVILEGED_DATA List_t xTasksWaitingTermination;
50 #endif // INCLUDE_vTaskDelete == 1
51
52 #if INCLUDE_vTaskSuspend == 1
53 // Tasks that are currently suspended.
54 extern "C" PRIVILEGED_DATA List_t xSuspendedTaskList;
55 #endif // INCLUDE_vTaskSuspend == 1
56
57 namespace pw::thread::freertos {
58 namespace {
59
ForEachThreadInList(List_t * list,const eTaskState default_list_state,const ThreadCallback & cb)60 Status ForEachThreadInList(List_t* list,
61 const eTaskState default_list_state,
62 const ThreadCallback& cb) {
63 if (listCURRENT_LIST_LENGTH(list) == 0) {
64 return OkStatus();
65 }
66
67 Status status = OkStatus();
68 // Note that these are pointers to the thread control blocks, however the
69 // list macros from FreeRTOS do not cast the types and ergo we use void *.
70 void* current_thread;
71 void* first_thread_in_list;
72 listGET_OWNER_OF_NEXT_ENTRY(first_thread_in_list, list);
73 do {
74 listGET_OWNER_OF_NEXT_ENTRY(current_thread, list);
75 // We must finish the list iteration to restore the list state, but
76 // we want to stop invoking callbacks upon the first failure.
77 if (status.ok()) {
78 // Note that the lists do not contain the running state, so instead
79 // check for each thread whether it is currently running.
80 const TaskHandle_t current_thread_handle =
81 reinterpret_cast<TaskHandle_t>(current_thread);
82 if (!cb(current_thread_handle,
83 current_thread_handle == pxCurrentTCB ? eRunning
84 : default_list_state)) {
85 status = Status::Aborted();
86 }
87 }
88 } while (current_thread != first_thread_in_list);
89 return status;
90 }
91
92 } // namespace
93
ForEachThread(const ThreadCallback & cb)94 Status ForEachThread(const ThreadCallback& cb) {
95 if (xSchedulerRunning == pdFALSE) {
96 return Status::FailedPrecondition();
97 }
98
99 for (size_t i = 0; i < configMAX_PRIORITIES; ++i) {
100 PW_TRY(ForEachThreadInList(&pxReadyTasksLists[i], eReady, cb));
101 }
102 PW_TRY(ForEachThreadInList(pxDelayedTaskList, eBlocked, cb));
103 PW_TRY(ForEachThreadInList(pxOverflowDelayedTaskList, eBlocked, cb));
104 #if INCLUDE_vTaskDelete == 1
105 PW_TRY(ForEachThreadInList(&xTasksWaitingTermination, eDeleted, cb));
106 #endif // INCLUDE_vTaskDelete == 1
107 #if INCLUDE_vTaskSuspend == 1
108 PW_TRY(ForEachThreadInList(&xSuspendedTaskList, eSuspended, cb));
109 #endif // INCLUDE_vTaskSuspend == 1
110 return OkStatus();
111 }
112
113 } // namespace pw::thread::freertos
114