1 /* 2 * Copyright (c) 2023 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 16 #ifndef JS_CONCURRENT_MODULE_TASKPOOL_MESSAGE_QUEUE_H 17 #define JS_CONCURRENT_MODULE_TASKPOOL_MESSAGE_QUEUE_H 18 19 #include <mutex> 20 #include <queue> 21 22 namespace Commonlibrary::Concurrent::TaskPoolModule { 23 template <typename MessageDataType> 24 class MessageQueue final { 25 public: EnQueue(MessageDataType data)26 void EnQueue(MessageDataType data) 27 { 28 std::lock_guard<std::mutex> lock(queueLock_); 29 queue_.push(data); 30 } 31 DeQueue()32 MessageDataType DeQueue() 33 { 34 std::lock_guard<std::mutex> lock(queueLock_); 35 if (queue_.empty()) { 36 return nullptr; 37 } 38 39 auto data = queue_.front(); 40 queue_.pop(); 41 return data; 42 } 43 IsEmpty()44 bool IsEmpty() 45 { 46 std::lock_guard<std::mutex> lock(queueLock_); 47 return queue_.empty(); 48 } 49 50 private: 51 std::mutex queueLock_; 52 std::queue<MessageDataType> queue_; 53 }; 54 } // namespace Commonlibrary::Concurrent::TaskPoolModule 55 #endif // JS_CONCURRENT_MODULE_TASKPOOL_MESSAGE_QUEUE_H