gioscheduler.c 8.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

23
#include "config.h"
24 25

#include "gioscheduler.h"
26
#include "gcancellable.h"
27
#include "gtask.h"
28

29 30 31
/**
 * SECTION:gioscheduler
 * @short_description: I/O Scheduler
Matthias Clasen's avatar
Matthias Clasen committed
32
 * @include: gio/gio.h
33
 * 
34 35 36 37 38
 * <note><para>
 *   As of GLib 2.36, the <literal>g_io_scheduler</literal> methods
 *   are deprecated in favor of #GThreadPool and #GTask.
 * </para></note>
 *
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 46
  GTask *task;

47
  GIOSchedulerJobFunc job_func;
48 49
  gpointer data;
  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 63 64
  if (job->destroy_notify)
    job->destroy_notify (job->data);

65 66 67
  G_LOCK (active_jobs);
  active_jobs = g_list_delete_link (active_jobs, job->active_link);
  G_UNLOCK (active_jobs);
68 69 70 71 72

  if (job->cancellable)
    g_object_unref (job->cancellable);
  g_main_context_unref (job->context);
  g_slice_free (GIOSchedulerJob, job);
73 74
}

75
static void
76 77 78 79
io_job_thread (GTask         *task,
               gpointer       source_object,
               gpointer       task_data,
               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 88 89 90 91
  do 
    {
      result = job->job_func (job, job->cancellable, job->data);
    }
  while (result);
92

93 94
  if (job->cancellable)
    g_cancellable_pop_current (job->cancellable);
95 96 97
}

/**
98 99
 * g_io_scheduler_push_job:
 * @job_func: a #GIOSchedulerJobFunc.
Matthias Clasen's avatar
Matthias Clasen committed
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 105
 * @cancellable: optional #GCancellable object, %NULL to ignore.
 *
106
 * Schedules the I/O job to run in another thread.
Matthias Clasen's avatar
Matthias Clasen committed
107 108 109
 *
 * @notify will be called on @user_data after @job_func has returned,
 * regardless whether the job was cancelled or has run to completion.
110
 * 
Matthias Clasen's avatar
Matthias Clasen committed
111 112 113
 * If @cancellable is not %NULL, it can be used to cancel the I/O job
 * by calling g_cancellable_cancel() or by calling 
 * g_io_scheduler_cancel_all_jobs().
114 115
 *
 * Deprecated: use #GThreadPool or g_task_run_in_thread()
116 117
 **/
void
Matthias Clasen's avatar
Matthias Clasen committed
118 119 120 121 122
g_io_scheduler_push_job (GIOSchedulerJobFunc  job_func,
			 gpointer             user_data,
			 GDestroyNotify       notify,
			 gint                 io_priority,
			 GCancellable        *cancellable)
123
{
124
  GIOSchedulerJob *job;
125
  GTask *task;
126 127 128

  g_return_if_fail (job_func != NULL);

129
  job = g_slice_new0 (GIOSchedulerJob);
130 131 132
  job->job_func = job_func;
  job->data = user_data;
  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 142 143
  job->active_link = active_jobs;
  G_UNLOCK (active_jobs);

144 145 146 147 148
  task = g_task_new (NULL, cancellable, NULL, NULL);
  g_task_set_task_data (task, job, (GDestroyNotify)g_io_job_free);
  g_task_set_priority (task, io_priority);
  g_task_run_in_thread (task, io_job_thread);
  g_object_unref (task);
149 150 151
}

/**
152
 * g_io_scheduler_cancel_all_jobs:
153
 * 
Matthias Clasen's avatar
Matthias Clasen committed
154 155 156 157
 * Cancels all cancellable I/O jobs. 
 *
 * A job is cancellable if a #GCancellable was passed into
 * g_io_scheduler_push_job().
158 159 160 161
 *
 * Deprecated: You should never call this function, since you don't
 * know how other libraries in your program might be making use of
 * gioscheduler.
162 163
 **/
void
164
g_io_scheduler_cancel_all_jobs (void)
165
{
166
  GList *cancellable_list, *l;
167 168 169 170 171
  
  G_LOCK (active_jobs);
  cancellable_list = NULL;
  for (l = active_jobs; l != NULL; l = l->next)
    {
172
      GIOSchedulerJob *job = l->data;
173
      if (job->cancellable)
174 175
	cancellable_list = g_list_prepend (cancellable_list,
					   g_object_ref (job->cancellable));
176 177 178 179 180 181 182 183 184
    }
  G_UNLOCK (active_jobs);

  for (l = cancellable_list; l != NULL; l = l->next)
    {
      GCancellable *c = l->data;
      g_cancellable_cancel (c);
      g_object_unref (c);
    }
185
  g_list_free (cancellable_list);
186 187 188
}

typedef struct {
189 190 191
  GSourceFunc func;
  gboolean ret_val;
  gpointer data;
192 193
  GDestroyNotify notify;

194 195
  GMutex ack_lock;
  GCond ack_condition;
196
  gboolean ack;
197 198 199 200 201 202 203
} MainLoopProxy;

