]> www.infradead.org Git - users/hch/misc.git/commitdiff
ALSA: firewire: digi00x: Use guard() for mutex locks
authorTakashi Iwai <tiwai@suse.de>
Thu, 28 Aug 2025 13:27:07 +0000 (15:27 +0200)
committerTakashi Iwai <tiwai@suse.de>
Sat, 30 Aug 2025 08:02:21 +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-4-tiwai@suse.de
sound/firewire/digi00x/digi00x-midi.c
sound/firewire/digi00x/digi00x-pcm.c
sound/firewire/digi00x/digi00x.c

index 8f4bace16050a16248c9c117af9cc0888bbf0463..0f6ca58cc4a0d592dca6987ec9e7b9c9a0006063 100644 (file)
@@ -16,15 +16,15 @@ static int midi_open(struct snd_rawmidi_substream *substream)
        if (err < 0)
                return err;
 
-       mutex_lock(&dg00x->mutex);
-       err = snd_dg00x_stream_reserve_duplex(dg00x, 0, 0, 0);
-       if (err >= 0) {
-               ++dg00x->substreams_counter;
-               err = snd_dg00x_stream_start_duplex(dg00x);
-               if (err < 0)
-                       --dg00x->substreams_counter;
+       scoped_guard(mutex, &dg00x->mutex) {
+               err = snd_dg00x_stream_reserve_duplex(dg00x, 0, 0, 0);
+               if (err >= 0) {
+                       ++dg00x->substreams_counter;
+                       err = snd_dg00x_stream_start_duplex(dg00x);
+                       if (err < 0)
+                               --dg00x->substreams_counter;
+               }
        }
-       mutex_unlock(&dg00x->mutex);
        if (err < 0)
                snd_dg00x_stream_lock_release(dg00x);
 
@@ -35,10 +35,10 @@ static int midi_close(struct snd_rawmidi_substream *substream)
 {
        struct snd_dg00x *dg00x = substream->rmidi->private_data;
 
-       mutex_lock(&dg00x->mutex);
-       --dg00x->substreams_counter;
-       snd_dg00x_stream_stop_duplex(dg00x);
-       mutex_unlock(&dg00x->mutex);
+       scoped_guard(mutex, &dg00x->mutex) {
+               --dg00x->substreams_counter;
+               snd_dg00x_stream_stop_duplex(dg00x);
+       }
 
        snd_dg00x_stream_lock_release(dg00x);
        return 0;
index 85e65cbc00c4b466b12100560afed76623cef546..75f81545d50c363e80d0685f910035a86bb520b9 100644 (file)
@@ -127,46 +127,38 @@ static int pcm_open(struct snd_pcm_substream *substream)
                }
        }
 
-       mutex_lock(&dg00x->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 ((clock != SND_DG00X_CLOCK_INTERNAL) ||
-           (dg00x->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_dg00x_stream_get_external_rate(dg00x, &rate);
-               if (err < 0) {
-                       mutex_unlock(&dg00x->mutex);
-                       goto err_locked;
-               }
-               substream->runtime->hw.rate_min = rate;
-               substream->runtime->hw.rate_max = rate;
-
-               if (frames_per_period > 0) {
-                       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(&dg00x->mutex);
-                               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(&dg00x->mutex);
+       scoped_guard(mutex, &dg00x->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 ((clock != SND_DG00X_CLOCK_INTERNAL) ||
+                   (dg00x->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_dg00x_stream_get_external_rate(dg00x, &rate);
+                       if (err < 0)
                                goto err_locked;
+                       substream->runtime->hw.rate_min = rate;
+                       substream->runtime->hw.rate_max = rate;
+
+                       if (frames_per_period > 0) {
+                               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(&dg00x->mutex);
-
        snd_pcm_set_sync(substream);
 
        return 0;
@@ -195,12 +187,11 @@ static int pcm_hw_params(struct snd_pcm_substream *substream,
                unsigned int frames_per_period = params_period_size(hw_params);
                unsigned int frames_per_buffer = params_buffer_size(hw_params);
 
-               mutex_lock(&dg00x->mutex);
+               guard(mutex)(&dg00x->mutex);
                err = snd_dg00x_stream_reserve_duplex(dg00x, rate,
                                        frames_per_period, frames_per_buffer);
                if (err >= 0)
                        ++dg00x->substreams_counter;
-               mutex_unlock(&dg00x->mutex);
        }
 
        return err;
@@ -210,15 +201,13 @@ static int pcm_hw_free(struct snd_pcm_substream *substream)
 {
        struct snd_dg00x *dg00x = substream->private_data;
 
-       mutex_lock(&dg00x->mutex);
+       guard(mutex)(&dg00x->mutex);
 
        if (substream->runtime->state != SNDRV_PCM_STATE_OPEN)
                --dg00x->substreams_counter;
 
        snd_dg00x_stream_stop_duplex(dg00x);
 
-       mutex_unlock(&dg00x->mutex);
-
        return 0;
 }
 
@@ -227,14 +216,12 @@ static int pcm_capture_prepare(struct snd_pcm_substream *substream)
        struct snd_dg00x *dg00x = substream->private_data;
        int err;
 
-       mutex_lock(&dg00x->mutex);
+       guard(mutex)(&dg00x->mutex);
 
        err = snd_dg00x_stream_start_duplex(dg00x);
        if (err >= 0)
                amdtp_stream_pcm_prepare(&dg00x->tx_stream);
 
-       mutex_unlock(&dg00x->mutex);
-
        return err;
 }
 
@@ -243,7 +230,7 @@ static int pcm_playback_prepare(struct snd_pcm_substream *substream)
        struct snd_dg00x *dg00x = substream->private_data;
        int err;
 
-       mutex_lock(&dg00x->mutex);
+       guard(mutex)(&dg00x->mutex);
 
        err = snd_dg00x_stream_start_duplex(dg00x);
        if (err >= 0) {
@@ -251,8 +238,6 @@ static int pcm_playback_prepare(struct snd_pcm_substream *substream)
                amdtp_dot_reset(&dg00x->rx_stream);
        }
 
-       mutex_unlock(&dg00x->mutex);
-
        return err;
 }
 
index cebc35dcf8cd1a4372764bb8941fa48a29c5824d..f73a9fc8adb17bc48385be574d91d7d7e9cf11b7 100644 (file)
@@ -116,9 +116,8 @@ static void snd_dg00x_update(struct fw_unit *unit)
 
        snd_dg00x_transaction_reregister(dg00x);
 
-       mutex_lock(&dg00x->mutex);
+       guard(mutex)(&dg00x->mutex);
        snd_dg00x_stream_update_duplex(dg00x);
-       mutex_unlock(&dg00x->mutex);
 }
 
 static void snd_dg00x_remove(struct fw_unit *unit)