提交 355c1a14 编写于 作者: L Lars-Peter Clausen 提交者: Jonathan Cameron

iio: kfifo_buf: Implement data_available() callback

This patch implements the data_available() callback for the kfifo buffer instead
of using the stufftoread flag. The kfifo used by the buffer already knows
whether it is empty or not based on the position of its read and write pointer.
Using this makes it a lot easier to tell whether data is available or not and it
is not necessary to take special measures to ensure that no race conditions
between reading and writing from the buffer occur.

Note, that we still have to take the buffers lock to protect against concurrent
resizeing of the kfifo.
Signed-off-by: NLars-Peter Clausen <lars@metafoo.de>
Signed-off-by: NJonathan Cameron <jic23@kernel.org>
上级 647cc7b9
...@@ -42,7 +42,6 @@ static int iio_request_update_kfifo(struct iio_buffer *r) ...@@ -42,7 +42,6 @@ static int iio_request_update_kfifo(struct iio_buffer *r)
} else { } else {
kfifo_reset_out(&buf->kf); kfifo_reset_out(&buf->kf);
} }
r->stufftoread = false;
mutex_unlock(&buf->user_lock); mutex_unlock(&buf->user_lock);
return ret; return ret;
...@@ -108,7 +107,7 @@ static int iio_store_to_kfifo(struct iio_buffer *r, ...@@ -108,7 +107,7 @@ static int iio_store_to_kfifo(struct iio_buffer *r,
ret = kfifo_in(&kf->kf, data, 1); ret = kfifo_in(&kf->kf, data, 1);
if (ret != 1) if (ret != 1)
return -EBUSY; return -EBUSY;
r->stufftoread = true;
wake_up_interruptible_poll(&r->pollq, POLLIN | POLLRDNORM); wake_up_interruptible_poll(&r->pollq, POLLIN | POLLRDNORM);
return 0; return 0;
...@@ -127,13 +126,6 @@ static int iio_read_first_n_kfifo(struct iio_buffer *r, ...@@ -127,13 +126,6 @@ static int iio_read_first_n_kfifo(struct iio_buffer *r,
ret = -EINVAL; ret = -EINVAL;
else else
ret = kfifo_to_user(&kf->kf, buf, n, &copied); ret = kfifo_to_user(&kf->kf, buf, n, &copied);
if (kfifo_is_empty(&kf->kf))
r->stufftoread = false;
/* verify it is still empty to avoid race */
if (!kfifo_is_empty(&kf->kf))
r->stufftoread = true;
mutex_unlock(&kf->user_lock); mutex_unlock(&kf->user_lock);
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -141,6 +133,18 @@ static int iio_read_first_n_kfifo(struct iio_buffer *r, ...@@ -141,6 +133,18 @@ static int iio_read_first_n_kfifo(struct iio_buffer *r,
return copied; return copied;
} }
static bool iio_kfifo_buf_data_available(struct iio_buffer *r)
{
struct iio_kfifo *kf = iio_to_kfifo(r);
bool empty;
mutex_lock(&kf->user_lock);
empty = kfifo_is_empty(&kf->kf);
mutex_unlock(&kf->user_lock);
return !empty;
}
static void iio_kfifo_buffer_release(struct iio_buffer *buffer) static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
{ {
struct iio_kfifo *kf = iio_to_kfifo(buffer); struct iio_kfifo *kf = iio_to_kfifo(buffer);
...@@ -153,6 +157,7 @@ static void iio_kfifo_buffer_release(struct iio_buffer *buffer) ...@@ -153,6 +157,7 @@ static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
static const struct iio_buffer_access_funcs kfifo_access_funcs = { static const struct iio_buffer_access_funcs kfifo_access_funcs = {
.store_to = &iio_store_to_kfifo, .store_to = &iio_store_to_kfifo,
.read_first_n = &iio_read_first_n_kfifo, .read_first_n = &iio_read_first_n_kfifo,
.data_available = iio_kfifo_buf_data_available,
.request_update = &iio_request_update_kfifo, .request_update = &iio_request_update_kfifo,
.get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo, .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
.set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo, .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册