2 * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef TIZEN_SHARED_QUEUE_SHARED_QUEUE_HPP_
18 #define TIZEN_SHARED_QUEUE_SHARED_QUEUE_HPP_
20 #include <condition_variable>
27 namespace tizen_base {
32 SharedQueue() = default;
33 virtual ~SharedQueue() = default;
36 std::lock_guard<std::mutex> lock(mutex_);
38 cond_var_.notify_one();
41 bool TryAndPop(T& item) {
42 std::lock_guard<std::mutex> lock(mutex_);
46 item = queue_.front();
52 std::unique_lock<std::mutex> lock(mutex_);
53 while (queue_.empty())
56 auto item = std::move(queue_.front());
61 bool WaitAndPopFor(T& item, int timeout) {
62 std::unique_lock<std::mutex> lock(mutex_);
63 if (!queue_.empty()) {
64 item = queue_.front();
69 std::chrono::milliseconds duration(timeout);
70 if (cond_var_.wait_for(lock, duration) == std::cv_status::timeout)
73 item = queue_.front();
78 bool IsEmpty() const {
79 std::lock_guard<std::mutex> lock(mutex_);
80 return queue_.empty();
83 unsigned int Size() const {
84 std::lock_guard<std::mutex> lock(mutex_);
90 mutable std::mutex mutex_;
91 std::condition_variable cond_var_;
94 } // namespace tizen_base
96 #endif // TIZEN_SHARED_QUEUE_SHARED_QUEUE_HPP_