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"
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 may use threads if they
38 * <para id="io-priority"><indexterm><primary>I/O priority</primary></indexterm>
39 * Each I/O operation has a priority, and the scheduler uses the priorities
40 * to determine the order in which operations are executed. They are
41 * <emphasis>not</emphasis> used to determine system-wide I/O scheduling.
42 * Priorities are integers, with lower numbers indicating higher priority.
43 * It is recommended to choose priorities between %G_PRIORITY_LOW and
44 * %G_PRIORITY_HIGH, with %G_PRIORITY_DEFAULT as a default.
48 struct _GIOSchedulerJob {
50 GIOSchedulerJobFunc job_func;
51 GSourceFunc cancel_func; /* Runs under job map lock */
53 GDestroyNotify destroy_notify;
56 GCancellable *cancellable;
61 G_LOCK_DEFINE_STATIC(active_jobs);
62 static GSList *active_jobs = NULL;
64 static GThreadPool *job_thread_pool = NULL;
66 static void io_job_thread (gpointer data,
70 g_io_job_free (GIOSchedulerJob *job)
73 g_object_unref (job->cancellable);
78 g_io_job_compare (gconstpointer a,
82 const GIOSchedulerJob *aa = a;
83 const GIOSchedulerJob *bb = b;
85 /* Cancelled jobs are set prio == -1, so that
86 they are executed as quickly as possible */
88 /* Lower value => higher priority */
89 if (aa->io_priority < bb->io_priority)
91 if (aa->io_priority == bb->io_priority)
97 init_scheduler (gpointer arg)
99 if (job_thread_pool == NULL)
101 /* TODO: thread_pool_new can fail */
102 job_thread_pool = g_thread_pool_new (io_job_thread,
107 if (job_thread_pool != NULL)
109 g_thread_pool_set_sort_function (job_thread_pool,
112 /* Its kinda weird that this is a global setting
113 * instead of per threadpool. However, we really
114 * want to cache some threads, but not keep around
115 * those threads forever. */
116 g_thread_pool_set_max_idle_time (15 * 1000);
117 g_thread_pool_set_max_unused_threads (2);
124 remove_active_job (GIOSchedulerJob *job)
126 GIOSchedulerJob *other_job;
128 gboolean resort_jobs;
130 G_LOCK (active_jobs);
131 active_jobs = g_slist_delete_link (active_jobs, job->active_link);
134 for (l = active_jobs; l != NULL; l = l->next)
137 if (other_job->io_priority >= 0 &&
138 g_cancellable_is_cancelled (other_job->cancellable))
140 other_job->io_priority = -1;
144 G_UNLOCK (active_jobs);
147 job_thread_pool != NULL)
148 g_thread_pool_set_sort_function (job_thread_pool,
155 job_destroy (gpointer data)
157 GIOSchedulerJob *job = data;
159 if (job->destroy_notify)
160 job->destroy_notify (job->data);
162 remove_active_job (job);
167 io_job_thread (gpointer data,
170 GIOSchedulerJob *job = data;
173 if (job->cancellable)
174 g_cancellable_push_current (job->cancellable);
178 result = job->job_func (job, job->cancellable, job->data);
182 if (job->cancellable)
183 g_cancellable_pop_current (job->cancellable);
189 run_job_at_idle (gpointer data)
191 GIOSchedulerJob *job = data;
194 if (job->cancellable)
195 g_cancellable_push_current (job->cancellable);
197 result = job->job_func (job, job->cancellable, job->data);
199 if (job->cancellable)
200 g_cancellable_pop_current (job->cancellable);
206 * g_io_scheduler_push_job:
207 * @job_func: a #GIOSchedulerJobFunc.
208 * @user_data: data to pass to @job_func
209 * @notify: a #GDestroyNotify for @user_data, or %NULL
210 * @io_priority: the <link linkend="gioscheduler">I/O priority</link>
212 * @cancellable: optional #GCancellable object, %NULL to ignore.
214 * Schedules the I/O job to run.
216 * @notify will be called on @user_data after @job_func has returned,
217 * regardless whether the job was cancelled or has run to completion.
219 * If @cancellable is not %NULL, it can be used to cancel the I/O job
220 * by calling g_cancellable_cancel() or by calling
221 * g_io_scheduler_cancel_all_jobs().
224 g_io_scheduler_push_job (GIOSchedulerJobFunc job_func,
226 GDestroyNotify notify,
228 GCancellable *cancellable)
230 static GOnce once_init = G_ONCE_INIT;
231 GIOSchedulerJob *job;
233 g_return_if_fail (job_func != NULL);
235 job = g_new0 (GIOSchedulerJob, 1);
236 job->job_func = job_func;
237 job->data = user_data;
238 job->destroy_notify = notify;
239 job->io_priority = io_priority;
242 job->cancellable = g_object_ref (cancellable);
244 G_LOCK (active_jobs);
245 active_jobs = g_slist_prepend (active_jobs, job);
246 job->active_link = active_jobs;
247 G_UNLOCK (active_jobs);
249 if (g_thread_supported())
251 g_once (&once_init, init_scheduler, NULL);
252 g_thread_pool_push (job_thread_pool, job, NULL);
256 /* Threads not available, instead do the i/o sync inside a
257 * low prio idle handler
259 job->idle_tag = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE + 1 + io_priority / 10,
266 * g_io_scheduler_cancel_all_jobs:
268 * Cancels all cancellable I/O jobs.
270 * A job is cancellable if a #GCancellable was passed into
271 * g_io_scheduler_push_job().
274 g_io_scheduler_cancel_all_jobs (void)
276 GSList *cancellable_list, *l;
278 G_LOCK (active_jobs);
279 cancellable_list = NULL;
280 for (l = active_jobs; l != NULL; l = l->next)
282 GIOSchedulerJob *job = l->data;
283 if (job->cancellable)
284 cancellable_list = g_slist_prepend (cancellable_list,
285 g_object_ref (job->cancellable));
287 G_UNLOCK (active_jobs);
289 for (l = cancellable_list; l != NULL; l = l->next)
291 GCancellable *c = l->data;
292 g_cancellable_cancel (c);
295 g_slist_free (cancellable_list);
302 GDestroyNotify notify;
305 GCond *ack_condition;
309 mainloop_proxy_func (gpointer data)
311 MainLoopProxy *proxy = data;
313 proxy->ret_val = proxy->func (proxy->data);
316 proxy->notify (proxy->data);
320 g_mutex_lock (proxy->ack_lock);
321 g_cond_signal (proxy->ack_condition);
322 g_mutex_unlock (proxy->ack_lock);
329 mainloop_proxy_free (MainLoopProxy *proxy)
333 g_mutex_free (proxy->ack_lock);
334 g_cond_free (proxy->ack_condition);
341 * g_io_scheduler_job_send_to_mainloop:
342 * @job: a #GIOSchedulerJob
343 * @func: a #GSourceFunc callback that will be called in the main thread
344 * @user_data: data to pass to @func
345 * @notify: a #GDestroyNotify for @user_data, or %NULL
347 * Used from an I/O job to send a callback to be run in the
348 * main loop (main thread), waiting for the result (and thus
349 * blocking the I/O job).
351 * Returns: The return value of @func
354 g_io_scheduler_job_send_to_mainloop (GIOSchedulerJob *job,
357 GDestroyNotify notify)
360 MainLoopProxy *proxy;
364 g_return_val_if_fail (job != NULL, FALSE);
365 g_return_val_if_fail (func != NULL, FALSE);
369 /* We just immediately re-enter in the case of idles (non-threads)
370 * Anything else would just deadlock. If you can't handle this, enable threads.
372 ret_val = func (user_data);
378 proxy = g_new0 (MainLoopProxy, 1);
380 proxy->data = user_data;
381 proxy->notify = notify;
382 proxy->ack_lock = g_mutex_new ();
383 proxy->ack_condition = g_cond_new ();
384 g_mutex_lock (proxy->ack_lock);
386 source = g_idle_source_new ();
387 g_source_set_priority (source, G_PRIORITY_DEFAULT);
388 g_source_set_callback (source, mainloop_proxy_func, proxy,
391 id = g_source_attach (source, NULL);
392 g_source_unref (source);
394 g_cond_wait (proxy->ack_condition, proxy->ack_lock);
395 g_mutex_unlock (proxy->ack_lock);
397 ret_val = proxy->ret_val;
398 mainloop_proxy_free (proxy);
404 * g_io_scheduler_job_send_to_mainloop_async:
405 * @job: a #GIOSchedulerJob
406 * @func: a #GSourceFunc callback that will be called in the main thread
407 * @user_data: data to pass to @func
408 * @notify: a #GDestroyNotify for @user_data, or %NULL
410 * Used from an I/O job to send a callback to be run asynchronously
411 * in the main loop (main thread). The callback will be run when the
412 * main loop is available, but at that time the I/O job might have
413 * finished. The return value from the callback is ignored.
415 * Note that if you are passing the @user_data from g_io_scheduler_push_job()
416 * on to this function you have to ensure that it is not freed before
417 * @func is called, either by passing %NULL as @notify to
418 * g_io_scheduler_push_job() or by using refcounting for @user_data.
421 g_io_scheduler_job_send_to_mainloop_async (GIOSchedulerJob *job,
424 GDestroyNotify notify)
427 MainLoopProxy *proxy;
430 g_return_if_fail (job != NULL);
431 g_return_if_fail (func != NULL);
435 /* We just immediately re-enter in the case of idles (non-threads)
436 * Anything else would just deadlock. If you can't handle this, enable threads.
444 proxy = g_new0 (MainLoopProxy, 1);
446 proxy->data = user_data;
447 proxy->notify = notify;
449 source = g_idle_source_new ();
450 g_source_set_priority (source, G_PRIORITY_DEFAULT);
451 g_source_set_callback (source, mainloop_proxy_func, proxy,
452 (GDestroyNotify)mainloop_proxy_free);
454 id = g_source_attach (source, NULL);
455 g_source_unref (source);
459 #define __G_IO_SCHEDULER_C__
460 #include "gioaliasdef.c"