]> www.infradead.org Git - users/hch/misc.git/commitdiff
ALSA: firewire: dice: Use guard() for mutex locks
authorTakashi Iwai <tiwai@suse.de>
Thu, 28 Aug 2025 13:27:06 +0000 (15:27 +0200)
committerTakashi Iwai <tiwai@suse.de>
Sat, 30 Aug 2025 08:02:20 +0000 (10:02 +0200)
Replace the manual mutex lock/unlock pairs with guard() for code
simplification.

Only code refactoring, and no behavior change.

Reviewed-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
Link: https://patch.msgid.link/20250828132802.9032-3-tiwai@suse.de
sound/firewire/dice/dice-midi.c
sound/firewire/dice/dice-pcm.c
sound/firewire/dice/dice.c

index 78988e44b8bcd23329dfd929e144c13163e8602b..9ca975c556ca713e377e2bbf3001cfcb2dbb8285 100644 (file)
@@ -15,18 +15,16 @@ static int midi_open(struct snd_rawmidi_substream *substream)
        if (err < 0)
                return err;
 
-       mutex_lock(&dice->mutex);
-
-       err = snd_dice_stream_reserve_duplex(dice, 0, 0, 0);
-       if (err >= 0) {
-               ++dice->substreams_counter;
-               err = snd_dice_stream_start_duplex(dice);
-               if (err < 0)
-                       --dice->substreams_counter;
+       scoped_guard(mutex, &dice->mutex) {
+               err = snd_dice_stream_reserve_duplex(dice, 0, 0, 0);
+               if (err >= 0) {
+                       ++dice->substreams_counter;
+                       err = snd_dice_stream_start_duplex(dice);
+                       if (err < 0)
+                               --dice->substreams_counter;
+               }
        }
 
-       mutex_unlock(&dice->mutex);
-
        if (err < 0)
                snd_dice_stream_lock_release(dice);
 
@@ -37,12 +35,10 @@ static int midi_close(struct snd_rawmidi_substream *substream)
 {
        struct snd_dice *dice = substream->rmidi->private_data;
 
-       mutex_lock(&dice->mutex);
-
-       --dice->substreams_counter;
-       snd_dice_stream_stop_duplex(dice);
-
-       mutex_unlock(&dice->mutex);
+       scoped_guard(mutex, &dice->mutex) {
+               --dice->substreams_counter;
+               snd_dice_stream_stop_duplex(dice);
+       }
 
        snd_dice_stream_lock_release(dice);
        return 0;
index cfc19bd0d5dd4e6136d290925493b8101d28e3c4..d5319cd2cc6f1a396e3498cc0fe1801192cade37 100644 (file)
@@ -196,53 +196,45 @@ static int pcm_open(struct snd_pcm_substream *substream)
                break;
        }
 
-       mutex_lock(&dice->mutex);
-
-       // When source of clock is not internal or any stream is reserved for
-       // transmission of PCM frames, the available sampling rate is limited
-       // at current one.
-       if (!internal ||
-           (dice->substreams_counter > 0 && d->events_per_period > 0)) {
-               unsigned int frames_per_period = d->events_per_period;
-               unsigned int frames_per_buffer = d->events_per_buffer;
-               unsigned int rate;
-
-               err = snd_dice_transaction_get_rate(dice, &rate);
-               if (err < 0) {
-                       mutex_unlock(&dice->mutex);
-                       goto err_locked;
-               }
-
-               substream->runtime->hw.rate_min = rate;
-               substream->runtime->hw.rate_max = rate;
-
-               if (frames_per_period > 0) {
-                       // For double_pcm_frame quirk.
-                       if (rate > 96000 && !dice->disable_double_pcm_frames) {
-                               frames_per_period *= 2;
-                               frames_per_buffer *= 2;
-                       }
-
-                       err = snd_pcm_hw_constraint_minmax(substream->runtime,
-                                       SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
-                                       frames_per_period, frames_per_period);
-                       if (err < 0) {
-                               mutex_unlock(&dice->mutex);
+       scoped_guard(mutex, &dice->mutex) {
+               // When source of clock is not internal or any stream is reserved for
+               // transmission of PCM frames, the available sampling rate is limited
+               // at current one.
+               if (!internal ||
+                   (dice->substreams_counter > 0 && d->events_per_period > 0)) {
+                       unsigned int frames_per_period = d->events_per_period;
+                       unsigned int frames_per_buffer = d->events_per_buffer;
+                       unsigned int rate;
+
+                       err = snd_dice_transaction_get_rate(dice, &rate);
+                       if (err < 0)
                                goto err_locked;
-                       }
 
-                       err = snd_pcm_hw_constraint_minmax(substream->runtime,
-                                       SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
-                                       frames_per_buffer, frames_per_buffer);
-                       if (err < 0) {
-                               mutex_unlock(&dice->mutex);
-                               goto err_locked;
+                       substream->runtime->hw.rate_min = rate;
+                       substream->runtime->hw.rate_max = rate;
+
+                       if (frames_per_period > 0) {
+                               // For double_pcm_frame quirk.
+                               if (rate > 96000 && !dice->disable_double_pcm_frames) {
+                                       frames_per_period *= 2;
+                                       frames_per_buffer *= 2;
+                               }
+
+                               err = snd_pcm_hw_constraint_minmax(substream->runtime,
+                                                                  SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
+                                                                  frames_per_period, frames_per_period);
+                               if (err < 0)
+                                       goto err_locked;
+
+                               err = snd_pcm_hw_constraint_minmax(substream->runtime,
+                                                                  SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
+                                                                  frames_per_buffer, frames_per_buffer);
+                               if (err < 0)
+                                       goto err_locked;
                        }
                }
        }
 
-       mutex_unlock(&dice->mutex);
-
        snd_pcm_set_sync(substream);
 
        return 0;
@@ -271,7 +263,7 @@ static int pcm_hw_params(struct snd_pcm_substream *substream,
                unsigned int events_per_period = params_period_size(hw_params);
                unsigned int events_per_buffer = params_buffer_size(hw_params);
 
-               mutex_lock(&dice->mutex);
+               guard(mutex)(&dice->mutex);
                // For double_pcm_frame quirk.
                if (rate > 96000 && !dice->disable_double_pcm_frames) {
                        events_per_period /= 2;
@@ -281,7 +273,6 @@ static int pcm_hw_params(struct snd_pcm_substream *substream,
                                        events_per_period, events_per_buffer);
                if (err >= 0)
                        ++dice->substreams_counter;
-               mutex_unlock(&dice->mutex);
        }
 
        return err;
@@ -291,15 +282,13 @@ static int pcm_hw_free(struct snd_pcm_substream *substream)
 {
        struct snd_dice *dice = substream->private_data;
 
-       mutex_lock(&dice->mutex);
+       guard(mutex)(&dice->mutex);
 
        if (substream->runtime->state != SNDRV_PCM_STATE_OPEN)
                --dice->substreams_counter;
 
        snd_dice_stream_stop_duplex(dice);
 
-       mutex_unlock(&dice->mutex);
-
        return 0;
 }
 
@@ -309,9 +298,9 @@ static int capture_prepare(struct snd_pcm_substream *substream)
        struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device];
        int err;
 
-       mutex_lock(&dice->mutex);
-       err = snd_dice_stream_start_duplex(dice);
-       mutex_unlock(&dice->mutex);
+       scoped_guard(mutex, &dice->mutex) {
+               err = snd_dice_stream_start_duplex(dice);
+       }
        if (err >= 0)
                amdtp_stream_pcm_prepare(stream);
 
@@ -323,9 +312,9 @@ static int playback_prepare(struct snd_pcm_substream *substream)
        struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device];
        int err;
 
-       mutex_lock(&dice->mutex);
-       err = snd_dice_stream_start_duplex(dice);
-       mutex_unlock(&dice->mutex);
+       scoped_guard(mutex, &dice->mutex) {
+               err = snd_dice_stream_start_duplex(dice);
+       }
        if (err >= 0)
                amdtp_stream_pcm_prepare(stream);
 
index 9675ec14271d04e7d1520e1919a0175131584b35..bcbe80344328e7821cfebabb9a3bfc7ee13d9aed 100644 (file)
@@ -238,9 +238,8 @@ static void dice_bus_reset(struct fw_unit *unit)
        /* The handler address register becomes initialized. */
        snd_dice_transaction_reinit(dice);
 
-       mutex_lock(&dice->mutex);
+       guard(mutex)(&dice->mutex);
        snd_dice_stream_update_duplex(dice);
-       mutex_unlock(&dice->mutex);
 }
 
 #define DICE_INTERFACE 0x000001