LMTQueue.h
1 /*
2  * PFPSim: Library for the Programmable Forwarding Plane Simulation Framework
3  *
4  * Copyright (C) 2016 Concordia Univ., Montreal
5  * Samar Abdi
6  * Umair Aftab
7  * Gordon Bailey
8  * Faras Dewal
9  * Shafigh Parsazad
10  * Eric Tremblay
11  *
12  * Copyright (C) 2016 Ericsson
13  * Bochra Boughzala
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28  * 02110-1301, USA.
29  */
30 
31 /*
32  * LMTQueue.h
33  *
34  * Created on: Oct 13, 2014
35  * Author: Kamil Saigol
36  */
42 #ifndef CORE_LMTQUEUE_H_
43 #define CORE_LMTQUEUE_H_
44 
45 #include <queue>
46 #include "systemc.h"
47 
48 template <typename T, size_t N>
49 class LMTQueue {
50  public:
55  : queue_(), mutex_(sc_gen_unique_name("mutex_")),
56  cond_not_empty_(sc_gen_unique_name("cond_not_empty_")), slots_(0) {
57  }
62  T pop() {
63  while (queue_.empty()) {
64  wait(cond_not_empty_);
65  }
66  mutex_.lock();
67  auto item = queue_.front();
68  queue_.pop();
69  slots_--;
70  mutex_.unlock();
71  cond_not_full_.notify();
72  return item;
73  }
74 
79  void pop(T& item) {
80  while (queue_.empty()) {
81  wait(cond_not_empty_);
82  }
83  mutex_.lock();
84  item = queue_.front();
85  queue_.pop();
86  slots_--;
87  mutex_.unlock();
88  cond_not_full_.notify();
89  }
90 
95  void push(const T& item) {
96  while (this->full()) {
97  wait(cond_not_full_);
98  }
99  mutex_.lock();
100  queue_.push(item);
101  slots_++;
102  mutex_.unlock();
103  cond_not_empty_.notify();
104  }
105 
110  void push(T&& item) {
111  while (this->full()) {
112  wait(cond_not_full_);
113  }
114  mutex_.lock();
115  queue_.push(std::move(item));
116  slots_++;
117  mutex_.unlock();
118  cond_not_empty_.notify();
119  }
120 
121  size_t available() const {
122  return N - slots_;
123  }
124 
125  bool full() const {
126  return (slots_ == N);
127  }
128 
129  bool empty() const {
130  return queue_.empty();
131  }
132 
133  private:
134  std::queue<T> queue_;
135  sc_mutex mutex_;
136  sc_event cond_not_empty_, cond_not_full_;
138  size_t slots_;
139 };
140 
141 #endif // CORE_LMTQUEUE_H_
A size-limited multiple-producer, multiple-consumer, thread-safe queue implemented using SystemC prim...
Definition: LMTQueue.h:49
void push(const T &item)
Push an item onto the LMTQueue.
Definition: LMTQueue.h:95
void pop(T &item)
Pop the top element from the LMTQueue and copy it into the output argument.
Definition: LMTQueue.h:79
void push(T &&item)
Push an item onto the LMTQueue (explicit move)
Definition: LMTQueue.h:110
T pop()
Pop the top element from the LMTQueue and return a copy.
Definition: LMTQueue.h:62
LMTQueue()
Construct a LMTQueue.
Definition: LMTQueue.h:54