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"
28 * SECTION:gioscheduler
29 * @short_description: I/O Scheduler
31 * Schedules asynchronous I/O operations for integration into the main
32 * event loop (#GMainLoop).
39 GIODataFunc cancel_func; /* Runs under job map lock */
41 GDestroyNotify destroy_notify;
44 GCancellable *cancellable;
49 G_LOCK_DEFINE_STATIC(active_jobs);
50 static GSList *active_jobs = NULL;
52 static GThreadPool *job_thread_pool = NULL;
54 static void io_job_thread (gpointer data,
58 g_io_job_free (GIOJob *job)
61 g_object_unref (job->cancellable);
66 g_io_job_compare (gconstpointer a,
73 /* Cancelled jobs are set prio == -1, so that
74 they are executed as quickly as possible */
76 /* Lower value => higher priority */
77 if (aa->io_priority < bb->io_priority)
79 if (aa->io_priority == bb->io_priority)
85 init_scheduler (gpointer arg)
87 if (job_thread_pool == NULL)
89 /* TODO: thread_pool_new can fail */
90 job_thread_pool = g_thread_pool_new (io_job_thread,
95 if (job_thread_pool != NULL)
97 g_thread_pool_set_sort_function (job_thread_pool,
100 /* Its kinda weird that this is a global setting
101 * instead of per threadpool. However, we really
102 * want to cache some threads, but not keep around
103 * those threads forever. */
104 g_thread_pool_set_max_idle_time (15 * 1000);
105 g_thread_pool_set_max_unused_threads (2);
112 remove_active_job (GIOJob *job)
116 gboolean resort_jobs;
118 G_LOCK (active_jobs);
119 active_jobs = g_slist_delete_link (active_jobs, job->active_link);
122 for (l = active_jobs; l != NULL; l = l->next)
125 if (other_job->io_priority >= 0 &&
126 g_cancellable_is_cancelled (other_job->cancellable))
128 other_job->io_priority = -1;
132 G_UNLOCK (active_jobs);
135 job_thread_pool != NULL)
136 g_thread_pool_set_sort_function (job_thread_pool,
143 io_job_thread (gpointer data,
148 if (job->cancellable)
149 g_push_current_cancellable (job->cancellable);
150 job->job_func (job, job->cancellable, job->data);
151 if (job->cancellable)
152 g_pop_current_cancellable (job->cancellable);
154 if (job->destroy_notify)
155 job->destroy_notify (job->data);
157 remove_active_job (job);
163 run_job_at_idle (gpointer data)
167 if (job->cancellable)
168 g_push_current_cancellable (job->cancellable);
170 job->job_func (job, job->cancellable, job->data);
172 if (job->cancellable)
173 g_pop_current_cancellable (job->cancellable);
175 if (job->destroy_notify)
176 job->destroy_notify (job->data);
178 remove_active_job (job);
186 * @job_func: a #GIOJobFunc.
187 * @user_data: a #gpointer.
188 * @notify: a #GDestroyNotify.
189 * @io_priority: the i/o priority of the request. a #gint.
190 * @cancellable: optional #GCancellable object, %NULL to ignore.
192 * Schedules the I/O Job.
196 g_schedule_io_job (GIOJobFunc job_func,
198 GDestroyNotify notify,
200 GCancellable *cancellable)
202 static GOnce once_init = G_ONCE_INIT;
205 g_return_if_fail (job_func != NULL);
207 job = g_new0 (GIOJob, 1);
208 job->job_func = job_func;
209 job->data = user_data;
210 job->destroy_notify = notify;
211 job->io_priority = io_priority;
214 job->cancellable = g_object_ref (cancellable);
216 G_LOCK (active_jobs);
217 active_jobs = g_slist_prepend (active_jobs, job);
218 job->active_link = active_jobs;
219 G_UNLOCK (active_jobs);
221 if (g_thread_supported())
223 g_once (&once_init, init_scheduler, NULL);
224 g_thread_pool_push (job_thread_pool, job, NULL);
228 /* Threads not available, instead do the i/o sync inside a
229 * low prio idle handler
231 job->idle_tag = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE + 1 + io_priority / 10,
238 * g_cancel_all_io_jobs:
240 * Cancels all cancellable I/O Jobs.
243 g_cancel_all_io_jobs (void)
245 GSList *cancellable_list, *l;
247 G_LOCK (active_jobs);
248 cancellable_list = NULL;
249 for (l = active_jobs; l != NULL; l = l->next)
251 GIOJob *job = l->data;
252 if (job->cancellable)
253 cancellable_list = g_slist_prepend (cancellable_list,
254 g_object_ref (job->cancellable));
256 G_UNLOCK (active_jobs);
258 for (l = cancellable_list; l != NULL; l = l->next)
260 GCancellable *c = l->data;
261 g_cancellable_cancel (c);
264 g_slist_free (cancellable_list);
270 GDestroyNotify notify;
273 GCond *ack_condition;
277 mainloop_proxy_func (gpointer data)
279 MainLoopProxy *proxy = data;
281 proxy->func (proxy->data);
285 g_mutex_lock (proxy->ack_lock);
286 g_cond_signal (proxy->ack_condition);
287 g_mutex_unlock (proxy->ack_lock);
294 mainloop_proxy_free (MainLoopProxy *proxy)
298 g_mutex_free (proxy->ack_lock);
299 g_cond_free (proxy->ack_condition);
306 mainloop_proxy_notify (gpointer data)
308 MainLoopProxy *proxy = data;
311 proxy->notify (proxy->data);
313 /* If nonblocking we free here, otherwise we free in io thread */
314 if (proxy->ack_lock == NULL)
315 mainloop_proxy_free (proxy);
319 * g_io_job_send_to_mainloop:
321 * @func: a #GIODataFunc.
322 * @user_data: a #gpointer.
323 * @notify: a #GDestroyNotify.
324 * @block: boolean flag indicating whether or not this job should block.
326 * Sends an I/O job to the application's main loop for processing.
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.
352 proxy = g_new0 (MainLoopProxy, 1);
354 proxy->data = user_data;
355 proxy->notify = notify;
359 proxy->ack_lock = g_mutex_new ();
360 proxy->ack_condition = g_cond_new ();
363 source = g_idle_source_new ();
364 g_source_set_priority (source, G_PRIORITY_DEFAULT);
366 g_source_set_callback (source, mainloop_proxy_func, proxy, mainloop_proxy_notify);
369 g_mutex_lock (proxy->ack_lock);
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 /* destroy notify didn't free proxy */
380 mainloop_proxy_free (proxy);