aboutsummaryrefslogtreecommitdiffstats
path: root/lib/fifo.cc
blob: 1d0d7e4849f25db0a5de3d79b817bf92145824b5 (plain)
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
108
109
110
111
112
/* -*- c++ -*- */
/*
 * Copyright 2013-2014 Sylvain Munaut <tnt@246tNt.com>
 *
 * This 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, or (at your option)
 * any later version.
 *
 * This software 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 software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#include <gnuradio/thread/thread.h>

#include "fifo.h"

namespace gr {
  namespace fosphor {

fifo::fifo(int length) :
	d_len(length), d_rp(0), d_wp(0)
{
	this->d_buf = new gr_complex[this->d_len];
}

fifo::~fifo()
{
	delete[] this->d_buf;
}

int
fifo::free()
{
	return (this->d_len - 1) - this->used();
}

int
fifo::used()
{
	return (this->d_wp - this->d_rp) & (this->d_len - 1);
}

int
fifo::write_max_size()
{
	return this->d_len - this->d_wp;
}

gr_complex *
fifo::write_prepare(int size, bool wait)
{
	gr::thread::scoped_lock lock(this->d_mutex);

	if (!wait && (this->free() < size))
		return NULL;

	while (this->free() < size)
		this->d_cond_full.wait(lock);

	return &this->d_buf[this->d_wp];
}

void
fifo::write_commit(int size)
{
	gr::thread::scoped_lock lock(this->d_mutex);

	this->d_wp = (this->d_wp + size) & (this->d_len - 1);

	this->d_cond_empty.notify_one();
}

int
fifo::read_max_size()
{
	return this->d_len - this->d_rp;
}

gr_complex *
fifo::read_peek(int size, bool wait)
{
	gr::thread::scoped_lock lock(this->d_mutex);

	if (!wait && (this->used() < size))
		return NULL;

	while (this->used() < size)
		this->d_cond_empty.wait(lock);

	return &this->d_buf[this->d_rp];
}

void
fifo::read_discard(int size)
{
	gr::thread::scoped_lock lock(this->d_mutex);

	this->d_rp = (this->d_rp + size) & (this->d_len - 1);

	this->d_cond_full.notify_one();
}

  } /* namespace fosphor */
} /* namespace gr */