Linux IIO development
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: rafasales@usp.br
Cc: andy@kernel.org, dlechner@baylibre.com, nuno.sa@analog.com,
	"Gustavo C. Arakaki" <gustavo.arakaki@usp.br>,
	linux-iio@vger.kernel.org
Subject: Re: [PATCH v3 2/3] iio: light: ltr501: use automatic cleanup of locks
Date: Fri, 24 Apr 2026 12:32:16 +0100	[thread overview]
Message-ID: <20260424123216.3e0a2969@jic23-huawei> (raw)
In-Reply-To: <20260422231305.677778-3-rafasales@usp.br>

On Wed, 22 Apr 2026 20:12:56 -0300
rafasales@usp.br wrote:

> From: "Rafael B. Sales" <rafasales@usp.br>
> 
> Replace `mutex_lock()` and `mutex_unlock()` calls with guards
> to reduce boilerplate and allow for simpler code blocks.
> 
> Signed-off-by: Rafael B. Sales <rafasales@usp.br>
> Co-developed-by: Gustavo C. Arakaki <gustavo.arakaki@usp.br>
> Signed-off-by: Gustavo C. Arakaki <gustavo.arakaki@usp.br>
> 
> ---
> Changes in v3:
> - Replaces `scoped_guard()` with {} + `guard()` in some cases
> - Simplifies some return statements
> - Includes cleanup.h header
> 
> Changes in v2:
> - Maintains original locking boundaries
> - Simplifies some return statements

>  static int ltr501_als_read_samp_period(const struct ltr501_data *data, int *val)
> @@ -504,9 +499,10 @@ static int ltr501_write_intr_prst(struct ltr501_data *data,
>  		if (new_val < 0 || new_val > 0x0f)
>  			return -EINVAL;
>  
> -		mutex_lock(&data->lock_als);
> -		ret = regmap_field_write(data->reg_als_prst, new_val);
> -		mutex_unlock(&data->lock_als);
> +		scoped_guard(mutex, &data->lock_als) {
> +			ret = regmap_field_write(data->reg_als_prst, new_val);
> +		}
> +
>  		if (ret >= 0)
Whilst here I'd like these to take more standard form.  

		if (ret)
			return ret;

		data->als_period = period;

		return 0; 

It would be a separate patch though so I don't mind if we leave it for another day.

Also removes suggestion that the current code makes that ret > 0 is a possibility
when it isn't.
>  			data->als_period = period;
>  
> @@ -524,9 +520,10 @@ static int ltr501_write_intr_prst(struct ltr501_data *data,
>  		if (new_val < 0 || new_val > 0x0f)
>  			return -EINVAL;
>  
> -		mutex_lock(&data->lock_ps);
> -		ret = regmap_field_write(data->reg_ps_prst, new_val);
> -		mutex_unlock(&data->lock_ps);
> +		scoped_guard(mutex, &data->lock_ps) {
> +			ret = regmap_field_write(data->reg_ps_prst, new_val);
> +		}
> +
>  		if (ret >= 0)
>  			data->ps_period = period;
Same on this one and any other similar cases.


> @@ -1095,19 +1085,16 @@ static int ltr501_write_event_config(struct iio_dev *indio_dev,
>  				     enum iio_event_direction dir, bool state)
>  {
>  	struct ltr501_data *data = iio_priv(indio_dev);
> -	int ret;
>  
>  	switch (chan->type) {
> -	case IIO_INTENSITY:
> -		mutex_lock(&data->lock_als);
> -		ret = regmap_field_write(data->reg_als_intr, state);
> -		mutex_unlock(&data->lock_als);
> -		return ret;
> -	case IIO_PROXIMITY:
> -		mutex_lock(&data->lock_ps);
> -		ret = regmap_field_write(data->reg_ps_intr, state);
> -		mutex_unlock(&data->lock_ps);
> -		return ret;
> +	case IIO_INTENSITY: {
> +		guard(mutex)(&data->lock_als);
> +		return regmap_field_write(data->reg_als_intr, state);
> +		}
If you end up keeping this stuff, I think conventional style would have
the bracket here:
	}
> +	case IIO_PROXIMITY: {
> +		guard(mutex)(&data->lock_ps);
> +		return regmap_field_write(data->reg_ps_intr, state);
> +		}
>  	default:
>  		return -EINVAL;
>  	}


  parent reply	other threads:[~2026-04-24 11:32 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-22 23:12 [PATCH v3 0/3] iio: light: ltr501: cleanups and modernizations rafasales
2026-04-22 23:12 ` [PATCH v3 1/3] iio: light: ltr501: update header inclusions rafasales
2026-04-24  9:27   ` Andy Shevchenko
2026-04-22 23:12 ` [PATCH v3 2/3] iio: light: ltr501: use automatic cleanup of locks rafasales
2026-04-24  9:33   ` Andy Shevchenko
2026-04-24 11:32   ` Jonathan Cameron [this message]
2026-04-22 23:12 ` [PATCH v3 3/3] iio: light: ltr501: use `sysfs_emit_at()` for showing scales rafasales
2026-04-24  9:34   ` Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260424123216.3e0a2969@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=gustavo.arakaki@usp.br \
    --cc=linux-iio@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=rafasales@usp.br \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox