GSimpleAsyncResult

GSimpleAsyncResult — Simple asynchronous results implementation

Synopsis

#include <gio/gio.h>

                    GSimpleAsyncResult;
void                (*GSimpleAsyncThreadFunc)           (GSimpleAsyncResult *res,
                                                         GObject *object,
                                                         GCancellable *cancellable);
GSimpleAsyncResult * g_simple_async_result_new          (GObject *source_object,
                                                         GAsyncReadyCallback callback,
                                                         gpointer user_data,
                                                         gpointer source_tag);
GSimpleAsyncResult * g_simple_async_result_new_error    (GObject *source_object,
                                                         GAsyncReadyCallback callback,
                                                         gpointer user_data,
                                                         GQuark domain,
                                                         gint code,
                                                         const char *format,
                                                         ...);
GSimpleAsyncResult * g_simple_async_result_new_from_error
                                                        (GObject *source_object,
                                                         GAsyncReadyCallback callback,
                                                         gpointer user_data,
                                                         GError *error);
void                g_simple_async_result_set_op_res_gpointer
                                                        (GSimpleAsyncResult *simple,
                                                         gpointer op_res,
                                                         GDestroyNotify destroy_op_res);
gpointer            g_simple_async_result_get_op_res_gpointer
                                                        (GSimpleAsyncResult *simple);
void                g_simple_async_result_set_op_res_gssize
                                                        (GSimpleAsyncResult *simple,
                                                         gssize op_res);
gssize              g_simple_async_result_get_op_res_gssize
                                                        (GSimpleAsyncResult *simple);
void                g_simple_async_result_set_op_res_gboolean
                                                        (GSimpleAsyncResult *simple,
                                                         gboolean op_res);
gboolean            g_simple_async_result_get_op_res_gboolean
                                                        (GSimpleAsyncResult *simple);
gpointer            g_simple_async_result_get_source_tag
                                                        (GSimpleAsyncResult *simple);
gboolean            g_simple_async_result_is_valid      (GAsyncResult *result,
                                                         GObject *source,
                                                         gpointer source_tag);
void                g_simple_async_result_set_handle_cancellation
                                                        (GSimpleAsyncResult *simple,
                                                         gboolean handle_cancellation);
void                g_simple_async_result_complete      (GSimpleAsyncResult *simple);
void                g_simple_async_result_complete_in_idle
                                                        (GSimpleAsyncResult *simple);
void                g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
                                                         GSimpleAsyncThreadFunc func,
                                                         int io_priority,
                                                         GCancellable *cancellable);
void                g_simple_async_result_set_from_error
                                                        (GSimpleAsyncResult *simple,
                                                         GError *error);
gboolean            g_simple_async_result_propagate_error
                                                        (GSimpleAsyncResult *simple,
                                                         GError **dest);
void                g_simple_async_result_set_error     (GSimpleAsyncResult *simple,
                                                         GQuark domain,
                                                         gint code,
                                                         const char *format,
                                                         ...);
void                g_simple_async_result_set_error_va  (GSimpleAsyncResult *simple,
                                                         GQuark domain,
                                                         gint code,
                                                         const char *format,
                                                         va_list args);
void                g_simple_async_report_error_in_idle (GObject *object,
                                                         GAsyncReadyCallback callback,
                                                         gpointer user_data,
                                                         GQuark domain,
                                                         gint code,
                                                         const char *format,
                                                         ...);
void                g_simple_async_report_gerror_in_idle
                                                        (GObject *object,
                                                         GAsyncReadyCallback callback,
                                                         gpointer user_data,
                                                         GError *error);

Object Hierarchy

  GObject
   +----GSimpleAsyncResult

Implemented Interfaces

GSimpleAsyncResult implements GAsyncResult.

Description

Implements GAsyncResult for simple cases. Most of the time, this will be all an application needs, and will be used transparently. Because of this, GSimpleAsyncResult is used throughout GIO for handling asynchronous functions.

