int queue_put(BlockingQueue * queue, void * data) {
pthread_mutex_lock(&queue->lock);
struct QNode * first = queue->first;
struct QNode * new_;
if(first != NULL) {
while(first != NULL) {
first = first->next;
}
make_node(data, &new_);
} else {
make_node(data, &new_);
}
if(new_ != NULL) {
first = new_;
} else {
return 0;
pthread_mutex_unlock(&queue->lock);
}
queue->nodes += 1;
pthread_cond_signal(&queue->empty_);
pthread_mutex_unlock(&queue->lock);
return 1;
}
void * queue_pop(BlockingQueue * queue) {
pthread_mutex_lock(&queue->lock);
while(!queue->empty_) {
pthread_cond_wait(&queue->empty_, &queue->lock);
}
struct QNode * first = queue->first;
void * data_;
if(first->next != NULL) {
struct QNode * next = first->next;
data_ = first->next->data;
queue->first = next;
} else {
data_ = first->data;
}
first = NULL;
first->next = NULL;
free(first);
queue->nodes -= 1;
pthread_mutex_lock(&queue->lock);
return data_;
}