Commit 355c1a14d4009a43e4d1f9cb42a382f0a94d01c4

Authored by Lars-Peter Clausen
Committed by Jonathan Cameron
1 parent 647cc7b9be

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: Lars-Peter Clausen <lars@metafoo.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>

Showing 1 changed file with 14 additions and 9 deletions Side-by-side Diff

drivers/iio/kfifo_buf.c
... ... @@ -42,7 +42,6 @@
42 42 } else {
43 43 kfifo_reset_out(&buf->kf);
44 44 }
45   - r->stufftoread = false;
46 45 mutex_unlock(&buf->user_lock);
47 46  
48 47 return ret;
... ... @@ -108,7 +107,7 @@
108 107 ret = kfifo_in(&kf->kf, data, 1);
109 108 if (ret != 1)
110 109 return -EBUSY;
111   - r->stufftoread = true;
  110 +
112 111 wake_up_interruptible_poll(&r->pollq, POLLIN | POLLRDNORM);
113 112  
114 113 return 0;
... ... @@ -127,13 +126,6 @@
127 126 ret = -EINVAL;
128 127 else
129 128 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
130   -
131   - if (kfifo_is_empty(&kf->kf))
132   - r->stufftoread = false;
133   - /* verify it is still empty to avoid race */
134   - if (!kfifo_is_empty(&kf->kf))
135   - r->stufftoread = true;
136   -
137 129 mutex_unlock(&kf->user_lock);
138 130 if (ret < 0)
139 131 return ret;
... ... @@ -141,6 +133,18 @@
141 133 return copied;
142 134 }
143 135  
  136 +static bool iio_kfifo_buf_data_available(struct iio_buffer *r)
  137 +{
  138 + struct iio_kfifo *kf = iio_to_kfifo(r);
  139 + bool empty;
  140 +
  141 + mutex_lock(&kf->user_lock);
  142 + empty = kfifo_is_empty(&kf->kf);
  143 + mutex_unlock(&kf->user_lock);
  144 +
  145 + return !empty;
  146 +}
  147 +
144 148 static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
145 149 {
146 150 struct iio_kfifo *kf = iio_to_kfifo(buffer);
... ... @@ -153,6 +157,7 @@
153 157 static const struct iio_buffer_access_funcs kfifo_access_funcs = {
154 158 .store_to = &iio_store_to_kfifo,
155 159 .read_first_n = &iio_read_first_n_kfifo,
  160 + .data_available = iio_kfifo_buf_data_available,
156 161 .request_update = &iio_request_update_kfifo,
157 162 .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
158 163 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,