1/* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright 2011 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
21#ifndef __G_TASK_H__
22#define __G_TASK_H__
23
24#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
25#error "Only <gio/gio.h> can be included directly."
26#endif
27
28#include <gio/giotypes.h>
29
30G_BEGIN_DECLS
31
32#define G_TYPE_TASK (g_task_get_type ())
33#define G_TASK(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_TASK, GTask))
34#define G_TASK_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), G_TYPE_TASK, GTaskClass))
35#define G_IS_TASK(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_TASK))
36#define G_IS_TASK_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), G_TYPE_TASK))
37#define G_TASK_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), G_TYPE_TASK, GTaskClass))
38
39typedef struct _GTaskClass GTaskClass;
40
41GIO_AVAILABLE_IN_2_36
42GType g_task_get_type (void) G_GNUC_CONST;
43
44GIO_AVAILABLE_IN_2_36
45GTask *g_task_new (gpointer source_object,
46 GCancellable *cancellable,
47 GAsyncReadyCallback callback,
48 gpointer callback_data);
49
50GIO_AVAILABLE_IN_2_36
51void g_task_report_error (gpointer source_object,
52 GAsyncReadyCallback callback,
53 gpointer callback_data,
54 gpointer source_tag,
55 GError *error);
56GIO_AVAILABLE_IN_2_36
57void g_task_report_new_error (gpointer source_object,
58 GAsyncReadyCallback callback,
59 gpointer callback_data,
60 gpointer source_tag,
61 GQuark domain,
62 gint code,
63 const char *format,
64 ...) G_GNUC_PRINTF(7, 8);
65
66GIO_AVAILABLE_IN_2_36
67void g_task_set_task_data (GTask *task,
68 gpointer task_data,
69 GDestroyNotify task_data_destroy);
70GIO_AVAILABLE_IN_2_36
71void g_task_set_priority (GTask *task,
72 gint priority);
73GIO_AVAILABLE_IN_2_36
74void g_task_set_check_cancellable (GTask *task,
75 gboolean check_cancellable);
76GIO_AVAILABLE_IN_2_36
77void g_task_set_source_tag (GTask *task,
78 gpointer source_tag);
79GIO_AVAILABLE_IN_2_60
80void g_task_set_name (GTask *task,
81 const gchar *name);
82GIO_AVAILABLE_IN_2_76
83void g_task_set_static_name (GTask *task,
84 const gchar *name);
85
86/* Macro wrapper to set the task name when setting the source tag. */
87#if GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_76
88#define g_task_set_source_tag(task, tag) G_STMT_START { \
89 GTask *_task = (task); \
90 (g_task_set_source_tag) (_task, tag); \
91 if (g_task_get_name (_task) == NULL) \
92 g_task_set_static_name (_task, G_STRINGIFY (tag)); \
93} G_STMT_END
94#endif
95
96#if GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_76
97#if defined (__GNUC__) && (__GNUC__ >= 2)
98#define g_task_set_name(task, name) G_STMT_START { \
99 GTask *_task = (task); \
100 if (__builtin_constant_p (name)) \
101 g_task_set_static_name (_task, name); \
102 else \
103 g_task_set_name (_task, name); \
104} G_STMT_END
105#endif
106#endif
107
108GIO_AVAILABLE_IN_2_36
109gpointer g_task_get_source_object (GTask *task);
110GIO_AVAILABLE_IN_2_36
111gpointer g_task_get_task_data (GTask *task);
112GIO_AVAILABLE_IN_2_36
113gint g_task_get_priority (GTask *task);
114GIO_AVAILABLE_IN_2_36
115GMainContext *g_task_get_context (GTask *task);
116GIO_AVAILABLE_IN_2_36
117GCancellable *g_task_get_cancellable (GTask *task);
118GIO_AVAILABLE_IN_2_36
119gboolean g_task_get_check_cancellable (GTask *task);
120GIO_AVAILABLE_IN_2_36
121gpointer g_task_get_source_tag (GTask *task);
122GIO_AVAILABLE_IN_2_60
123const gchar *g_task_get_name (GTask *task);
124
125GIO_AVAILABLE_IN_2_36
126gboolean g_task_is_valid (gpointer result,
127 gpointer source_object);
128
129
130typedef void (*GTaskThreadFunc) (GTask *task,
131 gpointer source_object,
132 gpointer task_data,
133 GCancellable *cancellable);
134GIO_AVAILABLE_IN_2_36
135void g_task_run_in_thread (GTask *task,
136 GTaskThreadFunc task_func);
137GIO_AVAILABLE_IN_2_36
138void g_task_run_in_thread_sync (GTask *task,
139 GTaskThreadFunc task_func);
140GIO_AVAILABLE_IN_2_36
141gboolean g_task_set_return_on_cancel (GTask *task,
142 gboolean return_on_cancel);
143GIO_AVAILABLE_IN_2_36
144gboolean g_task_get_return_on_cancel (GTask *task);
145
146GIO_AVAILABLE_IN_2_36
147void g_task_attach_source (GTask *task,
148 GSource *source,
149 GSourceFunc callback);
150
151
152GIO_AVAILABLE_IN_2_36
153void g_task_return_pointer (GTask *task,
154 gpointer result,
155 GDestroyNotify result_destroy);
156GIO_AVAILABLE_IN_2_36
157void g_task_return_boolean (GTask *task,
158 gboolean result);
159GIO_AVAILABLE_IN_2_36
160void g_task_return_int (GTask *task,
161 gssize result);
162
163GIO_AVAILABLE_IN_2_36
164void g_task_return_error (GTask *task,
165 GError *error);
166GIO_AVAILABLE_IN_2_80
167void g_task_return_prefixed_error (GTask *task,
168 GError *error,
169 const char *format,
170 ...) G_GNUC_PRINTF (3, 4);
171
172GIO_AVAILABLE_IN_2_36
173void g_task_return_new_error (GTask *task,
174 GQuark domain,
175 gint code,
176 const char *format,
177 ...) G_GNUC_PRINTF (4, 5);
178
179GIO_AVAILABLE_IN_2_80
180void g_task_return_new_error_literal (GTask *task,
181 GQuark domain,
182 gint code,
183 const char *message);
184GIO_AVAILABLE_IN_2_64
185void g_task_return_value (GTask *task,
186 GValue *result);
187
188GIO_AVAILABLE_IN_2_36
189gboolean g_task_return_error_if_cancelled (GTask *task);
190
191GIO_AVAILABLE_IN_2_36
192gpointer g_task_propagate_pointer (GTask *task,
193 GError **error);
194GIO_AVAILABLE_IN_2_36
195gboolean g_task_propagate_boolean (GTask *task,
196 GError **error);
197GIO_AVAILABLE_IN_2_36
198gssize g_task_propagate_int (GTask *task,
199 GError **error);
200GIO_AVAILABLE_IN_2_64
201gboolean g_task_propagate_value (GTask *task,
202 GValue *value,
203 GError **error);
204GIO_AVAILABLE_IN_2_36
205gboolean g_task_had_error (GTask *task);
206GIO_AVAILABLE_IN_2_44
207gboolean g_task_get_completed (GTask *task);
208
209/*< private >*/
210#ifndef __GTK_DOC_IGNORE__
211#ifndef __GI_SCANNER__
212/* Debugging API, not part of the public API */
213void g_task_print_alive_tasks (void);
214#endif /* !__GI_SCANNER__ */
215#endif /* !__GTK_DOC_IGNORE__ */
216
217G_END_DECLS
218
219#endif /* __G_TASK_H__ */
220