LCOV - code coverage report
Current view: top level - /opt/cloud/slavespace/usr1/096471637100f3de0fcfc01072822a80/ut/pkg_inc/watchdog - awatchdog.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 78.9 % 19 15
Test Date: 2026-07-28 10:53:44 Functions: 100.0 % 3 3

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : #ifndef AWATCHDOG_H
      11              : #define AWATCHDOG_H
      12              : #include "awatchdog_types.h"
      13              : 
      14              : #define AWD_ATOMIC_FETCH_AND_ADD(ptr, value)        ((__typeof__(*(ptr)))__sync_fetch_and_add((ptr), (value)))
      15              : #define AWD_ATOMIC_SUB_AND_FETCH(ptr, value)        ((__typeof__(*(ptr)))__sync_sub_and_fetch((ptr), (value)))
      16              : #define AWD_ATOMIC_TEST_AND_SET(ptr, value)          ((void)__sync_lock_test_and_set((ptr), (value)))
      17              : #define AWD_ATOMIC_CMP_AND_SWAP(ptr, comp, value)   (__sync_bool_compare_and_swap((ptr), (comp), (value)))
      18              : 
      19              : #ifdef __cplusplus
      20              : extern "C" {
      21              : #endif // __cplusplus
      22              : 
      23              : typedef enum AwdWatchdogType {
      24              :     AWD_WATCHDOG_TYPE_THREAD = 0,
      25              :     AWD_WATCHDOG_TYPE_MAX,
      26              : } AwdWatchdogType;
      27              : 
      28              : #define DEFINE_THREAD_WATCHDOG(name)        static __thread AwdHandle name = -1
      29              : #define WATCHDOG_TYPE_BIT 16U
      30              : #define DEFINE_THREAD_WATCHDOG_ID(moduleId)  \
      31              :     ((uint32_t)moduleId) | ((uint32_t)1U << WATCHDOG_TYPE_BIT) | ((uint32_t)AWD_WATCHDOG_TYPE_THREAD)
      32              : 
      33              : /*
      34              :  * @brief       Create thread watchdog
      35              :  * @param [in]  dogId:    dog id get from DEFINE_THREAD_WATCHDOG_ID(moduleId)
      36              :  * @param [in]  timeout:  watchdog timeout, s
      37              :  * @param [in]  callback: watchdog callback function, will be called when timeout
      38              :  * @return      watchdog handle, AWD_INVALID_HANDLE for failure
      39              :  */
      40              : AWD_EXPORT AwdHandle AwdCreateThreadWatchdog(uint32_t dogId, uint32_t timeout, AwatchdogCallbackFunc callback);
      41              : 
      42              : /*
      43              :  * @brief       Destroy thread watchdog. if not called, watch dog will be destroyed when thread is detected no exist.
      44              :  * @param [in]  handle:   watchdog handle
      45              :  * @return      NA
      46              :  */
      47              : AWD_EXPORT void AwdDestroyThreadWatchdog(AwdHandle handle);
      48              : 
      49              : /*
      50              :  * @brief       start watchdog
      51              :  * @param [in]  handle:   watchdog handle
      52              :  * @return      AwdStatus
      53              :  */
      54            3 : static inline AwdStatus AwdStartThreadWatchdog(const AwdHandle handle)
      55              : {
      56            3 :     if (handle == AWD_INVALID_HANDLE) {
      57            3 :         return AWD_FAILURE;
      58              :     }
      59            0 :     AwdThreadWatchdog *dog = (AwdThreadWatchdog *)handle;
      60            0 :     AWD_ATOMIC_TEST_AND_SET(&dog->runCount, 0);
      61            0 :     AWD_ATOMIC_TEST_AND_SET(&dog->startCount, AWD_STATUS_STARTED);
      62            0 :     return AWD_SUCCESS;
      63              : }
      64              : 
      65              : /*
      66              :  * @brief       feed watchdog
      67              :  * @param [in]  handle:   watchdog handle
      68              :  * @return      AwdStatus
      69              :  */
      70            2 : static inline AwdStatus AwdFeedThreadWatchdog(const AwdHandle handle)
      71              : {
      72            2 :     if (handle == AWD_INVALID_HANDLE) {
      73            1 :         return AWD_FAILURE;
      74              :     }
      75            1 :     AwdThreadWatchdog *dog = (AwdThreadWatchdog *)handle;
      76            1 :     AWD_ATOMIC_TEST_AND_SET(&dog->startCount, AWD_ATOMIC_FETCH_AND_ADD(&dog->runCount, 0));
      77            1 :     return AWD_SUCCESS;
      78              : }
      79              : 
      80              : /*
      81              :  * @brief       stop watchdog
      82              :  * @param [in]  handle:   watchdog handle
      83              :  * @return      AwdStatus
      84              :  */
      85            2 : static inline AwdStatus AwdStopThreadWatchdog(const AwdHandle handle)
      86              : {
      87            2 :     if (handle == AWD_INVALID_HANDLE) {
      88            1 :         return AWD_FAILURE;
      89              :     }
      90            1 :     AwdThreadWatchdog *dog = (AwdThreadWatchdog *)handle;
      91            1 :     AWD_ATOMIC_TEST_AND_SET(&dog->startCount, AWD_STATUS_INIT);
      92            1 :     return AWD_SUCCESS;
      93              : }
      94              : 
      95              : #ifdef __cplusplus
      96              : }
      97              : #endif // __cplusplus
      98              : 
      99              : #endif
        

Generated by: LCOV version 2.0-1