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 <new>
11 : #include <mutex>
12 : #include <string>
13 : #include <unistd.h>
14 : #include "aicpusd_lastword.h"
15 : #include "aicpusd_status.h"
16 :
17 : namespace AicpuSchedule {
18 35 : void AicpusdLastword::RegLastwordCallback(const std::string mark,
19 : std::function<void ()> callback, std::function<void ()> &cancelReg)
20 : {
21 35 : if (callback == nullptr) {
22 1 : aicpusd_run_info("Reg lastword mark[%s], callback is null.", mark.c_str());
23 1 : return;
24 : }
25 :
26 : uint64_t lastwordKey;
27 : {
28 34 : std::lock_guard<std::mutex> lk(lastwordMux_);
29 34 : lastwordKey = lastwordKey_;
30 34 : lastwords_.emplace(lastwordKey, std::make_pair(mark, callback));
31 34 : lastwordKey_++;
32 34 : }
33 :
34 68 : cancelReg = [lastwordKey, mark, this]() {
35 4 : std::lock_guard<std::mutex> lk(lastwordMux_);
36 4 : lastwords_.erase(lastwordKey);
37 38 : };
38 : }
39 :
40 18 : void AicpusdLastword::LastwordCallback()
41 : {
42 18 : std::lock_guard<std::mutex> lk(lastwordMux_);
43 18 : aicpusd_run_info("Call lastword callback, count[%zu].", lastwords_.size());
44 391 : for (auto it : lastwords_) {
45 373 : aicpusd_run_info("callback[%u][%s].", it.first, it.second.first.c_str());
46 373 : it.second.second();
47 373 : }
48 18 : }
49 : }
50 :
51 3 : __attribute__ ((constructor)) static void InitLastwordInstance()
52 : {
53 3 : AicpuSchedule::AicpusdLastword::GetInstance();
54 3 : }
|