Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/shell/ev-job-queue.c
blob: a860f377f39b2d30a572c5bbc1c967ca0e526756 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include "ev-job-queue.h"

/* Like glib calling convention, all functions with _locked in their name assume
 * that we've already locked the doc mutex and can freely and safely access
 * data.
 */
GCond *render_cond = NULL;
GMutex *ev_queue_mutex = NULL;

static GQueue *links_queue = NULL;
static GQueue *render_queue_high = NULL;
static GQueue *render_queue_low = NULL;
static GQueue *thumbnail_queue_high = NULL;
static GQueue *thumbnail_queue_low = NULL;
static GQueue *load_queue = NULL;
static GQueue *xfer_queue = NULL;
static GQueue *fonts_queue = NULL;

/* Queues used for backends supporting EvAsyncRender interface,
   they are executed on the main thread */
static GQueue *async_render_queue_high = NULL;
static GQueue *async_render_queue_low = NULL;
static gboolean async_rendering = FALSE;

static void ev_job_queue_run_next (void);

static gboolean
remove_job_from_queue_locked (GQueue *queue, EvJob *job)
{
	GList *list;

	list = g_queue_find (queue, job);
	if (list) {
		g_object_unref (G_OBJECT (job));
		g_queue_delete_link (queue, list);

		return TRUE;
	}
	return FALSE;
}

static gboolean
remove_job_from_async_queue (GQueue *queue, EvJob *job)
{
	return remove_job_from_queue_locked (queue, job);
}

static void
add_job_to_async_queue (GQueue *queue, EvJob *job)
{
	g_object_ref (job);
	g_queue_push_tail (queue, job);
}

static void
add_job_to_queue_locked (GQueue *queue,
			 EvJob  *job)
{
	g_object_ref (job);
	g_queue_push_tail (queue, job);
	g_cond_broadcast (render_cond);
}

static gboolean
notify_finished (GObject *job)
{
	ev_job_finished (EV_JOB (job));

	return FALSE;
}

static void
job_finished_cb (EvJob *job)
{
	g_object_unref (job);
	async_rendering = FALSE;
	ev_job_queue_run_next ();
}

static void
handle_job (EvJob *job)
{
	g_object_ref (G_OBJECT (job));

	if (EV_JOB (job)->async) {
		async_rendering = TRUE;
		if (EV_IS_JOB_RENDER (job)) {
			g_signal_connect (job, "finished",
					  G_CALLBACK (job_finished_cb), NULL);
		} else {
			g_assert_not_reached ();
		}
	}

	if (EV_IS_JOB_THUMBNAIL (job))
		ev_job_thumbnail_run (EV_JOB_THUMBNAIL (job));
	else if (EV_IS_JOB_LINKS (job))
		ev_job_links_run (EV_JOB_LINKS (job));
	else if (EV_IS_JOB_LOAD (job))
		ev_job_load_run (EV_JOB_LOAD (job));
	else if (EV_IS_JOB_XFER (job))
		ev_job_xfer_run (EV_JOB_XFER (job));
	else if (EV_IS_JOB_RENDER (job))
		ev_job_render_run (EV_JOB_RENDER (job));
	else if (EV_IS_JOB_FONTS (job))
		ev_job_fonts_run (EV_JOB_FONTS (job));

	if (!EV_JOB (job)->async) {
		/* We let the idle own a ref, as we (the queue) are done with the job. */
		g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
				 (GSourceFunc) notify_finished,
				 job,
				 g_object_unref);
	}
}

static EvJob *
search_for_jobs_unlocked (void)
{
	EvJob *job;

	job = (EvJob *) g_queue_pop_head (render_queue_high);
	if (job)
		return job;

	job = (EvJob *) g_queue_pop_head (thumbnail_queue_high);
	if (job)
		return job;

	job = (EvJob *) g_queue_pop_head (render_queue_low);
	if (job)
		return job;

	job = (EvJob *) g_queue_pop_head (links_queue);
	if (job)
		return job;

	job = (EvJob *) g_queue_pop_head (load_queue);
	if (job)
		return job;

	job = (EvJob *) g_queue_pop_head (xfer_queue);
	if (job)
		return job;

	job = (EvJob *) g_queue_pop_head (thumbnail_queue_low);
	if (job)
		return job;

	job = (EvJob *) g_queue_pop_head (fonts_queue);
	if (job)
		return job;

	return NULL;
}

