/src/glib/gio/gasyncresult.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* GIO - GLib Input, Output and Streaming Library |
2 | | * |
3 | | * Copyright (C) 2006-2007 Red Hat, Inc. |
4 | | * |
5 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * This library is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General |
18 | | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
19 | | * |
20 | | * Author: Alexander Larsson <alexl@redhat.com> |
21 | | */ |
22 | | |
23 | | #include "config.h" |
24 | | #include "gasyncresult.h" |
25 | | #include "gsimpleasyncresult.h" |
26 | | #include "glibintl.h" |
27 | | |
28 | | |
29 | | /** |
30 | | * SECTION:gasyncresult |
31 | | * @short_description: Asynchronous Function Results |
32 | | * @include: gio/gio.h |
33 | | * @see_also: #GTask |
34 | | * |
35 | | * Provides a base class for implementing asynchronous function results. |
36 | | * |
37 | | * Asynchronous operations are broken up into two separate operations |
38 | | * which are chained together by a #GAsyncReadyCallback. To begin |
39 | | * an asynchronous operation, provide a #GAsyncReadyCallback to the |
40 | | * asynchronous function. This callback will be triggered when the |
41 | | * operation has completed, and must be run in a later iteration of |
42 | | * the [thread-default main context][g-main-context-push-thread-default] |
43 | | * from where the operation was initiated. It will be passed a |
44 | | * #GAsyncResult instance filled with the details of the operation's |
45 | | * success or failure, the object the asynchronous function was |
46 | | * started for and any error codes returned. The asynchronous callback |
47 | | * function is then expected to call the corresponding "_finish()" |
48 | | * function, passing the object the function was called for, the |
49 | | * #GAsyncResult instance, and (optionally) an @error to grab any |
50 | | * error conditions that may have occurred. |
51 | | * |
52 | | * The "_finish()" function for an operation takes the generic result |
53 | | * (of type #GAsyncResult) and returns the specific result that the |
54 | | * operation in question yields (e.g. a #GFileEnumerator for a |
55 | | * "enumerate children" operation). If the result or error status of the |
56 | | * operation is not needed, there is no need to call the "_finish()" |
57 | | * function; GIO will take care of cleaning up the result and error |
58 | | * information after the #GAsyncReadyCallback returns. You can pass |
59 | | * %NULL for the #GAsyncReadyCallback if you don't need to take any |
60 | | * action at all after the operation completes. Applications may also |
61 | | * take a reference to the #GAsyncResult and call "_finish()" later; |
62 | | * however, the "_finish()" function may be called at most once. |
63 | | * |
64 | | * Example of a typical asynchronous operation flow: |
65 | | * |[<!-- language="C" --> |
66 | | * void _theoretical_frobnitz_async (Theoretical *t, |
67 | | * GCancellable *c, |
68 | | * GAsyncReadyCallback cb, |
69 | | * gpointer u); |
70 | | * |
71 | | * gboolean _theoretical_frobnitz_finish (Theoretical *t, |
72 | | * GAsyncResult *res, |
73 | | * GError **e); |
74 | | * |
75 | | * static void |
76 | | * frobnitz_result_func (GObject *source_object, |
77 | | * GAsyncResult *res, |
78 | | * gpointer user_data) |
79 | | * { |
80 | | * gboolean success = FALSE; |
81 | | * |
82 | | * success = _theoretical_frobnitz_finish (source_object, res, NULL); |
83 | | * |
84 | | * if (success) |
85 | | * g_printf ("Hurray!\n"); |
86 | | * else |
87 | | * g_printf ("Uh oh!\n"); |
88 | | * |
89 | | * ... |
90 | | * |
91 | | * } |
92 | | * |
93 | | * int main (int argc, void *argv[]) |
94 | | * { |
95 | | * ... |
96 | | * |
97 | | * _theoretical_frobnitz_async (theoretical_data, |
98 | | * NULL, |
99 | | * frobnitz_result_func, |
100 | | * NULL); |
101 | | * |
102 | | * ... |
103 | | * } |
104 | | * ]| |
105 | | * |
106 | | * The callback for an asynchronous operation is called only once, and is |
107 | | * always called, even in the case of a cancelled operation. On cancellation |
108 | | * the result is a %G_IO_ERROR_CANCELLED error. |
109 | | * |
110 | | * ## I/O Priority # {#io-priority} |
111 | | * |
112 | | * Many I/O-related asynchronous operations have a priority parameter, |
113 | | * which is used in certain cases to determine the order in which |
114 | | * operations are executed. They are not used to determine system-wide |
115 | | * I/O scheduling. Priorities are integers, with lower numbers indicating |
116 | | * higher priority. It is recommended to choose priorities between |
117 | | * %G_PRIORITY_LOW and %G_PRIORITY_HIGH, with %G_PRIORITY_DEFAULT |
118 | | * as a default. |
119 | | */ |
120 | | |
121 | | typedef GAsyncResultIface GAsyncResultInterface; |
122 | | G_DEFINE_INTERFACE (GAsyncResult, g_async_result, G_TYPE_OBJECT) |
123 | | |
124 | | static void |
125 | | g_async_result_default_init (GAsyncResultInterface *iface) |
126 | 0 | { |
127 | 0 | } |
128 | | |
129 | | /** |
130 | | * g_async_result_get_user_data: |
131 | | * @res: a #GAsyncResult. |
132 | | * |
133 | | * Gets the user data from a #GAsyncResult. |
134 | | * |
135 | | * Returns: (transfer full): the user data for @res. |
136 | | **/ |
137 | | gpointer |
138 | | g_async_result_get_user_data (GAsyncResult *res) |
139 | 0 | { |
140 | 0 | GAsyncResultIface *iface; |
141 | |
|
142 | 0 | g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL); |
143 | | |
144 | 0 | iface = G_ASYNC_RESULT_GET_IFACE (res); |
145 | |
|
146 | 0 | return (* iface->get_user_data) (res); |
147 | 0 | } |
148 | | |
149 | | /** |
150 | | * g_async_result_get_source_object: |
151 | | * @res: a #GAsyncResult |
152 | | * |
153 | | * Gets the source object from a #GAsyncResult. |
154 | | * |
155 | | * Returns: (transfer full) (nullable): a new reference to the source |
156 | | * object for the @res, or %NULL if there is none. |
157 | | */ |
158 | | GObject * |
159 | | g_async_result_get_source_object (GAsyncResult *res) |
160 | 0 | { |
161 | 0 | GAsyncResultIface *iface; |
162 | |
|
163 | 0 | g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL); |
164 | | |
165 | 0 | iface = G_ASYNC_RESULT_GET_IFACE (res); |
166 | |
|
167 | 0 | return (* iface->get_source_object) (res); |
168 | 0 | } |
169 | | |
170 | | /** |
171 | | * g_async_result_legacy_propagate_error: |
172 | | * @res: a #GAsyncResult |
173 | | * @error: (out): a location to propagate the error to. |
174 | | * |
175 | | * If @res is a #GSimpleAsyncResult, this is equivalent to |
176 | | * g_simple_async_result_propagate_error(). Otherwise it returns |
177 | | * %FALSE. |
178 | | * |
179 | | * This can be used for legacy error handling in async *_finish() |
180 | | * wrapper functions that traditionally handled #GSimpleAsyncResult |
181 | | * error returns themselves rather than calling into the virtual method. |
182 | | * This should not be used in new code; #GAsyncResult errors that are |
183 | | * set by virtual methods should also be extracted by virtual methods, |
184 | | * to enable subclasses to chain up correctly. |
185 | | * |
186 | | * Returns: %TRUE if @error is has been filled in with an error from |
187 | | * @res, %FALSE if not. |
188 | | * |
189 | | * Since: 2.34 |
190 | | **/ |
191 | | gboolean |
192 | | g_async_result_legacy_propagate_error (GAsyncResult *res, |
193 | | GError **error) |
194 | 0 | { |
195 | | /* This doesn't use a vmethod, because it's only for code that used |
196 | | * to use GSimpleAsyncResult. (But it's a GAsyncResult method so |
197 | | * that callers don't need to worry about GSimpleAsyncResult |
198 | | * deprecation warnings in the future.) |
199 | | */ |
200 | |
|
201 | 0 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
202 | 0 | if (G_IS_SIMPLE_ASYNC_RESULT (res)) |
203 | 0 | { |
204 | 0 | return g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), |
205 | 0 | error); |
206 | 0 | } |
207 | 0 | else |
208 | 0 | return FALSE; |
209 | 0 | G_GNUC_END_IGNORE_DEPRECATIONS |
210 | 0 | } |
211 | | |
212 | | /** |
213 | | * g_async_result_is_tagged: |
214 | | * @res: a #GAsyncResult |
215 | | * @source_tag: an application-defined tag |
216 | | * |
217 | | * Checks if @res has the given @source_tag (generally a function |
218 | | * pointer indicating the function @res was created by). |
219 | | * |
220 | | * Returns: %TRUE if @res has the indicated @source_tag, %FALSE if |
221 | | * not. |
222 | | * |
223 | | * Since: 2.34 |
224 | | **/ |
225 | | gboolean |
226 | | g_async_result_is_tagged (GAsyncResult *res, |
227 | | gpointer source_tag) |
228 | 0 | { |
229 | 0 | GAsyncResultIface *iface; |
230 | |
|
231 | 0 | g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE); |
232 | | |
233 | 0 | iface = G_ASYNC_RESULT_GET_IFACE (res); |
234 | |
|
235 | 0 | if (!iface->is_tagged) |
236 | 0 | return FALSE; |
237 | | |
238 | 0 | return (* iface->is_tagged) (res, source_tag); |
239 | 0 | } |