1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "gioscheduler.h"
26 #include "gcancellable.h"
30 * SECTION:gioscheduler
31 * @short_description: I/O Scheduler
34 * As of GLib 2.36, #GIOScheduler is deprecated in favor of
35 * #GThreadPool and #GTask.
37 * Schedules asynchronous I/O operations. #GIOScheduler integrates
38 * into the main event loop (#GMainLoop) and uses threads.
41 struct _GIOSchedulerJob {
45 GIOSchedulerJobFunc job_func;
47 GDestroyNotify destroy_notify;
49 GCancellable *cancellable;
50 gulong cancellable_id;
51 GMainContext *context;
54 G_LOCK_DEFINE_STATIC(active_jobs);
55 static GList *active_jobs = NULL;
58 g_io_job_free (GIOSchedulerJob *job)
60 if (job->destroy_notify)
61 job->destroy_notify (job->data);
64 active_jobs = g_list_delete_link (active_jobs, job->active_link);
65 G_UNLOCK (active_jobs);
68 g_object_unref (job->cancellable);
69 g_main_context_unref (job->context);
70 g_slice_free (GIOSchedulerJob, job);
74 io_job_thread (GTask *task,
75 gpointer source_object,
77 GCancellable *cancellable)
79 GIOSchedulerJob *job = task_data;
83 g_cancellable_push_current (job->cancellable);
87 result = job->job_func (job, job->cancellable, job->data);
92 g_cancellable_pop_current (job->cancellable);
96 * g_io_scheduler_push_job:
97 * @job_func: a #GIOSchedulerJobFunc.
98 * @user_data: data to pass to @job_func
99 * @notify: (nullable): a #GDestroyNotify for @user_data, or %NULL
100 * @io_priority: the [I/O priority][io-priority]
102 * @cancellable: optional #GCancellable object, %NULL to ignore.
104 * Schedules the I/O job to run in another thread.
106 * @notify will be called on @user_data after @job_func has returned,
107 * regardless whether the job was cancelled or has run to completion.
109 * If @cancellable is not %NULL, it can be used to cancel the I/O job
110 * by calling g_cancellable_cancel() or by calling
111 * g_io_scheduler_cancel_all_jobs().
113 * Deprecated: use #GThreadPool or g_task_run_in_thread()
116 g_io_scheduler_push_job (GIOSchedulerJobFunc job_func,
118 GDestroyNotify notify,
120 GCancellable *cancellable)
122 GIOSchedulerJob *job;
125 g_return_if_fail (job_func != NULL);
127 job = g_slice_new0 (GIOSchedulerJob);
128 job->job_func = job_func;
129 job->data = user_data;
130 job->destroy_notify = notify;
133 job->cancellable = g_object_ref (cancellable);
135 job->context = g_main_context_ref_thread_default ();
137 G_LOCK (active_jobs);
138 active_jobs = g_list_prepend (active_jobs, job);
139 job->active_link = active_jobs;
140 G_UNLOCK (active_jobs);
142 task = g_task_new (NULL, cancellable, NULL, NULL);
143 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
144 g_task_set_source_tag (task, g_io_scheduler_push_job);
145 G_GNUC_END_IGNORE_DEPRECATIONS
146 g_task_set_task_data (task, job, (GDestroyNotify)g_io_job_free);
147 g_task_set_priority (task, io_priority);
148 g_task_run_in_thread (task, io_job_thread);
149 g_object_unref (task);
153 * g_io_scheduler_cancel_all_jobs:
155 * Cancels all cancellable I/O jobs.
157 * A job is cancellable if a #GCancellable was passed into
158 * g_io_scheduler_push_job().
160 * Deprecated: You should never call this function, since you don't
161 * know how other libraries in your program might be making use of
165 g_io_scheduler_cancel_all_jobs (void)
167 GList *cancellable_list, *l;
169 G_LOCK (active_jobs);
170 cancellable_list = NULL;
171 for (l = active_jobs; l != NULL; l = l->next)
173 GIOSchedulerJob *job = l->data;
174 if (job->cancellable)
175 cancellable_list = g_list_prepend (cancellable_list,
176 g_object_ref (job->cancellable));
178 G_UNLOCK (active_jobs);
180 for (l = cancellable_list; l != NULL; l = l->next)
182 GCancellable *c = l->data;
183 g_cancellable_cancel (c);
186 g_list_free (cancellable_list);
193 GDestroyNotify notify;
201 mainloop_proxy_func (gpointer data)
203 MainLoopProxy *proxy = data;
205 proxy->ret_val = proxy->func (proxy->data);
208 proxy->notify (proxy->data);
210 g_mutex_lock (&proxy->ack_lock);
212 g_cond_signal (&proxy->ack_condition);
213 g_mutex_unlock (&proxy->ack_lock);
219 mainloop_proxy_free (MainLoopProxy *proxy)
221 g_mutex_clear (&proxy->ack_lock);
222 g_cond_clear (&proxy->ack_condition);
227 * g_io_scheduler_job_send_to_mainloop:
228 * @job: a #GIOSchedulerJob
229 * @func: a #GSourceFunc callback that will be called in the original thread
230 * @user_data: data to pass to @func
231 * @notify: (nullable): a #GDestroyNotify for @user_data, or %NULL
233 * Used from an I/O job to send a callback to be run in the thread
234 * that the job was started from, waiting for the result (and thus
235 * blocking the I/O job).
237 * Returns: The return value of @func
239 * Deprecated: Use g_main_context_invoke().
242 g_io_scheduler_job_send_to_mainloop (GIOSchedulerJob *job,
245 GDestroyNotify notify)
248 MainLoopProxy *proxy;
251 g_return_val_if_fail (job != NULL, FALSE);
252 g_return_val_if_fail (func != NULL, FALSE);
254 proxy = g_new0 (MainLoopProxy, 1);
256 proxy->data = user_data;
257 proxy->notify = notify;
258 g_mutex_init (&proxy->ack_lock);
259 g_cond_init (&proxy->ack_condition);
260 g_mutex_lock (&proxy->ack_lock);
262 source = g_idle_source_new ();
263 g_source_set_priority (source, G_PRIORITY_DEFAULT);
264 g_source_set_callback (source, mainloop_proxy_func, proxy,
266 g_source_set_static_name (source, "[gio] mainloop_proxy_func");
268 g_source_attach (source, job->context);
269 g_source_unref (source);
272 g_cond_wait (&proxy->ack_condition, &proxy->ack_lock);
273 g_mutex_unlock (&proxy->ack_lock);
275 ret_val = proxy->ret_val;
276 mainloop_proxy_free (proxy);
282 * g_io_scheduler_job_send_to_mainloop_async:
283 * @job: a #GIOSchedulerJob
284 * @func: a #GSourceFunc callback that will be called in the original thread
285 * @user_data: data to pass to @func
286 * @notify: (nullable): a #GDestroyNotify for @user_data, or %NULL
288 * Used from an I/O job to send a callback to be run asynchronously in
289 * the thread that the job was started from. The callback will be run
290 * when the main loop is available, but at that time the I/O job might
291 * have finished. The return value from the callback is ignored.
293 * Note that if you are passing the @user_data from g_io_scheduler_push_job()
294 * on to this function you have to ensure that it is not freed before
295 * @func is called, either by passing %NULL as @notify to
296 * g_io_scheduler_push_job() or by using refcounting for @user_data.
298 * Deprecated: Use g_main_context_invoke().
301 g_io_scheduler_job_send_to_mainloop_async (GIOSchedulerJob *job,
304 GDestroyNotify notify)
307 MainLoopProxy *proxy;
309 g_return_if_fail (job != NULL);
310 g_return_if_fail (func != NULL);
312 proxy = g_new0 (MainLoopProxy, 1);
314 proxy->data = user_data;
315 proxy->notify = notify;
316 g_mutex_init (&proxy->ack_lock);
317 g_cond_init (&proxy->ack_condition);
319 source = g_idle_source_new ();
320 g_source_set_priority (source, G_PRIORITY_DEFAULT);
321 g_source_set_callback (source, mainloop_proxy_func, proxy,
322 (GDestroyNotify)mainloop_proxy_free);
323 g_source_set_static_name (source, "[gio] mainloop_proxy_func");
325 g_source_attach (source, job->context);
326 g_source_unref (source);