Re: [PATCH v5] iio: chemical: scd30: Replace manual locking with RAII locking

From: Jonathan Cameron

Date: Fri May 29 2026 - 06:49:48 EST


On Thu, 28 May 2026 17:29:06 -0500
Maxwell Doose <m32285159@xxxxxxxxx> wrote:

> scd30_core.c currently uses manual mutex_lock() and mutex_unlock()
> calls. Replace them with the newer guard(mutex)() for cleaner RAII
> patterns and to improve maintainability.
>
> Add new helper function scd30_trigger_handler_helper() containing
> the critical section for scd30_trigger_handler().
>
> In addition, small refactor to replace "?:" operator with regular
> if/else returns.
>
> Signed-off-by: Maxwell Doose <m32285159@xxxxxxxxx>

Looks like you sent a version that won't build.
> +static int scd30_trigger_handler_helper(struct iio_dev *indio_dev, int *scan_data,
> + size_t scan_data_size)
> +{
> + struct scd30_state *state = iio_priv(indio_dev);
> + int ret;
> +
> + guard(mutex)(&state->lock);
> +
> + if (!iio_trigger_using_own(indio_dev))
> + ret = scd30_read_poll(state);
> + else
> + ret = scd30_read_meas(state);
> + memcpy(scan_data, state->meas, scan_data_size);
> +
> + return ret;
> +}
> +
> static irqreturn_t scd30_trigger_handler(int irq, void *p)
> {
> struct iio_poll_func *pf = p;
> struct iio_dev *indio_dev = pf->indio_dev;
> - struct scd30_state *state = iio_priv(indio_dev);
> struct {
> int data[SCD30_MEAS_COUNT];
> aligned_s64 ts;
> } scan = { };
> int ret;
>
> - mutex_lock(&state->lock);
> - if (!iio_trigger_using_own(indio_dev))
> - ret = scd30_read_poll(state);
> - else
> - ret = scd30_read_meas(state);
> - memcpy(scan.data, state->meas, sizeof(state->meas));
> - mutex_unlock(&state->lock);
> + ret = scd30_trigger_handler_helper_locked(indio_dev, scan.data, sizeof(scan.data));
The call needs an update t the new naming.

> if (ret)
> goto out;
>