Commit df8f2d41 authored by rna's avatar rna Committed by Andreas Schmidt
Browse files

Reformat pipe and make compliant.

parent 5fb9a18d
......@@ -4,89 +4,83 @@
#include <stdlib.h>
Pipe *Pipe_create()
{
Pipe *p = malloc(sizeof(Pipe));
check_mem(p);
p->list = List_create();
p->size = 1;
pthread_mutex_init(&p->lock, NULL);
pthread_cond_init(&p->wait_for_data, NULL);
pthread_cond_init(&p->wait_for_space, NULL);
Pipe *Pipe_create(void) {
Pipe *p = malloc(sizeof(Pipe));
check_mem(p);
p->list = List_create();
p->size = 1;
pthread_mutex_init(&p->lock, NULL);
pthread_cond_init(&p->wait_for_data, NULL);
pthread_cond_init(&p->wait_for_space, NULL);
return p;
return p;
error:
PERROR("Could not create pipe");
abort();
error:
PERROR("Could not create pipe");
abort();
}
void Pipe_destroy(Pipe *p)
{
List_clear_destroy(p->list);
pthread_mutex_destroy(&p->lock);
pthread_cond_destroy(&p->wait_for_data);
pthread_cond_destroy(&p->wait_for_space);
free(p);
void Pipe_destroy(Pipe *p) {
List_clear_destroy(p->list);
pthread_mutex_destroy(&p->lock);
pthread_cond_destroy(&p->wait_for_data);
pthread_cond_destroy(&p->wait_for_space);
free(p);
}
void Pipe_push(Pipe *p, ListNode *n)
{
pthread_mutex_lock(&p->lock);
while(List_count(p->list) > p->size) {
pthread_cond_wait(&p->wait_for_space, &p->lock);
}
List_push(p->list, n);
pthread_cond_broadcast(&p->wait_for_data);
pthread_mutex_unlock(&p->lock);
void Pipe_push(Pipe *p, ListNode *n) {
pthread_mutex_lock(&p->lock);
while (List_count(p->list) > p->size) {
pthread_cond_wait(&p->wait_for_space, &p->lock);
}
List_push(p->list, n);
pthread_cond_broadcast(&p->wait_for_data);
pthread_mutex_unlock(&p->lock);
}
ListNode *Pipe_pull(Pipe *p)
{
ListNode *n;
pthread_mutex_lock(&p->lock);
n = List_shift(p->list);
if (!n) {
pthread_cond_wait(&p->wait_for_data, &p->lock);
n = List_shift(p->list);
}
pthread_cond_broadcast(&p->wait_for_space);
pthread_mutex_unlock(&p->lock);
return n;
ListNode *Pipe_pull(Pipe *p) {
ListNode *n;
pthread_mutex_lock(&p->lock);
n = List_shift(p->list);
if (!n) {
pthread_cond_wait(&p->wait_for_data, &p->lock);
n = List_shift(p->list);
}
pthread_cond_broadcast(&p->wait_for_space);
pthread_mutex_unlock(&p->lock);
return n;
}
ListNode *Pipe_poll(Pipe *p)
{
ListNode *n;
pthread_mutex_lock(&p->lock);
n = List_shift(p->list);
pthread_mutex_unlock(&p->lock);
return n;
ListNode *Pipe_poll(Pipe *p) {
ListNode *n;
pthread_mutex_lock(&p->lock);
n = List_shift(p->list);
pthread_mutex_unlock(&p->lock);
return n;
}
void Pipe_wake(Pipe *p)
{
pthread_mutex_lock(&p->lock);
pthread_cond_broadcast(&p->wait_for_data);
pthread_mutex_unlock(&p->lock);
void Pipe_wake(Pipe *p) {
pthread_mutex_lock(&p->lock);
pthread_cond_broadcast(&p->wait_for_data);
pthread_mutex_unlock(&p->lock);
}
void Pipe_set_size(Pipe* p, uint32_t size) {
uint32_t old_size = 0;
pthread_mutex_lock(&p->lock);
old_size = p->size;
p->size = size;
if(old_size < size) {
pthread_cond_broadcast(&p->wait_for_space);
}
pthread_mutex_unlock(&p->lock);
void Pipe_set_size(Pipe *p, uint32_t size) {
uint32_t old_size = 0;
pthread_mutex_lock(&p->lock);
old_size = p->size;
p->size = size;
if (old_size < size) {
pthread_cond_broadcast(&p->wait_for_space);
}
pthread_mutex_unlock(&p->lock);
}
uint32_t Pipe_get_size(Pipe* p) {
uint32_t size = 0;
pthread_mutex_lock(&p->lock);
size = p->size;
pthread_mutex_unlock(&p->lock);
return size;
uint32_t Pipe_get_size(Pipe *p) {
uint32_t size = 0;
pthread_mutex_lock(&p->lock);
size = p->size;
pthread_mutex_unlock(&p->lock);
return size;
}
......@@ -7,26 +7,30 @@
typedef struct pipe {
List *list;
pthread_mutex_t lock;
pthread_cond_t wait_for_data;
pthread_cond_t wait_for_space;
List *list;
pthread_mutex_t lock;
pthread_cond_t wait_for_data;
pthread_cond_t wait_for_space;
uint32_t size;
uint32_t size;
} Pipe;
Pipe *Pipe_create();
Pipe *Pipe_create(void);
void Pipe_destroy(Pipe *);
void Pipe_push(Pipe *, ListNode *);
ListNode *Pipe_pull(Pipe *);
ListNode *Pipe_poll(Pipe *);
void Pipe_wake(Pipe *);
void Pipe_set_size(Pipe* pipe, uint32_t size);
uint32_t Pipe_get_size(Pipe* pipe);
void Pipe_set_size(Pipe *pipe, uint32_t size);
uint32_t Pipe_get_size(Pipe *pipe);
#endif //PRRT_PIPE_H
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment