• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 
16 #ifndef POWER_MESSAGE_QUEUE_H
17 #define POWER_MESSAGE_QUEUE_H
18 
19 #include <condition_variable>
20 #include <mutex>
21 #include <queue>
22 
23 #include "logging.h"
24 #include "xpower_common.h"
25 
26 
27 class PowerMessageQueue {
28 public:
29     explicit PowerMessageQueue(size_t maxSize);
30     ~PowerMessageQueue();
31     bool WaitAndPop(std::shared_ptr<PowerOptimizeData> &value, const std::chrono::microseconds rel_time);
32     bool WaitAndPopBatch(std::vector<std::shared_ptr<PowerOptimizeData>> &array,
33                          const std::chrono::microseconds realTime, size_t batchCount);
34     void PushBack(std::shared_ptr<PowerOptimizeData> &item);
35     void ShutDown();
36     size_t Size();
37     bool Empty();
38     bool IsShutDown();
39 
40 private:
41     std::deque<std::shared_ptr<PowerOptimizeData>> dataQueue_;
42     std::mutex mutex_;
43     std::condition_variable fullCon_;
44     std::condition_variable emptyCon_;
45     bool shutDown_ = false;
46     size_t maxSize_;
47 };
48 #endif // POWER_MESSAGE_QUEUE_H
49