• 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     while (dataQueue_.size() >= maxSize_ && !closed_) {
55         slotCondVar_.wait(lock);
56     }
57     if (closed_) {
58         return false;
59     }
60 
61     dataQueue_.push_back(pluginData);
62     lock.unlock();
63 
64     itemCondVar_.notify_one();
65     return true;
66 }
67 
TakePluginData()68 ProfilerPluginDataPtr ProfilerDataRepeater::TakePluginData()
69 {
70     std::unique_lock<std::mutex> lock(mutex_);
71     while (dataQueue_.empty() && !closed_) {
72         itemCondVar_.wait(lock);
73     }
74     if (closed_) {
75         return nullptr;
76     }
77 
78     auto result = dataQueue_.front();
79     dataQueue_.pop_front();
80     lock.unlock();
81 
82     slotCondVar_.notify_one();
83     return result;
84 }
85 
TakePluginData(std::vector<ProfilerPluginDataPtr> & pluginDataVec)86 int ProfilerDataRepeater::TakePluginData(std::vector<ProfilerPluginDataPtr>& pluginDataVec)
87 {
88     std::unique_lock<std::mutex> lock(mutex_);
89     while (dataQueue_.empty() && !closed_) {
90         itemCondVar_.wait(lock);
91     }
92     if (closed_) {
93         return -1;
94     }
95 
96     int count = 0;
97     while (dataQueue_.size() > 0) {
98         auto result = dataQueue_.front();
99         pluginDataVec.push_back(result);
100         dataQueue_.pop_front();
101         count++;
102     }
103     lock.unlock();
104 
105     slotCondVar_.notify_one();
106     return count;
107 }
108