Blame gio/gioscheduler.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 * 
Packit ae235b
 * Copyright (C) 2006-2007 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gioscheduler.h"
Packit ae235b
#include "gcancellable.h"
Packit ae235b
#include "gtask.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gioscheduler
Packit ae235b
 * @short_description: I/O Scheduler
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * 
Packit ae235b
 * As of GLib 2.36, #GIOScheduler is deprecated in favor of
Packit ae235b
 * #GThreadPool and #GTask.
Packit ae235b
 *
Packit ae235b
 * Schedules asynchronous I/O operations. #GIOScheduler integrates 
Packit ae235b
 * into the main event loop (#GMainLoop) and uses threads.
Packit ae235b
 */
Packit ae235b
Packit ae235b
struct _GIOSchedulerJob {
Packit ae235b
  GList *active_link;
Packit ae235b
  GTask *task;
Packit ae235b
Packit ae235b
  GIOSchedulerJobFunc job_func;
Packit ae235b
  gpointer data;
Packit ae235b
  GDestroyNotify destroy_notify;
Packit ae235b
Packit ae235b
  GCancellable *cancellable;
Packit ae235b
  gulong cancellable_id;
Packit ae235b
  GMainContext *context;
Packit ae235b
};
Packit ae235b
Packit ae235b
G_LOCK_DEFINE_STATIC(active_jobs);
Packit ae235b
static GList *active_jobs = NULL;
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_io_job_free (GIOSchedulerJob *job)
Packit ae235b
{
Packit ae235b
  if (job->destroy_notify)
Packit ae235b
    job->destroy_notify (job->data);
Packit ae235b
Packit ae235b
  G_LOCK (active_jobs);
Packit ae235b
  active_jobs = g_list_delete_link (active_jobs, job->active_link);
Packit ae235b
  G_UNLOCK (active_jobs);
Packit ae235b
Packit ae235b
  if (job->cancellable)
Packit ae235b
    g_object_unref (job->cancellable);
Packit ae235b
  g_main_context_unref (job->context);
Packit ae235b
  g_slice_free (GIOSchedulerJob, job);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
io_job_thread (GTask         *task,
Packit ae235b
               gpointer       source_object,
Packit ae235b
               gpointer       task_data,
Packit ae235b
               GCancellable  *cancellable)
Packit ae235b
{
Packit ae235b
  GIOSchedulerJob *job = task_data;
Packit ae235b
  gboolean result;
Packit ae235b
Packit ae235b
  if (job->cancellable)
Packit ae235b
    g_cancellable_push_current (job->cancellable);
Packit ae235b
Packit ae235b
  do 
Packit ae235b
    {
Packit ae235b
      result = job->job_func (job, job->cancellable, job->data);
Packit ae235b
    }
Packit ae235b
  while (result);
Packit ae235b
Packit ae235b
  if (job->cancellable)
Packit ae235b
    g_cancellable_pop_current (job->cancellable);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_scheduler_push_job:
Packit ae235b
 * @job_func: a #GIOSchedulerJobFunc.
Packit ae235b
 * @user_data: data to pass to @job_func
Packit ae235b
 * @notify: (nullable): a #GDestroyNotify for @user_data, or %NULL
Packit ae235b
 * @io_priority: the [I/O priority][io-priority]
Packit ae235b
 * of the request.
Packit ae235b
 * @cancellable: optional #GCancellable object, %NULL to ignore.
Packit ae235b
 *
Packit ae235b
 * Schedules the I/O job to run in another thread.
Packit ae235b
 *
Packit ae235b
 * @notify will be called on @user_data after @job_func has returned,
Packit ae235b
 * regardless whether the job was cancelled or has run to completion.
Packit ae235b
 * 
Packit ae235b
 * If @cancellable is not %NULL, it can be used to cancel the I/O job
Packit ae235b
 * by calling g_cancellable_cancel() or by calling 
Packit ae235b
 * g_io_scheduler_cancel_all_jobs().
Packit ae235b
 *
Packit ae235b
 * Deprecated: use #GThreadPool or g_task_run_in_thread()
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_io_scheduler_push_job (GIOSchedulerJobFunc  job_func,
Packit ae235b
			 gpointer             user_data,
Packit ae235b
			 GDestroyNotify       notify,
Packit ae235b
			 gint                 io_priority,
Packit ae235b
			 GCancellable        *cancellable)
Packit ae235b
{
Packit ae235b
  GIOSchedulerJob *job;
Packit ae235b
  GTask *task;
Packit ae235b
Packit ae235b
  g_return_if_fail (job_func != NULL);
Packit ae235b
Packit ae235b
  job = g_slice_new0 (GIOSchedulerJob);
Packit ae235b
  job->job_func = job_func;
Packit ae235b
  job->data = user_data;
Packit ae235b
  job->destroy_notify = notify;
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    job->cancellable = g_object_ref (cancellable);
Packit ae235b
Packit ae235b
  job->context = g_main_context_ref_thread_default ();
Packit ae235b
Packit ae235b
  G_LOCK (active_jobs);
Packit ae235b
  active_jobs = g_list_prepend (active_jobs, job);
Packit ae235b
  job->active_link = active_jobs;
Packit ae235b
  G_UNLOCK (active_jobs);
Packit ae235b
Packit ae235b
  task = g_task_new (NULL, cancellable, NULL, NULL);
Packit ae235b
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
Packit ae235b
  g_task_set_source_tag (task, g_io_scheduler_push_job);
Packit ae235b
G_GNUC_END_IGNORE_DEPRECATIONS
Packit ae235b
  g_task_set_task_data (task, job, (GDestroyNotify)g_io_job_free);
Packit ae235b
  g_task_set_priority (task, io_priority);
Packit ae235b
  g_task_run_in_thread (task, io_job_thread);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_scheduler_cancel_all_jobs:
Packit ae235b
 * 
Packit ae235b
 * Cancels all cancellable I/O jobs. 
Packit ae235b
 *
Packit ae235b
 * A job is cancellable if a #GCancellable was passed into
Packit ae235b
 * g_io_scheduler_push_job().
Packit ae235b
 *
Packit ae235b
 * Deprecated: You should never call this function, since you don't
Packit ae235b
 * know how other libraries in your program might be making use of
Packit ae235b
 * gioscheduler.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_io_scheduler_cancel_all_jobs (void)
Packit ae235b
{
Packit ae235b
  GList *cancellable_list, *l;
Packit ae235b
  
Packit ae235b
  G_LOCK (active_jobs);
Packit ae235b
  cancellable_list = NULL;
Packit ae235b
  for (l = active_jobs; l != NULL; l = l->next)
Packit ae235b
    {
Packit ae235b
      GIOSchedulerJob *job = l->data;
Packit ae235b
      if (job->cancellable)
Packit ae235b
	cancellable_list = g_list_prepend (cancellable_list,
Packit ae235b
					   g_object_ref (job->cancellable));
Packit ae235b
    }
Packit ae235b
  G_UNLOCK (active_jobs);
Packit ae235b
Packit ae235b
  for (l = cancellable_list; l != NULL; l = l->next)
Packit ae235b
    {
Packit ae235b
      GCancellable *c = l->data;
Packit ae235b
      g_cancellable_cancel (c);
Packit ae235b
      g_object_unref (c);
Packit ae235b
    }
Packit ae235b
  g_list_free (cancellable_list);
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  GSourceFunc func;
Packit ae235b
  gboolean ret_val;
Packit ae235b
  gpointer data;
Packit ae235b
  GDestroyNotify notify;
Packit ae235b
Packit ae235b
  GMutex ack_lock;
Packit ae235b
  GCond ack_condition;
Packit ae235b
  gboolean ack;
Packit ae235b
} MainLoopProxy;
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
mainloop_proxy_func (gpointer data)
Packit ae235b
{
Packit ae235b
  MainLoopProxy *proxy = data;
Packit ae235b
Packit ae235b
  proxy->ret_val = proxy->func (proxy->data);
Packit ae235b
Packit ae235b
  if (proxy->notify)
Packit ae235b
    proxy->notify (proxy->data);
Packit ae235b
Packit ae235b
  g_mutex_lock (&proxy->ack_lock);
Packit ae235b
  proxy->ack = TRUE;
Packit ae235b
  g_cond_signal (&proxy->ack_condition);
Packit ae235b
  g_mutex_unlock (&proxy->ack_lock);
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
mainloop_proxy_free (MainLoopProxy *proxy)
Packit ae235b
{
Packit ae235b
  g_mutex_clear (&proxy->ack_lock);
Packit ae235b
  g_cond_clear (&proxy->ack_condition);
Packit ae235b
  g_free (proxy);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_scheduler_job_send_to_mainloop:
Packit ae235b
 * @job: a #GIOSchedulerJob
Packit ae235b
 * @func: a #GSourceFunc callback that will be called in the original thread
Packit ae235b
 * @user_data: data to pass to @func
Packit ae235b
 * @notify: (nullable): a #GDestroyNotify for @user_data, or %NULL
Packit ae235b
 * 
Packit ae235b
 * Used from an I/O job to send a callback to be run in the thread
Packit ae235b
 * that the job was started from, waiting for the result (and thus
Packit ae235b
 * blocking the I/O job).
Packit ae235b
 *
Packit ae235b
 * Returns: The return value of @func
Packit ae235b
 *
Packit ae235b
 * Deprecated: Use g_main_context_invoke().
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_io_scheduler_job_send_to_mainloop (GIOSchedulerJob *job,
Packit ae235b
				     GSourceFunc      func,
Packit ae235b
				     gpointer         user_data,
Packit ae235b
				     GDestroyNotify   notify)
Packit ae235b
{
Packit ae235b
  GSource *source;
Packit ae235b
  MainLoopProxy *proxy;
Packit ae235b
  gboolean ret_val;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (job != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (func != NULL, FALSE);
Packit ae235b
Packit ae235b
  proxy = g_new0 (MainLoopProxy, 1);
Packit ae235b
  proxy->func = func;
Packit ae235b
  proxy->data = user_data;
Packit ae235b
  proxy->notify = notify;
Packit ae235b
  g_mutex_init (&proxy->ack_lock);
Packit ae235b
  g_cond_init (&proxy->ack_condition);
Packit ae235b
  g_mutex_lock (&proxy->ack_lock);
Packit ae235b
Packit ae235b
  source = g_idle_source_new ();
Packit ae235b
  g_source_set_priority (source, G_PRIORITY_DEFAULT);
Packit ae235b
  g_source_set_callback (source, mainloop_proxy_func, proxy,
Packit ae235b
			 NULL);
Packit ae235b
  g_source_set_name (source, "[gio] mainloop_proxy_func");
Packit ae235b
Packit ae235b
  g_source_attach (source, job->context);
Packit ae235b
  g_source_unref (source);
Packit ae235b
Packit ae235b
  while (!proxy->ack)
Packit ae235b
    g_cond_wait (&proxy->ack_condition, &proxy->ack_lock);
Packit ae235b
  g_mutex_unlock (&proxy->ack_lock);
Packit ae235b
Packit ae235b
  ret_val = proxy->ret_val;
Packit ae235b
  mainloop_proxy_free (proxy);
Packit ae235b
  
Packit ae235b
  return ret_val;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_scheduler_job_send_to_mainloop_async:
Packit ae235b
 * @job: a #GIOSchedulerJob
Packit ae235b
 * @func: a #GSourceFunc callback that will be called in the original thread
Packit ae235b
 * @user_data: data to pass to @func
Packit ae235b
 * @notify: (nullable): a #GDestroyNotify for @user_data, or %NULL
Packit ae235b
 * 
Packit ae235b
 * Used from an I/O job to send a callback to be run asynchronously in
Packit ae235b
 * the thread that the job was started from. The callback will be run
Packit ae235b
 * when the main loop is available, but at that time the I/O job might
Packit ae235b
 * have finished. The return value from the callback is ignored.
Packit ae235b
 *
Packit ae235b
 * Note that if you are passing the @user_data from g_io_scheduler_push_job()
Packit ae235b
 * on to this function you have to ensure that it is not freed before
Packit ae235b
 * @func is called, either by passing %NULL as @notify to 
Packit ae235b
 * g_io_scheduler_push_job() or by using refcounting for @user_data.
Packit ae235b
 *
Packit ae235b
 * Deprecated: Use g_main_context_invoke().
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_io_scheduler_job_send_to_mainloop_async (GIOSchedulerJob *job,
Packit ae235b
					   GSourceFunc      func,
Packit ae235b
					   gpointer         user_data,
Packit ae235b
					   GDestroyNotify   notify)
Packit ae235b
{
Packit ae235b
  GSource *source;
Packit ae235b
  MainLoopProxy *proxy;
Packit ae235b
Packit ae235b
  g_return_if_fail (job != NULL);
Packit ae235b
  g_return_if_fail (func != NULL);
Packit ae235b
Packit ae235b
  proxy = g_new0 (MainLoopProxy, 1);
Packit ae235b
  proxy->func = func;
Packit ae235b
  proxy->data = user_data;
Packit ae235b
  proxy->notify = notify;
Packit ae235b
  g_mutex_init (&proxy->ack_lock);
Packit ae235b
  g_cond_init (&proxy->ack_condition);
Packit ae235b
Packit ae235b
  source = g_idle_source_new ();
Packit ae235b
  g_source_set_priority (source, G_PRIORITY_DEFAULT);
Packit ae235b
  g_source_set_callback (source, mainloop_proxy_func, proxy,
Packit ae235b
			 (GDestroyNotify)mainloop_proxy_free);
Packit ae235b
  g_source_set_name (source, "[gio] mainloop_proxy_func");
Packit ae235b
Packit ae235b
  g_source_attach (source, job->context);
Packit ae235b
  g_source_unref (source);
Packit ae235b
}