1 /** 2 * Copyright 2019-2022 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_CCSRC_INCLUDE_BACKEND_DATA_QUEUE_BLOCKING_QUEUE_H 18 #define MINDSPORE_CCSRC_INCLUDE_BACKEND_DATA_QUEUE_BLOCKING_QUEUE_H 19 20 #include <iostream> 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 #include <vector> 25 #include <condition_variable> 26 #include <functional> 27 #include "include/backend/data_queue/data_queue.h" 28 namespace mindspore { 29 namespace device { 30 class BACKEND_EXPORT BlockingQueue { 31 public: BlockingQueue()32 BlockingQueue() : queue_(nullptr) {} 33 ~BlockingQueue() = default; 34 35 DataQueueStatus Create(const std::shared_ptr<DataQueue> &data_queue); 36 void RegisterRelease(const std::function<void(void *, int32_t)> &func); 37 DataQueueStatus Push(const std::vector<DataQueueItem> &data, unsigned int timeout = 0); 38 DataQueueStatus Front(std::vector<DataQueueItem> *data); 39 DataQueueStatus FrontAsync(std::vector<DataQueueItem> *data); 40 DataQueueStatus Pop(); 41 DataQueueStatus Clear(); 42 void Close(); 43 bool IsOpen(); Size()44 size_t Size() { return queue_->Size(); } Capacity()45 size_t Capacity() { return queue_->Capacity(); } Queue()46 const std::shared_ptr<DataQueue> &Queue() const { return queue_; } 47 48 private: 49 std::mutex mutex_; 50 std::condition_variable not_full_cond_; 51 std::condition_variable not_empty_cond_; 52 std::shared_ptr<DataQueue> queue_; 53 const size_t kPushTimeoutMicroseconds = 100; 54 const size_t kPopTimeoutSeconds = 1; 55 }; 56 } // namespace device 57 } // namespace mindspore 58 #endif // MINDSPORE_CCSRC_INCLUDE_BACKEND_DATA_QUEUE_BLOCKING_QUEUE_H 59