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
33 * Schedules asynchronous I/O operations. #GIOScheduler integrates into the main
34 * event loop (#GMainLoop) and may use threads if they are available.
36 * <para id="io-priority"><indexterm><primary>I/O priority</primary></indexterm>
37 * Each I/O operation has a priority, and the scheduler uses the priorities
38 * to determine the order in which operations are executed. They are
39 * <emphasis>not</emphasis> used to determine system-wide I/O scheduling.
40 * Priorities are integers, with lower numbers indicating higher priority.
41 * It is recommended to choose priorities between %G_PRIORITY_LOW and
42 * %G_PRIORITY_HIGH, with %G_PRIORITY_DEFAULT as a default.
49 GIODataFunc cancel_func; /* Runs under job map lock */
51 GDestroyNotify destroy_notify;
54 GCancellable *cancellable;
59 G_LOCK_DEFINE_STATIC(active_jobs);
60 static GSList *active_jobs = NULL;
62 static GThreadPool *job_thread_pool = NULL;
64 static void io_job_thread (gpointer data,
68 g_io_job_free (GIOJob *job)
71 g_object_unref (job->cancellable);
76 g_io_job_compare (gconstpointer a,
83 /* Cancelled jobs are set prio == -1, so that
84 they are executed as quickly as possible */
86 /* Lower value => higher priority */
87 if (aa->io_priority < bb->io_priority)
89 if (aa->io_priority == bb->io_priority)
95 init_scheduler (gpointer arg)
97 if (job_thread_pool == NULL)
99 /* TODO: thread_pool_new can fail */
100 job_thread_pool = g_thread_pool_new (io_job_thread,
105 if (job_thread_pool != NULL)
107 g_thread_pool_set_sort_function (job_thread_pool,
110 /* Its kinda weird that this is a global setting
111 * instead of per threadpool. However, we really
112 * want to cache some threads, but not keep around
113 * those threads forever. */
114 g_thread_pool_set_max_idle_time (15 * 1000);
115 g_thread_pool_set_max_unused_threads (2);
122 remove_active_job (GIOJob *job)
126 gboolean resort_jobs;
128 G_LOCK (active_jobs);
129 active_jobs = g_slist_delete_link (active_jobs, job->active_link);
132 for (l = active_jobs; l != NULL; l = l->next)
135 if (other_job->io_priority >= 0 &&
136 g_cancellable_is_cancelled (other_job->cancellable))
138 other_job->io_priority = -1;
142 G_UNLOCK (active_jobs);
145 job_thread_pool != NULL)
146 g_thread_pool_set_sort_function (job_thread_pool,
153 io_job_thread (gpointer data,
158 if (job->cancellable)
159 g_push_current_cancellable (job->cancellable);
160 job->job_func (job, job->cancellable, job->data);
161 if (job->cancellable)
162 g_pop_current_cancellable (job->cancellable);
164 if (job->destroy_notify)
165 job->destroy_notify (job->data);
167 remove_active_job (job);
173 run_job_at_idle (gpointer data)
177 if (job->cancellable)
178 g_push_current_cancellable (job->cancellable);
180 job->job_func (job, job->cancellable, job->data);
182 if (job->cancellable)
183 g_pop_current_cancellable (job->cancellable);
185 if (job->destroy_notify)
186 job->destroy_notify (job->data);
188 remove_active_job (job);
196 * @job_func: a #GIOJobFunc.
197 * @user_data: a #gpointer.
198 * @notify: a #GDestroyNotify.
199 * @io_priority: the <link linkend="gioscheduler">I/O priority</link>
201 * @cancellable: optional #GCancellable object, %NULL to ignore.
203 * Schedules the I/O Job.
207 g_schedule_io_job (GIOJobFunc job_func,
209 GDestroyNotify notify,
211 GCancellable *cancellable)
213 static GOnce once_init = G_ONCE_INIT;
216 g_return_if_fail (job_func != NULL);
218 job = g_new0 (GIOJob, 1);
219 job->job_func = job_func;
220 job->data = user_data;
221 job->destroy_notify = notify;
222 job->io_priority = io_priority;
225 job->cancellable = g_object_ref (cancellable);
227 G_LOCK (active_jobs);
228 active_jobs = g_slist_prepend (active_jobs, job);
229 job->active_link = active_jobs;
230 G_UNLOCK (active_jobs);
232 if (g_thread_supported())
234 g_once (&once_init, init_scheduler, NULL);
235 g_thread_pool_push (job_thread_pool, job, NULL);
239 /* Threads not available, instead do the i/o sync inside a
240 * low prio idle handler
242 job->idle_tag = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE + 1 + io_priority / 10,
249 * g_cancel_all_io_jobs:
251 * Cancels all cancellable I/O Jobs.
254 g_cancel_all_io_jobs (void)
256 GSList *cancellable_list, *l;
258 G_LOCK (active_jobs);
259 cancellable_list = NULL;
260 for (l = active_jobs; l != NULL; l = l->next)
262 GIOJob *job = l->data;
263 if (job->cancellable)
264 cancellable_list = g_slist_prepend (cancellable_list,
265 g_object_ref (job->cancellable));
267 G_UNLOCK (active_jobs);
269 for (l = cancellable_list; l != NULL; l = l->next)
271 GCancellable *c = l->data;
272 g_cancellable_cancel (c);
275 g_slist_free (cancellable_list);
281 GDestroyNotify notify;
284 GCond *ack_condition;
288 mainloop_proxy_func (gpointer data)
290 MainLoopProxy *proxy = data;
292 proxy->func (proxy->data);
295 proxy->notify (proxy->data);
299 g_mutex_lock (proxy->ack_lock);
300 g_cond_signal (proxy->ack_condition);
301 g_mutex_unlock (proxy->ack_lock);
308 mainloop_proxy_free (MainLoopProxy *proxy)
312 g_mutex_free (proxy->ack_lock);
313 g_cond_free (proxy->ack_condition);
320 * g_io_job_send_to_mainloop:
322 * @func: a #GIODataFunc callback that will be called the main thread.
323 * @user_data: a #gpointer.
324 * @notify: a #GDestroyNotify.
325 * @block: boolean flag indicating whether or not the job should block until the callback has returned.
327 * Sends a notification from an I/O job to the mainloop processing on the main thread.
330 g_io_job_send_to_mainloop (GIOJob *job,
333 GDestroyNotify notify,
337 MainLoopProxy *proxy;
340 g_return_if_fail (job != NULL);
341 g_return_if_fail (func != NULL);
345 /* We just immediately re-enter in the case of idles (non-threads)
346 * Anything else would just deadlock. If you can't handle this, enable threads.
354 proxy = g_new0 (MainLoopProxy, 1);
356 proxy->data = user_data;
357 proxy->notify = notify;
361 proxy->ack_lock = g_mutex_new ();
362 proxy->ack_condition = g_cond_new ();
363 g_mutex_lock (proxy->ack_lock);
366 source = g_idle_source_new ();
367 g_source_set_priority (source, G_PRIORITY_DEFAULT);
368 g_source_set_callback (source, mainloop_proxy_func, proxy,
369 block ? NULL : (GDestroyNotify)mainloop_proxy_free);
371 id = g_source_attach (source, NULL);
372 g_source_unref (source);
376 g_cond_wait (proxy->ack_condition, proxy->ack_lock);
377 g_mutex_unlock (proxy->ack_lock);
379 /* In the blocking case we free the proxy here */
380 mainloop_proxy_free (proxy);
384 #define __G_IO_SCHEDULER_C__
385 #include "gioaliasdef.c"