static gboolean
mainloop_proxy_func (gpointer data)
{
  MainLoopProxy *proxy = data;

204
  proxy->ret_val = proxy->func (proxy->data);
205

206 207
  if (proxy->notify)
    proxy->notify (proxy->data);
208 209

  g_mutex_lock (&proxy->ack_lock);
210
  proxy->ack = TRUE;
211 212 213
  g_cond_signal (&proxy->ack_condition);
  g_mutex_unlock (&proxy->ack_lock);

214 215 216 217 218 219
  return FALSE;
}

static void
mainloop_proxy_free (MainLoopProxy *proxy)
{
220 221
  g_mutex_clear (&proxy->ack_lock);
  g_cond_clear (&proxy->ack_condition);
222 223 224 225
  g_free (proxy);
}

/**
226
 * g_io_scheduler_job_send_to_mainloop:
Matthias Clasen's avatar
Matthias Clasen committed
227
 * @job: a #GIOSchedulerJob
228
 * @func: a #GSourceFunc callback that will be called in the original thread
Matthias Clasen's avatar
Matthias Clasen committed
229
 * @user_data: data to pass to @func
230
 * @notify: (allow-none): a #GDestroyNotify for @user_data, or %NULL
231
 * 
232 233
 * Used from an I/O job to send a callback to be run in the thread
 * that the job was started from, waiting for the result (and thus
Matthias Clasen's avatar
Matthias Clasen committed
234
 * blocking the I/O job).
235 236
 *
 * Returns: The return value of @func
237 238
 *
 * Deprecated: Use g_main_context_invoke().
239 240 241
 **/
gboolean
g_io_scheduler_job_send_to_mainloop (GIOSchedulerJob *job,
Matthias Clasen's avatar
Matthias Clasen committed
242 243 244
				     GSourceFunc      func,
				     gpointer         user_data,
				     GDestroyNotify   notify)
245 246 247 248 249 250 251 252 253 254 255 256
{
  GSource *source;
  MainLoopProxy *proxy;
  gboolean ret_val;

  g_return_val_if_fail (job != NULL, FALSE);
  g_return_val_if_fail (func != NULL, FALSE);

  proxy = g_new0 (MainLoopProxy, 1);
  proxy->func = func;
  proxy->data = user_data;
  proxy->notify = notify;
257 258 259 260
  g_mutex_init (&proxy->ack_lock);
  g_cond_init (&proxy->ack_condition);
  g_mutex_lock (&proxy->ack_lock);

261 262 263 264 265
  source = g_idle_source_new ();
  g_source_set_priority (source, G_PRIORITY_DEFAULT);
  g_source_set_callback (source, mainloop_proxy_func, proxy,
			 NULL);

266
  g_source_attach (source, job->context);
267 268
  g_source_unref (source);

269 270
  while (!proxy->ack)
    g_cond_wait (&proxy->ack_condition, &proxy->ack_lock);
271
  g_mutex_unlock (&proxy->ack_lock);
272 273 274 275 276 277 278 279

  ret_val = proxy->ret_val;
  mainloop_proxy_free (proxy);
  
  return ret_val;
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
280
 * g_io_scheduler_job_send_to_mainloop_async:
Matthias Clasen's avatar
Matthias Clasen committed
281
 * @job: a #GIOSchedulerJob
282
 * @func: a #GSourceFunc callback that will be called in the original thread
Matthias Clasen's avatar
Matthias Clasen committed
283
 * @user_data: data to pass to @func
284
 * @notify: (allow-none): a #GDestroyNotify for @user_data, or %NULL
285
 * 
286 287 288 289
 * Used from an I/O job to send a callback to be run asynchronously in
 * the thread that the job was started from. The callback will be run
 * when the main loop is available, but at that time the I/O job might
 * have finished. The return value from the callback is ignored.
Matthias Clasen's avatar
Matthias Clasen committed
290 291 292 293 294
 *
 * Note that if you are passing the @user_data from g_io_scheduler_push_job()
 * on to this function you have to ensure that it is not freed before
 * @func is called, either by passing %NULL as @notify to 
 * 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,
Matthias Clasen's avatar
Matthias Clasen committed
300 301 302
					   GSourceFunc      func,
					   gpointer         user_data,
					   GDestroyNotify   notify)
303 304 305 306 307 308 309 310 311 312 313
{
  GSource *source;
  MainLoopProxy *proxy;

  g_return_if_fail (job != NULL);
  g_return_if_fail (func != NULL);

  proxy = g_new0 (MainLoopProxy, 1);
  proxy->func = func;
  proxy->data = user_data;
  proxy->notify = notify;
314 315 316
  g_mutex_init (&proxy->ack_lock);
  g_cond_init (&proxy->ack_condition);

317 318
  source = g_idle_source_new ();
  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 324
  g_source_unref (source);
}