Debian Codegraph @

libretro-common/queues/task_queue.c

raw symbols SCIP
1 /* Copyright (C) 2010-2016 The RetroArch team
2 *
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (task_queue.c).
5 * ---------------------------------------------------------------------------------------
6 *
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26
27 #include <queues/task_queue.h>
28
29 #ifdef HAVE_THREADS
30 #include <rthreads/rthreads.h>
31 #endif
32
33 typedef struct
34 {
35 retro_task_t *front;
36 retro_task_t *back;
37 } task_queue_t;
38
39 struct retro_task_impl
40 {
41 void (*push_running)(retro_task_t *);
42 void (*reset)(void);
43 void (*wait)(void);
44 void (*gather)(void);
45 bool (*find)(retro_task_finder_t, void*);
46 void (*init)(void);
47 void (*deinit)(void);
48 };
49
50 static task_queue_t tasks_running = {NULL, NULL};
51 static task_queue_t tasks_finished = {NULL, NULL};
52
53 #ifndef RARCH_INTERNAL
54 static void task_queue_msg_push(unsigned prio, unsigned duration,
55 bool flush, const char *fmt, ...)
56 {
57 char buf[1024];
58 va_list ap;
59
60 va_start(ap, fmt);
61 vsnprintf(buf, sizeof(buf), fmt, ap);
62 va_end(ap);
63
64 /* print something here */
65 }
66
67 void task_queue_push_progress(retro_task_t *task)
68 {
69 if (task->title)
70 {
71 if (task->finished)
72 {
73 if (task->error)
74 task_queue_msg_push(1, 60, true, "%s: %s",
75 "Task failed\n", task->title);
76 else
77 task_queue_msg_push(1, 60, true, "100%%: %s", task->title);
78 }
79 else
80 {
81 if (task->progress >= 0 && task->progress <= 100)
82 task_queue_msg_push(1, 60, true, "%i%%: %s",
83 task->progress, task->title);
84 else
85 task_queue_msg_push(1, 60, true, "%s...", task->title);
86 }
87 }
88 }
89 #endif
90
91 static void task_queue_put(task_queue_t *queue, retro_task_t *task)
92 {
93 task->next = NULL;
94
95 if (queue->front)
96 queue->back->next = task;
97 else
98 queue->front = task;
99
100 queue->back = task;
101 }
102
103 static retro_task_t *task_queue_get(task_queue_t *queue)
104 {
105 retro_task_t *task = queue->front;
106
107 if (task)
108 {
109 queue->front = task->next;
110 task->next = NULL;
111 }
112
113 return task;
114 }
115
116 static void retro_task_internal_gather(void)
117 {
118 retro_task_t *task = NULL;
119 while ((task = task_queue_get(&tasks_finished)) != NULL)
120 {
121 task_queue_push_progress(task);
122
123 if (task->callback)
124 task->callback(task->task_data, task->user_data, task->error);
125
126 if (task->error)
127 free(task->error);
128
129 if (task->title)
130 free(task->title);
131
132 free(task);
133 }
134 }
135
136 static void retro_task_regular_push_running(retro_task_t *task)
137 {
138 task_queue_put(&tasks_running, task);
139 }
140
141 static void retro_task_regular_gather(void)
142 {
143 retro_task_t *task = NULL;
144 retro_task_t *queue = NULL;
145 retro_task_t *next = NULL;
146
147 while ((task = task_queue_get(&tasks_running)) != NULL)
148 {
149 task->next = queue;
150 queue = task;
151 }
152
153 for (task = queue; task; task = next)
154 {
155 next = task->next;
156 task->handler(task);
157
158 task_queue_push_progress(task);
159
160 if (task->finished)
161 task_queue_put(&tasks_finished, task);
162 else
163 retro_task_regular_push_running(task);
164 }
165
166 retro_task_internal_gather();
167 }
168
169 static void retro_task_regular_wait(void)
170 {
171 while (tasks_running.front)
172 retro_task_regular_gather();
173 }
174
175 static void retro_task_regular_reset(void)
176 {
177 retro_task_t *task = tasks_running.front;
178
179 for (; task; task = task->next)
180 task->cancelled = true;
181 }
182
183 static void retro_task_regular_init(void)
184 {
185 }
186
187 static void retro_task_regular_deinit(void)
188 {
189 }
190
191 static bool retro_task_regular_find(retro_task_finder_t func, void *user_data)
192 {
193 retro_task_t *task = tasks_running.front;
194
195 for (; task; task = task->next)
196 {
197 if (func(task, user_data))
198 return true;
199 }
200
201 return false;
202 }
203
204 static struct retro_task_impl impl_regular = {
205 retro_task_regular_push_running,
206 retro_task_regular_reset,
207 retro_task_regular_wait,
208 retro_task_regular_gather,
209 retro_task_regular_find,
210 retro_task_regular_init,
211 retro_task_regular_deinit
212 };
213
214 #ifdef HAVE_THREADS
215 static slock_t *running_lock = NULL;
216 static slock_t *finished_lock = NULL;
217 static scond_t *worker_cond = NULL;
218 static sthread_t *worker_thread = NULL;
219 static bool worker_continue = true; /* use running_lock when touching it */
220
221 static void retro_task_threaded_push_running(retro_task_t *task)
222 {
223 slock_lock(running_lock);
224 task_queue_put(&tasks_running, task);
225 scond_signal(worker_cond);
226 slock_unlock(running_lock);
227 }
228
229 static void retro_task_threaded_gather(void)
230 {
231 retro_task_t *task = NULL;
232
233 slock_lock(running_lock);
234 for (task = tasks_running.front; task; task = task->next)
235 task_queue_push_progress(task);
236
237 slock_unlock(running_lock);
238
239 slock_lock(finished_lock);
240 retro_task_internal_gather();
241 slock_unlock(finished_lock);
242 }
243
244 static void retro_task_threaded_wait(void)
245 {
246 bool wait = false;
247
248 do
249 {
250 retro_task_threaded_gather();
251
252 slock_lock(running_lock);
253 wait = (tasks_running.front != NULL);
254 slock_unlock(running_lock);
255 } while (wait);
256 }
257
258 static void retro_task_threaded_reset(void)
259 {
260 retro_task_t *task = NULL;
261
262 slock_lock(running_lock);
263 for (task = tasks_running.front; task; task = task->next)
264 task->cancelled = true;
265 slock_unlock(running_lock);
266 }
267
268
269 static bool retro_task_threaded_find(
270 retro_task_finder_t func, void *user_data)
271 {
272 retro_task_t *task = NULL;
273
274 slock_lock(running_lock);
275 for (task = tasks_running.front; task; task = task->next)
276 {
277 if (func(task, user_data))
278 return true;
279 }
280 slock_unlock(running_lock);
281
282 return false;
283 }
284
285 static void threaded_worker(void *userdata)
286 {
287 (void)userdata;
288
289 for (;;)
290 {
291 retro_task_t *queue = NULL;
292 retro_task_t *task = NULL;
293 retro_task_t *next = NULL;
294
295 /* pop all into a local queue,
296 * tasks are in the reverse order here. */
297 slock_lock(running_lock);
298
299 if (!worker_continue)
300 break; /* should we keep running until all tasks finished? */
301
302 while ((task = task_queue_get(&tasks_running)) != NULL)
303 {
304 task->next = queue;
305 queue = task;
306 }
307
308 if (queue == NULL) /* no tasks running, lets wait a bit */
309 {
310 scond_wait(worker_cond, running_lock);
311 slock_unlock(running_lock);
312 continue;
313 }
314
315 slock_unlock(running_lock);
316
317 for (task = queue; task; task = next)
318 {
319 next = task->next;
320 task->handler(task);
321
322 if (task->finished)
323 {
324 slock_lock(finished_lock);
325 task_queue_put(&tasks_finished, task);
326 slock_unlock(finished_lock);
327 }
328 else
329 retro_task_threaded_push_running(task);
330 }
331 }
332
333 slock_unlock(running_lock);
334 }
335
336 static void retro_task_threaded_init(void)
337 {
338 running_lock = slock_new();
339 finished_lock = slock_new();
340 worker_cond = scond_new();
341
342 slock_lock(running_lock);
343 worker_continue = true;
344 slock_unlock(running_lock);
345
346 worker_thread = sthread_create(threaded_worker, NULL);
347 }
348
349 static void retro_task_threaded_deinit(void)
350 {
351 slock_lock(running_lock);
352 worker_continue = false;
353 scond_signal(worker_cond);
354 slock_unlock(running_lock);
355
356 sthread_join(worker_thread);
357
358 scond_free(worker_cond);
359 slock_free(running_lock);
360 slock_free(finished_lock);
361
362 worker_thread = NULL;
363 worker_cond = NULL;
364 running_lock = NULL;
365 finished_lock = NULL;
366 }
367
368 static struct retro_task_impl impl_threaded = {
369 retro_task_threaded_push_running,
370 retro_task_threaded_reset,
371 retro_task_threaded_wait,
372 retro_task_threaded_gather,
373 retro_task_threaded_find,
374 retro_task_threaded_init,
375 retro_task_threaded_deinit
376 };
377 #endif
378
379 bool task_queue_ctl(enum task_queue_ctl_state state, void *data)
380 {
381 static struct retro_task_impl *impl_current = NULL;
382 static bool task_threaded_enable = false;
383
384 switch (state)
385 {
386 case TASK_QUEUE_CTL_DEINIT:
387 if (impl_current)
388 impl_current->deinit();
389 impl_current = NULL;
390 break;
391 case TASK_QUEUE_CTL_SET_THREADED:
392 task_threaded_enable = true;
393 break;
394 case TASK_QUEUE_CTL_UNSET_THREADED:
395 task_threaded_enable = false;
396 break;
397 case TASK_QUEUE_CTL_IS_THREADED:
398 return task_threaded_enable;
399 case TASK_QUEUE_CTL_INIT:
400 {
401 #ifdef HAVE_THREADS
402 bool *boolean_val = (bool*)data;
403 #endif
404
405 impl_current = &impl_regular;
406 #ifdef HAVE_THREADS
407 if (*boolean_val)
408 {
409 task_queue_ctl(TASK_QUEUE_CTL_SET_THREADED, NULL);
410 impl_current = &impl_threaded;
411 }
412 #endif
413
414 impl_current->init();
415 }
416 break;
417 case TASK_QUEUE_CTL_FIND:
418 {
419 task_finder_data_t *find_data = (task_finder_data_t*)data;
420 if (!impl_current->find(find_data->func, find_data->userdata))
421 return false;
422 }
423 break;
424 case TASK_QUEUE_CTL_CHECK:
425 {
426 #ifdef HAVE_THREADS
427 bool current_threaded = (impl_current == &impl_threaded);
428 bool want_threaded =
429 task_queue_ctl(TASK_QUEUE_CTL_IS_THREADED, NULL);
430
431 if (want_threaded != current_threaded)
432 task_queue_ctl(TASK_QUEUE_CTL_DEINIT, NULL);
433
434 if (!impl_current)
435 task_queue_ctl(TASK_QUEUE_CTL_INIT, NULL);
436 #endif
437
438 impl_current->gather();
439 }
440 break;
441 case TASK_QUEUE_CTL_PUSH:
442 {
443 /* The lack of NULL checks in the following functions
444 * is proposital to ensure correct control flow by the users. */
445 retro_task_t *task = (retro_task_t*)data;
446 impl_current->push_running(task);
447 break;
448 }
449 case TASK_QUEUE_CTL_RESET:
450 impl_current->reset();
451 break;
452 case TASK_QUEUE_CTL_WAIT:
453 impl_current->wait();
454 break;
455 case TASK_QUEUE_CTL_NONE:
456 default:
457 break;
458 }
459
460 return true;
461 }