* 1000,
* myobject_operation_timer,
* task,
- * NULL);
+ * qio_task_free);
* }
* </programlisting>
* </example>
* the callback func 'myobject_operation_notify' shown
* earlier to deal with the results.
*
- * Once this function returns false, object_unref will be called
- * automatically on the task causing it to be released and the
- * ref on QMyObject dropped too.
+ * Once this function returns FALSE, the task will be freed,
+ * causing it release the ref on QMyObject too.
*
* The QIOTask module can also be used to perform operations
* in a background thread context, while still reporting the
* 'err' attribute in the task object to determine if
* the operation was successful or not.
*
- * The returned task will be released when qio_task_complete()
- * is invoked.
+ * The returned task must be released by calling
+ * qio_task_free() when no longer required.
*
* Returns: the task struct
*/
gpointer opaque,
GDestroyNotify destroy);
+/**
+ * qio_task_free:
+ * task: the task object to free
+ *
+ * Free the resources associated with the task. Typically
+ * the qio_task_complete() method will be called immediately
+ * before this to trigger the task callback, however, it is
+ * permissible to free the task in the case of cancellation.
+ * The destroy callback will be used to release the opaque
+ * data provided to qio_task_new().
+ */
+void qio_task_free(QIOTask *task);
+
/**
* qio_task_run_in_thread:
* @task: the task struct
* qio_task_complete:
* @task: the task struct
*
- * Invoke the completion callback for @task and
- * then free its memory.
+ * Invoke the completion callback for @task. This should typically
+ * only be invoked once on a task, and then qio_task_free() used
+ * to free it.
*/
void qio_task_complete(QIOTask *task);
trace_qio_channel_tls_handshake_fail(ioc);
qio_task_set_error(task, err);
qio_task_complete(task);
+ qio_task_free(task);
return;
}
trace_qio_channel_tls_credentials_allow(ioc);
}
qio_task_complete(task);
+ qio_task_free(task);
} else {
GIOCondition condition;
QIOChannelTLSData *data = g_new0(typeof(*data), 1);
trace_qio_channel_tls_bye_fail(ioc);
qio_task_set_error(task, err);
qio_task_complete(task);
+ qio_task_free(task);
return;
}
if (status == QCRYPTO_TLS_BYE_COMPLETE) {
qio_task_complete(task);
+ qio_task_free(task);
return;
}
trace_qio_channel_websock_handshake_fail(ioc, error_get_pretty(err));
qio_task_set_error(task, err);
qio_task_complete(task);
+ qio_task_free(task);
wioc->hs_io_tag = 0;
return FALSE;
}
trace_qio_channel_websock_handshake_complete(ioc);
qio_task_complete(task);
}
+ qio_task_free(task);
wioc->hs_io_tag = 0;
return FALSE;
}
trace_qio_channel_websock_handshake_fail(ioc, error_get_pretty(err));
qio_task_set_error(task, err);
qio_task_complete(task);
+ qio_task_free(task);
wioc->hs_io_tag = 0;
return FALSE;
}
return task;
}
-static void qio_task_free(QIOTask *task)
+void qio_task_free(QIOTask *task)
{
+ if (!task) {
+ return;
+ }
+
qemu_mutex_lock(&task->thread_lock);
if (task->thread) {
if (task->thread->destroy) {
trace_qio_task_thread_result(task);
qio_task_complete(task);
+ qio_task_free(task);
return FALSE;
}
{
task->func(task, task->opaque);
trace_qio_task_complete(task);
- qio_task_free(task);
}
src = qio_task_get_source(task);
qio_task_complete(task);
+ qio_task_free(task);
g_assert(obj == src);
}
+static void test_task_cancel(void)
+{
+ QIOTask *task;
+ Object *obj = object_new(TYPE_DUMMY);
+ Object *src;
+ struct TestTaskData data = { NULL, NULL, false };
+
+ task = qio_task_new(obj, task_callback, &data, NULL);
+ src = qio_task_get_source(task);
+
+ qio_task_free(task);
+
+ g_assert(obj == src);
+
+ object_unref(obj);
+
+ g_assert(data.source == NULL);
+ g_assert(data.err == NULL);
+ g_assert(data.freed == false);
+}
+
+
static void task_data_free(gpointer opaque)
{
struct TestTaskData *data = opaque;
task = qio_task_new(obj, task_callback, &data, task_data_free);
qio_task_complete(task);
+ qio_task_free(task);
object_unref(obj);
qio_task_set_error(task, err);
qio_task_complete(task);
+ qio_task_free(task);
object_unref(obj);
module_call_init(MODULE_INIT_QOM);
type_register_static(&dummy_info);
g_test_add_func("/crypto/task/complete", test_task_complete);
+ g_test_add_func("/crypto/task/cancel", test_task_cancel);
g_test_add_func("/crypto/task/datafree", test_task_data_free);
g_test_add_func("/crypto/task/failure", test_task_failure);
g_test_add_func("/crypto/task/thread_complete", test_task_thread_complete);