-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpipeline.c
92 lines (73 loc) · 1.64 KB
/
pipeline.c
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
/*
* Data averaging pipeline. Used to smooth data and connect different functional blocks
*
* pipeline.c
*
* Created on: Feb 26, 2012
* Author: aml6195
*/
#include <semaphore.h>
#include <stdlib.h>
#include "pipeline.h"
/**
* Initailize a pipeline of length size.
*
* This pipeline will fill, then post to the mutex when the average is ready
*
*/
void init_pipeline(pipeline_dat* dat, int size){
sem_init(&dat->mutex,0,0);
sem_init(&dat->read_mutex,0,1);
dat->values= malloc(size*sizeof(double));
if (dat->values == NULL){
perror("Memory allocation error");
}
dat->size=size;
dat->index=0;
int i;
for (i=0;i<dat->size;i++){
dat->values[i]=0.0;
}
}
/**
* Add data to a pipeline.
*
* This function will release the mutex when the pipeline is full.
*/
void add_to_pipeline(pipeline_dat* dat, double val){
sem_wait(&dat->read_mutex);
if (dat->index > dat->size || dat->size > 100){
perror("array out of bounds");
}
dat->lastInput=val;
dat->values[dat->index]= val;
dat->index = (dat->index+1) % dat->size;
sem_post(&dat->read_mutex);
//if we have filled the pipeline, post to the mutex, saying we are done.
if(dat->index==0){
sem_post(&dat->mutex);
}
}
/**
* Blocking call to get the next data average
*
*/
double pipe_get_next_average(pipeline_dat* dat){
sem_wait(&dat->mutex);
return pipe_get_average(dat);
}
/**
* Nonblocking call to get the next data average.
*
*/
double pipe_get_average(pipeline_dat* dat){
sem_wait(&dat->read_mutex);
int i;
double average=0.0;
for (i=0;i<(dat->size);i++){
average+=dat->values[i];
}
average /= dat->size;
sem_post(&dat->read_mutex);
return average;
}