xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/xcore/safe_queue.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * safe_queue.h - safe queue template
3  *
4  *  Copyright (c) 2022 Rockchip ISP Team
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Author: Cody Xie <cody.xie@rock-chips.com>
19  */
20 
21 #ifndef ROCKCHIP_ISP_SAFE_QUEUE_H
22 #define ROCKCHIP_ISP_SAFE_QUEUE_H
23 
24 #include <condition_variable>
25 #include <mutex>
26 #include <queue>
27 
28 namespace RkCam {
29 
30 template <typename T>
31 class SafeQueue {
32  public:
33     SafeQueue()  = default;
34     ~SafeQueue() = default;
35 
push(T && t)36     void push(T&& t) {
37         {
38             std::lock_guard<std::mutex> lk(mtx_);
39             q_.push_back(t);
40         }
41         cv_.notify_one();
42     }
43 
front()44     T& front() {
45         std::unique_lock<std::mutex> lk(mtx_);
46         cv_.wait(lk, [&]() { return !q_.empty(); });
47         return q_.front();
48     }
49 
pop()50     void pop() {
51         std::lock_guard<std::mutex> lk(mtx_);
52         q_.pop_front();
53     }
54 
clear()55     void clear() {
56         std::lock_guard<std::mutex> lk(mtx_);
57         q_.clear();
58     }
59 
60  private:
61     std::deque<T> q_;
62     std::mutex mtx_;
63     std::condition_variable cv_;
64 };
65 
66 }  // namespace RkCam
67 
68 #endif  // ROCKCHIP_ISP_SAFE_QUEUE_H
69