1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
/* pps-job-scheduler.c
* this file is part of papers, a gnome document viewer
*
* Copyright (C) 2008 Carlos Garcia Campos <carlosgc@gnome.org>
*
* Papers is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Papers 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "pps-job-scheduler.h"
#ifdef G_LOG_DOMAIN
#undef G_LOG_DOMAIN
#endif
#define G_LOG_DOMAIN "PpsJobScheduler"
typedef struct _PpsSchedulerJob {
PpsJob *job;
PpsJobPriority priority;
} PpsSchedulerJob;
static gpointer
pps_jobs_hash_init (gpointer data)
{
return g_hash_table_new (NULL, NULL);
}
static GHashTable *
pps_jobs_hash (void)
{
static GOnce once_init = G_ONCE_INIT;
g_once (&once_init, pps_jobs_hash_init, NULL);
return once_init.retval;
}
static void
pps_scheduler_job_free (PpsSchedulerJob *job)
{
if (!job)
return;
g_object_unref (job->job);
g_free (job);
}
static void
pps_scheduler_job_destroy (PpsSchedulerJob *job)
{
g_debug ("destroying job: %s", PPS_GET_TYPE_NAME (job->job));
g_hash_table_remove (pps_jobs_hash (), job);
pps_scheduler_job_free (job);
}
static void
pps_job_thread (PpsSchedulerJob *scheduler_job, gpointer data)
{
PpsJob *job = scheduler_job->job;
g_debug ("running thread for job: %s", PPS_GET_TYPE_NAME (job));
if (!g_cancellable_is_cancelled (pps_job_get_cancellable (job)))
pps_job_run (job);
pps_scheduler_job_destroy (scheduler_job);
}
static gint
job_priority_compare (PpsSchedulerJob *a,
PpsSchedulerJob *b,
gpointer data)
{
return (gint) a->priority - (gint) b->priority;
}
static gpointer
pps_job_scheduler_init (gpointer data)
{
/* We limit the thread numbers to 8 since threads above 8 may result
* increased latency on some machine. For example, AMD 5950x.
*/
GThreadPool *pool = g_thread_pool_new_full ((GFunc) pps_job_thread, NULL,
(GDestroyNotify) pps_scheduler_job_destroy,
/* MIN (8, g_get_num_processors()),
* Temporary remove multi-threading. See
* https://gitlab.gnome.org/GNOME/papers/-/issues/107
* for more context
*/
1,
TRUE, NULL);
g_thread_pool_set_sort_function (pool, (GCompareDataFunc) job_priority_compare, NULL);
return pool;
}
static GThreadPool *
pps_thread_pool (void)
{
static GOnce once_init = G_ONCE_INIT;
g_once (&once_init, pps_job_scheduler_init, NULL);
return once_init.retval;
}
static void
pps_job_queue_push (PpsSchedulerJob *job)
{
g_debug ("pushing job: %s, priority: %d",
PPS_GET_TYPE_NAME (job->job), job->priority);
g_hash_table_insert (pps_jobs_hash (), job->job, job);
g_thread_pool_push (pps_thread_pool (), job, NULL);
}
void
pps_job_scheduler_push_job (PpsJob *job,
PpsJobPriority priority)
{
PpsSchedulerJob *s_job;
s_job = g_new0 (PpsSchedulerJob, 1);
s_job->job = g_object_ref (job);
s_job->priority = priority;
pps_job_queue_push (s_job);
}
void
pps_job_scheduler_update_job (PpsJob *job,
PpsJobPriority priority)
{
g_debug ("update priority for job: %s, priority %d",
PPS_GET_TYPE_NAME (job), priority);
if (priority == PPS_JOB_PRIORITY_URGENT)
g_thread_pool_move_to_front (pps_thread_pool (),
g_hash_table_lookup (pps_jobs_hash (), job));
}
/**
* pps_job_scheduler_wait:
*
* Synchronously waits until all jobs are done.
* Remember that main loop is not running already probably.
*/
void
pps_job_scheduler_wait (void)
{
g_debug ("Waiting for empty job list");
while (g_thread_pool_unprocessed (pps_thread_pool ()))
g_usleep (100);
g_debug ("Job list is empty");
}
|