MTQueue.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  * MTQueue.h
33  *
34  * Created on: Oct 13, 2014
35  * Author: Kamil Saigol
36  */
42 #ifndef CORE_MTQUEUE_H_
43 #define CORE_MTQUEUE_H_
44 
45 #include <queue>
46 #include "systemc.h"
47 
48 template <typename T>
49 class MTQueue {
50  public:
54  MTQueue() : /*sem_(1),*/ queue_(),
55  mutex_(sc_gen_unique_name("mutex_")),
56  cond_(sc_gen_unique_name("cond_")) {
57  }
62  T pop() {
63  while (queue_.empty()) {
64  wait(cond_);
65  }
66  mutex_.lock();
67  // sem_.wait();
68  auto item = queue_.front();
69  queue_.pop();
70  mutex_.unlock();
71  // sem_.post();
72  return item;
73  }
74 
79  void pop(T& item) {
80  while (queue_.empty()) {
81  wait(cond_);
82  }
83  mutex_.lock();
84  // sem_.wait();
85  item = queue_.front();
86  queue_.pop();
87  mutex_.unlock();
88  // sem_.post();
89  }
90 
95  void push(const T& item) {
96  mutex_.lock();
97  // sem_.wait();
98  queue_.push(item);
99  // sem_.post();
100  mutex_.unlock();
101  cond_.notify();
102  }
103 
108  void push(T&& item) {
109  mutex_.lock();
110  // sem_.wait();
111  queue_.push(std::move(item));
112  // sem_.post();
113  mutex_.unlock();
114  cond_.notify();
115  }
116  /*
117  * Return Size of Queue
118  * @param none
119  */
120  void size(int& qsize) {
121  mutex_.lock();
122  qsize = queue_.size();
123  mutex_.unlock();
124  }
125 
126  private:
127  std::queue<T> queue_;
128  sc_mutex mutex_;
129  sc_event cond_;
130  // sc_semaphore sem_;
131 };
132 
133 #endif // CORE_MTQUEUE_H_
A multiple-producer, multiple-consumer, thread-safe queue implemented using SystemC primitives The MT...
Definition: MTQueue.h:49
void push(T &&item)
Push an item onto the MTQueue (explicit move)
Definition: MTQueue.h:108
T pop()
Pop the top element from the MTQueue and return a copy.
Definition: MTQueue.h:62
void pop(T &item)
Pop the top element from the MTQueue and copy it into the output argument.
Definition: MTQueue.h:79
void push(const T &item)
Push an item onto the MTQueue.
Definition: MTQueue.h:95
MTQueue()
Construct a MTQueue.
Definition: MTQueue.h:54