File: gnome-vfs-job-queue.c

package info (click to toggle)
gnome-vfs 1%3A2.24.4-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,120 kB
  • ctags: 10,397
  • sloc: ansic: 78,516; sh: 10,341; makefile: 902; perl: 99
file content (222 lines) | stat: -rw-r--r-- 5,419 bytes parent folder | download | duplicates (5)
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gnome-vfs-job-queue.c - Job queue for asynchronous GnomeVFSJobs
   
   Copyright (C) 2005 Christian Kellner
   
   The Gnome Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Christian Kellner <gicmo@gnome.org>
*/

#include <config.h>
#include "gnome-vfs-job-queue.h"
#include "gnome-vfs-async-job-map.h"
#include <libgnomevfs/gnome-vfs-job-limit.h>

#ifndef DEFAULT_THREAD_COUNT_LIMIT
#define DEFAULT_THREAD_COUNT_LIMIT 10
#endif

#ifndef MIN_THREADS
#define MIN_THREADS 2
#endif

static GThreadPool *thread_pool = NULL;

static volatile gboolean gnome_vfs_quitting = FALSE;

static void
thread_entry_point (gpointer data, gpointer user_data)
{
	GnomeVFSJob *job;
	gboolean complete;

	job = (GnomeVFSJob *) data;
	/* job map must always be locked before the job_lock
	 * if both locks are needed */
	_gnome_vfs_async_job_map_lock ();
	
	if (_gnome_vfs_async_job_map_get_job (job->job_handle) == NULL) {
		JOB_DEBUG (("job already dead, bail %p",
			    job->job_handle));
		_gnome_vfs_async_job_map_unlock ();

		/* FIXME: doesn't that leak here? */
		return;
	}
	
	JOB_DEBUG (("locking job_lock %p", job->job_handle));
	g_mutex_lock (job->job_lock);
	_gnome_vfs_async_job_map_unlock ();

	_gnome_vfs_job_execute (job);
	complete = _gnome_vfs_job_complete (job);
	
	JOB_DEBUG (("Unlocking access lock %p", job->job_handle));
	g_mutex_unlock (job->job_lock);

	if (complete) {
		_gnome_vfs_async_job_map_lock ();
		JOB_DEBUG (("job %p done, removing from map and destroying", 
			    job->job_handle));
		_gnome_vfs_async_job_completed (job->job_handle);
		_gnome_vfs_job_destroy (job);
		_gnome_vfs_async_job_map_unlock ();
	}
}

static gint
prioritize_threads (gconstpointer a,
		    gconstpointer b,
		    gpointer      user_data)
{
	GnomeVFSJob *job_a;
	GnomeVFSJob *job_b;
	int          prio_a;
	int          prio_b;
	int          retval;
	
	job_a = (GnomeVFSJob *) a;
	job_b = (GnomeVFSJob *) b;

	prio_a = job_a->priority;
	prio_b = job_b->priority;

	/* From glib gtk-doc:
	 * 
	 * a negative value if the first task should be processed 
	 * before the second or a positive value if the 
	 * second task should be processed first. 
	 *
	 */
	
	if (prio_a > prio_b) {
		return -1;
	} else if (prio_a < prio_b) {
		return 1;
	}

	/* Since job_handles are just increasing u-ints
	 * we return a negative value if job_a->job_handle >
	 * job_b->job_handle so we have sort the old job
	 * before the newer one  */
	retval = GPOINTER_TO_UINT (job_a->job_handle) -
		 GPOINTER_TO_UINT (job_b->job_handle);

	return retval;
}

void
_gnome_vfs_job_queue_init (void)
{
	GError *err = NULL;

	thread_pool = g_thread_pool_new (thread_entry_point,
					 NULL,
					 DEFAULT_THREAD_COUNT_LIMIT,
					 FALSE,
					 &err);

	if (G_UNLIKELY (thread_pool == NULL)) {
		g_error ("Could not create threadpool: %s",
			 err->message);
	}
	
	g_thread_pool_set_sort_function (thread_pool,
					 prioritize_threads,
					 NULL);
}


gboolean
_gnome_vfs_job_schedule (GnomeVFSJob *job)
{
	GError *err = NULL;
	
	if (G_UNLIKELY (gnome_vfs_quitting)) {
		/* The application is quitting, the threadpool might already
		 * be dead, just return FALSE 
		 * We are also not calling _gnome_vfs_async_job_completed 
		 * because the job map might also be dead */
		g_warning ("Starting of GnomeVFS async calls after quit.");
		return FALSE;
	}

	g_thread_pool_push (thread_pool, job, &err);

	if (G_UNLIKELY (err != NULL)) {
		g_warning ("Could not push thread %s into pool\n",
			   err->message);

		/* thread did not start up, remove the job from the hash table */
		_gnome_vfs_async_job_completed (job->job_handle);
		
		return FALSE;
	}

	return TRUE;	
}

/**
 * gnome_vfs_async_set_job_limit:
 * @limit: maximum number of allowable threads.
 *
 * Restrict the number of worker threads used by async operations
 * to @limit.
 */
void
gnome_vfs_async_set_job_limit (int limit)
{
	if (limit < MIN_THREADS) {
		g_warning ("Attempt to set the thread_count_limit below %d", 
			   MIN_THREADS);
		return;
	}

	g_thread_pool_set_max_threads (thread_pool, limit, NULL);
}

/**
 * gnome_vfs_async_get_job_limit:
 * 
 * Get the current maximum allowable number of
 * worker threads for async operations.
 *
 * Return value: current maximum number of threads.
 */
int
gnome_vfs_async_get_job_limit (void)
{
	return g_thread_pool_get_max_threads (thread_pool);
}

void
_gnome_vfs_job_queue_shutdown (void)
{
	g_thread_pool_free (thread_pool, FALSE, FALSE);

	gnome_vfs_quitting = TRUE;

	while (gnome_vfs_job_get_count () != 0) {
		
		g_main_context_iteration (NULL, FALSE);
		g_usleep (20000);

	}

	_gnome_vfs_async_job_map_shutdown ();
}