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
|
/*
Weborf
Copyright (C) 2007 Giuseppe Pappalardo
Weborf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
@author Giuseppe Pappalardo <pappalardo@dmi.unict.it>
@author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it>
@author Salvo Rinaldi <salvin@anche.no>
*/
#include "options.h"
#include <stdlib.h>
#include <pthread.h>
#include <netinet/in.h>
#include "queue.h"
/**
Inits the syncronized queue, allocating memory.
Requires the syn_queue_t struct and the size of the queue itself.
To deallocate the queue, use the q_free function.
*/
int q_init(syn_queue_t * q, int size) {
q->num = q->head = q->tail = 0;
q->size = size;
q->data = (int *) malloc(sizeof(int) * size);
if (q->data == NULL) { //Error, unable to allocate memory
return 1;
}
pthread_mutex_init(&q->mutex, NULL);
pthread_cond_init(&q->for_space, NULL);
pthread_cond_init(&q->for_data, NULL);
q->n_wait_dt = q->n_wait_sp = 0;
return 0;
}
/** Frees the memory taken by the queue.
Requires the pointer to the queue struct
*/
void q_free(syn_queue_t * q) {
free(q->data);
}
int q_get(syn_queue_t * q, int *val) {
pthread_mutex_lock(&q->mutex);
while (q->num == 0) {
q->n_wait_dt++;
pthread_cond_wait(&q->for_data, &q->mutex);
}
*val = q->data[q->head]; //Sets the value
q->head = (q->head + 1) % q->size; //Moves the head
q->num--; //Reduces count of the queue
/*if ((q->num == q->size) && (q->n_wait_sp > 0)) {
q->n_wait_sp--;
pthread_cond_signal(&q->for_space);
} // unlock also needed after signal*/
pthread_mutex_unlock(&q->mutex); // or threads blocked on wait
return 0; // will not proceed
}
int q_put(syn_queue_t * q, int val) {
pthread_mutex_lock(&q->mutex);
//Wakes up a sleeping thread
if (q->n_wait_dt > 0) {
q->n_wait_dt--;
pthread_cond_signal(&q->for_data);
} // unlock also needed after signal
//Fails if queue is full
if (q->num == q->size) {
pthread_mutex_unlock(&q->mutex); // or threads blocked on wait
return 1; // will not proceed
//while (q->num == q->size) {
//q->n_wait_sp++;
//pthread_cond_wait(&q->for_space, &q->mutex);
}
q->data[q->tail] = val; //Set the data in position
q->tail = (q->tail + 1) % q->size; //Moves the tail
q->num++; //Increases count of filled positions
pthread_mutex_unlock(&q->mutex); // or threads blocked on wait
return 0; // will not proceed
}
|