1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
#include <iostream>
#include <unistd.h>
#include <math.h>
#include <pthread.h>
#include <semaphore.h>
#include <string.h>
#define noinline __attribute__((noinline))
#define END_TOKEN (1 << 30)
#define END 10000000L
#define NUM_CONSUMERS 1
#define NUM_PRODUCERS 1
#define QMAX 32
/** A producer-consumer queue. This structure creates a channel in which
two threads can communicate, one by enqueuing elements and another by
dequeuing elements. */
template<int MAX, typename T>
class Queue
{
public:
Queue(void);
~Queue(void);
void Push(T x);
T Pop(void);
protected:
/** Position of the last inserted element.*/
int head;
/** Position of the oldest element in the queue. */
int tail;
/** Semaphore that will block any attempt of dequeuing an element if the
queue is empty. */
sem_t empty;
/** Semaphore that will block any attempt of enqueuing an element if the
queue is full. */
sem_t full;
/** Lock for head & tail. This is unnecessary if there is only one producer
and one consumer. If you wish to support many producers and many
consumers, define MORE_THAN_ONE_ONE. */
pthread_mutex_t lock;
/** The queue buffer. */
T elem[MAX];
};
template<int MAX, typename T>
Queue<MAX, T>::Queue(void)
: head(0),
tail(0)
{
//memset(this, 0, sizeof(*this));
int ret;
ret = pthread_mutex_init(&lock, NULL);
if (ret != 0) {
throw "failed initializing mutex lock";
}
ret = sem_init(&empty, 0, 0);
if (ret != 0) {
throw "failed initializing semaphore";
}
ret = sem_init(&full, 0, MAX);
if (ret != 0) {
throw "failed initializing semaphore";
}
}
template<int MAX, typename T>
Queue<MAX, T>::~Queue(void)
{
int ret;
ret = pthread_mutex_destroy(&lock);
if (ret != 0) {
throw "failed deinitializing lock";
}
ret = sem_destroy(&full);
if (ret != 0) {
throw "failed deinitializing semaphore";
}
ret = sem_destroy(&empty);
if (ret != 0) {
throw "failed deinitializing semaphore";
}
}
// Will be livepatched.
template <int MAX, typename T>
void Queue<MAX, T>::Push(T x)
{
int ret;
/* Block if the queue is full. */
ret = sem_wait(&full);
if (ret != 0) {
throw "semaphore error";
}
/* Acquire lock of queue. */
ret = pthread_mutex_lock(&lock);
if (ret != 0) {
throw "mutex lock error";
}
/* ----------------------------- */
elem[head++] = x;
/* Wraps around if end of buffer. */
if (head >= MAX) {
head = 0;
}
/* ----------------------------- */
/* Release lock of queue. */
ret = pthread_mutex_unlock(&lock);
if (ret != 0) {
throw "mutex release error";
}
/* Alert other threads that we inserted something. */
ret = sem_post(&empty);
if (ret != 0) {
throw "semaphore post error";
}
}
template <int MAX, typename T>
T Queue<MAX, T>::Pop(void)
{
T ret;
/* Block if the queue is empty. */
if (sem_wait(&empty) != 0) {
throw "semaphore wait error";
}
/* Acquire lock of queue. */
if (pthread_mutex_lock(&lock) != 0) {
throw "mutex lock error";
}
/* ----------------------------- */
ret = elem[tail++];
/* Wraps around if end of buffer. */
if (tail >= MAX) {
tail = 0;
}
/* ----------------------------- */
/* Release lock of queue. */
if (pthread_mutex_unlock(&lock) != 0) {
throw "mutex unlock error";
}
/* Alert other threads that we inserted something. */
if (sem_post(&full) != 0) {
throw "semaphore post error";
}
return ret;
}
void *consumer(void *p)
{
Queue<QMAX, long> *q = (Queue<QMAX, long> *) p;
long x;
while ((x = q->Pop()) != END_TOKEN) {
}
return nullptr;
}
void *producer(void *p)
{
Queue<QMAX, long> *q = (Queue<QMAX, long> *) p;
for (long i = 0; i < END; i++) {
q->Push(i);
}
q->Push(END_TOKEN);
return nullptr;
}
int long_queue(void)
{
Queue<QMAX, long> q;
pthread_t producers[NUM_PRODUCERS];
pthread_t consumers[NUM_CONSUMERS];
for (int i = 0; i < NUM_PRODUCERS; i++) {
if (pthread_create(&producers[i], NULL, producer, (void*) &q) != 0) {
std::cout << "Error creating producer thread " << i << '\n';
return 1;
}
}
for (int i = 0; i < NUM_CONSUMERS; i++) {
if (pthread_create(&consumers[i], NULL, consumer, (void*) &q) != 0) {
std::cout << "Error creating consumer thread " << i << '\n';
return 1;
}
}
for (int i = 0; i < NUM_PRODUCERS; i++) {
if (pthread_join(producers[i], NULL) != 0) {
std::cout << "Error joining producer thread " << i << '\n';
return 1;
}
}
for (int i = 0; i < NUM_CONSUMERS; i++) {
if (pthread_join(consumers[i], NULL) != 0) {
std::cout << "Error joining consumers thread " << i << '\n';
return 1;
}
}
return 0;
}
void *consumer_dbl(void *p)
{
Queue<QMAX, double> *q = (Queue<QMAX, double> *) p;
double x;
while ((x = q->Pop()) != (1./0.)) {
}
return nullptr;
}
void *producer_dbl(void *p)
{
Queue<QMAX, double> *q = (Queue<QMAX, double> *) p;
for (long i = 0; i < END; i++) {
q->Push((double)i);
}
q->Push((double)(1./0.));
return nullptr;
}
int double_queue(void)
{
Queue<QMAX, double> q;
pthread_t producers[NUM_PRODUCERS];
pthread_t consumers[NUM_CONSUMERS];
for (int i = 0; i < NUM_PRODUCERS; i++) {
if (pthread_create(&producers[i], NULL, producer_dbl, (void*) &q) != 0) {
std::cout << "Error creating producer thread " << i << '\n';
return 1;
}
}
for (int i = 0; i < NUM_CONSUMERS; i++) {
if (pthread_create(&consumers[i], NULL, consumer_dbl, (void*) &q) != 0) {
std::cout << "Error creating consumer thread " << i << '\n';
return 1;
}
}
for (int i = 0; i < NUM_PRODUCERS; i++) {
if (pthread_join(producers[i], NULL) != 0) {
std::cout << "Error joining producer thread " << i << '\n';
return 1;
}
}
for (int i = 0; i < NUM_CONSUMERS; i++) {
if (pthread_join(consumers[i], NULL) != 0) {
std::cout << "Error joining consumers thread " << i << '\n';
return 1;
}
}
return 0;
}
int main(void)
{
long_queue();
double_queue();
return 0;
}
|