GSimpleAsyncResult handles GAsyncReadyCallbacks, error reporting, operation cancellation and the final state of an operation, completely transparent to the application. Results can be returned as a pointer e.g. for functions that return data that is collected asynchronously, a boolean value for checking the success or failure of an operation, or a gssize for operations which return the number of bytes modified by the operation; all of the simple return cases are covered.

Most of the time, an application will not need to know of the details of this API; it is handled transparently, and any necessary operations are handled by GAsyncResult's interface. However, if implementing a new GIO module, for writing language bindings, or for complex applications that need better control of how asynchronous operations are completed, it is important to understand this functionality.

GSimpleAsyncResults are tagged with the calling function to ensure that asynchronous functions and their finishing functions are used together correctly.

To create a new GSimpleAsyncResult, call g_simple_async_result_new(). If the result needs to be created for a GError, use g_simple_async_result_new_from_error(). If a GError is not available (e.g. the asynchronous operation's doesn't take a GError argument), but the result still needs to be created for an error condition, use g_simple_async_result_new_error() (or g_simple_async_result_set_error_va() if your application or binding requires passing a variable argument list directly), and the error can then be propegated through the use of g_simple_async_result_propagate_error().

An asynchronous operation can be made to ignore a cancellation event by calling g_simple_async_result_set_handle_cancellation() with a GSimpleAsyncResult for the operation and FALSE. This is useful for operations that are dangerous to cancel, such as close (which would cause a leak if cancelled before being run).

GSimpleAsyncResult can integrate into GLib's event loop, GMainLoop, or it can use GThreads if available. g_simple_async_result_complete() will finish an I/O task directly within the main event loop. g_simple_async_result_complete_in_idle() will integrate the I/O task into the main event loop as an idle function and g_simple_async_result_run_in_thread() will run the job in a separate thread.

To set the results of an asynchronous function, g_simple_async_result_set_op_res_gpointer(), g_simple_async_result_set_op_res_gboolean(), and g_simple_async_result_set_op_res_gssize() are provided, setting the operation's result to a gpointer, gboolean, or gssize, respectively.

Likewise, to get the result of an asynchronous function, g_simple_async_result_get_op_res_gpointer(), g_simple_async_result_get_op_res_gboolean(), and g_simple_async_result_get_op_res_gssize() are provided, getting the operation's result as a gpointer, gboolean, and gssize, respectively.

Details

GSimpleAsyncResult

typedef struct _GSimpleAsyncResult GSimpleAsyncResult;

A simple implementation of GAsyncResult.


GSimpleAsyncThreadFunc ()

void                (*GSimpleAsyncThreadFunc)           (GSimpleAsyncResult *res,
                                                         GObject *object,
                                                         GCancellable *cancellable);

Simple thread function that runs an asynchronous operation and checks for cancellation.

res :

a GSimpleAsyncResult.

object :

a GObject.

cancellable :

optional GCancellable object, NULL to ignore.

g_simple_async_result_new ()

GSimpleAsyncResult * g_simple_async_result_new          (GObject *source_object,
                                                         GAsyncReadyCallback callback,
                                                         gpointer user_data,
                                                         gpointer source_tag);

Creates a GSimpleAsyncResult.

source_object :

a GObject the asynchronous function was called with, or NULL.

callback :

a GAsyncReadyCallback.

user_data :

user data passed to callback.

source_tag :

the asynchronous function.

Returns :

a GSimpleAsyncResult.

g_simple_async_result_new_error ()

GSimpleAsyncResult * g_simple_async_result_new_error    (GObject *source_object,
                                                         GAsyncReadyCallback callback,
                                                         gpointer user_data,
                                                         GQuark domain,
                                                         gint code,
                                                         const char *format,
                                                         ...);

Creates a new GSimpleAsyncResult with a set error.

source_object :

a GObject, or NULL.

callback :

a GAsyncReadyCallback.

user_data :

user data passed to callback.

domain :

a GQuark.

code :

an error code.

format :

a string with format characters.

