@@ -7,6 +7,7 @@
*/
#include <linux/anon_inodes.h>
+#include <linux/cleanup.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/kernel.h>
@@ -146,11 +147,10 @@ static ssize_t iio_event_chrdev_read(struct file *filep,
return -ENODEV;
}
- if (mutex_lock_interruptible(&ev_int->read_lock))
- return -ERESTARTSYS;
- ret = kfifo_to_user(&ev_int->det_events, buf, count, &copied);
- mutex_unlock(&ev_int->read_lock);
-
+ scoped_cond_guard(mutex_intr, return -ERESTARTSYS,
+ &ev_int->read_lock)
+ ret = kfifo_to_user(&ev_int->det_events, buf, count,
+ &copied);
if (ret)
return ret;
@@ -198,28 +198,22 @@ static int iio_event_getfd(struct iio_dev *indio_dev)
if (ev_int == NULL)
return -ENODEV;
- fd = mutex_lock_interruptible(&iio_dev_opaque->mlock);
- if (fd)
- return fd;
+ scoped_cond_guard(mutex_intr, return -EINTR, &iio_dev_opaque->mlock) {
+ if (test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags))
+ return -EBUSY;
- if (test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
- fd = -EBUSY;
- goto unlock;
+ iio_device_get(indio_dev);
+
+ fd = anon_inode_getfd("iio:event", &iio_event_chrdev_fileops,
+ indio_dev, O_RDONLY | O_CLOEXEC);
+ if (fd < 0) {
+ clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
+ iio_device_put(indio_dev);
+ } else {
+ kfifo_reset_out(&ev_int->det_events);
+ }
}
- iio_device_get(indio_dev);
-
- fd = anon_inode_getfd("iio:event", &iio_event_chrdev_fileops,
- indio_dev, O_RDONLY | O_CLOEXEC);
- if (fd < 0) {
- clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
- iio_device_put(indio_dev);
- } else {
- kfifo_reset_out(&ev_int->det_events);
- }
-
-unlock:
- mutex_unlock(&iio_dev_opaque->mlock);
return fd;
}