static gboolean
no_jobs_available_unlocked (void)
{
	return g_queue_is_empty (render_queue_high)
		&& g_queue_is_empty (render_queue_low)
		&& g_queue_is_empty (links_queue)
		&& g_queue_is_empty (load_queue)
		&& g_queue_is_empty (xfer_queue)
		&& g_queue_is_empty (thumbnail_queue_high)
		&& g_queue_is_empty (thumbnail_queue_low)
		&& g_queue_is_empty (fonts_queue);
}

/* the thread mainloop function */
static gpointer
ev_render_thread (gpointer data)
{
	while (TRUE) {
		EvJob *job;

		g_mutex_lock (ev_queue_mutex);
		if (no_jobs_available_unlocked ()) {
			g_cond_wait (render_cond, ev_queue_mutex);
		}

		job = search_for_jobs_unlocked ();
		g_mutex_unlock (ev_queue_mutex);

		/* Now that we have our job, we handle it */
		if (job) {
			handle_job (job);
			g_object_unref (G_OBJECT (job));
		}
	}
	return NULL;

}

static void
ev_job_queue_run_next (void)
{
	EvJob *job;

	job = (EvJob *) g_queue_pop_head (async_render_queue_high);

	if (job == NULL) {
		job = (EvJob *) g_queue_pop_head (async_render_queue_low);
	}

	/* Now that we have our job, we handle it */
	if (job) {
		handle_job (job);
		g_object_unref (G_OBJECT (job));
	}
}

/* Public Functions */
void
ev_job_queue_init (void)
{
	if (!g_thread_supported ()) g_thread_init (NULL);

	render_cond = g_cond_new ();
	ev_queue_mutex = g_mutex_new ();

	links_queue = g_queue_new ();
	load_queue = g_queue_new ();
	xfer_queue = g_queue_new ();
	render_queue_high = g_queue_new ();
	render_queue_low = g_queue_new ();
	async_render_queue_high = g_queue_new ();
	async_render_queue_low = g_queue_new ();
	thumbnail_queue_high = g_queue_new ();
	thumbnail_queue_low = g_queue_new ();
	fonts_queue = g_queue_new ();

	g_thread_create (ev_render_thread, NULL, FALSE, NULL);

}

static GQueue *
find_queue (EvJob         *job,
	    EvJobPriority  priority)
{
	if (EV_JOB (job)->async) {
		if (EV_IS_JOB_RENDER (job)) {
			if (priority == EV_JOB_PRIORITY_HIGH)
				return async_render_queue_high;
			else
				return async_render_queue_low;
		}
	} else {
		if (EV_IS_JOB_RENDER (job)) {
			if (priority == EV_JOB_PRIORITY_HIGH)
				return render_queue_high;
			else
				return render_queue_low;
		} else if (EV_IS_JOB_THUMBNAIL (job)) {
			if (priority == EV_JOB_PRIORITY_HIGH)
				return thumbnail_queue_high;
			else
				return thumbnail_queue_low;
		} else if (EV_IS_JOB_LOAD (job)) {
			/* the priority doesn't effect load */
			return load_queue;
		} else if (EV_IS_JOB_XFER (job)) {
			/* the priority doesn't effect xfer */
			return xfer_queue;
		} else if (EV_IS_JOB_LINKS (job)) {
			/* the priority doesn't effect links */
			return links_queue;
		} else if (EV_IS_JOB_FONTS (job)) {
			/* the priority doesn't effect fonts */
			return fonts_queue;
		}
	}

	g_assert_not_reached ();
	return NULL;
}

void
ev_job_queue_add_job (EvJob         *job,
		      EvJobPriority  priority)
{
	GQueue *queue;

	g_return_if_fail (EV_IS_JOB (job));

	queue = find_queue (job, priority);

	if (!EV_JOB (job)->async) {
		g_mutex_lock (ev_queue_mutex);
		add_job_to_queue_locked (queue, job);
		g_mutex_unlock (ev_queue_mutex);
	} else {
		add_job_to_async_queue (queue, job);
		if (!async_rendering) {
			ev_job_queue_run_next ();
		}
	}
}

