ev-job-scheduler.c 8.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* ev-job-scheduler.c
 *  this file is part of evince, a gnome document viewer
 *
 * Copyright (C) 2008 Carlos Garcia Campos <carlosgc@gnome.org>
 *
 * Evince 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.
 *
 * Evince 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
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 20 21 22 23 24 25 26 27 28 29 30 31 32
 */

#include "ev-debug.h"
#include "ev-job-scheduler.h"

typedef struct _EvSchedulerJob {
	EvJob         *job;
	EvJobPriority  priority;
	GSList        *job_link;
} EvSchedulerJob;

G_LOCK_DEFINE_STATIC(job_list);
static GSList *job_list = NULL;

33 34
static volatile EvJob *running_job = NULL;

35 36 37 38 39 40 41 42 43 44
static gpointer ev_job_thread_proxy               (gpointer        data);
static void     ev_scheduler_thread_job_cancelled (EvSchedulerJob *job,
						   GCancellable   *cancellable);

/* EvJobQueue */
static GQueue queue_urgent = G_QUEUE_INIT;
static GQueue queue_high = G_QUEUE_INIT;
static GQueue queue_low = G_QUEUE_INIT;
static GQueue queue_none = G_QUEUE_INIT;

45 46 47 48 49
#if (!GLIB_CHECK_VERSION(2,31,0))
/* Remove this once we bump dependencies to glib >= 2.31.0 */
static GCond *p_job_queue_cond = NULL;
static GMutex *p_job_queue_mutex = NULL;
#else
50 51
static GCond job_queue_cond;
static GMutex job_queue_mutex;
52 53 54 55 56 57 58 59
/* Remove these defines once we bump dependencies to glib >= 2.31.0
   and replace occurences in this file of p_job_queue_{cond|mutex} with 
   &job_queue_{cond|mutex}.
 */
#define p_job_queue_cond (&job_queue_cond)
#define p_job_queue_mutex (&job_queue_mutex)
#endif

60 61 62 63 64 65 66 67 68 69 70 71 72
static GQueue *job_queue[EV_JOB_N_PRIORITIES] = {
	&queue_urgent,
	&queue_high,
	&queue_low,
	&queue_none
};

static void
ev_job_queue_push (EvSchedulerJob *job,
		   EvJobPriority   priority)
{
	ev_debug_message (DEBUG_JOBS, "%s priority %d", EV_GET_TYPE_NAME (job->job), priority);
	
73
	g_mutex_lock (p_job_queue_mutex);
74 75

	g_queue_push_tail (job_queue[priority], job);
76
	g_cond_broadcast (p_job_queue_cond);
77
	
78
	g_mutex_unlock (p_job_queue_mutex);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
}

static EvSchedulerJob *
ev_job_queue_get_next_unlocked (void)
{
	gint i;
	EvSchedulerJob *job = NULL;
	
	for (i = EV_JOB_PRIORITY_URGENT; i < EV_JOB_N_PRIORITIES; i++) {
		job = (EvSchedulerJob *) g_queue_pop_head (job_queue[i]);
		if (job)
			break;
	}

	ev_debug_message (DEBUG_JOBS, "%s", job ? EV_GET_TYPE_NAME (job->job) : "No jobs in queue");

	return job;
}

static gpointer
ev_job_scheduler_init (gpointer data)
{
101 102 103 104 105 106
#if (!GLIB_CHECK_VERSION(2,31,0))
/* Remove this once we bump dependencies to glib >= 2.31.0 */
	p_job_queue_cond = g_cond_new ();
	p_job_queue_mutex = g_mutex_new ();
	g_thread_create (ev_job_thread_proxy, NULL, FALSE, NULL);
#else
107
	g_thread_new ("EvJobScheduler", ev_job_thread_proxy, NULL);
108
#endif
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 167 168 169 170 171 172 173 174
	return NULL;
}

static void
ev_scheduler_job_list_add (EvSchedulerJob *job)
{
	ev_debug_message (DEBUG_JOBS, "%s", EV_GET_TYPE_NAME (job->job));
	
	G_LOCK (job_list);

	job_list = g_slist_prepend (job_list, job);
	job->job_link = job_list;
	
	G_UNLOCK (job_list);
}

static void
ev_scheduler_job_list_remove (EvSchedulerJob *job)
{
	ev_debug_message (DEBUG_JOBS, "%s", EV_GET_TYPE_NAME (job->job));
	
	G_LOCK (job_list);

	job_list = g_slist_delete_link (job_list, job->job_link);
	
	G_UNLOCK (job_list);
}

static void
ev_scheduler_job_free (EvSchedulerJob *job)
{
	if (!job)
		return;

	g_object_unref (job->job);
	g_free (job);
}

static void
ev_scheduler_job_destroy (EvSchedulerJob *job)
{
	ev_debug_message (DEBUG_JOBS, "%s", EV_GET_TYPE_NAME (job->job));

	if (job->job->run_mode == EV_JOB_RUN_MAIN_LOOP) {
		g_signal_handlers_disconnect_by_func (job->job, 
						      G_CALLBACK (ev_scheduler_job_destroy),
						      job);
	} else {
		g_signal_handlers_disconnect_by_func (job->job->cancellable,
						      G_CALLBACK (ev_scheduler_thread_job_cancelled),
						      job);
	}
	
	ev_scheduler_job_list_remove (job);
	ev_scheduler_job_free (job);
}

