All of lore.kernel.org
 help / color / mirror / Atom feed
From: Takashi Sakamoto <o-takashi@sakamocchi.jp>
To: tiwai@suse.de, perex@perex.cz
Cc: alsa-devel@alsa-project.org
Subject: [PATCH 28/35] axfer: add a common interface of waiter for I/O event notification
Date: Tue, 13 Nov 2018 15:41:40 +0900	[thread overview]
Message-ID: <20181113064147.13577-28-o-takashi@sakamocchi.jp> (raw)
In-Reply-To: <20181113064147.13577-1-o-takashi@sakamocchi.jp>

There're several types of system calls for multiplexed I/O. They're used to
receive notifications of I/O events. Typically, userspace applications call
them against file descriptor to yield CPU. When I/O is enabled on any of
the descriptors, a task of the application is rescheduled, then the
application execute I/O calls.

This commit adds a common interface for this type of system calls, named as
'waiter'. This is expected to be used with non-blocking file operation and
operations on mapped page frame.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
---
 axfer/Makefile.am               |  5 +-
 axfer/waiter.c                  | 99 +++++++++++++++++++++++++++++++++
 axfer/waiter.h                  | 54 ++++++++++++++++++
 axfer/xfer-libasound-irq-mmap.c | 14 ++++-
 axfer/xfer-libasound-irq-rw.c   | 26 ++++++++-
 axfer/xfer-libasound.c          | 76 +++++++++++++++++++++++++
 axfer/xfer-libasound.h          |  7 +++
 7 files changed, 277 insertions(+), 4 deletions(-)
 create mode 100644 axfer/waiter.c
 create mode 100644 axfer/waiter.h

diff --git a/axfer/Makefile.am b/axfer/Makefile.am
index 960811e..e425a28 100644
--- a/axfer/Makefile.am
+++ b/axfer/Makefile.am
@@ -22,6 +22,7 @@ noinst_HEADERS = \
 	xfer.h \
 	xfer-libasound.h \
 	frame-cache.h
+	waiter.h
 
 axfer_SOURCES = \
 	misc.h \
@@ -47,4 +48,6 @@ axfer_SOURCES = \
 	frame-cache.c \
 	xfer-libasound-irq-rw.c \
 	subcmd-transfer.c \
