~aleteoryx/muditaos

ref: 5f03b9148ed78d558e139b8a3d563c6f08f33447 muditaos/module-sys/Service/CpuSentinel.hpp -rw-r--r-- 1.6 KiB
5f03b914 — Marcin Smoczyński [EGD-7641] Disable service desktop tests 4 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <string>
#include <functional>
#include <bsp/common.hpp>
#include "Service.hpp"

namespace sys
{
    /// Each sentinel manages the requests, i.e. when it is needed it sends messages to CpuGovernor with the required
    /// minimum CPU frequency to perform the task (e.g. screen redraw). Furthermore, every sentinel is informed
    /// immediately after changing the frequency. This allows it to invoke a callback to the service to update their
    /// resources (e.g. PWM filling). Every sentinel must register itself on startup to CpuGovernor by sending
    /// "SentinelRegistrationMessage".
    class CpuSentinel
    {
      public:
        explicit CpuSentinel(std::string name,
                             sys::Service *service,
                             std::function<void(bsp::CpuFrequencyHz)> callback = nullptr);
        ~CpuSentinel() = default;

        [[nodiscard]] auto GetName() const noexcept -> std::string;
        void HoldMinimumFrequency(bsp::CpuFrequencyHz frequencyToHold);
        void ReleaseMinimumFrequency();
        void CpuFrequencyHasChanged(bsp::CpuFrequencyHz newFrequency);

      protected:
        const std::string name;
        bsp::CpuFrequencyHz currentFrequencyToHold{bsp::CpuFrequencyHz::Level_1};
        sys::Service *owner{nullptr};

        /// function called from the PowerManager context
        /// to update resources immediately
        /// critical section or mutex support necessary
        std::function<void(bsp::CpuFrequencyHz)> callback;
    };

} // namespace sys