/
queue.c
262 lines (221 loc) · 6.04 KB
/
queue.c
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
/* This file is part of herzi's playground
*
* AUTHORS
* Sven Herzberg <sven@imendio.com>
*
* Copyright (C) 2008 Sven Herzberg
*
* This program 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.1 of the
* License, or (at your option) any later version.
*
* This program 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 Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include "queue.h"
#include <unistd.h>
#ifdef __APPLE_CC__
#include <CoreServices/CoreServices.h>
#endif
#include "worker.h"
struct _Queue {
GList * threads;
GList * idle_threads;
GQueue* jobs;
guint schedule_idle;
guint32 last_job_id;
};
struct QueueJob {
Queue * queue;
Worker * worker;
GThreadFunc async;
gpointer async_result;
GFunc destroy;
gpointer user_data;
guint id;
guint started : 1;
guint finished : 1;
};
static void queue_schedule_job (Queue * queue,
struct QueueJob* job);
static inline guint
get_n_processors (void)
{
#ifdef __linux
return sysconf(_SC_NPROCESSORS_ONLN);
/* FIXME: add Mac/Windows implementations */
#elif __APPLE_CC__
return MPProcessors ();
#else
g_warning ("Your operating system is unsupported; you'll only get one core. "
"Feel free to fix this at " G_STRLOC);
return 1;
#endif
}
/*
* queue_new:
*
* Create a new workload queue.
*
* Returns the newly created #Queue.
*/
Queue*
queue_new (void)
{
Queue* queue = g_slice_new0 (Queue);
gint thread;
gint n_threads = get_n_processors (); /* FIXME: make G_GNUC_CONST and put into loop argument? */
for (thread = 0; thread < n_threads; thread++) {
GError* error = NULL;
Worker* worker = worker_new (thread, &error);
if (!error) {
queue->threads = g_list_prepend (queue->threads, worker);
queue->idle_threads = g_list_prepend (queue->idle_threads, worker);
} else {
g_printerr ("error creating thread %d (%d of %d)\n",
worker->id, thread + 1, n_threads);
}
}
queue->threads = g_list_reverse (queue->threads);
queue->jobs = g_queue_new ();
return queue;
}
/*
* queue_free:
* queue: the #Queue to free
*
* Releases all ressources assotiated to the queue.
*/
void
queue_free (Queue* queue)
{
if (queue->schedule_idle) {
g_warning (G_STRLOC ": FIXME: cancel scheduling thread");
}
while (queue->threads) {
Worker* worker = queue->threads->data;
worker_shutdown (worker);
queue->threads = g_list_delete_link (queue->threads, queue->threads);
}
g_list_free (queue->idle_threads);
if (queue->jobs->length) {
g_warning (G_STRLOC ": FIXME: cancel unfinished jobs first");
}
g_queue_free (queue->jobs);
g_slice_free (Queue, queue);
}
/* START: QueueJob API */
static struct QueueJob*
queue_job_new (guint id,
Queue * queue,
GThreadFunc async,
GFunc destroy,
gpointer user_data)
{
struct QueueJob* self = g_slice_new0 (struct QueueJob);
self->id = id;
self->queue = queue;
self->async = async;
self->destroy = destroy;
self->user_data = user_data;
return self;
}
static void
queue_job_free (struct QueueJob* self)
{
g_slice_free (struct QueueJob, self);
}
/* END: QueueJob API */
static gboolean
job_done (gpointer user_data)
{
struct QueueJob* job = user_data;
struct QueueJob* next_job;
if (job->destroy) {
job->destroy (job->async_result,
job->user_data);
}
/* return the worker into the list of idle threads */
job->queue->idle_threads = g_list_prepend (job->queue->idle_threads,
job->worker);
next_job = g_queue_pop_head (job->queue->jobs);
if (next_job) {
queue_schedule_job (job->queue, next_job);
}
queue_job_free (job);
return FALSE; /* execute once */
}
static gboolean
execute_job (gpointer user_data)
{
/* WARNING START: executed in worker thread */
struct QueueJob* job = user_data;
job->async_result = job->async (job->user_data);
GSource* source = g_idle_source_new ();
g_source_set_callback (source,
job_done,
job,
NULL);
g_source_attach (source, g_main_context_default ());
g_source_unref (source);
return FALSE; /* execute once */
/* WARNING END: executed in worker thread */
}
static void
queue_schedule_job (Queue * queue,
struct QueueJob* job)
{
Worker* worker = queue->idle_threads->data;
g_return_if_fail (worker);
GSource* source = g_idle_source_new ();
g_source_set_callback (source,
execute_job,
job,
NULL);
queue->idle_threads = g_list_delete_link (queue->idle_threads, queue->idle_threads);
job->worker = worker;
g_source_attach (source, worker->context);
g_source_unref (source);
}
/*
* queue_queue:
* @queue: the #Queue to attach this job request to
* @async: the function (running in a separate thread) to be executed
* asynchronously
* @destroy: the function (running in the main thread) to be executed
* after the asynchronous function finished. The result of @async will be
* passed as the first parameter, the @user_data as the second.
* @user_data: the user data for the GThreadFunc and GFunc
*
* Queue a job request.
*
*/
/* Returns: the id of the job within this queue */
guint32
queue_queue (Queue * queue,
GThreadFunc async,
GFunc destroy,
gpointer user_data)
{
struct QueueJob* job;
g_return_if_fail (async != NULL);
g_return_if_fail (queue != NULL);
job = queue_job_new (queue->last_job_id++, queue, async, destroy, user_data);
if (G_UNLIKELY (queue->idle_threads)) {
/* schedule immediately */
queue_schedule_job (queue, job);
} else {
/* queue for scheduling */
g_queue_push_tail (queue->jobs,
job);
}
return job->id;
}