-	xfer-libasound-irq-mmap.c
+	xfer-libasound-irq-mmap.c \
+	waiter.h \
+	waiter.c
diff --git a/axfer/waiter.c b/axfer/waiter.c
new file mode 100644
index 00000000..0bc4740
--- /dev/null
+++ b/axfer/waiter.c
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0
+//
+// waiter.c - I/O event waiter.
+//
+// Copyright (c) 2018 Takashi Sakamoto <o-takashi@sakamocchi.jp>
+//
+// Licensed under the terms of the GNU General Public License, version 2.
+
+#include "waiter.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include "misc.h"
+
+static const char *const waiter_type_labels[] = {
+	[WAITER_TYPE_DEFAULT] = "default",
+};
+
+enum waiter_type waiter_type_from_label(const char *label)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(waiter_type_labels); ++i) {
+		if (!strcmp(waiter_type_labels[i], label))
+			return i;
+	}
+
+	return WAITER_TYPE_DEFAULT;
+}
+
+const char *waiter_label_from_type(enum waiter_type type)
+{
+	return waiter_type_labels[type];
+}
+
+int waiter_context_init(struct waiter_context *waiter,
+			enum waiter_type type, unsigned int pfd_count)
+{
+	struct {
+		enum waiter_type type;
+		const struct waiter_data *waiter;
+	} entries[] = {
+		{WAITER_TYPE_COUNT,	NULL},
+	};
+	int i;
+
+	if (pfd_count == 0)
+		return -EINVAL;
+
+	for (i = 0; i < ARRAY_SIZE(entries); ++i) {
+		if (entries[i].type == type)
+			break;
+	}
+	if (i == ARRAY_SIZE(entries))
+		return -EINVAL;
+
+	waiter->private_data = malloc(entries[i].waiter->private_size);
+	if (waiter->private_data == NULL)
+		return -ENOMEM;
+	memset(waiter->private_data, 0, entries[i].waiter->private_size);
+
+	waiter->type = type;
+	waiter->ops = &entries[i].waiter->ops;
+
+	waiter->pfds = calloc(pfd_count, sizeof(*waiter->pfds));
+	if (waiter->pfds == NULL)
+		return -ENOMEM;
+	waiter->pfd_count = pfd_count;
+
+	return 0;
+}
+
+int waiter_context_prepare(struct waiter_context *waiter)
+{
+	return waiter->ops->prepare(waiter);
+}
+
+int waiter_context_wait_event(struct waiter_context *waiter,
+				int timeout_msec)
+{
+	return waiter->ops->wait_event(waiter, timeout_msec);
+}
+
+void waiter_context_release(struct waiter_context *waiter)
+{
+	waiter->ops->release(waiter);
+}
+
+void waiter_context_destroy(struct waiter_context *waiter)
+{
+	if (waiter->pfds)
+		free(waiter->pfds);
+	waiter->pfd_count = 0;
+	if (waiter->private_data)
+		free(waiter->private_data);
+	waiter->private_data = NULL;
+}
diff --git a/axfer/waiter.h b/axfer/waiter.h
new file mode 100644
index 00000000..17e01cb
--- /dev/null
+++ b/axfer/waiter.h
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: GPL-2.0
+//
+// waiter.h - a header for I/O event waiter.
+//
+// Copyright (c) 2018 Takashi Sakamoto <o-takashi@sakamocchi.jp>
+//
+// Licensed under the terms of the GNU General Public License, version 2.
+
+#ifndef __ALSA_UTILS_AXFER_WAITER__H_
+#define __ALSA_UTILS_AXFER_WAITER__H_
+
+#include <poll.h>
+
+enum waiter_type {
+	WAITER_TYPE_DEFAULT = 0,
+	WAITER_TYPE_COUNT,
+};
+
+struct waiter_ops;
+
+struct waiter_context {
+	enum waiter_type type;
+	const struct waiter_ops *ops;
+	void *private_data;
+
+	struct pollfd *pfds;
+	unsigned int pfd_count;
+};
+
+enum waiter_type waiter_type_from_label(const char *label);
+const char *waiter_label_from_type(enum waiter_type type);
+
+int waiter_context_init(struct waiter_context *waiter,
+			enum waiter_type type, unsigned int pfd_count);
+int waiter_context_prepare(struct waiter_context *waiter);
+int waiter_context_wait_event(struct waiter_context *waiter,
+				int timeout_msec);
+void waiter_context_release(struct waiter_context *waiter);
+void waiter_context_destroy(struct waiter_context *waiter);
+
+// For internal use in 'waiter' module.
+
+struct waiter_ops {
+	int (*prepare)(struct waiter_context *waiter);
+	int (*wait_event)(struct waiter_context *waiter, int timeout_msec);
+	void (*release)(struct waiter_context *waiter);
+};
+
+struct waiter_data {
+	struct waiter_ops ops;
+	unsigned int private_size;
+};
+
+#endif
diff --git a/axfer/xfer-libasound-irq-mmap.c b/axfer/xfer-libasound-irq-mmap.c
index 18f6dfe..0c96ee5 100644
--- a/axfer/xfer-libasound-irq-mmap.c
+++ b/axfer/xfer-libasound-irq-mmap.c
@@ -82,10 +82,22 @@ static int irq_mmap_process_frames(struct libasound_state *state,
 	int err;
 
 	if (state->use_waiter) {
+		unsigned short revents;
+
 		// Wait for hardware IRQ when no avail space in buffer.
-		err = snd_pcm_wait(state->handle, -1);
+		err = xfer_libasound_wait_event(state, -1, &revents);
 		if (err < 0)
 			return err;
+		if (revents & POLLERR) {
+			// TODO: error reporting?
+			return -EIO;
+		}
+		if (!(revents & (POLLIN | POLLOUT)))
+			return -EAGAIN;
+
+		// When rescheduled, current position of data transmission was
+		// queried to actual hardware by a handler of IRQ. No need to
+		// perform it; e.g. ioctl(2) with SNDRV_PCM_IOCTL_HWSYNC.
 	}
 
 	// Sync cache in user space to data in kernel space to calculate avail
diff --git a/axfer/xfer-libasound-irq-rw.c b/axfer/xfer-libasound-irq-rw.c
index 625c095..cf4155f 100644
--- a/axfer/xfer-libasound-irq-rw.c
+++ b/axfer/xfer-libasound-irq-rw.c
@@ -134,10 +134,21 @@ static int r_process_frames_nonblocking(struct libasound_state *state,
 	}
 
 	if (state->use_waiter) {
+		unsigned short revents;
+
 		// Wait for hardware IRQ when no available space.
-		err = snd_pcm_wait(state->handle, -1);
+		err = xfer_libasound_wait_event(state, -1, &revents);
 		if (err < 0)
 			goto error;
+		if (revents & POLLERR) {
+			// TODO: error reporting.
+			err = -EIO;
+			goto error;
+		}
+		if (!(revents & POLLIN)) {
+			err = -EAGAIN;
+			goto error;
+		}
 	}
 
 	// Check available space on the buffer.
@@ -289,10 +300,21 @@ static int w_process_frames_nonblocking(struct libasound_state *state,
 	int err;
 
 	if (state->use_waiter) {
+		unsigned short revents;
+
 		// Wait for hardware IRQ when no left space.
-		err = snd_pcm_wait(state->handle, -1);
+		err = xfer_libasound_wait_event(state, -1, &revents);
 		if (err < 0)
 			goto error;
+		if (revents & POLLERR) {
+			// TODO: error reporting.
+			err = -EIO;
+			goto error;
+		}
+		if (!(revents & POLLOUT)) {
+			err = -EAGAIN;
+			goto error;
+		}
 	}
 
 	// Check available space on the buffer.
diff --git a/axfer/xfer-libasound.c b/axfer/xfer-libasound.c
index a731423..5cef9f1 100644
--- a/axfer/xfer-libasound.c
+++ b/axfer/xfer-libasound.c
@@ -201,6 +201,7 @@ static int open_handle(struct xfer_context *xfer)
 
 	if ((state->nonblock || state->mmap) && !state->test_nowait)
 		state->use_waiter = true;
+	state->waiter_type = WAITER_TYPE_DEFAULT;
 
 	err = snd_pcm_hw_params_any(state->handle, state->hw_params);
 	if (err < 0)
@@ -220,6 +221,66 @@ static int open_handle(struct xfer_context *xfer)
 	return set_access_hw_param(state);
 }
 
+static int prepare_waiter(struct libasound_state *state)
+{
+	unsigned int pfd_count;
+	int err;
+
+	// Nothing to do for dafault waiter (=snd_pcm_wait()).
+	if (state->waiter_type == WAITER_TYPE_DEFAULT)
+		return 0;
+
+	err = snd_pcm_poll_descriptors_count(state->handle);
+	if (err < 0)
+		return err;
+	if (err == 0)
+		return -ENXIO;
+	pfd_count = (unsigned int)err;
+
+	state->waiter = malloc(sizeof(*state->waiter));
+	if (state->waiter == NULL)
+		return -ENOMEM;
+
+	err = waiter_context_init(state->waiter, state->waiter_type, pfd_count);
+	if (err < 0)
+		return err;
+
+	err = snd_pcm_poll_descriptors(state->handle, state->waiter->pfds,
+				       pfd_count);
+	if (err < 0)
+		return err;
+
+	return waiter_context_prepare(state->waiter);
+}
+
+int xfer_libasound_wait_event(struct libasound_state *state, int timeout_msec,
+			      unsigned short *revents)
+{
+	int err;
+
+	if (state->waiter_type != WAITER_TYPE_DEFAULT) {
+		struct waiter_context *waiter = state->waiter;
+
+		err = waiter_context_wait_event(waiter, timeout_msec);
+		if (err < 0)
+			return err;
+
+		err = snd_pcm_poll_descriptors_revents(state->handle,
+				waiter->pfds, waiter->pfd_count, revents);
+	} else {
+		err = snd_pcm_wait(state->handle, timeout_msec);
+		if (err < 0)
+			return err;
+
+		if (snd_pcm_stream(state->handle) == SND_PCM_STREAM_PLAYBACK)
+			*revents = POLLOUT;
+		else
+			*revents = POLLIN;
+	}
+
+	return err;
+}
+
 static int configure_hw_params(struct libasound_state *state,
 			       snd_pcm_format_t format,
 			       unsigned int samples_per_frame,
@@ -559,6 +620,21 @@ static int xfer_libasound_pre_process(struct xfer_context *xfer,
 	if (xfer->verbose > 0)
 		snd_pcm_dump(state->handle, state->log);
 
+	if (state->use_waiter) {
+		// NOTE: This should be after configuring sw_params due to
+		// timer descriptor for time-based scheduling model.
+		err = prepare_waiter(state);
+		if (err < 0)
+			return err;
+
+		if (xfer->verbose > 0) {
+			logging(state, "Waiter type:\n");
+			logging(state,
+				"  %s\n",
+				waiter_label_from_type(state->waiter_type));
+		}
+	}
+
 	return 0;
 }
 
diff --git a/axfer/xfer-libasound.h b/axfer/xfer-libasound.h
index cf940e5..0bcfb40 100644
--- a/axfer/xfer-libasound.h
+++ b/axfer/xfer-libasound.h
@@ -10,6 +10,7 @@
 #define __ALSA_UTILS_AXFER_XFER_LIBASOUND__H_
 
 #include "xfer.h"
+#include "waiter.h"
 
 #define logging(state, ...) \
 	snd_output_printf(state->log, __VA_ARGS__)
@@ -49,6 +50,9 @@ struct libasound_state {
 	bool no_softvol:1;
 
 	bool use_waiter:1;
+
+	enum waiter_type waiter_type;
+	struct waiter_context *waiter;
 };
 
 // For internal use in 'libasound' module.
@@ -63,6 +67,9 @@ struct xfer_libasound_ops {
 	unsigned int private_size;
 };
 
+int xfer_libasound_wait_event(struct libasound_state *state, int timeout_msec,
+			      unsigned short *revents);
+
 extern const struct xfer_libasound_ops xfer_libasound_irq_rw_ops;
 
 extern const struct xfer_libasound_ops xfer_libasound_irq_mmap_r_ops;
-- 
2.19.1

  parent reply	other threads:[~2018-11-13  6:42 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1542090296222634077-webhooks-bot@alsa-project.org>
2018-11-13  6:24 ` alsa-utils: axfer: rewrite aplay, adding 'timer-based scheduling' option GitHub pull_request - opened
2018-11-13  6:41   ` [PATCH 01/35] axfer: add an entry point for this command Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 02/35] axfer: add a sub-command to print list of PCMs/devices Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 03/35] axfer: add a common interface to handle a file with audio-specific data format Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 04/35] axfer: add support for a container of Microsoft/IBM RIFF/Wave format Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 05/35] axfer: add support for a container of Sparc AU format Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 06/35] axfer: add support for a container of Creative Tech. voice format Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 07/35] axfer: add support for a container of raw data Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 08/35] axfer: add unit test for container interface Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 09/35] axfer: add a common interface to align data frames on different layout Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 10/35] axfer: add support for a mapper for single target Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 11/35] axfer: add support for a mapper for multiple target Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 12/35] axfer: add a unit test for mapper interface Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 13/35] axfer: add a common interface to transfer data frames Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 14/35] axfer: add a parser for command-line options Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 15/35] axfer: add support to transfer data frames by alsa-lib PCM APIs Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 16/35] axfer: add support for blocking data transmission operation of alsa-lib PCM API Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 17/35] axfer: add a sub-command to transfer data frames Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 18/35] axfer: add informative output and an option to suppress it Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 19/35] axfer: add an option to dump available hardware parameters Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 20/35] axfer: add options related to duration and obsolete '--max-file-size' option Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 21/35] axfer: add an option to finish transmission at XRUN Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 22/35] axfer: add support for non-blocking operation Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 23/35] axfer: add support for MMAP PCM operation Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 24/35] axfer: add an option to suppress event waiting Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 25/35] axfer: add options for buffer arrangement Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 26/35] axfer: add options for software parameters of PCM substream Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 27/35] axfer: add options for plugins in alsa-lib Takashi Sakamoto
2018-11-13  6:41     ` Takashi Sakamoto [this message]
2018-11-13  6:41     ` [PATCH 29/35] axfer: add an option for waiter type Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 30/35] axfer: add an implementation of waiter for poll(2) Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 31/35] axfer: add an implementation of waiter for select(2) Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 32/35] axfer: add an implementation of waiter for epoll(7) Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 33/35] axfer: add support for timer-based scheduling model with MMAP operation Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 34/35] axfer: obsolete some unimplemented options Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 35/35] axfer: add support for libffado transmission backend Takashi Sakamoto
2018-11-13 11:27     ` [PATCH 01/35] axfer: add an entry point for this command Takashi Iwai
2018-11-14 16:05       ` Takashi Sakamoto
2018-11-14 16:23         ` Takashi Iwai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181113064147.13577-28-o-takashi@sakamocchi.jp \
    --to=o-takashi@sakamocchi.jp \
    --cc=alsa-devel@alsa-project.org \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.