static void
ev_scheduler_thread_job_cancelled (EvSchedulerJob *job,
				   GCancellable   *cancellable)
{
	GList   *list;
	
	ev_debug_message (DEBUG_JOBS, "%s", EV_GET_TYPE_NAME (job->job));

175
	g_mutex_lock (p_job_queue_mutex);
176 177 178 179 180 181 182 183 184

	/* If the job is not still running,
	 * remove it from the job queue and job list.
	 * If the job is currently running, it will be
	 * destroyed as soon as it finishes. 
	 */
	list = g_queue_find (job_queue[job->priority], job);
	if (list) {
		g_queue_delete_link (job_queue[job->priority], list);
185
		g_mutex_unlock (p_job_queue_mutex);
186 187
		ev_scheduler_job_destroy (job);
	} else {
188
		g_mutex_unlock (p_job_queue_mutex);
189 190 191 192 193 194 195 196 197 198 199 200 201
	}
}

static void
ev_job_thread (EvJob *job)
{
	gboolean result;

	ev_debug_message (DEBUG_JOBS, "%s", EV_GET_TYPE_NAME (job));

	do {
		if (g_cancellable_is_cancelled (job->cancellable))
			result = FALSE;
202 203
		else {
                        g_atomic_pointer_set (&running_job, job);
204
			result = ev_job_run (job);
205
                }
206
	} while (result);
207 208

        g_atomic_pointer_set (&running_job, NULL);
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
}

static gboolean
ev_job_idle (EvJob *job)
{
	ev_debug_message (DEBUG_JOBS, "%s", EV_GET_TYPE_NAME (job));

	if (g_cancellable_is_cancelled (job->cancellable))
		return FALSE;

	return ev_job_run (job);
}

static gpointer
ev_job_thread_proxy (gpointer data)
{
	while (TRUE) {
		EvSchedulerJob *job;

228
		g_mutex_lock (p_job_queue_mutex);
229 230
		job = ev_job_queue_get_next_unlocked ();
		if (!job) {
231 232
			g_cond_wait (p_job_queue_cond, p_job_queue_mutex);
			g_mutex_unlock (p_job_queue_mutex);
233 234
			continue;
		}
235
		g_mutex_unlock (p_job_queue_mutex);
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
		
		ev_job_thread (job->job);
		ev_scheduler_job_destroy (job);
	}

	return NULL;
}

void
ev_job_scheduler_push_job (EvJob         *job,
			   EvJobPriority  priority)
{
	static GOnce once_init = G_ONCE_INIT;
	EvSchedulerJob *s_job;

	g_once (&once_init, ev_job_scheduler_init, NULL);

	ev_debug_message (DEBUG_JOBS, "%s pirority %d", EV_GET_TYPE_NAME (job), priority);

	s_job = g_new0 (EvSchedulerJob, 1);
	s_job->job = g_object_ref (job);
	s_job->priority = priority;

	ev_scheduler_job_list_add (s_job);
	
	switch (ev_job_get_run_mode (job)) {
	case EV_JOB_RUN_THREAD:
		g_signal_connect_swapped (job->cancellable, "cancelled",
					  G_CALLBACK (ev_scheduler_thread_job_cancelled),
					  s_job);
		ev_job_queue_push (s_job, priority);
		break;
	case EV_JOB_RUN_MAIN_LOOP:
		g_signal_connect_swapped (job, "finished",
					  G_CALLBACK (ev_scheduler_job_destroy),
					  s_job);
		g_signal_connect_swapped (job, "cancelled",
					  G_CALLBACK (ev_scheduler_job_destroy),
					  s_job);
		g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
				 (GSourceFunc)ev_job_idle,
				 g_object_ref (job),
				 (GDestroyNotify)g_object_unref);
		break;
	default:
		g_assert_not_reached ();
	}
}

void
ev_job_scheduler_update_job (EvJob         *job,
			     EvJobPriority  priority)
{
	GSList         *l;
	EvSchedulerJob *s_job = NULL;
	gboolean        need_resort = FALSE;

	/* Main loop jobs are scheduled inmediately */
	if (ev_job_get_run_mode (job) == EV_JOB_RUN_MAIN_LOOP)
		return;

	ev_debug_message (DEBUG_JOBS, "%s pirority %d", EV_GET_TYPE_NAME (job), priority);
	
	G_LOCK (job_list);

	for (l = job_list; l; l = l->next) {
		s_job = (EvSchedulerJob *)l->data;

		if (s_job->job == job) {
			need_resort = (s_job->priority != priority);
			break;
		}
	}
	
	G_UNLOCK (job_list);

	if (need_resort) {
		GList *list;
	
315
		g_mutex_lock (p_job_queue_mutex);
316 317 318 319 320 321 322
		
		list = g_queue_find (job_queue[s_job->priority], s_job);
		if (list) {
			ev_debug_message (DEBUG_JOBS, "Moving job %s from pirority %d to %d",
					  EV_GET_TYPE_NAME (job), s_job->priority, priority);
			g_queue_delete_link (job_queue[s_job->priority], list);
			g_queue_push_tail (job_queue[priority], s_job);
323
			g_cond_broadcast (p_job_queue_cond);
324 325
		}
		
326
		g_mutex_unlock (p_job_queue_mutex);
327 328 329
	}
}

330 331 332 333 334
EvJob *
ev_job_scheduler_get_running_thread_job (void)
{
        return g_atomic_pointer_get (&running_job);
}