Commit 1250932e authored by Jaroslav Kysela's avatar Jaroslav Kysela

ALSA: pcm_lib - optimize wake_up() calls for PCM I/O

As noted by pl bossart <bossart.nospam@gmail.com>, the PCM I/O routines
(snd_pcm_lib_write1, snd_pcm_lib_read1) should block wake_up() calls
until all samples are not processed.
Signed-off-by: default avatarJaroslav Kysela <perex@perex.cz>
parent f240406b
...@@ -311,6 +311,7 @@ struct snd_pcm_runtime { ...@@ -311,6 +311,7 @@ struct snd_pcm_runtime {
struct snd_pcm_mmap_control *control; struct snd_pcm_mmap_control *control;
/* -- locking / scheduling -- */ /* -- locking / scheduling -- */
unsigned int nowake: 1; /* no wakeup (data-copy in progress) */
wait_queue_head_t sleep; wait_queue_head_t sleep;
struct fasync_struct *fasync; struct fasync_struct *fasync;
...@@ -839,6 +840,8 @@ void snd_pcm_set_sync(struct snd_pcm_substream *substream); ...@@ -839,6 +840,8 @@ void snd_pcm_set_sync(struct snd_pcm_substream *substream);
int snd_pcm_lib_interleave_len(struct snd_pcm_substream *substream); int snd_pcm_lib_interleave_len(struct snd_pcm_substream *substream);
int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream, int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
unsigned int cmd, void *arg); unsigned int cmd, void *arg);
int snd_pcm_update_state(struct snd_pcm_substream *substream,
struct snd_pcm_runtime *runtime);
int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream); int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream);
int snd_pcm_playback_xrun_check(struct snd_pcm_substream *substream); int snd_pcm_playback_xrun_check(struct snd_pcm_substream *substream);
int snd_pcm_capture_xrun_check(struct snd_pcm_substream *substream); int snd_pcm_capture_xrun_check(struct snd_pcm_substream *substream);
......
...@@ -263,8 +263,8 @@ static void xrun_log_show(struct snd_pcm_substream *substream) ...@@ -263,8 +263,8 @@ static void xrun_log_show(struct snd_pcm_substream *substream)
#endif #endif
static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream, int snd_pcm_update_state(struct snd_pcm_substream *substream,
struct snd_pcm_runtime *runtime) struct snd_pcm_runtime *runtime)
{ {
snd_pcm_uframes_t avail; snd_pcm_uframes_t avail;
...@@ -285,7 +285,7 @@ static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream, ...@@ -285,7 +285,7 @@ static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
return -EPIPE; return -EPIPE;
} }
} }
if (avail >= runtime->control->avail_min) if (!runtime->nowake && avail >= runtime->control->avail_min)
wake_up(&runtime->sleep); wake_up(&runtime->sleep);
return 0; return 0;
} }
...@@ -441,7 +441,7 @@ static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream, ...@@ -441,7 +441,7 @@ static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp); snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
return snd_pcm_update_hw_ptr_post(substream, runtime); return snd_pcm_update_state(substream, runtime);
} }
/* CAUTION: call it with irq disabled */ /* CAUTION: call it with irq disabled */
...@@ -1792,6 +1792,7 @@ static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream, ...@@ -1792,6 +1792,7 @@ static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
goto _end_unlock; goto _end_unlock;
} }
runtime->nowake = 1;
while (size > 0) { while (size > 0) {
snd_pcm_uframes_t frames, appl_ptr, appl_ofs; snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
snd_pcm_uframes_t avail; snd_pcm_uframes_t avail;
...@@ -1813,15 +1814,17 @@ static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream, ...@@ -1813,15 +1814,17 @@ static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
if (frames > cont) if (frames > cont)
frames = cont; frames = cont;
if (snd_BUG_ON(!frames)) { if (snd_BUG_ON(!frames)) {
runtime->nowake = 0;
snd_pcm_stream_unlock_irq(substream); snd_pcm_stream_unlock_irq(substream);
return -EINVAL; return -EINVAL;
} }
appl_ptr = runtime->control->appl_ptr; appl_ptr = runtime->control->appl_ptr;
appl_ofs = appl_ptr % runtime->buffer_size; appl_ofs = appl_ptr % runtime->buffer_size;
snd_pcm_stream_unlock_irq(substream); snd_pcm_stream_unlock_irq(substream);
if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0) err = transfer(substream, appl_ofs, data, offset, frames);
goto _end;
snd_pcm_stream_lock_irq(substream); snd_pcm_stream_lock_irq(substream);
if (err < 0)
goto _end_unlock;
switch (runtime->status->state) { switch (runtime->status->state) {
case SNDRV_PCM_STATE_XRUN: case SNDRV_PCM_STATE_XRUN:
err = -EPIPE; err = -EPIPE;
...@@ -1850,8 +1853,10 @@ static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream, ...@@ -1850,8 +1853,10 @@ static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
} }
} }
_end_unlock: _end_unlock:
runtime->nowake = 0;
if (xfer > 0 && err >= 0)
snd_pcm_update_state(substream, runtime);
snd_pcm_stream_unlock_irq(substream); snd_pcm_stream_unlock_irq(substream);
_end:
return xfer > 0 ? (snd_pcm_sframes_t)xfer : err; return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
} }
...@@ -2009,6 +2014,7 @@ static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream, ...@@ -2009,6 +2014,7 @@ static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
goto _end_unlock; goto _end_unlock;
} }
runtime->nowake = 1;
while (size > 0) { while (size > 0) {
snd_pcm_uframes_t frames, appl_ptr, appl_ofs; snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
snd_pcm_uframes_t avail; snd_pcm_uframes_t avail;
...@@ -2037,15 +2043,17 @@ static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream, ...@@ -2037,15 +2043,17 @@ static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
if (frames > cont) if (frames > cont)
frames = cont; frames = cont;
if (snd_BUG_ON(!frames)) { if (snd_BUG_ON(!frames)) {
runtime->nowake = 0;
snd_pcm_stream_unlock_irq(substream); snd_pcm_stream_unlock_irq(substream);
return -EINVAL; return -EINVAL;
} }
appl_ptr = runtime->control->appl_ptr; appl_ptr = runtime->control->appl_ptr;
appl_ofs = appl_ptr % runtime->buffer_size; appl_ofs = appl_ptr % runtime->buffer_size;
snd_pcm_stream_unlock_irq(substream); snd_pcm_stream_unlock_irq(substream);
if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0) err = transfer(substream, appl_ofs, data, offset, frames);
goto _end;
snd_pcm_stream_lock_irq(substream); snd_pcm_stream_lock_irq(substream);
if (err < 0)
goto _end_unlock;
switch (runtime->status->state) { switch (runtime->status->state) {
case SNDRV_PCM_STATE_XRUN: case SNDRV_PCM_STATE_XRUN:
err = -EPIPE; err = -EPIPE;
...@@ -2068,8 +2076,10 @@ static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream, ...@@ -2068,8 +2076,10 @@ static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
xfer += frames; xfer += frames;
} }
_end_unlock: _end_unlock:
runtime->nowake = 0;
if (xfer > 0 && err >= 0)
snd_pcm_update_state(substream, runtime);
snd_pcm_stream_unlock_irq(substream); snd_pcm_stream_unlock_irq(substream);
_end:
return xfer > 0 ? (snd_pcm_sframes_t)xfer : err; return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
} }
......
...@@ -516,6 +516,7 @@ static int snd_pcm_sw_params(struct snd_pcm_substream *substream, ...@@ -516,6 +516,7 @@ static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
struct snd_pcm_sw_params *params) struct snd_pcm_sw_params *params)
{ {
struct snd_pcm_runtime *runtime; struct snd_pcm_runtime *runtime;
int err;
if (PCM_RUNTIME_CHECK(substream)) if (PCM_RUNTIME_CHECK(substream))
return -ENXIO; return -ENXIO;
...@@ -540,6 +541,7 @@ static int snd_pcm_sw_params(struct snd_pcm_substream *substream, ...@@ -540,6 +541,7 @@ static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
if (params->silence_threshold > runtime->buffer_size) if (params->silence_threshold > runtime->buffer_size)
return -EINVAL; return -EINVAL;
} }
err = 0;
snd_pcm_stream_lock_irq(substream); snd_pcm_stream_lock_irq(substream);
runtime->tstamp_mode = params->tstamp_mode; runtime->tstamp_mode = params->tstamp_mode;
runtime->period_step = params->period_step; runtime->period_step = params->period_step;
...@@ -553,10 +555,10 @@ static int snd_pcm_sw_params(struct snd_pcm_substream *substream, ...@@ -553,10 +555,10 @@ static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
runtime->silence_size > 0) runtime->silence_size > 0)
snd_pcm_playback_silence(substream, ULONG_MAX); snd_pcm_playback_silence(substream, ULONG_MAX);
wake_up(&runtime->sleep); err = snd_pcm_update_state(substream, runtime);
} }
snd_pcm_stream_unlock_irq(substream); snd_pcm_stream_unlock_irq(substream);
return 0; return err;
} }
static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream, static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment