aboutsummaryrefslogtreecommitdiff
path: root/io/task.c
diff options
context:
space:
mode:
authorPeter Maydell <peter.maydell@linaro.org>2019-02-14 14:34:32 +0000
committerPeter Maydell <peter.maydell@linaro.org>2019-02-14 14:34:32 +0000
commitc4c5f6573a93dfbd351c41a27ea29a662d7445fa (patch)
treec4fd3d30fd6025ac4bec8a6c245ab5d57849c596 /io/task.c
parent4856c2c70c87d7a76c8ea208e7568f5637e78840 (diff)
parentf7ea2038bea04628eaa55156fc34edf9d0c4a2bb (diff)
Merge remote-tracking branch 'remotes/elmarco/tags/chardev-pull-request' into staging
Chardev fixes # gpg: Signature made Wed 13 Feb 2019 16:18:36 GMT # gpg: using RSA key DAE8E10975969CE5 # gpg: Good signature from "Marc-André Lureau <marcandre.lureau@redhat.com>" [full] # gpg: aka "Marc-André Lureau <marcandre.lureau@gmail.com>" [full] # Primary key fingerprint: 87A9 BD93 3F87 C606 D276 F62D DAE8 E109 7596 9CE5 * remotes/elmarco/tags/chardev-pull-request: (25 commits) char-pty: remove write_lock usage char-pty: remove the check for connection on write chardev: add a note about frontend sources and context switch terminal3270: do not use backend timer sources char: update the mux handlers in class callback chardev/wctablet: Fix a typo char: allow specifying a GMainContext at opening time chardev: ensure termios is fully initialized tests: expand coverage of socket chardev test chardev: fix race with client connections in tcp_chr_wait_connected chardev: disallow TLS/telnet/websocket with tcp_chr_wait_connected chardev: honour the reconnect setting in tcp_chr_wait_connected chardev: use a state machine for socket connection state chardev: split up qmp_chardev_open_socket connection code chardev: split tcp_chr_wait_connected into two methods chardev: remove unused 'sioc' variable & cleanup paths chardev: ensure qemu_chr_parse_compat reports missing driver error chardev: remove many local variables in qemu_chr_parse_socket chardev: forbid 'wait' option with client sockets chardev: forbid 'reconnect' option with server sockets ... Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Diffstat (limited to 'io/task.c')
-rw-r--r--io/task.c101
1 files changed, 69 insertions, 32 deletions
diff --git a/io/task.c b/io/task.c
index 2886a2c1bc..64c4c7126a 100644
--- a/io/task.c
+++ b/io/task.c
@@ -24,6 +24,15 @@
#include "qemu/thread.h"
#include "trace.h"
+struct QIOTaskThreadData {
+ QIOTaskWorker worker;
+ gpointer opaque;
+ GDestroyNotify destroy;
+ GMainContext *context;
+ GSource *completion;
+};
+
+
struct QIOTask {
Object *source;
QIOTaskFunc func;
@@ -32,6 +41,9 @@ struct QIOTask {
Error *err;
gpointer result;
GDestroyNotify destroyResult;
+ QemuMutex thread_lock;
+ QemuCond thread_cond;
+ struct QIOTaskThreadData *thread;
};
@@ -49,6 +61,8 @@ QIOTask *qio_task_new(Object *source,
task->func = func;
task->opaque = opaque;
task->destroy = destroy;
+ qemu_mutex_init(&task->thread_lock);
+ qemu_cond_init(&task->thread_cond);
trace_qio_task_new(task, source, func, opaque);
@@ -57,6 +71,19 @@ QIOTask *qio_task_new(Object *source,
static void qio_task_free(QIOTask *task)
{
+ qemu_mutex_lock(&task->thread_lock);
+ if (task->thread) {
+ if (task->thread->destroy) {
+ task->thread->destroy(task->thread->opaque);
+ }
+
+ if (task->thread->context) {
+ g_main_context_unref(task->thread->context);
+ }
+
+ g_free(task->thread);
+ }
+
if (task->destroy) {
task->destroy(task->opaque);
}
@@ -68,35 +95,20 @@ static void qio_task_free(QIOTask *task)
}
object_unref(task->source);
+ qemu_mutex_unlock(&task->thread_lock);
+ qemu_mutex_destroy(&task->thread_lock);
+ qemu_cond_destroy(&task->thread_cond);
+
g_free(task);
}
-struct QIOTaskThreadData {
- QIOTask *task;
- QIOTaskWorker worker;
- gpointer opaque;
- GDestroyNotify destroy;
- GMainContext *context;
-};
-
-
static gboolean qio_task_thread_result(gpointer opaque)
{
- struct QIOTaskThreadData *data = opaque;
-
- trace_qio_task_thread_result(data->task);
- qio_task_complete(data->task);
-
- if (data->destroy) {
- data->destroy(data->opaque);
- }
-
- if (data->context) {
- g_main_context_unref(data->context);
- }
+ QIOTask *task = opaque;
- g_free(data);
+ trace_qio_task_thread_result(task);
+ qio_task_complete(task);
return FALSE;
}
@@ -104,22 +116,30 @@ static gboolean qio_task_thread_result(gpointer opaque)
static gpointer qio_task_thread_worker(gpointer opaque)
{
- struct QIOTaskThreadData *data = opaque;
- GSource *idle;
+ QIOTask *task = opaque;
- trace_qio_task_thread_run(data->task);
- data->worker(data->task, data->opaque);
+ trace_qio_task_thread_run(task);
+
+ task->thread->worker(task, task->thread->opaque);
/* We're running in the background thread, and must only
* ever report the task results in the main event loop
* thread. So we schedule an idle callback to report
* the worker results
*/
- trace_qio_task_thread_exit(data->task);
+ trace_qio_task_thread_exit(task);
+
+ qemu_mutex_lock(&task->thread_lock);
- idle = g_idle_source_new();
- g_source_set_callback(idle, qio_task_thread_result, data, NULL);
- g_source_attach(idle, data->context);
+ task->thread->completion = g_idle_source_new();
+ g_source_set_callback(task->thread->completion,
+ qio_task_thread_result, task, NULL);
+ g_source_attach(task->thread->completion,
+ task->thread->context);
+ trace_qio_task_thread_source_attach(task, task->thread->completion);
+
+ qemu_cond_signal(&task->thread_cond);
+ qemu_mutex_unlock(&task->thread_lock);
return NULL;
}
@@ -138,21 +158,38 @@ void qio_task_run_in_thread(QIOTask *task,
g_main_context_ref(context);
}
- data->task = task;
data->worker = worker;
data->opaque = opaque;
data->destroy = destroy;
data->context = context;
+ task->thread = data;
+
trace_qio_task_thread_start(task, worker, opaque);
qemu_thread_create(&thread,
"io-task-worker",
qio_task_thread_worker,
- data,
+ task,
QEMU_THREAD_DETACHED);
}
+void qio_task_wait_thread(QIOTask *task)
+{
+ qemu_mutex_lock(&task->thread_lock);
+ g_assert(task->thread != NULL);
+ while (task->thread->completion == NULL) {
+ qemu_cond_wait(&task->thread_cond, &task->thread_lock);
+ }
+
+ trace_qio_task_thread_source_cancel(task, task->thread->completion);
+ g_source_destroy(task->thread->completion);
+ qemu_mutex_unlock(&task->thread_lock);
+
+ qio_task_thread_result(task);
+}
+
+
void qio_task_complete(QIOTask *task)
{
task->func(task, task->opaque);