~aleteoryx/muditaos

ref: 5c2032ae929d48b62c2bcd9c0a27a38970350bfb muditaos/module-services/service-gui/DrawCommandsQueue.cpp -rw-r--r-- 1.9 KiB
5c2032ae — Maciej-Mudita [MOS-836] Fix for selecting SIM during onboarding 3 years 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "DrawCommandsQueue.hpp"

namespace service::gui
{
    namespace
    {
        constexpr std::chrono::milliseconds WaitTimeout{5000};
    } // namespace

    DrawCommandsQueue::DrawCommandsQueue(std::size_t expectedSize,
                                         std::unique_ptr<SynchronizationMechanism> &&synchronization)
        : synchronization{std::move(synchronization)}
    {
        queue.reserve(expectedSize);
    }

    void DrawCommandsQueue::enqueue(QueueItem &&item)
    {
        cpp_freertos::LockGuard lock{queueMutex};
        queue.push_back(std::move(item));

        synchronization->notify();
    }

    auto DrawCommandsQueue::dequeue() -> QueueItem
    {
        cpp_freertos::LockGuard lock{queueMutex};
        synchronization->wait(queueMutex, WaitTimeout, [this]() { return !queue.empty(); });

        auto item = std::move(queue.front());
        queue.erase(queue.begin());
        return item;
    }

    auto DrawCommandsQueue::getMaxRefreshModeAndClear() -> ::gui::RefreshModes
    {
        cpp_freertos::LockGuard lock{queueMutex};
        const auto deepRefreshRequested = std::any_of(queue.begin(), queue.end(), [](const auto &item) {
            return item.refreshMode == ::gui::RefreshModes::GUI_REFRESH_DEEP;
        });
        const auto maxRefreshMode =
            deepRefreshRequested ? ::gui::RefreshModes::GUI_REFRESH_DEEP : ::gui::RefreshModes::GUI_REFRESH_FAST;

        queue.clear();
        return maxRefreshMode;
    }

    void DrawCommandsQueue::clear()
    {
        cpp_freertos::LockGuard lock{queueMutex};
        queue.clear();
    }

    auto DrawCommandsQueue::size() const noexcept -> QueueContainer::size_type
    {
        cpp_freertos::LockGuard lock{queueMutex};
        return queue.size();
    }
} // namespace service::gui