... :

a list of values to insert into format.

Returns :

a GSimpleAsyncResult.

g_simple_async_result_new_from_error ()

GSimpleAsyncResult * g_simple_async_result_new_from_error
                                                        (GObject *source_object,
                                                         GAsyncReadyCallback callback,
                                                         gpointer user_data,
                                                         GError *error);

Creates a GSimpleAsyncResult from an error condition.

source_object :

a GObject, or NULL.

callback :

a GAsyncReadyCallback.

user_data :

user data passed to callback.

error :

a GError location.

Returns :

a GSimpleAsyncResult.

g_simple_async_result_set_op_res_gpointer ()

void                g_simple_async_result_set_op_res_gpointer
                                                        (GSimpleAsyncResult *simple,
                                                         gpointer op_res,
                                                         GDestroyNotify destroy_op_res);

Sets the operation result within the asynchronous result to a pointer.

simple :

a GSimpleAsyncResult.

op_res :

a pointer result from an asynchronous function.

destroy_op_res :

a GDestroyNotify function.

g_simple_async_result_get_op_res_gpointer ()

gpointer            g_simple_async_result_get_op_res_gpointer
                                                        (GSimpleAsyncResult *simple);

Gets a pointer result as returned by the asynchronous function.

simple :

a GSimpleAsyncResult.

Returns :

a pointer from the result.

g_simple_async_result_set_op_res_gssize ()

void                g_simple_async_result_set_op_res_gssize
                                                        (GSimpleAsyncResult *simple,
                                                         gssize op_res);

Sets the operation result within the asynchronous result to the given op_res.

simple :

a GSimpleAsyncResult.

op_res :

a gssize.

g_simple_async_result_get_op_res_gssize ()

gssize              g_simple_async_result_get_op_res_gssize
                                                        (GSimpleAsyncResult *simple);

Gets a gssize from the asynchronous result.

simple :

a GSimpleAsyncResult.

Returns :

a gssize returned from the asynchronous function.

g_simple_async_result_set_op_res_gboolean ()

void                g_simple_async_result_set_op_res_gboolean
                                                        (GSimpleAsyncResult *simple,
                                                         gboolean op_res);

Sets the operation result to a boolean within the asynchronous result.

simple :

a GSimpleAsyncResult.

op_res :

a gboolean.

g_simple_async_result_get_op_res_gboolean ()

gboolean            g_simple_async_result_get_op_res_gboolean
                                                        (GSimpleAsyncResult *simple);

Gets the operation result boolean from within the asynchronous result.

simple :

a GSimpleAsyncResult.

Returns :

TRUE if the operation's result was TRUE, FALSE if the operation's result was FALSE.

g_simple_async_result_get_source_tag ()

gpointer            g_simple_async_result_get_source_tag
                                                        (GSimpleAsyncResult *simple);

Gets the source tag for the GSimpleAsyncResult.

simple :

a GSimpleAsyncResult.

Returns :

a gpointer to the source object for the GSimpleAsyncResult.

g_simple_async_result_is_valid ()

gboolean            g_simple_async_result_is_valid      (GAsyncResult *result,
                                                         GObject *source,
                                                         gpointer source_tag);

Ensures that the data passed to the _finish function of an async operation is consistent. Three checks are performed.

First, result is checked to ensure that it is really a GSimpleAsyncResult. Second, source is checked to ensure that it matches the source object of result. Third, source_tag is checked to ensure that it is equal to the source_tag argument given to g_simple_async_result_new() (which, by convention, is a pointer to the _async function corresponding to the _finish function from which this function is called).

result :

the GAsyncResult passed to the _finish function.

source :

the GObject passed to the _finish function.

source_tag :

the asynchronous function.

Returns :

TRUE if all checks passed or FALSE if any failed.

g_simple_async_result_set_handle_cancellation ()

void                g_simple_async_result_set_handle_cancellation
                                                        (GSimpleAsyncResult *simple,
                                                         gboolean handle_cancellation);

