LCOV - code coverage report
Current view: top level - adcore/common - thread.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 76.0 % 50 38
Test Date: 2026-08-12 11:04:53 Functions: 76.9 % 13 10

            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              : #include "thread.h"
      11              : namespace Adx {
      12              : static const int32_t WAIT_TID_TIME = 500;
      13              : /**
      14              :  * @brief create thread with default attributes
      15              :  * @param [out]tid : thread id
      16              :  * @param [int]funcBlock : function name and parameter
      17              :  *
      18              :  * @return
      19              :  *        EN_OK: succ
      20              :  *        other: failed
      21              :  */
      22           10 : int32_t Thread::CreateTaskWithDefaultAttr(mmThread& tid, mmUserBlock_t& funcBlock)
      23              : {
      24           10 :     mmThreadAttr threadAttr = IDE_DAEMON_DEFAULT_THREAD_ATTR;
      25           20 :     return mmCreateTaskWithThreadAttr(&tid, &funcBlock, &threadAttr);
      26              : }
      27              : 
      28              : /**
      29              :  * @brief create detach thread with default attributes
      30              :  * @param [out] tid : thread id
      31              :  * @param [in]  funcBlock : function name and parameter
      32              :  *
      33              :  * @return
      34              :  *        EN_OK: succ
      35              :  *        other: failed
      36              :  */
      37           15 : int32_t Thread::CreateDetachTaskWithDefaultAttr(mmThread& tid, mmUserBlock_t& funcBlock)
      38              : {
      39           15 :     mmThreadAttr threadAttr = IDE_DAEMON_DEFAULT_DETACH_THREAD_ATTR;
      40           30 :     return mmCreateTaskWithThreadAttr(&tid, &funcBlock, &threadAttr);
      41              : }
      42              : 
      43           87 : Runnable::Runnable() : tid_(0), quit_(false), isStarted_(false), threadName_("adx") {}
      44              : 
      45           29 : Runnable::~Runnable() { Stop(); }
      46              : 
      47           10 : int32_t Runnable::Start()
      48              : {
      49           10 :     if (isStarted_) {
      50            0 :         return IDE_DAEMON_ERROR;
      51              :     }
      52              : 
      53              :     mmUserBlock_t funcBlock;
      54           10 :     funcBlock.procFunc = Runnable::Process;
      55           10 :     funcBlock.pulArg = this;
      56           10 :     quit_ = false;
      57           10 :     int32_t ret = Thread::CreateTaskWithDefaultAttr(tid_, funcBlock);
      58           10 :     if (ret != EN_OK) {
      59            0 :         tid_ = 0;
      60            0 :         return IDE_DAEMON_ERROR;
      61              :     }
      62           10 :     isStarted_ = true;
      63           10 :     return IDE_DAEMON_OK;
      64              : }
      65              : 
      66           10 : int32_t Runnable::Terminate()
      67              : {
      68           10 :     quit_ = true;
      69           10 :     while (tid_ != 0) {
      70            0 :         mmSleep(WAIT_TID_TIME);
      71              :     }
      72           10 :     isStarted_ = false;
      73           10 :     return IDE_DAEMON_OK;
      74              : }
      75              : 
      76           29 : int32_t Runnable::Stop()
      77              : {
      78           29 :     quit_ = true;
      79           29 :     if (isStarted_) {
      80            1 :         isStarted_ = false;
      81            1 :         return Join();
      82              :     }
      83              : 
      84           28 :     return IDE_DAEMON_OK;
      85              : }
      86              : 
      87            2 : int32_t Runnable::Join()
      88              : {
      89            2 :     if (tid_ != 0) {
      90            0 :         int32_t ret = mmJoinTask(&tid_);
      91            0 :         if (ret != EN_OK) {
      92            0 :             return IDE_DAEMON_ERROR;
      93              :         }
      94            0 :         isStarted_ = false;
      95            0 :         tid_ = 0;
      96              :     }
      97              : 
      98            2 :     return IDE_DAEMON_OK;
      99              : }
     100              : 
     101            0 : bool Runnable::IsQuit() const { return quit_; }
     102              : 
     103            1 : void Runnable::SetThreadName(const std::string& name) { threadName_ = name; }
     104              : 
     105            0 : const std::string& Runnable::GetThreadName() const { return threadName_; }
     106              : 
     107            9 : IdeThreadArg Runnable::Process(IdeThreadArg arg)
     108              : {
     109            9 :     if (arg == nullptr) {
     110            0 :         return nullptr;
     111              :     }
     112            9 :     auto runnable = reinterpret_cast<Runnable*>(arg);
     113            9 :     (void)mmSetCurrentThreadName(runnable->threadName_.c_str());
     114            9 :     runnable->Run();
     115            9 :     return nullptr;
     116              : }
     117              : } // namespace Adx
        

Generated by: LCOV version 2.0-1