Skip to content

Commit

Permalink
ALSA: rawmidi: A lightweight function to discard pending bytes
Browse files Browse the repository at this point in the history
For discarding the pending bytes on rawmidi, we process with a loop of
snd_rawmidi_transmit() which is just a waste of CPU power.
Implement a lightweight API function to discard the pending bytes and
the proceed the ring buffer instantly, and use it instead of open
codes.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
  • Loading branch information
Takashi Iwai committed Oct 4, 2018
1 parent cd3b711 commit 6aea570
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions include/sound/rawmidi.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
unsigned char *buffer, int count);
int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream,
int count);
int snd_rawmidi_proceed(struct snd_rawmidi_substream *substream);

/* main midi functions */

Expand Down
22 changes: 22 additions & 0 deletions sound/core/rawmidi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,28 @@ int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
}
EXPORT_SYMBOL(snd_rawmidi_transmit);

/**
* snd_rawmidi_proceed - Discard the all pending bytes and proceed
* @substream: rawmidi substream
*
* Return: the number of discarded bytes
*/
int snd_rawmidi_proceed(struct snd_rawmidi_substream *substream)
{
struct snd_rawmidi_runtime *runtime = substream->runtime;
unsigned long flags;
int count = 0;

spin_lock_irqsave(&runtime->lock, flags);
if (runtime->avail < runtime->buffer_size) {
count = runtime->buffer_size - runtime->avail;
__snd_rawmidi_transmit_ack(substream, count);
}
spin_unlock_irqrestore(&runtime->lock, flags);
return count;
}
EXPORT_SYMBOL(snd_rawmidi_proceed);

static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
const unsigned char __user *userbuf,
const unsigned char *kernelbuf,
Expand Down
4 changes: 1 addition & 3 deletions sound/core/seq/seq_virmidi.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ static void snd_vmidi_output_work(struct work_struct *work)
/* discard the outputs in dispatch mode unless subscribed */
if (vmidi->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH &&
!(vmidi->rdev->flags & SNDRV_VIRMIDI_SUBSCRIBE)) {
char buf[32];
while (snd_rawmidi_transmit(substream, buf, sizeof(buf)) > 0)
; /* ignored */
snd_rawmidi_proceed(substream);
return;
}

Expand Down
3 changes: 1 addition & 2 deletions sound/usb/midi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1175,8 +1175,7 @@ static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream,
if (port->ep->umidi->disconnected) {
/* gobble up remaining bytes to prevent wait in
* snd_rawmidi_drain_output */
while (!snd_rawmidi_transmit_empty(substream))
snd_rawmidi_transmit_ack(substream, 1);
snd_rawmidi_proceed(substream);
return;
}
tasklet_schedule(&port->ep->tasklet);
Expand Down

0 comments on commit 6aea570

Please sign in to comment.