Android-cuttlefish cvd tool
multiplexer.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020 The Android Open Source Project
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#pragma once
18
19#include <functional>
20#include <memory>
21#include <vector>
22
24
25namespace cuttlefish {
26template <typename T, typename Queue>
28 public:
29 using QueuePtr = std::unique_ptr<Queue>;
30 using QueueSelector = std::function<int(void)>;
31
32 template <typename... Args>
33 static QueuePtr CreateQueue(Args&&... args) {
34 auto raw_ptr = new Queue(std::forward<Args>(args)...);
35 return QueuePtr(raw_ptr);
36 }
37
39
40 int RegisterQueue(QueuePtr&& queue) {
41 const int id_to_return = queues_.size();
42 queues_.push_back(std::move(queue));
43 return id_to_return;
44 }
45
46 void Push(const int idx, T&& t) {
47 CheckIdx(idx);
48 queues_[idx]->Push(std::move(t));
50 }
51
52 T Pop(QueueSelector selector) {
53 SemWait();
54 int q_id = selector();
55 CheckIdx(q_id); // check, if weird, will die there
56 QueuePtr& queue = queues_[q_id];
57 CHECK(queue) << "queue must not be null.";
58 return queue->Pop();
59 }
60
61 T Pop() {
62 auto default_selector = [this]() -> int {
63 for (int i = 0; i < queues_.size(); i++) {
64 if (!queues_[i]->IsEmpty()) {
65 return i;
66 }
67 }
68 return -1;
69 };
70 return Pop(default_selector);
71 }
72
73 bool IsEmpty(const int idx) { return queues_[idx]->IsEmpty(); }
74
76
77 private:
78 void CheckIdx(const int idx) {
79 CHECK(idx >= 0 && idx < queues_.size()) << "queues_ array out of bound";
80 }
81 // total items across the queues
83 std::vector<QueuePtr> queues_;
85};
86} // end of namespace cuttlefish
Definition: multiplexer.h:27
std::function< int(void)> QueueSelector
Definition: multiplexer.h:30
std::vector< QueuePtr > queues_
Definition: multiplexer.h:83
static QueuePtr CreateQueue(Args &&... args)
Definition: multiplexer.h:33
Semaphore sem_items_
Definition: multiplexer.h:82
void CheckIdx(const int idx)
Definition: multiplexer.h:78
QueuePtr null_ptr_
Definition: multiplexer.h:84
T Pop()
Definition: multiplexer.h:61
Multiplexer()
Definition: multiplexer.h:38
T Pop(QueueSelector selector)
Definition: multiplexer.h:52
int RegisterQueue(QueuePtr &&queue)
Definition: multiplexer.h:40
void Push(const int idx, T &&t)
Definition: multiplexer.h:46
bool IsEmpty(const int idx)
Definition: multiplexer.h:73
std::unique_ptr< Queue > QueuePtr
Definition: multiplexer.h:29
void SemWait()
Definition: multiplexer.h:75
Definition: semaphore.h:23
void SemPost()
Definition: semaphore.h:35
void SemWait()
Definition: semaphore.h:28
#define CHECK(x)
Definition: logging.h:251
Definition: alloc_utils.cpp:23
std::vector< std::string_view > Args
Definition: incremental.h:28