LCOV - code coverage report
Current view: top level - adcore/common - thread.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 75.9 % 58 44
Test Date: 2026-07-28 10:54:24 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           26 : Runnable::Runnable()
      44           52 :     : tid_(0), quit_(false), isStarted_(false), threadName_("adx")
      45              : {
      46           26 : }
      47              : 
      48           26 : Runnable::~Runnable()
      49              : {
      50           26 :     Stop();
      51           26 : }
      52              : 
      53           10 : int32_t Runnable::Start()
      54              : {
      55           10 :     if (isStarted_) {
      56            0 :         return IDE_DAEMON_ERROR;
      57              :     }
      58              : 
      59              :     mmUserBlock_t funcBlock;
      60           10 :     funcBlock.procFunc = Runnable::Process;
      61           10 :     funcBlock.pulArg = this;
      62           10 :     quit_ = false;
      63           10 :     int32_t ret = Thread::CreateTaskWithDefaultAttr(tid_, funcBlock);
      64           10 :     if (ret != EN_OK) {
      65            0 :         tid_ = 0;
      66            0 :         return IDE_DAEMON_ERROR;
      67              :     }
      68           10 :     isStarted_ = true;
      69           10 :     return IDE_DAEMON_OK;
      70              : }
      71              : 
      72           10 : int32_t Runnable::Terminate()
      73              : {
      74           10 :     quit_ = true;
      75           10 :     while (tid_ != 0) {
      76            0 :         mmSleep(WAIT_TID_TIME);
      77              :     }
      78           10 :     isStarted_ = false;
      79           10 :     return IDE_DAEMON_OK;
      80              : }
      81              : 
      82           26 : int32_t Runnable::Stop()
      83              : {
      84           26 :     quit_ = true;
      85           26 :     if (isStarted_) {
      86            1 :         isStarted_ = false;
      87            1 :         return Join();
      88              :     }
      89              : 
      90           25 :     return IDE_DAEMON_OK;
      91              : }
      92              : 
      93            2 : int32_t Runnable::Join()
      94              : {
      95            2 :     if (tid_ != 0) {
      96            0 :         int32_t ret = mmJoinTask(&tid_);
      97            0 :         if (ret != EN_OK) {
      98            0 :             return IDE_DAEMON_ERROR;
      99              :         }
     100            0 :         isStarted_ = false;
     101            0 :         tid_ = 0;
     102              :     }
     103              : 
     104            2 :     return IDE_DAEMON_OK;
     105              : }
     106              : 
     107            0 : bool Runnable::IsQuit() const
     108              : {
     109            0 :     return quit_;
     110              : }
     111              : 
     112            1 : void Runnable::SetThreadName(const std::string &name)
     113              : {
     114            1 :     threadName_ = name;
     115            1 : }
     116              : 
     117            0 : const std::string &Runnable::GetThreadName() const
     118              : {
     119            0 :     return threadName_;
     120              : }
     121              : 
     122            9 : IdeThreadArg Runnable::Process(IdeThreadArg arg)
     123              : {
     124            9 :     if (arg == nullptr) {
     125            0 :         return nullptr;
     126              :     }
     127            9 :     auto runnable = reinterpret_cast<Runnable *>(arg);
     128            9 :     (void)mmSetCurrentThreadName(runnable->threadName_.c_str());
     129            9 :     runnable->Run();
     130            9 :     return nullptr;
     131              : }
     132              : }
        

Generated by: LCOV version 2.0-1