Sets whether to handle cancellation within the asynchronous operation.

simple :

a GSimpleAsyncResult.

handle_cancellation :

a gboolean.

g_simple_async_result_complete ()

void                g_simple_async_result_complete      (GSimpleAsyncResult *simple);

Completes an asynchronous I/O job. Must be called in the main thread, as it invokes the callback that should be called in the main thread. If you are in a different thread use g_simple_async_result_complete_in_idle().

simple :

a GSimpleAsyncResult.

g_simple_async_result_complete_in_idle ()

void                g_simple_async_result_complete_in_idle
                                                        (GSimpleAsyncResult *simple);

Completes an asynchronous function in the main event loop using an idle function.

simple :

a GSimpleAsyncResult.

g_simple_async_result_run_in_thread ()

void                g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
                                                         GSimpleAsyncThreadFunc func,
                                                         int io_priority,
                                                         GCancellable *cancellable);

Runs the asynchronous job in a separated thread.

simple :

a GSimpleAsyncResult.

func :

a GSimpleAsyncThreadFunc.

io_priority :

the io priority of the request.

cancellable :

optional GCancellable object, NULL to ignore.

g_simple_async_result_set_from_error ()

void                g_simple_async_result_set_from_error
                                                        (GSimpleAsyncResult *simple,
                                                         GError *error);

Sets the result from a GError.

simple :

a GSimpleAsyncResult.

error :

GError.

g_simple_async_result_propagate_error ()

gboolean            g_simple_async_result_propagate_error
                                                        (GSimpleAsyncResult *simple,
                                                         GError **dest);

Propagates an error from within the simple asynchronous result to a given destination.

simple :

a GSimpleAsyncResult.

dest :

a location to propegate the error to.

Returns :

TRUE if the error was propegated to dest. FALSE otherwise.

g_simple_async_result_set_error ()

void                g_simple_async_result_set_error     (GSimpleAsyncResult *simple,
                                                         GQuark domain,
                                                         gint code,
                                                         const char *format,
                                                         ...);

Sets an error within the asynchronous result without a GError.

simple :

a GSimpleAsyncResult.

domain :

a GQuark (usually G_IO_ERROR).

code :

an error code.

format :

a formatted error reporting string.

... :

a list of variables to fill in format.

g_simple_async_result_set_error_va ()

void                g_simple_async_result_set_error_va  (GSimpleAsyncResult *simple,
                                                         GQuark domain,
                                                         gint code,
                                                         const char *format,
                                                         va_list args);

Sets an error within the asynchronous result without a GError. Unless writing a binding, see g_simple_async_result_set_error().

simple :

a GSimpleAsyncResult.

domain :

a GQuark (usually G_IO_ERROR).

code :

an error code.

format :

a formatted error reporting string.

args :

va_list of arguments.

g_simple_async_report_error_in_idle ()

void                g_simple_async_report_error_in_idle (GObject *object,
                                                         GAsyncReadyCallback callback,
                                                         gpointer user_data,
                                                         GQuark domain,
                                                         gint code,
                                                         const char *format,
                                                         ...);

Reports an error in an asynchronous function in an idle function by directly setting the contents of the GAsyncResult with the given error information.

object :

a GObject.

callback :

a GAsyncReadyCallback.

user_data :

user data passed to callback.

domain :

a GQuark containing the error domain (usually G_IO_ERROR).

code :

a specific error code.

format :

a formatted error reporting string.

... :

a list of variables to fill in format.

g_simple_async_report_gerror_in_idle ()

void                g_simple_async_report_gerror_in_idle
                                                        (GObject *object,
                                                         GAsyncReadyCallback callback,
                                                         gpointer user_data,
                                                         GError *error);

Reports an error in an idle function. Similar to g_simple_async_report_error_in_idle(), but takes a GError rather than building a new one.

object :

a GObject.

callback :

a GAsyncReadyCallback.

user_data :

user data passed to callback.

error :

the GError to report

See Also

GAsyncResult