2007-11-26 17:13:05 +01:00
|
|
|
/* 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
|
2014-01-23 12:58:29 +01:00
|
|
|
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
|
|
|
* Author: Alexander Larsson <alexl@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2008-06-22 17:10:51 +02:00
|
|
|
#include "config.h"
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
#include "gioscheduler.h"
|
2008-07-01 08:32:35 +02:00
|
|
|
#include "gcancellable.h"
|
2011-11-21 15:19:56 +01:00
|
|
|
#include "gtask.h"
|
2007-11-28 13:39:07 +01:00
|
|
|
|
2007-11-27 15:00:13 +01:00
|
|
|
/**
|
|
|
|
* SECTION:gioscheduler
|
|
|
|
* @short_description: I/O Scheduler
|
2008-02-21 19:20:17 +01:00
|
|
|
* @include: gio/gio.h
|
2007-11-27 15:00:13 +01:00
|
|
|
*
|
2014-01-31 20:56:10 +01:00
|
|
|
* As of GLib 2.36, #GIOScheduler is deprecated in favor of
|
|
|
|
* #GThreadPool and #GTask.
|
2011-11-21 15:19:56 +01:00
|
|
|
*
|
2007-12-18 03:52:11 +01:00
|
|
|
* Schedules asynchronous I/O operations. #GIOScheduler integrates
|
2011-08-31 20:33:33 +02:00
|
|
|
* into the main event loop (#GMainLoop) and uses threads.
|
2014-01-31 20:56:10 +01:00
|
|
|
*/
|
2007-11-27 15:00:13 +01:00
|
|
|
|
2007-12-14 10:42:59 +01:00
|
|
|
struct _GIOSchedulerJob {
|
2012-06-21 17:10:00 +02:00
|
|
|
GList *active_link;
|
2011-11-21 15:19:56 +01:00
|
|
|
GTask *task;
|
|
|
|
|
2007-12-14 10:42:59 +01:00
|
|
|
GIOSchedulerJobFunc job_func;
|
2007-11-26 17:13:05 +01:00
|
|
|
gpointer data;
|
|
|
|
GDestroyNotify destroy_notify;
|
2011-08-31 20:30:11 +02:00
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
GCancellable *cancellable;
|
2012-06-25 23:05:45 +02:00
|
|
|
gulong cancellable_id;
|
2009-06-17 02:22:58 +02:00
|
|
|
GMainContext *context;
|
2007-11-26 17:13:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
G_LOCK_DEFINE_STATIC(active_jobs);
|
2012-06-21 17:10:00 +02:00
|
|
|
static GList *active_jobs = NULL;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
static void
|
2007-12-14 10:42:59 +01:00
|
|
|
g_io_job_free (GIOSchedulerJob *job)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2008-01-25 15:40:54 +01:00
|
|
|
if (job->destroy_notify)
|
|
|
|
job->destroy_notify (job->data);
|
|
|
|
|
2012-06-25 23:05:45 +02:00
|
|
|
G_LOCK (active_jobs);
|
|
|
|
active_jobs = g_list_delete_link (active_jobs, job->active_link);
|
|
|
|
G_UNLOCK (active_jobs);
|
2011-11-21 15:19:56 +01:00
|
|
|
|
|
|
|
if (job->cancellable)
|
|
|
|
g_object_unref (job->cancellable);
|
|
|
|
g_main_context_unref (job->context);
|
|
|
|
g_slice_free (GIOSchedulerJob, job);
|
2008-01-25 15:40:54 +01:00
|
|
|
}
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
static void
|
2011-11-21 15:19:56 +01:00
|
|
|
io_job_thread (GTask *task,
|
|
|
|
gpointer source_object,
|
|
|
|
gpointer task_data,
|
|
|
|
GCancellable *cancellable)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2011-11-21 15:19:56 +01:00
|
|
|
GIOSchedulerJob *job = task_data;
|
2008-01-25 15:40:54 +01:00
|
|
|
gboolean result;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
if (job->cancellable)
|
2007-12-13 17:48:06 +01:00
|
|
|
g_cancellable_push_current (job->cancellable);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-01-25 15:40:54 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
result = job->job_func (job, job->cancellable, job->data);
|
|
|
|
}
|
|
|
|
while (result);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-01-25 15:40:54 +01:00
|
|
|
if (job->cancellable)
|
|
|
|
g_cancellable_pop_current (job->cancellable);
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-12-14 10:42:59 +01:00
|
|
|
* g_io_scheduler_push_job:
|
|
|
|
* @job_func: a #GIOSchedulerJobFunc.
|
2008-01-23 06:46:43 +01:00
|
|
|
* @user_data: data to pass to @job_func
|
2012-03-24 13:58:45 +01:00
|
|
|
* @notify: (allow-none): a #GDestroyNotify for @user_data, or %NULL
|
2014-02-08 18:26:56 +01:00
|
|
|
* @io_priority: the [I/O priority][io-priority]
|
2007-11-30 06:51:10 +01:00
|
|
|
* of the request.
|
2007-11-26 17:13:05 +01:00
|
|
|
* @cancellable: optional #GCancellable object, %NULL to ignore.
|
|
|
|
*
|
2011-10-05 17:45:51 +02:00
|
|
|
* Schedules the I/O job to run in another thread.
|
2008-01-23 06:46:43 +01:00
|
|
|
*
|
|
|
|
* @notify will be called on @user_data after @job_func has returned,
|
|
|
|
* regardless whether the job was cancelled or has run to completion.
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
2008-01-23 06:46:43 +01:00
|
|
|
* 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().
|
2011-11-21 15:19:56 +01:00
|
|
|
*
|
|
|
|
* Deprecated: use #GThreadPool or g_task_run_in_thread()
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
|
|
|
void
|
2008-01-23 06:46:43 +01:00
|
|
|
g_io_scheduler_push_job (GIOSchedulerJobFunc job_func,
|
|
|
|
gpointer user_data,
|
|
|
|
GDestroyNotify notify,
|
|
|
|
gint io_priority,
|
|
|
|
GCancellable *cancellable)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2007-12-14 10:42:59 +01:00
|
|
|
GIOSchedulerJob *job;
|
2011-11-21 15:19:56 +01:00
|
|
|
GTask *task;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
g_return_if_fail (job_func != NULL);
|
|
|
|
|
2011-11-21 15:19:56 +01:00
|
|
|
job = g_slice_new0 (GIOSchedulerJob);
|
2007-11-26 17:13:05 +01:00
|
|
|
job->job_func = job_func;
|
|
|
|
job->data = user_data;
|
|
|
|
job->destroy_notify = notify;
|
2011-11-21 15:19:56 +01:00
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
if (cancellable)
|
2011-11-21 15:19:56 +01:00
|
|
|
job->cancellable = g_object_ref (cancellable);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2011-10-05 16:46:57 +02:00
|
|
|
job->context = g_main_context_ref_thread_default ();
|
2009-06-17 02:22:58 +02:00
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
G_LOCK (active_jobs);
|
2012-06-21 17:10:00 +02:00
|
|
|
active_jobs = g_list_prepend (active_jobs, job);
|
2007-11-26 17:13:05 +01:00
|
|
|
job->active_link = active_jobs;
|
|
|
|
G_UNLOCK (active_jobs);
|
|
|
|
|
2011-11-21 15:19:56 +01:00
|
|
|
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);
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-12-14 10:42:59 +01:00
|
|
|
* g_io_scheduler_cancel_all_jobs:
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
2008-01-23 06:46:43 +01:00
|
|
|
* Cancels all cancellable I/O jobs.
|
|
|
|
*
|
|
|
|
* A job is cancellable if a #GCancellable was passed into
|
|
|
|
* g_io_scheduler_push_job().
|
2011-11-21 15:19:56 +01:00
|
|
|
*
|
|
|
|
* Deprecated: You should never call this function, since you don't
|
|
|
|
* know how other libraries in your program might be making use of
|
|
|
|
* gioscheduler.
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
|
|
|
void
|
2007-12-14 10:42:59 +01:00
|
|
|
g_io_scheduler_cancel_all_jobs (void)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2012-06-21 17:10:00 +02:00
|
|
|
GList *cancellable_list, *l;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
G_LOCK (active_jobs);
|
|
|
|
cancellable_list = NULL;
|
|
|
|
for (l = active_jobs; l != NULL; l = l->next)
|
|
|
|
{
|
2007-12-14 10:42:59 +01:00
|
|
|
GIOSchedulerJob *job = l->data;
|
2007-11-26 17:13:05 +01:00
|
|
|
if (job->cancellable)
|
2012-06-21 17:10:00 +02:00
|
|
|
cancellable_list = g_list_prepend (cancellable_list,
|
|
|
|
g_object_ref (job->cancellable));
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2012-06-21 17:10:00 +02:00
|
|
|
g_list_free (cancellable_list);
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
2007-12-14 10:42:59 +01:00
|
|
|
GSourceFunc func;
|
|
|
|
gboolean ret_val;
|
|
|
|
gpointer data;
|
2007-11-26 17:13:05 +01:00
|
|
|
GDestroyNotify notify;
|
|
|
|
|
2011-10-04 05:44:37 +02:00
|
|
|
GMutex ack_lock;
|
|
|
|
GCond ack_condition;
|
2011-10-04 17:07:15 +02:00
|
|
|
gboolean ack;
|
2007-11-26 17:13:05 +01:00
|
|
|
} MainLoopProxy;
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
mainloop_proxy_func (gpointer data)
|
|
|
|
{
|
|
|
|
MainLoopProxy *proxy = data;
|
|
|
|
|
2007-12-14 10:42:59 +01:00
|
|
|
proxy->ret_val = proxy->func (proxy->data);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2007-12-12 20:34:19 +01:00
|
|
|
if (proxy->notify)
|
|
|
|
proxy->notify (proxy->data);
|
2011-10-04 05:44:37 +02:00
|
|
|
|
|
|
|
g_mutex_lock (&proxy->ack_lock);
|
2011-10-04 17:07:15 +02:00
|
|
|
proxy->ack = TRUE;
|
2011-10-04 05:44:37 +02:00
|
|
|
g_cond_signal (&proxy->ack_condition);
|
|
|
|
g_mutex_unlock (&proxy->ack_lock);
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mainloop_proxy_free (MainLoopProxy *proxy)
|
|
|
|
{
|
2011-10-04 05:44:37 +02:00
|
|
|
g_mutex_clear (&proxy->ack_lock);
|
|
|
|
g_cond_clear (&proxy->ack_condition);
|
2007-11-26 17:13:05 +01:00
|
|
|
g_free (proxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-12-14 10:42:59 +01:00
|
|
|
* g_io_scheduler_job_send_to_mainloop:
|
2008-01-23 06:46:43 +01:00
|
|
|
* @job: a #GIOSchedulerJob
|
2009-06-17 02:22:58 +02:00
|
|
|
* @func: a #GSourceFunc callback that will be called in the original thread
|
2008-01-23 06:46:43 +01:00
|
|
|
* @user_data: data to pass to @func
|
2012-03-24 13:58:45 +01:00
|
|
|
* @notify: (allow-none): a #GDestroyNotify for @user_data, or %NULL
|
2007-12-14 10:42:59 +01:00
|
|
|
*
|
2009-06-17 02:22:58 +02:00
|
|
|
* 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
|
2007-12-17 08:04:51 +01:00
|
|
|
* blocking the I/O job).
|
2007-12-14 10:42:59 +01:00
|
|
|
*
|
|
|
|
* Returns: The return value of @func
|
2011-11-21 15:19:56 +01:00
|
|
|
*
|
|
|
|
* Deprecated: Use g_main_context_invoke().
|
2007-12-14 10:42:59 +01:00
|
|
|
**/
|
|
|
|
gboolean
|
|
|
|
g_io_scheduler_job_send_to_mainloop (GIOSchedulerJob *job,
|
2007-12-17 08:04:51 +01:00
|
|
|
GSourceFunc func,
|
|
|
|
gpointer user_data,
|
|
|
|
GDestroyNotify notify)
|
2007-12-14 10:42:59 +01:00
|
|
|
{
|
|
|
|
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;
|
2011-10-04 05:44:37 +02:00
|
|
|
g_mutex_init (&proxy->ack_lock);
|
|
|
|
g_cond_init (&proxy->ack_condition);
|
|
|
|
g_mutex_lock (&proxy->ack_lock);
|
|
|
|
|
2007-12-14 10:42:59 +01:00
|
|
|
source = g_idle_source_new ();
|
|
|
|
g_source_set_priority (source, G_PRIORITY_DEFAULT);
|
|
|
|
g_source_set_callback (source, mainloop_proxy_func, proxy,
|
|
|
|
NULL);
|
2014-03-22 13:15:45 +01:00
|
|
|
g_source_set_name (source, "[gio] mainloop_proxy_func");
|
2007-12-14 10:42:59 +01:00
|
|
|
|
2009-06-17 02:22:58 +02:00
|
|
|
g_source_attach (source, job->context);
|
2007-12-14 10:42:59 +01:00
|
|
|
g_source_unref (source);
|
|
|
|
|
2011-10-04 17:07:15 +02:00
|
|
|
while (!proxy->ack)
|
|
|
|
g_cond_wait (&proxy->ack_condition, &proxy->ack_lock);
|
2011-10-04 05:44:37 +02:00
|
|
|
g_mutex_unlock (&proxy->ack_lock);
|
2007-12-14 10:42:59 +01:00
|
|
|
|
|
|
|
ret_val = proxy->ret_val;
|
|
|
|
mainloop_proxy_free (proxy);
|
|
|
|
|
|
|
|
return ret_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-12-17 08:04:51 +01:00
|
|
|
* g_io_scheduler_job_send_to_mainloop_async:
|
2008-01-23 06:46:43 +01:00
|
|
|
* @job: a #GIOSchedulerJob
|
2009-06-17 02:22:58 +02:00
|
|
|
* @func: a #GSourceFunc callback that will be called in the original thread
|
2008-01-23 06:46:43 +01:00
|
|
|
* @user_data: data to pass to @func
|
2012-03-24 13:58:45 +01:00
|
|
|
* @notify: (allow-none): a #GDestroyNotify for @user_data, or %NULL
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
2009-06-17 02:22:58 +02:00
|
|
|
* 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.
|
2008-01-23 06:46:43 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2011-11-21 15:19:56 +01:00
|
|
|
*
|
|
|
|
* Deprecated: Use g_main_context_invoke().
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
|
|
|
void
|
2007-12-14 10:42:59 +01:00
|
|
|
g_io_scheduler_job_send_to_mainloop_async (GIOSchedulerJob *job,
|
2007-12-17 08:04:51 +01:00
|
|
|
GSourceFunc func,
|
|
|
|
gpointer user_data,
|
|
|
|
GDestroyNotify notify)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
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;
|
2011-10-04 05:44:37 +02:00
|
|
|
g_mutex_init (&proxy->ack_lock);
|
|
|
|
g_cond_init (&proxy->ack_condition);
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
source = g_idle_source_new ();
|
|
|
|
g_source_set_priority (source, G_PRIORITY_DEFAULT);
|
2007-12-12 20:34:19 +01:00
|
|
|
g_source_set_callback (source, mainloop_proxy_func, proxy,
|
2007-12-14 10:42:59 +01:00
|
|
|
(GDestroyNotify)mainloop_proxy_free);
|
2014-03-22 13:15:45 +01:00
|
|
|
g_source_set_name (source, "[gio] mainloop_proxy_func");
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2009-06-17 02:22:58 +02:00
|
|
|
g_source_attach (source, job->context);
|
2007-11-26 17:13:05 +01:00
|
|
|
g_source_unref (source);
|
|
|
|
}
|