DATE CREATED: Oct 15 2012
A FIFO design useful in gluing the FDMDV modem and codec together in
- integrated applications.
+ integrated applications. The unittest/tfifo indicates these
+ routines are thread safe without the need for syncronisisation
+ object, e.g. a different thread can read and write to a fifo at the
+ same time.
\*---------------------------------------------------------------------------*/
short *pin;
short *pout;
int nshort;
- int n;
};
struct FIFO *fifo_create(int nshort) {
fifo->pin = fifo->buf;
fifo->pout = fifo->buf;
fifo->nshort = nshort;
- fifo->n = 0;
return fifo;
}
short *pdata;
short *pin = fifo->pin;
- //printf(" write %d in: %d\n", n, pin-fifo->pin);
assert(fifo != NULL);
assert(data != NULL);
- fifo_free = fifo->nshort - fifo->n;
+ // available storage is one less than nshort as prd == pwr
+ // is reserved for empty rather than full
+
+ fifo_free = fifo->nshort - fifo_used(fifo) - 1;
if (n > fifo_free) {
return -1;
if (pin == (fifo->buf + fifo->nshort))
pin = fifo->buf;
}
- fifo->n += n;
fifo->pin = pin;
}
{
int i;
short *pdata;
+ short *pin = fifo->pin;
short *pout = fifo->pout;
- //printf(" read %d pout: %d\n", n, pout-fifo->buf);
assert(fifo != NULL);
assert(data != NULL);
- if (n > fifo->n) {
+ if (n > fifo_used(fifo)) {
return -1;
}
else {
if (pout == (fifo->buf + fifo->nshort))
pout = fifo->buf;
}
- fifo->n -= n;
fifo->pout = pout;
}
return 0;
}
-int fifo_n(struct FIFO *fifo)
+int fifo_used(struct FIFO *fifo)
{
- assert(fifo != NULL);
- return fifo->n;
+ short *pin = fifo->pin;
+ short *pout = fifo->pout;
+ unsigned int used;
+
+ assert(fifo != NULL);
+ if (pin >= pout)
+ used = pin - pout;
+ else
+ used = fifo->nshort + (unsigned int)(pin - pout);
+
+ return used;
}
void fifo_destroy(struct FIFO *fifo);
int fifo_write(struct FIFO *fifo, short data[], int n);
int fifo_read(struct FIFO *fifo, short data[], int n);
-int fifo_n(struct FIFO *fifo);
+int fifo_used(struct FIFO *fifo);
#ifdef __cplusplus
}
#define WRITE_SZ 10
#define READ_SZ 8
#define N_MAX 100
-#define LOOPS 10000
+#define LOOPS 1000000
int run_thread = 1;
struct FIFO *f;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
#define USE_THREADS
-#define USE_MUTEX
+//#define USE_MUTEX
int main() {
pthread_t awriter_thread;
short write_buf[WRITE_SZ];
int i;
- if ((FIFO_SZ - fifo_n(f)) > WRITE_SZ) {
+ if ((FIFO_SZ - fifo_used(f)) > WRITE_SZ) {
for(i=0; i<WRITE_SZ; i++) {
write_buf[i] = n_in++;
if (n_in == N_MAX)