1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
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 * Schedules asynchronous I/O operations. #GIOScheduler integrates
35 * into the main event loop (#GMainLoop) and uses threads.
37 * <para id="io-priority"><indexterm><primary>I/O priority</primary></indexterm>
38 * Each I/O operation has a priority, and the scheduler uses the priorities
39 * to determine the order in which operations are executed. They are
40 * <emphasis>not</emphasis> used to determine system-wide I/O scheduling.
41 * Priorities are integers, with lower numbers indicating higher priority.
42 * It is recommended to choose priorities between %G_PRIORITY_LOW and
43 * %G_PRIORITY_HIGH, with %G_PRIORITY_DEFAULT as a default.
47 struct _GIOSchedulerJob {
49 GIOSchedulerJobFunc job_func;
50 GSourceFunc cancel_func; /* Runs under job map lock */
52 GDestroyNotify destroy_notify;
55 GCancellable *cancellable;
56 gulong cancellable_id;
57 GMainContext *context;
60 G_LOCK_DEFINE_STATIC(active_jobs);
61 static GList *active_jobs = NULL;
63 static GThreadPool *job_thread_pool = NULL;
65 static void io_job_thread (gpointer data,
69 g_io_job_free (GIOSchedulerJob *job)
73 if (job->cancellable_id)
74 g_cancellable_disconnect (job->cancellable, job->cancellable_id);
75 g_object_unref (job->cancellable);
77 g_main_context_unref (job->context);
82 g_io_job_compare (gconstpointer a,
86 const GIOSchedulerJob *aa = a;
87 const GIOSchedulerJob *bb = b;
89 /* Cancelled jobs are set prio == -1, so that
90 they are executed as quickly as possible */
92 /* Lower value => higher priority */
93 if (aa->io_priority < bb->io_priority)
95 if (aa->io_priority == bb->io_priority)
101 init_scheduler (gpointer arg)
103 if (job_thread_pool == NULL)
105 /* TODO: thread_pool_new can fail */
106 job_thread_pool = g_thread_pool_new (io_job_thread,
111 if (job_thread_pool != NULL)
113 g_thread_pool_set_sort_function (job_thread_pool,
122 on_job_canceled (GCancellable *cancellable,
125 GIOSchedulerJob *job = user_data;
127 /* This might be called more than once */
128 job->io_priority = -1;
130 if (job_thread_pool != NULL)
131 g_thread_pool_set_sort_function (job_thread_pool,
137 job_destroy (gpointer data)
139 GIOSchedulerJob *job = data;
141 if (job->destroy_notify)
142 job->destroy_notify (job->data);
144 G_LOCK (active_jobs);
145 active_jobs = g_list_delete_link (active_jobs, job->active_link);
146 G_UNLOCK (active_jobs);
151 io_job_thread (gpointer data,
154 GIOSchedulerJob *job = data;
157 if (job->cancellable)
158 g_cancellable_push_current (job->cancellable);
162 result = job->job_func (job, job->cancellable, job->data);
166 if (job->cancellable)
167 g_cancellable_pop_current (job->cancellable);
173 * g_io_scheduler_push_job:
174 * @job_func: a #GIOSchedulerJobFunc.
175 * @user_data: data to pass to @job_func
176 * @notify: (allow-none): a #GDestroyNotify for @user_data, or %NULL
177 * @io_priority: the <link linkend="gioscheduler">I/O priority</link>
179 * @cancellable: optional #GCancellable object, %NULL to ignore.
181 * Schedules the I/O job to run in another thread.
183 * @notify will be called on @user_data after @job_func has returned,
184 * regardless whether the job was cancelled or has run to completion.
186 * If @cancellable is not %NULL, it can be used to cancel the I/O job
187 * by calling g_cancellable_cancel() or by calling
188 * g_io_scheduler_cancel_all_jobs().
191 g_io_scheduler_push_job (GIOSchedulerJobFunc job_func,
193 GDestroyNotify notify,
195 GCancellable *cancellable)
197 static GOnce once_init = G_ONCE_INIT;
198 GIOSchedulerJob *job;
200 g_return_if_fail (job_func != NULL);
202 job = g_new0 (GIOSchedulerJob, 1);
203 job->job_func = job_func;
204 job->data = user_data;
205 job->destroy_notify = notify;
206 job->io_priority = io_priority;
210 job->cancellable = g_object_ref (cancellable);
211 job->cancellable_id = g_cancellable_connect (job->cancellable, (GCallback)on_job_canceled,
215 job->context = g_main_context_ref_thread_default ();
217 G_LOCK (active_jobs);
218 active_jobs = g_list_prepend (active_jobs, job);
219 job->active_link = active_jobs;
220 G_UNLOCK (active_jobs);
222 g_once (&once_init, init_scheduler, NULL);
223 g_thread_pool_push (job_thread_pool, job, NULL);
227 * g_io_scheduler_cancel_all_jobs:
229 * Cancels all cancellable I/O jobs.
231 * A job is cancellable if a #GCancellable was passed into
232 * g_io_scheduler_push_job().
235 g_io_scheduler_cancel_all_jobs (void)
237 GList *cancellable_list, *l;
239 G_LOCK (active_jobs);
240 cancellable_list = NULL;
241 for (l = active_jobs; l != NULL; l = l->next)
243 GIOSchedulerJob *job = l->data;
244 if (job->cancellable)
245 cancellable_list = g_list_prepend (cancellable_list,
246 g_object_ref (job->cancellable));
248 G_UNLOCK (active_jobs);
250 for (l = cancellable_list; l != NULL; l = l->next)
252 GCancellable *c = l->data;
253 g_cancellable_cancel (c);
256 g_list_free (cancellable_list);
263 GDestroyNotify notify;
271 mainloop_proxy_func (gpointer data)
273 MainLoopProxy *proxy = data;
275 proxy->ret_val = proxy->func (proxy->data);
278 proxy->notify (proxy->data);
280 g_mutex_lock (&proxy->ack_lock);
282 g_cond_signal (&proxy->ack_condition);
283 g_mutex_unlock (&proxy->ack_lock);
289 mainloop_proxy_free (MainLoopProxy *proxy)
291 g_mutex_clear (&proxy->ack_lock);
292 g_cond_clear (&proxy->ack_condition);
297 * g_io_scheduler_job_send_to_mainloop:
298 * @job: a #GIOSchedulerJob
299 * @func: a #GSourceFunc callback that will be called in the original thread
300 * @user_data: data to pass to @func
301 * @notify: (allow-none): a #GDestroyNotify for @user_data, or %NULL
303 * Used from an I/O job to send a callback to be run in the thread
304 * that the job was started from, waiting for the result (and thus
305 * blocking the I/O job).
307 * Returns: The return value of @func
310 g_io_scheduler_job_send_to_mainloop (GIOSchedulerJob *job,
313 GDestroyNotify notify)
316 MainLoopProxy *proxy;
319 g_return_val_if_fail (job != NULL, FALSE);
320 g_return_val_if_fail (func != NULL, FALSE);
322 proxy = g_new0 (MainLoopProxy, 1);
324 proxy->data = user_data;
325 proxy->notify = notify;
326 g_mutex_init (&proxy->ack_lock);
327 g_cond_init (&proxy->ack_condition);
328 g_mutex_lock (&proxy->ack_lock);
330 source = g_idle_source_new ();
331 g_source_set_priority (source, G_PRIORITY_DEFAULT);
332 g_source_set_callback (source, mainloop_proxy_func, proxy,
335 g_source_attach (source, job->context);
336 g_source_unref (source);
339 g_cond_wait (&proxy->ack_condition, &proxy->ack_lock);
340 g_mutex_unlock (&proxy->ack_lock);
342 ret_val = proxy->ret_val;
343 mainloop_proxy_free (proxy);
349 * g_io_scheduler_job_send_to_mainloop_async:
350 * @job: a #GIOSchedulerJob
351 * @func: a #GSourceFunc callback that will be called in the original thread
352 * @user_data: data to pass to @func
353 * @notify: (allow-none): a #GDestroyNotify for @user_data, or %NULL
355 * Used from an I/O job to send a callback to be run asynchronously in
356 * the thread that the job was started from. The callback will be run
357 * when the main loop is available, but at that time the I/O job might
358 * have finished. The return value from the callback is ignored.
360 * Note that if you are passing the @user_data from g_io_scheduler_push_job()
361 * on to this function you have to ensure that it is not freed before
362 * @func is called, either by passing %NULL as @notify to
363 * g_io_scheduler_push_job() or by using refcounting for @user_data.
366 g_io_scheduler_job_send_to_mainloop_async (GIOSchedulerJob *job,
369 GDestroyNotify notify)
372 MainLoopProxy *proxy;
374 g_return_if_fail (job != NULL);
375 g_return_if_fail (func != NULL);
377 proxy = g_new0 (MainLoopProxy, 1);
379 proxy->data = user_data;
380 proxy->notify = notify;
381 g_mutex_init (&proxy->ack_lock);
382 g_cond_init (&proxy->ack_condition);
384 source = g_idle_source_new ();
385 g_source_set_priority (source, G_PRIORITY_DEFAULT);
386 g_source_set_callback (source, mainloop_proxy_func, proxy,
387 (GDestroyNotify)mainloop_proxy_free);
389 g_source_attach (source, job->context);
390 g_source_unref (source);