~aleteoryx/muditaos

ref: master muditaos/module-services/service-gui/ContextPool.cpp -rw-r--r-- 3.2 KiB
2cd0e472 — Lefucjusz [BH-000] Update Harmony 2.10.0 changelog 2 months 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// 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 "ContextPool.hpp"

#include <mutex.hpp>

#include <algorithm>

namespace service::gui
{
    using namespace std::literals::chrono_literals;

    namespace
    {
        std::unique_ptr<::gui::Context> allocateContext(::gui::Size screenSize)
        {
            return std::make_unique<::gui::Context>(screenSize.width, screenSize.height);
        }

        constexpr auto DefaultWaitTime = 5000ms;
    } // namespace

    ContextPool::ContextPool(::gui::Size screenSize,
                             std::size_t capacity,
                             std::unique_ptr<SynchronizationMechanism> &&synchronization)
        : synchronization{std::move(synchronization)}
    {
        contexts.reserve(capacity);
        freeContextIds.reserve(capacity);
        lockedContextIds.reserve(capacity);

        for (std::size_t i = 0; i < capacity; ++i) {
            contexts.push_back(allocateContext(screenSize));
            freeContextIds.push_back(i);
        }
    }

    auto ContextPool::peekContext(int id) noexcept -> ::gui::Context *
    {
        return contexts[id].get();
    }

    auto ContextPool::borrowContext() -> std::pair<int, ::gui::Context *>
    {
        cpp_freertos::LockGuard lock(mutex);
        synchronization->wait(mutex, DefaultWaitTime, [this]() { return isAvailable(); });

        const auto contextId = freeContextIds.front();
        lockContext(contextId);
        return std::make_pair(contextId, contexts[contextId].get());
    }

    auto ContextPool::borrowContext(int id) -> ::gui::Context *
    {
        cpp_freertos::LockGuard lock(mutex);
        const auto it = std::find(freeContextIds.begin(), freeContextIds.end(), id);
        if (it == freeContextIds.end()) {
            return nullptr;
        }

        lockContext(id);
        return contexts[id].get();
    }

    auto ContextPool::isAnyContextLocked() const -> bool
    {
        cpp_freertos::LockGuard lock(mutex);
        return !lockedContextIds.empty();
    }

    auto ContextPool::isAvailable() const noexcept -> bool
    {
        return !freeContextIds.empty();
    }

    void ContextPool::lockContext(int contextId)
    {
        removeContextId(freeContextIds, contextId);
        addContextId(lockedContextIds, contextId);
    }

    void ContextPool::returnContext(int id)
    {
        cpp_freertos::LockGuard lock(mutex);
        freeContext(id);

        synchronization->notify();
    }

    void ContextPool::freeContext(int contextId)
    {
        removeContextId(lockedContextIds, contextId);
        addContextId(freeContextIds, contextId);
    }

    void ContextPool::removeContextId(std::vector<int> &sequence, int contextId)
    {
        sequence.erase(std::remove(sequence.begin(), sequence.end(), contextId), sequence.end());
    }

    void ContextPool::addContextId(std::vector<int> &sequence, int contextId)
    {
        if (const auto it = std::find(sequence.begin(), sequence.end(), contextId); it == sequence.end()) {
            // Add if not found.
            sequence.push_back(contextId);
        }
    }
} // namespace service::gui