ALSA: aloop: Use guard() for spin locks

Clean up the code using guard() for spin locks.

Merely code refactoring, and no behavior change.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
Link: https://patch.msgid.link/20250829150026.6379-3-tiwai@suse.de
This commit is contained in:
Takashi Iwai 2025-08-29 17:00:13 +02:00
parent ebd9b6c91d
commit 1ef2cb6b29
1 changed files with 96 additions and 97 deletions

View File

@ -414,39 +414,39 @@ static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
dpcm->last_jiffies = jiffies; dpcm->last_jiffies = jiffies;
dpcm->pcm_rate_shift = 0; dpcm->pcm_rate_shift = 0;
dpcm->last_drift = 0; dpcm->last_drift = 0;
spin_lock(&cable->lock); scoped_guard(spinlock, &cable->lock) {
cable->running |= stream; cable->running |= stream;
cable->pause &= ~stream; cable->pause &= ~stream;
err = cable->ops->start(dpcm); err = cable->ops->start(dpcm);
spin_unlock(&cable->lock); }
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
loopback_active_notify(dpcm); loopback_active_notify(dpcm);
break; break;
case SNDRV_PCM_TRIGGER_STOP: case SNDRV_PCM_TRIGGER_STOP:
spin_lock(&cable->lock); scoped_guard(spinlock, &cable->lock) {
cable->running &= ~stream; cable->running &= ~stream;
cable->pause &= ~stream; cable->pause &= ~stream;
err = cable->ops->stop(dpcm); err = cable->ops->stop(dpcm);
spin_unlock(&cable->lock); }
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
loopback_active_notify(dpcm); loopback_active_notify(dpcm);
break; break;
case SNDRV_PCM_TRIGGER_PAUSE_PUSH: case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
case SNDRV_PCM_TRIGGER_SUSPEND: case SNDRV_PCM_TRIGGER_SUSPEND:
spin_lock(&cable->lock); scoped_guard(spinlock, &cable->lock) {
cable->pause |= stream; cable->pause |= stream;
err = cable->ops->stop(dpcm); err = cable->ops->stop(dpcm);
spin_unlock(&cable->lock); }
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
loopback_active_notify(dpcm); loopback_active_notify(dpcm);
break; break;
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
case SNDRV_PCM_TRIGGER_RESUME: case SNDRV_PCM_TRIGGER_RESUME:
spin_lock(&cable->lock); scoped_guard(spinlock, &cable->lock) {
dpcm->last_jiffies = jiffies; dpcm->last_jiffies = jiffies;
cable->pause &= ~stream; cable->pause &= ~stream;
err = cable->ops->start(dpcm); err = cable->ops->start(dpcm);
spin_unlock(&cable->lock); }
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
loopback_active_notify(dpcm); loopback_active_notify(dpcm);
break; break;
@ -700,21 +700,22 @@ static unsigned int loopback_jiffies_timer_pos_update
static void loopback_jiffies_timer_function(struct timer_list *t) static void loopback_jiffies_timer_function(struct timer_list *t)
{ {
struct loopback_pcm *dpcm = timer_container_of(dpcm, t, timer); struct loopback_pcm *dpcm = timer_container_of(dpcm, t, timer);
unsigned long flags; bool period_elapsed = false;
spin_lock_irqsave(&dpcm->cable->lock, flags); scoped_guard(spinlock_irqsave, &dpcm->cable->lock) {
if (loopback_jiffies_timer_pos_update(dpcm->cable) & if (loopback_jiffies_timer_pos_update(dpcm->cable) &
(1 << dpcm->substream->stream)) { (1 << dpcm->substream->stream)) {
loopback_jiffies_timer_start(dpcm); loopback_jiffies_timer_start(dpcm);
if (dpcm->period_update_pending) { if (dpcm->period_update_pending) {
dpcm->period_update_pending = 0; dpcm->period_update_pending = 0;
spin_unlock_irqrestore(&dpcm->cable->lock, flags); period_elapsed = true;
/* need to unlock before calling below */ break;
snd_pcm_period_elapsed(dpcm->substream); }
return;
} }
} }
spin_unlock_irqrestore(&dpcm->cable->lock, flags);
if (period_elapsed)
snd_pcm_period_elapsed(dpcm->substream);
} }
/* call in cable->lock */ /* call in cable->lock */
@ -759,69 +760,69 @@ static void loopback_snd_timer_period_elapsed(struct loopback_cable *cable,
struct snd_pcm_substream *substream_play, *substream_capt; struct snd_pcm_substream *substream_play, *substream_capt;
struct snd_pcm_runtime *valid_runtime; struct snd_pcm_runtime *valid_runtime;
unsigned int running, elapsed_bytes; unsigned int running, elapsed_bytes;
unsigned long flags; bool xrun = false;
spin_lock_irqsave(&cable->lock, flags); scoped_guard(spinlock_irqsave, &cable->lock) {
running = cable->running ^ cable->pause; running = cable->running ^ cable->pause;
/* no need to do anything if no stream is running */ /* no need to do anything if no stream is running */
if (!running) { if (!running)
spin_unlock_irqrestore(&cable->lock, flags); return;
dpcm_play = cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
dpcm_capt = cable->streams[SNDRV_PCM_STREAM_CAPTURE];
if (event == SNDRV_TIMER_EVENT_MSTOP) {
if (!dpcm_play ||
dpcm_play->substream->runtime->state !=
SNDRV_PCM_STATE_DRAINING)
return;
}
substream_play = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
dpcm_play->substream : NULL;
substream_capt = (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) ?
dpcm_capt->substream : NULL;
valid_runtime = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
dpcm_play->substream->runtime :
dpcm_capt->substream->runtime;
/* resolution is only valid for SNDRV_TIMER_EVENT_TICK events */
if (event == SNDRV_TIMER_EVENT_TICK) {
/* The hardware rules guarantee that playback and capture period
* are the same. Therefore only one device has to be checked
* here.
*/
if (loopback_snd_timer_check_resolution(valid_runtime,
resolution) < 0) {
xrun = true;
break;
}
}
elapsed_bytes = frames_to_bytes(valid_runtime,
valid_runtime->period_size);
/* The same timer interrupt is used for playback and capture device */
if ((running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
(running & (1 << SNDRV_PCM_STREAM_CAPTURE))) {
copy_play_buf(dpcm_play, dpcm_capt, elapsed_bytes);
bytepos_finish(dpcm_play, elapsed_bytes);
bytepos_finish(dpcm_capt, elapsed_bytes);
} else if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
bytepos_finish(dpcm_play, elapsed_bytes);
} else if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
clear_capture_buf(dpcm_capt, elapsed_bytes);
bytepos_finish(dpcm_capt, elapsed_bytes);
}
}
if (xrun) {
if (substream_play)
snd_pcm_stop_xrun(substream_play);
if (substream_capt)
snd_pcm_stop_xrun(substream_capt);
return; return;
} }
dpcm_play = cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
dpcm_capt = cable->streams[SNDRV_PCM_STREAM_CAPTURE];
if (event == SNDRV_TIMER_EVENT_MSTOP) {
if (!dpcm_play ||
dpcm_play->substream->runtime->state !=
SNDRV_PCM_STATE_DRAINING) {
spin_unlock_irqrestore(&cable->lock, flags);
return;
}
}
substream_play = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
dpcm_play->substream : NULL;
substream_capt = (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) ?
dpcm_capt->substream : NULL;
valid_runtime = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
dpcm_play->substream->runtime :
dpcm_capt->substream->runtime;
/* resolution is only valid for SNDRV_TIMER_EVENT_TICK events */
if (event == SNDRV_TIMER_EVENT_TICK) {
/* The hardware rules guarantee that playback and capture period
* are the same. Therefore only one device has to be checked
* here.
*/
if (loopback_snd_timer_check_resolution(valid_runtime,
resolution) < 0) {
spin_unlock_irqrestore(&cable->lock, flags);
if (substream_play)
snd_pcm_stop_xrun(substream_play);
if (substream_capt)
snd_pcm_stop_xrun(substream_capt);
return;
}
}
elapsed_bytes = frames_to_bytes(valid_runtime,
valid_runtime->period_size);
/* The same timer interrupt is used for playback and capture device */
if ((running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
(running & (1 << SNDRV_PCM_STREAM_CAPTURE))) {
copy_play_buf(dpcm_play, dpcm_capt, elapsed_bytes);
bytepos_finish(dpcm_play, elapsed_bytes);
bytepos_finish(dpcm_capt, elapsed_bytes);
} else if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
bytepos_finish(dpcm_play, elapsed_bytes);
} else if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
clear_capture_buf(dpcm_capt, elapsed_bytes);
bytepos_finish(dpcm_capt, elapsed_bytes);
}
spin_unlock_irqrestore(&cable->lock, flags);
if (substream_play) if (substream_play)
snd_pcm_period_elapsed(substream_play); snd_pcm_period_elapsed(substream_play);
if (substream_capt) if (substream_capt)
@ -909,11 +910,10 @@ static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
struct loopback_pcm *dpcm = runtime->private_data; struct loopback_pcm *dpcm = runtime->private_data;
snd_pcm_uframes_t pos; snd_pcm_uframes_t pos;
spin_lock(&dpcm->cable->lock); guard(spinlock)(&dpcm->cable->lock);
if (dpcm->cable->ops->pos_update) if (dpcm->cable->ops->pos_update)
dpcm->cable->ops->pos_update(dpcm->cable); dpcm->cable->ops->pos_update(dpcm->cable);
pos = dpcm->buf_pos; pos = dpcm->buf_pos;
spin_unlock(&dpcm->cable->lock);
return bytes_to_frames(runtime, pos); return bytes_to_frames(runtime, pos);
} }
@ -1045,9 +1045,8 @@ static void free_cable(struct snd_pcm_substream *substream)
return; return;
if (cable->streams[!substream->stream]) { if (cable->streams[!substream->stream]) {
/* other stream is still alive */ /* other stream is still alive */
spin_lock_irq(&cable->lock); guard(spinlock_irq)(&cable->lock);
cable->streams[substream->stream] = NULL; cable->streams[substream->stream] = NULL;
spin_unlock_irq(&cable->lock);
} else { } else {
struct loopback_pcm *dpcm = substream->runtime->private_data; struct loopback_pcm *dpcm = substream->runtime->private_data;
@ -1313,9 +1312,9 @@ static int loopback_open(struct snd_pcm_substream *substream)
else else
runtime->hw = cable->hw; runtime->hw = cable->hw;
spin_lock_irq(&cable->lock); scoped_guard(spinlock_irq, &cable->lock) {
cable->streams[substream->stream] = dpcm; cable->streams[substream->stream] = dpcm;
spin_unlock_irq(&cable->lock); }
unlock: unlock:
if (err < 0) { if (err < 0) {