Loading...
Searching...
No Matches
blocking_queue.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. 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,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21
22#include <glog/logging.h>
23
24#include <condition_variable>
25#include <queue>
26
27#include "absl/status/statusor.h"
28
29namespace resdb {
30
31template <typename T>
33 public:
34 BlockingQueue() = default;
35 BlockingQueue(const std::string& name) : name_(name) {}
36 void Push(T&& data) {
37 std::lock_guard<std::mutex> lk(mutex_);
38 queue_.push(std::move(data));
39 cv_.notify_all();
40 }
41
42 void Push(T& data) {
43 std::lock_guard<std::mutex> lk(mutex_);
44 queue_.push(std::move(data));
45 cv_.notify_all();
46 }
47
48 absl::StatusOr<T*> Front() {
49 std::unique_lock<std::mutex> lk(mutex_);
50 if (queue_.empty()) {
51 return nullptr;
52 }
53 return &queue_.front();
54 }
55
56 T Pop() {
57 std::unique_lock<std::mutex> lk(mutex_);
58 cv_.wait_for(lk, std::chrono::microseconds(timeout_ms_),
59 [&] { return !queue_.empty(); });
60
61 if (queue_.empty()) {
62 return nullptr;
63 }
64 auto resp = std::move(queue_.front());
65 queue_.pop();
66 return resp;
67 }
68
69 T Pop(int timeout_ms) {
70 std::unique_lock<std::mutex> lk(mutex_);
71 if (timeout_ms > 0) {
72 cv_.wait_for(lk, std::chrono::microseconds(timeout_ms),
73 [&] { return !queue_.empty(); });
74 }
75 if (queue_.empty()) {
76 return nullptr;
77 }
78 auto resp = std::move(queue_.front());
79 queue_.pop();
80 return resp;
81 }
82
83 T PopWithSize(int timeout_ms, size_t size) {
84 std::unique_lock<std::mutex> lk(mutex_);
85 if (timeout_ms > 0) {
86 cv_.wait_for(lk, std::chrono::microseconds(timeout_ms),
87 [&] { return queue_.size() >= size; });
88 }
89 if (queue_.empty()) {
90 return nullptr;
91 }
92 auto resp = std::move(queue_.front());
93 queue_.pop();
94 return resp;
95 }
96
97 private:
98 std::string name_;
99 std::condition_variable cv_;
100 std::mutex mutex_;
101 // std::queue<T> queue_ GUARDED_BY(mutex_);
102 std::queue<T> queue_;
103 int64_t timeout_ms_ = 500; // microsecond for timeout.
104};
105
106} // namespace resdb
std::string data
T PopWithSize(int timeout_ms, size_t size)
absl::StatusOr< T * > Front()
BlockingQueue(const std::string &name)
void Push(T &&data)
T Pop(int timeout_ms)