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 GIODataFunc cancel_func; /* Runs under job map lock */
32 GDestroyNotify destroy_notify;
35 GCancellable *cancellable;
40 G_LOCK_DEFINE_STATIC(active_jobs);
41 static GSList *active_jobs = NULL;
43 static GThreadPool *job_thread_pool = NULL;
45 static void io_job_thread (gpointer data,
49 g_io_job_free (GIOJob *job)
52 g_object_unref (job->cancellable);
57 g_io_job_compare (gconstpointer a,
64 /* Cancelled jobs are set prio == -1, so that
65 they are executed as quickly as possible */
67 /* Lower value => higher priority */
68 if (aa->io_priority < bb->io_priority)
70 if (aa->io_priority == bb->io_priority)
76 init_scheduler (gpointer arg)
78 if (job_thread_pool == NULL)
80 /* TODO: thread_pool_new can fail */
81 job_thread_pool = g_thread_pool_new (io_job_thread,
86 if (job_thread_pool != NULL)
88 g_thread_pool_set_sort_function (job_thread_pool,
91 /* Its kinda weird that this is a global setting
92 * instead of per threadpool. However, we really
93 * want to cache some threads, but not keep around
94 * those threads forever. */
95 g_thread_pool_set_max_idle_time (15 * 1000);
96 g_thread_pool_set_max_unused_threads (2);
103 remove_active_job (GIOJob *job)
107 gboolean resort_jobs;
109 G_LOCK (active_jobs);
110 active_jobs = g_slist_delete_link (active_jobs, job->active_link);
113 for (l = active_jobs; l != NULL; l = l->next)
116 if (other_job->io_priority >= 0 &&
117 g_cancellable_is_cancelled (other_job->cancellable))
119 other_job->io_priority = -1;
123 G_UNLOCK (active_jobs);
126 job_thread_pool != NULL)
127 g_thread_pool_set_sort_function (job_thread_pool,
134 io_job_thread (gpointer data,
139 if (job->cancellable)
140 g_push_current_cancellable (job->cancellable);
141 job->job_func (job, job->cancellable, job->data);
142 if (job->cancellable)
143 g_pop_current_cancellable (job->cancellable);
145 if (job->destroy_notify)
146 job->destroy_notify (job->data);
148 remove_active_job (job);
154 run_job_at_idle (gpointer data)
158 if (job->cancellable)
159 g_push_current_cancellable (job->cancellable);
161 job->job_func (job, job->cancellable, job->data);
163 if (job->cancellable)
164 g_pop_current_cancellable (job->cancellable);
166 if (job->destroy_notify)
167 job->destroy_notify (job->data);
169 remove_active_job (job);
177 * @job_func: a #GIOJobFunc.
178 * @user_data: a #gpointer.
179 * @notify: a #GDestroyNotify.
180 * @io_priority: the io priority of the request. a #gint.
181 * @cancellable: optional #GCancellable object, %NULL to ignore.
183 * Schedules the @job_func.
187 g_schedule_io_job (GIOJobFunc job_func,
189 GDestroyNotify notify,
191 GCancellable *cancellable)
193 static GOnce once_init = G_ONCE_INIT;
196 g_return_if_fail (job_func != NULL);
198 job = g_new0 (GIOJob, 1);
199 job->job_func = job_func;
200 job->data = user_data;
201 job->destroy_notify = notify;
202 job->io_priority = io_priority;
205 job->cancellable = g_object_ref (cancellable);
207 G_LOCK (active_jobs);
208 active_jobs = g_slist_prepend (active_jobs, job);
209 job->active_link = active_jobs;
210 G_UNLOCK (active_jobs);
212 if (g_thread_supported())
214 g_once (&once_init, init_scheduler, NULL);
215 g_thread_pool_push (job_thread_pool, job, NULL);
219 /* Threads not available, instead do the i/o sync inside a
220 * low prio idle handler
222 job->idle_tag = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE + 1 + io_priority / 10,
229 * g_cancel_all_io_jobs:
231 * Cancels all cancellable I/O Jobs.
234 g_cancel_all_io_jobs (void)
236 GSList *cancellable_list, *l;
238 G_LOCK (active_jobs);
239 cancellable_list = NULL;
240 for (l = active_jobs; l != NULL; l = l->next)
242 GIOJob *job = l->data;
243 if (job->cancellable)
244 cancellable_list = g_slist_prepend (cancellable_list,
245 g_object_ref (job->cancellable));
247 G_UNLOCK (active_jobs);
249 for (l = cancellable_list; l != NULL; l = l->next)
251 GCancellable *c = l->data;
252 g_cancellable_cancel (c);
255 g_slist_free (cancellable_list);
261 GDestroyNotify notify;
264 GCond *ack_condition;
268 mainloop_proxy_func (gpointer data)
270 MainLoopProxy *proxy = data;
272 proxy->func (proxy->data);
276 g_mutex_lock (proxy->ack_lock);
277 g_cond_signal (proxy->ack_condition);
278 g_mutex_unlock (proxy->ack_lock);
285 mainloop_proxy_free (MainLoopProxy *proxy)
289 g_mutex_free (proxy->ack_lock);
290 g_cond_free (proxy->ack_condition);
297 mainloop_proxy_notify (gpointer data)
299 MainLoopProxy *proxy = data;
302 proxy->notify (proxy->data);
304 /* If nonblocking we free here, otherwise we free in io thread */
305 if (proxy->ack_lock == NULL)
306 mainloop_proxy_free (proxy);
310 * g_io_job_send_to_mainloop:
312 * @func: a #GIODataFunc.
313 * @user_data: a #gpointer.
314 * @notify: a #GDestroyNotify.
315 * @block: boolean flag indicating whether or not this job should block.
320 g_io_job_send_to_mainloop (GIOJob *job,
323 GDestroyNotify notify,
327 MainLoopProxy *proxy;
330 g_return_if_fail (job != NULL);
331 g_return_if_fail (func != NULL);
335 /* We just immediately re-enter in the case of idles (non-threads)
336 * Anything else would just deadlock. If you can't handle this, enable threads.
342 proxy = g_new0 (MainLoopProxy, 1);
344 proxy->data = user_data;
345 proxy->notify = notify;
349 proxy->ack_lock = g_mutex_new ();
350 proxy->ack_condition = g_cond_new ();
353 source = g_idle_source_new ();
354 g_source_set_priority (source, G_PRIORITY_DEFAULT);
356 g_source_set_callback (source, mainloop_proxy_func, proxy, mainloop_proxy_notify);
359 g_mutex_lock (proxy->ack_lock);
361 id = g_source_attach (source, NULL);
362 g_source_unref (source);
366 g_cond_wait (proxy->ack_condition, proxy->ack_lock);
367 g_mutex_unlock (proxy->ack_lock);
369 /* destroy notify didn't free proxy */
370 mainloop_proxy_free (proxy);