~aleteoryx/muditaos

muditaos/module-sys/SystemManager/GovernorSentinelOperations.cpp -rw-r--r-- 1.8 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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include "GovernorSentinelOperations.hpp"

namespace sys
{

    void for_each_governed_sentinel(GovernorSentinelsVector &sentinels, governed_callback &foo)
    {
        for (auto sentinel = sentinels.rbegin(); sentinels.rend() != sentinel; ++sentinel) {
            // remove dangling unique ptrs
            if (not *sentinel) {
                sentinels.erase(sentinel.base());
                continue;
            }
            auto sentinelWeakPointer = (*sentinel)->GetSentinel();
            // remove expired ptrs
            if (sentinelWeakPointer.expired()) {
                // shift iterator to point on the correct successor,
                // without std::next it removes (sentinel - 1) element
                sentinels.erase(std::next(sentinel).base());
                continue;
            }
            // call foo on sentinel
            if (foo(**sentinel)) {
                return;
            };
        }
    };

    void for_each_sentinel(GovernorSentinelsVector &sentinels, sentinel_foo &foo)
    {
        governed_callback cb = [&](const GovernorSentinel &sentinel) {
            std::shared_ptr<CpuSentinel> sharedResource = sentinel.GetSentinel().lock();
            if (sharedResource) {
                if (foo(sharedResource)) {
                    return true;
                }
            }
            return false;
        };
        for_each_governed_sentinel(sentinels, cb);
    };

    void clean(GovernorSentinelsVector &sentinels)
    {
        governed_callback empty_governed_callback = [&](const GovernorSentinel &) { return false; };
        for_each_governed_sentinel(sentinels, empty_governed_callback);
    }
} // namespace sys