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,
116 /* It's kinda weird that this is a global setting
117 * instead of per threadpool. However, we really
118 * want to cache some threads, but not keep around
119 * those threads forever. */
120 g_thread_pool_set_max_idle_time (15 * 1000);
121 g_thread_pool_set_max_unused_threads (2);
128 on_job_canceled (GCancellable *cancellable,
131 GIOSchedulerJob *job = user_data;
133 job->io_priority = -1;
134 job->cancellable_id = 0;
136 if (job_thread_pool != NULL)
137 g_thread_pool_set_sort_function (job_thread_pool,
143 job_destroy (gpointer data)
145 GIOSchedulerJob *job = data;
147 if (job->destroy_notify)
148 job->destroy_notify (job->data);
150 G_LOCK (active_jobs);
151 active_jobs = g_list_delete_link (active_jobs, job->active_link);
152 G_UNLOCK (active_jobs);
157 io_job_thread (gpointer data,
160 GIOSchedulerJob *job = data;
163 if (job->cancellable)
164 g_cancellable_push_current (job->cancellable);
168 result = job->job_func (job, job->cancellable, job->data);
172 if (job->cancellable)
173 g_cancellable_pop_current (job->cancellable);
179 * g_io_scheduler_push_job:
180 * @job_func: a #GIOSchedulerJobFunc.
181 * @user_data: data to pass to @job_func
182 * @notify: (allow-none): a #GDestroyNotify for @user_data, or %NULL
183 * @io_priority: the <link linkend="gioscheduler">I/O priority</link>
185 * @cancellable: optional #GCancellable object, %NULL to ignore.
187 * Schedules the I/O job to run in another thread.
189 * @notify will be called on @user_data after @job_func has returned,
190 * regardless whether the job was cancelled or has run to completion.
192 * If @cancellable is not %NULL, it can be used to cancel the I/O job
193 * by calling g_cancellable_cancel() or by calling
194 * g_io_scheduler_cancel_all_jobs().
197 g_io_scheduler_push_job (GIOSchedulerJobFunc job_func,
199 GDestroyNotify notify,
201 GCancellable *cancellable)
203 static GOnce once_init = G_ONCE_INIT;
204 GIOSchedulerJob *job;
206 g_return_if_fail (job_func != NULL);
208 job = g_new0 (GIOSchedulerJob, 1);
209 job->job_func = job_func;
210 job->data = user_data;
211 job->destroy_notify = notify;
212 job->io_priority = io_priority;
216 job->cancellable = g_object_ref (cancellable);
217 job->cancellable_id = g_cancellable_connect (job->cancellable, (GCallback)on_job_canceled,
221 job->context = g_main_context_ref_thread_default ();
223 G_LOCK (active_jobs);
224 active_jobs = g_list_prepend (active_jobs, job);
225 job->active_link = active_jobs;
226 G_UNLOCK (active_jobs);
228 g_once (&once_init, init_scheduler, NULL);
229 g_thread_pool_push (job_thread_pool, job, NULL);
233 * g_io_scheduler_cancel_all_jobs:
235 * Cancels all cancellable I/O jobs.
237 * A job is cancellable if a #GCancellable was passed into
238 * g_io_scheduler_push_job().
241 g_io_scheduler_cancel_all_jobs (void)
243 GList *cancellable_list, *l;
245 G_LOCK (active_jobs);
246 cancellable_list = NULL;
247 for (l = active_jobs; l != NULL; l = l->next)
249 GIOSchedulerJob *job = l->data;
250 if (job->cancellable)
251 cancellable_list = g_list_prepend (cancellable_list,
252 g_object_ref (job->cancellable));
254 G_UNLOCK (active_jobs);
256 for (l = cancellable_list; l != NULL; l = l->next)
258 GCancellable *c = l->data;
259 g_cancellable_cancel (c);
262 g_list_free (cancellable_list);
269 GDestroyNotify notify;
277 mainloop_proxy_func (gpointer data)
279 MainLoopProxy *proxy = data;
281 proxy->ret_val = proxy->func (proxy->data);
284 proxy->notify (proxy->data);
286 g_mutex_lock (&proxy->ack_lock);
288 g_cond_signal (&proxy->ack_condition);
289 g_mutex_unlock (&proxy->ack_lock);
295 mainloop_proxy_free (MainLoopProxy *proxy)
297 g_mutex_clear (&proxy->ack_lock);
298 g_cond_clear (&proxy->ack_condition);
303 * g_io_scheduler_job_send_to_mainloop:
304 * @job: a #GIOSchedulerJob
305 * @func: a #GSourceFunc callback that will be called in the original thread
306 * @user_data: data to pass to @func
307 * @notify: (allow-none): a #GDestroyNotify for @user_data, or %NULL
309 * Used from an I/O job to send a callback to be run in the thread
310 * that the job was started from, waiting for the result (and thus
311 * blocking the I/O job).
313 * Returns: The return value of @func
316 g_io_scheduler_job_send_to_mainloop (GIOSchedulerJob *job,
319 GDestroyNotify notify)
322 MainLoopProxy *proxy;
325 g_return_val_if_fail (job != NULL, FALSE);
326 g_return_val_if_fail (func != NULL, FALSE);
328 proxy = g_new0 (MainLoopProxy, 1);
330 proxy->data = user_data;
331 proxy->notify = notify;
332 g_mutex_init (&proxy->ack_lock);
333 g_cond_init (&proxy->ack_condition);
334 g_mutex_lock (&proxy->ack_lock);
336 source = g_idle_source_new ();
337 g_source_set_priority (source, G_PRIORITY_DEFAULT);
338 g_source_set_callback (source, mainloop_proxy_func, proxy,
341 g_source_attach (source, job->context);
342 g_source_unref (source);
345 g_cond_wait (&proxy->ack_condition, &proxy->ack_lock);
346 g_mutex_unlock (&proxy->ack_lock);
348 ret_val = proxy->ret_val;
349 mainloop_proxy_free (proxy);
355 * g_io_scheduler_job_send_to_mainloop_async:
356 * @job: a #GIOSchedulerJob
357 * @func: a #GSourceFunc callback that will be called in the original thread
358 * @user_data: data to pass to @func
359 * @notify: (allow-none): a #GDestroyNotify for @user_data, or %NULL
361 * Used from an I/O job to send a callback to be run asynchronously in
362 * the thread that the job was started from. The callback will be run
363 * when the main loop is available, but at that time the I/O job might
364 * have finished. The return value from the callback is ignored.
366 * Note that if you are passing the @user_data from g_io_scheduler_push_job()
367 * on to this function you have to ensure that it is not freed before
368 * @func is called, either by passing %NULL as @notify to
369 * g_io_scheduler_push_job() or by using refcounting for @user_data.
372 g_io_scheduler_job_send_to_mainloop_async (GIOSchedulerJob *job,
375 GDestroyNotify notify)
378 MainLoopProxy *proxy;
380 g_return_if_fail (job != NULL);
381 g_return_if_fail (func != NULL);
383 proxy = g_new0 (MainLoopProxy, 1);
385 proxy->data = user_data;
386 proxy->notify = notify;
387 g_mutex_init (&proxy->ack_lock);
388 g_cond_init (&proxy->ack_condition);
390 source = g_idle_source_new ();
391 g_source_set_priority (source, G_PRIORITY_DEFAULT);
392 g_source_set_callback (source, mainloop_proxy_func, proxy,
393 (GDestroyNotify)mainloop_proxy_free);
395 g_source_attach (source, job->context);
396 g_source_unref (source);