• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "profiler_data_repeater.h"
16 
ProfilerDataRepeater(size_t maxSize)17 ProfilerDataRepeater::ProfilerDataRepeater(size_t maxSize)
18 {
19     maxSize_ = maxSize;
20     closed_ = false;
21 }
22 
~ProfilerDataRepeater()23 ProfilerDataRepeater::~ProfilerDataRepeater()
24 {
25     Close();
26 }
27 
Size()28 size_t ProfilerDataRepeater::Size()
29 {
30     std::unique_lock<std::mutex> lock(mutex_);
31     return dataQueue_.size();
32 }
33 
Reset()34 void ProfilerDataRepeater::Reset()
35 {
36     std::unique_lock<std::mutex> lock(mutex_);
37     closed_ = false;
38 }
39 
Close()40 void ProfilerDataRepeater::Close()
41 {
42     {
43         std::unique_lock<std::mutex> lock(mutex_);
44         dataQueue_.clear();
45         closed_ = true;
46     }
47     slotCondVar_.notify_all();
48     itemCondVar_.notify_all();
49 }
50 
PutPluginData(const ProfilerPluginDataPtr & pluginData)51 bool ProfilerDataRepeater::PutPluginData(const ProfilerPluginDataPtr& pluginData)
52 {
53     std::unique_lock<std::mutex> lock(mutex_);
54 
55     if ((pluginData == nullptr) && (dataQueue_.size() > 0)) {
56         HILOG_INFO(LOG_CORE, "no need put nullptr if queue has data, dataQueue_.size() = %zu", dataQueue_.size());
57         return true;
58     }
59 
60     while (dataQueue_.size() >= maxSize_ && !closed_) {
61         slotCondVar_.wait(lock);
62     }
63     if (closed_) {
64         return false;
65     }
66 
67     dataQueue_.push_back(pluginData);
68     lock.unlock();
69 
70     itemCondVar_.notify_one();
71     return true;
72 }
73 
TakePluginData()74 ProfilerPluginDataPtr ProfilerDataRepeater::TakePluginData()
75 {
76     std::unique_lock<std::mutex> lock(mutex_);
77     while (dataQueue_.empty() && !closed_) {
78         itemCondVar_.wait(lock);
79     }
80     if (closed_) {
81         return nullptr;
82     }
83 
84     auto result = dataQueue_.front();
85     dataQueue_.pop_front();
86     lock.unlock();
87 
88     slotCondVar_.notify_one();
89     return result;
90 }
91 
TakePluginData(std::vector<ProfilerPluginDataPtr> & pluginDataVec)92 int ProfilerDataRepeater::TakePluginData(std::vector<ProfilerPluginDataPtr>& pluginDataVec)
93 {
94     std::unique_lock<std::mutex> lock(mutex_);
95     while (dataQueue_.empty() && !closed_) {
96         itemCondVar_.wait(lock);
97     }
98     if (closed_) {
99         return -1;
100     }
101 
102     int count = 0;
103     while (dataQueue_.size() > 0) {
104         auto result = dataQueue_.front();
105         pluginDataVec.push_back(result);
106         dataQueue_.pop_front();
107         count++;
108     }
109     lock.unlock();
110 
111     slotCondVar_.notify_one();
112     return count;
113 }
114 
ClearQueue()115 void ProfilerDataRepeater::ClearQueue()
116 {
117     std::unique_lock<std::mutex> lock(mutex_);
118     dataQueue_.clear();
119 }
120