AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AWatchdog.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12#pragma once
   13
   14#include <AUI/Common/AObject.h>
   15#include <cassert>
   16#include <chrono>
   17#include "AUI/Common/AOptional.h"
   18#include "AUI/Thread/AMutex.h"
   19#include "AUI/Traits/concepts.h"
   20#include "AUI/Util/ARaiiHelper.h"
   21#include "AUI/Util/kAUI.h"
   22
   38class API_AUI_CORE AWatchdog {
   39public:
   43    AWatchdog(std::chrono::high_resolution_clock::duration hangDuration = std::chrono::seconds(10)): mHangDuration(hangDuration) {}
   44
   51    template<aui::invocable Operation>
   52    auto runOperation(Operation&& operation) -> decltype(operation()) {
   53        std::unique_lock lock(mSync);
   54        mBeginPoint = std::chrono::high_resolution_clock::now();
   55        AUI_DEFER {
   56            lock.lock();
   57            mBeginPoint.reset();
   58        };
   59        lock.unlock();
   60        return operation();
   61    }
   62
   67    [[nodiscard]]
   68    bool isHang() const noexcept;
   69    
   70private:
   71    mutable AMutex mSync;
   72    std::chrono::high_resolution_clock::duration mHangDuration;
   73    AOptional<std::chrono::high_resolution_clock::time_point> mBeginPoint;
   74};
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition AOptional.h:33
auto runOperation(Operation &&operation) -> decltype(operation())
Runs callback which is subject to check for hangs.
Definition AWatchdog.h:52
AWatchdog(std::chrono::high_resolution_clock::duration hangDuration=std::chrono::seconds(10))
Definition AWatchdog.h:43
bool isHang() const noexcept
Checks for hang state.
#define AUI_DEFER
Defers execution of the next block to the end of current block (RAII scope).
Definition kAUI.h:196
Basic syscall-based synchronization primitive.
Definition AMutex.h:33