static gboolean
move_job_async (EvJob *job, GQueue *old_queue, GQueue *new_queue)
{
	gboolean retval = FALSE;

	g_object_ref (job);

	if (remove_job_from_queue_locked (old_queue, job)) {
		add_job_to_async_queue (new_queue, job);
		retval = TRUE;
	}

	g_object_unref (job);

	return retval;
}

static gboolean
move_job (EvJob *job, GQueue *old_queue, GQueue *new_queue)
{
	gboolean retval = FALSE;

	g_mutex_lock (ev_queue_mutex);
	g_object_ref (job);

	if (remove_job_from_queue_locked (old_queue, job)) {
		add_job_to_queue_locked (new_queue, job);
		retval = TRUE;
	}

	g_object_unref (job);
	g_mutex_unlock (ev_queue_mutex);

	return retval;
}

gboolean
ev_job_queue_update_job (EvJob         *job,
			 EvJobPriority  new_priority)
{
	gboolean retval = FALSE;
	
	g_return_val_if_fail (EV_IS_JOB (job), FALSE);

	if (EV_JOB (job)->async) {
		if (EV_IS_JOB_RENDER (job)) {
			if (new_priority == EV_JOB_PRIORITY_LOW) {
				retval = move_job_async (job, async_render_queue_high,
					                 async_render_queue_low);
			} else if (new_priority == EV_JOB_PRIORITY_HIGH) {
				retval = move_job_async (job, async_render_queue_low,
					                 async_render_queue_high);
			}
		} else {
			g_assert_not_reached ();
		}
	} else {
		if (EV_IS_JOB_THUMBNAIL (job)) {
			if (new_priority == EV_JOB_PRIORITY_LOW) {
				retval = move_job (job, thumbnail_queue_high,
					           thumbnail_queue_low);
			} else if (new_priority == EV_JOB_PRIORITY_HIGH) {
				retval = move_job (job, thumbnail_queue_low,
					           thumbnail_queue_high);
			}
		} else if (EV_IS_JOB_RENDER (job)) {
			if (new_priority == EV_JOB_PRIORITY_LOW) {
				retval = move_job (job, render_queue_high,
					           render_queue_low);
			} else if (new_priority == EV_JOB_PRIORITY_HIGH) {
				retval = move_job (job, render_queue_low,
					           render_queue_high);
			}
		} else {
			g_assert_not_reached ();
		}
	}	

	return retval;
}

gboolean
ev_job_queue_remove_job (EvJob *job)
{
	gboolean retval = FALSE;

	g_return_val_if_fail (EV_IS_JOB (job), FALSE);

	if (EV_JOB (job)->async) {
		if (EV_IS_JOB_RENDER (job)) {
			retval = remove_job_from_async_queue (async_render_queue_high, job);
			retval = retval || remove_job_from_async_queue (async_render_queue_low, job);
		} else {
			g_assert_not_reached ();
		}
	} else {
		g_mutex_lock (ev_queue_mutex);

		if (EV_IS_JOB_THUMBNAIL (job)) {
			retval = remove_job_from_queue_locked (thumbnail_queue_high, job);
			retval = retval || remove_job_from_queue_locked (thumbnail_queue_low, job);
		} else if (EV_IS_JOB_RENDER (job)) {
			retval = remove_job_from_queue_locked (render_queue_high, job);
			retval = retval || remove_job_from_queue_locked (render_queue_low, job);
		} else if (EV_IS_JOB_LINKS (job)) {
			retval = remove_job_from_queue_locked (links_queue, job);
		} else if (EV_IS_JOB_LOAD (job)) {
			retval = remove_job_from_queue_locked (load_queue, job);
		} else if (EV_IS_JOB_XFER (job)) {
			retval = remove_job_from_queue_locked (xfer_queue, job);
		} else if (EV_IS_JOB_FONTS (job)) {
			retval = remove_job_from_queue_locked (fonts_queue, job);
		} else {
			g_assert_not_reached ();
		}

		g_mutex_unlock (ev_queue_mutex);
	}
	
	return retval;
}