Make GSettingsSchemaKey public
[platform/upstream/glib.git] / gio / gioscheduler.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include "gioscheduler.h"
26 #include "gcancellable.h"
27 #include "gtask.h"
28
29 /**
30  * SECTION:gioscheduler
31  * @short_description: I/O Scheduler
32  * @include: gio/gio.h
33  * 
34  * <note><para>
35  *   As of GLib 2.36, the <literal>g_io_scheduler</literal> methods
36  *   are deprecated in favor of #GThreadPool and #GTask.
37  * </para></note>
38  *
39  * Schedules asynchronous I/O operations. #GIOScheduler integrates 
40  * into the main event loop (#GMainLoop) and uses threads.
41  **/
42
43 struct _GIOSchedulerJob {
44   GList *active_link;
45   GTask *task;
46
47   GIOSchedulerJobFunc job_func;
48   gpointer data;
49   GDestroyNotify destroy_notify;
50
51   GCancellable *cancellable;
52   gulong cancellable_id;
53   GMainContext *context;
54 };
55
56 G_LOCK_DEFINE_STATIC(active_jobs);
57 static GList *active_jobs = NULL;
58
59 static void
60 g_io_job_free (GIOSchedulerJob *job)
61 {
62   if (job->destroy_notify)
63     job->destroy_notify (job->data);
64
65   G_LOCK (active_jobs);
66   active_jobs = g_list_delete_link (active_jobs, job->active_link);
67   G_UNLOCK (active_jobs);
68
69   if (job->cancellable)
70     g_object_unref (job->cancellable);
71   g_main_context_unref (job->context);
72   g_slice_free (GIOSchedulerJob, job);
73 }
74
75 static void
76 io_job_thread (GTask         *task,
77                gpointer       source_object,
78                gpointer       task_data,
79                GCancellable  *cancellable)
80 {
81   GIOSchedulerJob *job = task_data;
82   gboolean result;
83
84   if (job->cancellable)
85     g_cancellable_push_current (job->cancellable);
86
87   do 
88     {
89       result = job->job_func (job, job->cancellable, job->data);
90     }
91   while (result);
92
93   if (job->cancellable)
94     g_cancellable_pop_current (job->cancellable);
95 }
96
97 /**
98  * g_io_scheduler_push_job:
99  * @job_func: a #GIOSchedulerJobFunc.
100  * @user_data: data to pass to @job_func
101  * @notify: (allow-none): a #GDestroyNotify for @user_data, or %NULL
102  * @io_priority: the <link linkend="io-priority">I/O priority</link>
103  * of the request.
104  * @cancellable: optional #GCancellable object, %NULL to ignore.
105  *
106  * Schedules the I/O job to run in another thread.
107  *
108  * @notify will be called on @user_data after @job_func has returned,
109  * regardless whether the job was cancelled or has run to completion.
110  * 
111  * If @cancellable is not %NULL, it can be used to cancel the I/O job
112  * by calling g_cancellable_cancel() or by calling 
113  * g_io_scheduler_cancel_all_jobs().
114  *
115  * Deprecated: use #GThreadPool or g_task_run_in_thread()
116  **/
117 void
118 g_io_scheduler_push_job (GIOSchedulerJobFunc  job_func,
119                          gpointer             user_data,
120                          GDestroyNotify       notify,
121                          gint                 io_priority,
122                          GCancellable        *cancellable)
123 {
124   GIOSchedulerJob *job;
125   GTask *task;
126
127   g_return_if_fail (job_func != NULL);
128
129   job = g_slice_new0 (GIOSchedulerJob);
130   job->job_func = job_func;
131   job->data = user_data;
132   job->destroy_notify = notify;
133
134   if (cancellable)
135     job->cancellable = g_object_ref (cancellable);
136
137   job->context = g_main_context_ref_thread_default ();
138
139   G_LOCK (active_jobs);
140   active_jobs = g_list_prepend (active_jobs, job);
141   job->active_link = active_jobs;
142   G_UNLOCK (active_jobs);
143
144   task = g_task_new (NULL, cancellable, NULL, NULL);
145   g_task_set_task_data (task, job, (GDestroyNotify)g_io_job_free);
146   g_task_set_priority (task, io_priority);
147   g_task_run_in_thread (task, io_job_thread);
148   g_object_unref (task);
149 }
150
151 /**
152  * g_io_scheduler_cancel_all_jobs:
153  * 
154  * Cancels all cancellable I/O jobs. 
155  *
156  * A job is cancellable if a #GCancellable was passed into
157  * g_io_scheduler_push_job().
158  *
159  * Deprecated: You should never call this function, since you don't
160  * know how other libraries in your program might be making use of
161  * gioscheduler.
162  **/
163 void
164 g_io_scheduler_cancel_all_jobs (void)
165 {
166   GList *cancellable_list, *l;
167   
168   G_LOCK (active_jobs);
169   cancellable_list = NULL;
170   for (l = active_jobs; l != NULL; l = l->next)
171     {
172       GIOSchedulerJob *job = l->data;
173       if (job->cancellable)
174         cancellable_list = g_list_prepend (cancellable_list,
175                                            g_object_ref (job->cancellable));
176     }
177   G_UNLOCK (active_jobs);
178
179   for (l = cancellable_list; l != NULL; l = l->next)
180     {
181       GCancellable *c = l->data;
182       g_cancellable_cancel (c);
183       g_object_unref (c);
184     }
185   g_list_free (cancellable_list);
186 }
187
188 typedef struct {
189   GSourceFunc func;
190   gboolean ret_val;
191   gpointer data;
192   GDestroyNotify notify;
193
194   GMutex ack_lock;
195   GCond ack_condition;
196   gboolean ack;
197 } MainLoopProxy;
198
199 static gboolean
200 mainloop_proxy_func (gpointer data)
201 {
202   MainLoopProxy *proxy = data;
203
204   proxy->ret_val = proxy->func (proxy->data);
205
206   if (proxy->notify)
207     proxy->notify (proxy->data);
208
209   g_mutex_lock (&proxy->ack_lock);
210   proxy->ack = TRUE;
211   g_cond_signal (&proxy->ack_condition);
212   g_mutex_unlock (&proxy->ack_lock);
213
214   return FALSE;
215 }
216
217 static void
218 mainloop_proxy_free (MainLoopProxy *proxy)
219 {
220   g_mutex_clear (&proxy->ack_lock);
221   g_cond_clear (&proxy->ack_condition);
222   g_free (proxy);
223 }
224
225 /**
226  * g_io_scheduler_job_send_to_mainloop:
227  * @job: a #GIOSchedulerJob
228  * @func: a #GSourceFunc callback that will be called in the original thread
229  * @user_data: data to pass to @func
230  * @notify: (allow-none): a #GDestroyNotify for @user_data, or %NULL
231  * 
232  * Used from an I/O job to send a callback to be run in the thread
233  * that the job was started from, waiting for the result (and thus
234  * blocking the I/O job).
235  *
236  * Returns: The return value of @func
237  *
238  * Deprecated: Use g_main_context_invoke().
239  **/
240 gboolean
241 g_io_scheduler_job_send_to_mainloop (GIOSchedulerJob *job,
242                                      GSourceFunc      func,
243                                      gpointer         user_data,
244                                      GDestroyNotify   notify)
245 {
246   GSource *source;
247   MainLoopProxy *proxy;
248   gboolean ret_val;
249
250   g_return_val_if_fail (job != NULL, FALSE);
251   g_return_val_if_fail (func != NULL, FALSE);
252
253   proxy = g_new0 (MainLoopProxy, 1);
254   proxy->func = func;
255   proxy->data = user_data;
256   proxy->notify = notify;
257   g_mutex_init (&proxy->ack_lock);
258   g_cond_init (&proxy->ack_condition);
259   g_mutex_lock (&proxy->ack_lock);
260
261   source = g_idle_source_new ();
262   g_source_set_priority (source, G_PRIORITY_DEFAULT);
263   g_source_set_callback (source, mainloop_proxy_func, proxy,
264                          NULL);
265
266   g_source_attach (source, job->context);
267   g_source_unref (source);
268
269   while (!proxy->ack)
270     g_cond_wait (&proxy->ack_condition, &proxy->ack_lock);
271   g_mutex_unlock (&proxy->ack_lock);
272
273   ret_val = proxy->ret_val;
274   mainloop_proxy_free (proxy);
275   
276   return ret_val;
277 }
278
279 /**
280  * g_io_scheduler_job_send_to_mainloop_async:
281  * @job: a #GIOSchedulerJob
282  * @func: a #GSourceFunc callback that will be called in the original thread
283  * @user_data: data to pass to @func
284  * @notify: (allow-none): a #GDestroyNotify for @user_data, or %NULL
285  * 
286  * Used from an I/O job to send a callback to be run asynchronously in
287  * the thread that the job was started from. The callback will be run
288  * when the main loop is available, but at that time the I/O job might
289  * have finished. The return value from the callback is ignored.
290  *
291  * Note that if you are passing the @user_data from g_io_scheduler_push_job()
292  * on to this function you have to ensure that it is not freed before
293  * @func is called, either by passing %NULL as @notify to 
294  * g_io_scheduler_push_job() or by using refcounting for @user_data.
295  *
296  * Deprecated: Use g_main_context_invoke().
297  **/
298 void
299 g_io_scheduler_job_send_to_mainloop_async (GIOSchedulerJob *job,
300                                            GSourceFunc      func,
301                                            gpointer         user_data,
302                                            GDestroyNotify   notify)
303 {
304   GSource *source;
305   MainLoopProxy *proxy;
306
307   g_return_if_fail (job != NULL);
308   g_return_if_fail (func != NULL);
309
310   proxy = g_new0 (MainLoopProxy, 1);
311   proxy->func = func;
312   proxy->data = user_data;
313   proxy->notify = notify;
314   g_mutex_init (&proxy->ack_lock);
315   g_cond_init (&proxy->ack_condition);
316
317   source = g_idle_source_new ();
318   g_source_set_priority (source, G_PRIORITY_DEFAULT);
319   g_source_set_callback (source, mainloop_proxy_func, proxy,
320                          (GDestroyNotify)mainloop_proxy_free);
321
322   g_source_attach (source, job->context);
323   g_source_unref (source);
324 }