~aleteoryx/muditaos

muditaos/module-os/trace/DeletedTasks.cpp -rw-r--r-- 1.5 KiB
a405cad6Aleteoryx trim readme 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "DeletedTasks.hpp"
#include <FreeRTOS.h>
#include <mutex.hpp>
#include <task.h>
#include <cstdint>
#include <algorithm>

namespace
{
    std::vector<sys::DeletedTasks::DeletedTaskDetails_t> deletedTasks;
    cpp_freertos::MutexStandard mutex;
} // namespace

// Function called from FreeRTOS when the task is deleted, works on static data therefore it is necessary to ensure
// mutex synchronization
void trace_deleteTask(const char *name)
{
    TaskHandle_t xHandle;
    TaskStatus_t xTaskDetails;

    cpp_freertos::LockGuard lock(mutex);

    xHandle = xTaskGetHandle(name);
    if (xHandle != NULL) {
        vTaskGetInfo(xHandle, &xTaskDetails, pdFALSE, eInvalid);

        auto taskIt = std::find_if(std::begin(deletedTasks), std::end(deletedTasks), [xTaskDetails](auto &el) {
            return el.name == xTaskDetails.pcTaskName;
        });

        if (taskIt != std::end(deletedTasks)) {
            taskIt->tickIncrements += xTaskDetails.ulRunTimeCounter;
        }
        else {
            deletedTasks.push_back(
                {.name = xTaskDetails.pcTaskName, .tickIncrements = xTaskDetails.ulRunTimeCounter});
        }
    }
}

namespace sys
{
    void DeletedTasks::MigrateDeletedTasks(std::vector<DeletedTaskDetails_t> &task)
    {
        cpp_freertos::LockGuard lock(mutex);

        task.assign(deletedTasks.begin(), deletedTasks.end()); 
        deletedTasks.clear();
    }
}