Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ALSA: firewire: dice: Use guard() for mutex locks

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

+53 -69
+12 -16
sound/firewire/dice/dice-midi.c
··· 15 15 if (err < 0) 16 16 return err; 17 17 18 - mutex_lock(&dice->mutex); 19 - 20 - err = snd_dice_stream_reserve_duplex(dice, 0, 0, 0); 21 - if (err >= 0) { 22 - ++dice->substreams_counter; 23 - err = snd_dice_stream_start_duplex(dice); 24 - if (err < 0) 25 - --dice->substreams_counter; 18 + scoped_guard(mutex, &dice->mutex) { 19 + err = snd_dice_stream_reserve_duplex(dice, 0, 0, 0); 20 + if (err >= 0) { 21 + ++dice->substreams_counter; 22 + err = snd_dice_stream_start_duplex(dice); 23 + if (err < 0) 24 + --dice->substreams_counter; 25 + } 26 26 } 27 - 28 - mutex_unlock(&dice->mutex); 29 27 30 28 if (err < 0) 31 29 snd_dice_stream_lock_release(dice); ··· 35 37 { 36 38 struct snd_dice *dice = substream->rmidi->private_data; 37 39 38 - mutex_lock(&dice->mutex); 39 - 40 - --dice->substreams_counter; 41 - snd_dice_stream_stop_duplex(dice); 42 - 43 - mutex_unlock(&dice->mutex); 40 + scoped_guard(mutex, &dice->mutex) { 41 + --dice->substreams_counter; 42 + snd_dice_stream_stop_duplex(dice); 43 + } 44 44 45 45 snd_dice_stream_lock_release(dice); 46 46 return 0;
+40 -51
sound/firewire/dice/dice-pcm.c
··· 196 196 break; 197 197 } 198 198 199 - mutex_lock(&dice->mutex); 199 + scoped_guard(mutex, &dice->mutex) { 200 + // When source of clock is not internal or any stream is reserved for 201 + // transmission of PCM frames, the available sampling rate is limited 202 + // at current one. 203 + if (!internal || 204 + (dice->substreams_counter > 0 && d->events_per_period > 0)) { 205 + unsigned int frames_per_period = d->events_per_period; 206 + unsigned int frames_per_buffer = d->events_per_buffer; 207 + unsigned int rate; 200 208 201 - // When source of clock is not internal or any stream is reserved for 202 - // transmission of PCM frames, the available sampling rate is limited 203 - // at current one. 204 - if (!internal || 205 - (dice->substreams_counter > 0 && d->events_per_period > 0)) { 206 - unsigned int frames_per_period = d->events_per_period; 207 - unsigned int frames_per_buffer = d->events_per_buffer; 208 - unsigned int rate; 209 - 210 - err = snd_dice_transaction_get_rate(dice, &rate); 211 - if (err < 0) { 212 - mutex_unlock(&dice->mutex); 213 - goto err_locked; 214 - } 215 - 216 - substream->runtime->hw.rate_min = rate; 217 - substream->runtime->hw.rate_max = rate; 218 - 219 - if (frames_per_period > 0) { 220 - // For double_pcm_frame quirk. 221 - if (rate > 96000 && !dice->disable_double_pcm_frames) { 222 - frames_per_period *= 2; 223 - frames_per_buffer *= 2; 224 - } 225 - 226 - err = snd_pcm_hw_constraint_minmax(substream->runtime, 227 - SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 228 - frames_per_period, frames_per_period); 229 - if (err < 0) { 230 - mutex_unlock(&dice->mutex); 209 + err = snd_dice_transaction_get_rate(dice, &rate); 210 + if (err < 0) 231 211 goto err_locked; 232 - } 233 212 234 - err = snd_pcm_hw_constraint_minmax(substream->runtime, 235 - SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 236 - frames_per_buffer, frames_per_buffer); 237 - if (err < 0) { 238 - mutex_unlock(&dice->mutex); 239 - goto err_locked; 213 + substream->runtime->hw.rate_min = rate; 214 + substream->runtime->hw.rate_max = rate; 215 + 216 + if (frames_per_period > 0) { 217 + // For double_pcm_frame quirk. 218 + if (rate > 96000 && !dice->disable_double_pcm_frames) { 219 + frames_per_period *= 2; 220 + frames_per_buffer *= 2; 221 + } 222 + 223 + err = snd_pcm_hw_constraint_minmax(substream->runtime, 224 + SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 225 + frames_per_period, frames_per_period); 226 + if (err < 0) 227 + goto err_locked; 228 + 229 + err = snd_pcm_hw_constraint_minmax(substream->runtime, 230 + SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 231 + frames_per_buffer, frames_per_buffer); 232 + if (err < 0) 233 + goto err_locked; 240 234 } 241 235 } 242 236 } 243 - 244 - mutex_unlock(&dice->mutex); 245 237 246 238 snd_pcm_set_sync(substream); 247 239 ··· 263 271 unsigned int events_per_period = params_period_size(hw_params); 264 272 unsigned int events_per_buffer = params_buffer_size(hw_params); 265 273 266 - mutex_lock(&dice->mutex); 274 + guard(mutex)(&dice->mutex); 267 275 // For double_pcm_frame quirk. 268 276 if (rate > 96000 && !dice->disable_double_pcm_frames) { 269 277 events_per_period /= 2; ··· 273 281 events_per_period, events_per_buffer); 274 282 if (err >= 0) 275 283 ++dice->substreams_counter; 276 - mutex_unlock(&dice->mutex); 277 284 } 278 285 279 286 return err; ··· 282 291 { 283 292 struct snd_dice *dice = substream->private_data; 284 293 285 - mutex_lock(&dice->mutex); 294 + guard(mutex)(&dice->mutex); 286 295 287 296 if (substream->runtime->state != SNDRV_PCM_STATE_OPEN) 288 297 --dice->substreams_counter; 289 298 290 299 snd_dice_stream_stop_duplex(dice); 291 - 292 - mutex_unlock(&dice->mutex); 293 300 294 301 return 0; 295 302 } ··· 298 309 struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device]; 299 310 int err; 300 311 301 - mutex_lock(&dice->mutex); 302 - err = snd_dice_stream_start_duplex(dice); 303 - mutex_unlock(&dice->mutex); 312 + scoped_guard(mutex, &dice->mutex) { 313 + err = snd_dice_stream_start_duplex(dice); 314 + } 304 315 if (err >= 0) 305 316 amdtp_stream_pcm_prepare(stream); 306 317 ··· 312 323 struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device]; 313 324 int err; 314 325 315 - mutex_lock(&dice->mutex); 316 - err = snd_dice_stream_start_duplex(dice); 317 - mutex_unlock(&dice->mutex); 326 + scoped_guard(mutex, &dice->mutex) { 327 + err = snd_dice_stream_start_duplex(dice); 328 + } 318 329 if (err >= 0) 319 330 amdtp_stream_pcm_prepare(stream); 320 331
+1 -2
sound/firewire/dice/dice.c
··· 238 238 /* The handler address register becomes initialized. */ 239 239 snd_dice_transaction_reinit(dice); 240 240 241 - mutex_lock(&dice->mutex); 241 + guard(mutex)(&dice->mutex); 242 242 snd_dice_stream_update_duplex(dice); 243 - mutex_unlock(&dice->mutex); 244 243 } 245 244 246 245 #define DICE_INTERFACE 0x000001