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"
31 * SECTION:gioscheduler
32 * @short_description: I/O Scheduler
35 * Schedules asynchronous I/O operations. #GIOScheduler integrates
36 * into the main event loop (#GMainLoop) and may use threads if they
39 * <para id="io-priority"><indexterm><primary>I/O priority</primary></indexterm>
40 * Each I/O operation has a priority, and the scheduler uses the priorities
41 * to determine the order in which operations are executed. They are
42 * <emphasis>not</emphasis> used to determine system-wide I/O scheduling.
43 * Priorities are integers, with lower numbers indicating higher priority.
44 * It is recommended to choose priorities between %G_PRIORITY_LOW and
45 * %G_PRIORITY_HIGH, with %G_PRIORITY_DEFAULT as a default.
49 struct _GIOSchedulerJob {
51 GIOSchedulerJobFunc job_func;
52 GSourceFunc cancel_func; /* Runs under job map lock */
54 GDestroyNotify destroy_notify;
57 GCancellable *cancellable;
58 GMainContext *context;
63 G_LOCK_DEFINE_STATIC(active_jobs);
64 static GSList *active_jobs = NULL;
66 static GThreadPool *job_thread_pool = NULL;
68 static void io_job_thread (gpointer data,
72 g_io_job_free (GIOSchedulerJob *job)
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 remove_active_job (GIOSchedulerJob *job)
130 GIOSchedulerJob *other_job;
132 gboolean resort_jobs;
134 G_LOCK (active_jobs);
135 active_jobs = g_slist_delete_link (active_jobs, job->active_link);
138 for (l = active_jobs; l != NULL; l = l->next)
141 if (other_job->io_priority >= 0 &&
142 g_cancellable_is_cancelled (other_job->cancellable))
144 other_job->io_priority = -1;
148 G_UNLOCK (active_jobs);
151 job_thread_pool != NULL)
152 g_thread_pool_set_sort_function (job_thread_pool,
159 job_destroy (gpointer data)
161 GIOSchedulerJob *job = data;
163 if (job->destroy_notify)
164 job->destroy_notify (job->data);
166 remove_active_job (job);
171 io_job_thread (gpointer data,
174 GIOSchedulerJob *job = data;
177 if (job->cancellable)
178 g_cancellable_push_current (job->cancellable);
182 result = job->job_func (job, job->cancellable, job->data);
186 if (job->cancellable)
187 g_cancellable_pop_current (job->cancellable);
193 run_job_at_idle (gpointer data)
195 GIOSchedulerJob *job = data;
198 if (job->cancellable)
199 g_cancellable_push_current (job->cancellable);
201 result = job->job_func (job, job->cancellable, job->data);
203 if (job->cancellable)
204 g_cancellable_pop_current (job->cancellable);
210 * g_io_scheduler_push_job:
211 * @job_func: a #GIOSchedulerJobFunc.
212 * @user_data: data to pass to @job_func
213 * @notify: a #GDestroyNotify for @user_data, or %NULL
214 * @io_priority: the <link linkend="gioscheduler">I/O priority</link>
216 * @cancellable: optional #GCancellable object, %NULL to ignore.
218 * Schedules the I/O job to run.
220 * @notify will be called on @user_data after @job_func has returned,
221 * regardless whether the job was cancelled or has run to completion.
223 * If @cancellable is not %NULL, it can be used to cancel the I/O job
224 * by calling g_cancellable_cancel() or by calling
225 * g_io_scheduler_cancel_all_jobs().
228 g_io_scheduler_push_job (GIOSchedulerJobFunc job_func,
230 GDestroyNotify notify,
232 GCancellable *cancellable)
234 static GOnce once_init = G_ONCE_INIT;
235 GIOSchedulerJob *job;
237 g_return_if_fail (job_func != NULL);
239 job = g_new0 (GIOSchedulerJob, 1);
240 job->job_func = job_func;
241 job->data = user_data;
242 job->destroy_notify = notify;
243 job->io_priority = io_priority;
246 job->cancellable = g_object_ref (cancellable);
248 job->context = g_main_context_get_thread_default ();
250 g_main_context_ref (job->context);
252 G_LOCK (active_jobs);
253 active_jobs = g_slist_prepend (active_jobs, job);
254 job->active_link = active_jobs;
255 G_UNLOCK (active_jobs);
257 if (g_thread_supported())
259 g_once (&once_init, init_scheduler, NULL);
260 g_thread_pool_push (job_thread_pool, job, NULL);
264 /* Threads not available, instead do the i/o sync inside a
265 * low prio idle handler
267 job->idle_tag = g_idle_add_full (io_priority,
274 * g_io_scheduler_cancel_all_jobs:
276 * Cancels all cancellable I/O jobs.
278 * A job is cancellable if a #GCancellable was passed into
279 * g_io_scheduler_push_job().
282 g_io_scheduler_cancel_all_jobs (void)
284 GSList *cancellable_list, *l;
286 G_LOCK (active_jobs);
287 cancellable_list = NULL;
288 for (l = active_jobs; l != NULL; l = l->next)
290 GIOSchedulerJob *job = l->data;
291 if (job->cancellable)
292 cancellable_list = g_slist_prepend (cancellable_list,
293 g_object_ref (job->cancellable));
295 G_UNLOCK (active_jobs);
297 for (l = cancellable_list; l != NULL; l = l->next)
299 GCancellable *c = l->data;
300 g_cancellable_cancel (c);
303 g_slist_free (cancellable_list);
310 GDestroyNotify notify;
313 GCond *ack_condition;
317 mainloop_proxy_func (gpointer data)
319 MainLoopProxy *proxy = data;
321 proxy->ret_val = proxy->func (proxy->data);
324 proxy->notify (proxy->data);
328 g_mutex_lock (proxy->ack_lock);
329 g_cond_signal (proxy->ack_condition);
330 g_mutex_unlock (proxy->ack_lock);
337 mainloop_proxy_free (MainLoopProxy *proxy)
341 g_mutex_free (proxy->ack_lock);
342 g_cond_free (proxy->ack_condition);
349 * g_io_scheduler_job_send_to_mainloop:
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: a #GDestroyNotify for @user_data, or %NULL
355 * Used from an I/O job to send a callback to be run in the thread
356 * that the job was started from, waiting for the result (and thus
357 * blocking the I/O job).
359 * Returns: The return value of @func
362 g_io_scheduler_job_send_to_mainloop (GIOSchedulerJob *job,
365 GDestroyNotify notify)
368 MainLoopProxy *proxy;
371 g_return_val_if_fail (job != NULL, FALSE);
372 g_return_val_if_fail (func != NULL, FALSE);
376 /* We just immediately re-enter in the case of idles (non-threads)
377 * Anything else would just deadlock. If you can't handle this, enable threads.
379 ret_val = func (user_data);
385 proxy = g_new0 (MainLoopProxy, 1);
387 proxy->data = user_data;
388 proxy->notify = notify;
389 proxy->ack_lock = g_mutex_new ();
390 proxy->ack_condition = g_cond_new ();
391 g_mutex_lock (proxy->ack_lock);
393 source = g_idle_source_new ();
394 g_source_set_priority (source, G_PRIORITY_DEFAULT);
395 g_source_set_callback (source, mainloop_proxy_func, proxy,
398 g_source_attach (source, job->context);
399 g_source_unref (source);
401 g_cond_wait (proxy->ack_condition, proxy->ack_lock);
402 g_mutex_unlock (proxy->ack_lock);
404 ret_val = proxy->ret_val;
405 mainloop_proxy_free (proxy);
411 * g_io_scheduler_job_send_to_mainloop_async:
412 * @job: a #GIOSchedulerJob
413 * @func: a #GSourceFunc callback that will be called in the original thread
414 * @user_data: data to pass to @func
415 * @notify: a #GDestroyNotify for @user_data, or %NULL
417 * Used from an I/O job to send a callback to be run asynchronously in
418 * the thread that the job was started from. The callback will be run
419 * when the main loop is available, but at that time the I/O job might
420 * have finished. The return value from the callback is ignored.
422 * Note that if you are passing the @user_data from g_io_scheduler_push_job()
423 * on to this function you have to ensure that it is not freed before
424 * @func is called, either by passing %NULL as @notify to
425 * g_io_scheduler_push_job() or by using refcounting for @user_data.
428 g_io_scheduler_job_send_to_mainloop_async (GIOSchedulerJob *job,
431 GDestroyNotify notify)
434 MainLoopProxy *proxy;
436 g_return_if_fail (job != NULL);
437 g_return_if_fail (func != NULL);
441 /* We just immediately re-enter in the case of idles (non-threads)
442 * Anything else would just deadlock. If you can't handle this, enable threads.
450 proxy = g_new0 (MainLoopProxy, 1);
452 proxy->data = user_data;
453 proxy->notify = notify;
455 source = g_idle_source_new ();
456 g_source_set_priority (source, G_PRIORITY_DEFAULT);
457 g_source_set_callback (source, mainloop_proxy_func, proxy,
458 (GDestroyNotify)mainloop_proxy_free);
460 g_source_attach (source, job->context);
461 g_source_unref (source);
465 #define __G_IO_SCHEDULER_C__
466 #include "gioaliasdef.c"