forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runloop_data.c
397 lines (324 loc) · 8.57 KB
/
runloop_data.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
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
/* RetroArch - A frontend for libretro.
* Copyright (C) 2011-2015 - Daniel De Matteis
*
* RetroArch 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <retro_miscellaneous.h>
#ifdef HAVE_THREADS
#include <rthreads/rthreads.h>
#endif
#include "general.h"
#include "runloop_data.h"
#include "tasks/tasks.h"
#include "input/input_overlay.h"
#ifdef HAVE_MENU
#include "menu/menu.h"
#include "menu/menu_entries.h"
#include "menu/menu_input.h"
#endif
enum
{
THREAD_CODE_INIT = 0,
THREAD_CODE_DEINIT,
THREAD_CODE_ALIVE
} thread_code_enum;
typedef struct data_runloop
{
bool inited;
#ifdef HAVE_THREADS
bool thread_inited;
unsigned thread_code;
bool alive;
slock_t *lock;
slock_t *cond_lock;
scond_t *cond;
sthread_t *thread;
#endif
} data_runloop_t;
static char data_runloop_msg[PATH_MAX_LENGTH];
static data_runloop_t *g_data_runloop;
static data_runloop_t *rarch_main_data_get_ptr(void)
{
return g_data_runloop;
}
#ifdef HAVE_THREADS
static void data_runloop_thread_deinit(data_runloop_t *runloop)
{
if (!runloop->thread_inited)
{
slock_lock(runloop->cond_lock);
runloop->alive = false;
scond_signal(runloop->cond);
slock_unlock(runloop->cond_lock);
sthread_join(runloop->thread);
slock_free(runloop->lock);
slock_free(runloop->cond_lock);
rarch_main_data_overlay_thread_uninit();
scond_free(runloop->cond);
}
}
#endif
void rarch_main_data_deinit(void)
{
data_runloop_t *runloop = rarch_main_data_get_ptr();
if (!runloop)
return;
#ifdef HAVE_THREADS
if (runloop->thread_inited)
{
data_runloop_thread_deinit(runloop);
runloop->thread_inited = false;
runloop->thread_code = THREAD_CODE_DEINIT;
}
#endif
runloop->inited = false;
}
static void rarch_main_data_free_internal(void)
{
data_runloop_t *runloop = rarch_main_data_get_ptr();
if (!runloop)
return;
if (runloop)
free(runloop);
runloop = NULL;
}
void rarch_main_data_free(void)
{
rarch_main_data_nbio_uninit();
#ifdef HAVE_NETWORKING
rarch_main_data_http_uninit();
#endif
#ifdef HAVE_LIBRETRODB
rarch_main_data_db_uninit();
#endif
rarch_main_data_free_internal();
}
static void data_runloop_iterate(bool is_thread)
{
rarch_main_data_nbio_iterate (is_thread);
#ifdef HAVE_RPNG
rarch_main_data_nbio_image_iterate (is_thread);
#endif
#ifdef HAVE_NETWORKING
rarch_main_data_http_iterate (is_thread);
#endif
#ifdef HAVE_LIBRETRODB
rarch_main_data_db_iterate (is_thread);
#endif
}
bool rarch_main_data_active(void)
{
bool active = false;
#ifdef HAVE_LIBRETRODB
if (rarch_main_data_db_is_active())
active = true;
#endif
#ifdef HAVE_OVERLAY
if (input_overlay_data_is_active())
active = true;
#endif
if (rarch_main_data_nbio_image_get_handle())
active = true;
if (rarch_main_data_nbio_get_handle())
active = true;
#ifdef HAVE_NETWORKING
if (rarch_main_data_http_get_handle())
active = true;
if (rarch_main_data_http_conn_get_handle())
active = true;
#endif
return active;
}
#ifdef HAVE_THREADS
static void data_thread_loop(void *data)
{
data_runloop_t *runloop = (data_runloop_t*)data;
RARCH_LOG("[Data Thread]: Initializing data thread.\n");
slock_lock(runloop->lock);
while (!runloop->thread_inited)
scond_wait(runloop->cond, runloop->lock);
slock_unlock(runloop->lock);
RARCH_LOG("[Data Thread]: Starting data thread.\n");
while (runloop->alive)
{
slock_lock(runloop->lock);
if (!runloop->alive)
break;
data_runloop_iterate(true);
if (!rarch_main_data_active())
rarch_sleep(10);
slock_unlock(runloop->lock);
}
RARCH_LOG("[Data Thread]: Stopping data thread.\n");
}
#endif
#ifdef HAVE_THREADS
static void rarch_main_data_thread_init(void)
{
data_runloop_t *runloop = rarch_main_data_get_ptr();
if (!runloop)
return;
runloop->lock = slock_new();
runloop->cond_lock = slock_new();
runloop->cond = scond_new();
#ifdef HAVE_OVERLAY
rarch_main_data_overlay_thread_init();
#endif
runloop->thread = sthread_create(data_thread_loop, runloop);
if (!runloop->thread)
goto error;
slock_lock(runloop->lock);
runloop->thread_inited = true;
runloop->alive = true;
runloop->thread_code = THREAD_CODE_ALIVE;
slock_unlock(runloop->lock);
return;
error:
data_runloop_thread_deinit(runloop);
}
#endif
void rarch_main_data_iterate(void)
{
data_runloop_t *runloop = rarch_main_data_get_ptr();
settings_t *settings = config_get_ptr();
(void)settings;
#ifdef HAVE_THREADS
if (settings->menu.threaded_data_runloop_enable)
{
switch (runloop->thread_code)
{
case THREAD_CODE_INIT:
rarch_main_data_thread_init();
break;
case THREAD_CODE_DEINIT:
case THREAD_CODE_ALIVE:
break;
}
}
#endif
#ifdef HAVE_OVERLAY
rarch_main_data_overlay_image_upload_iterate(false);
#endif
#ifdef HAVE_RPNG
rarch_main_data_nbio_image_upload_iterate(false);
#endif
#ifdef HAVE_OVERLAY
rarch_main_data_overlay_iterate (false);
#endif
#ifdef HAVE_MENU
#ifdef HAVE_LIBRETRODB
if (rarch_main_data_db_pending_scan_finished())
menu_environment_cb(MENU_ENVIRON_RESET_HORIZONTAL_LIST, NULL);
#endif
#endif
if (data_runloop_msg[0] != '\0')
{
rarch_main_msg_queue_push(data_runloop_msg, 1, 10, true);
data_runloop_msg[0] = '\0';
}
#ifdef HAVE_MENU
menu_entries_refresh(MENU_ACTION_REFRESH);
#endif
#ifdef HAVE_THREADS
if (settings->menu.threaded_data_runloop_enable && runloop->alive)
return;
#endif
data_runloop_iterate(false);
}
static data_runloop_t *rarch_main_data_new(void)
{
data_runloop_t *runloop = (data_runloop_t*)
calloc(1, sizeof(data_runloop_t));
if (!runloop)
return NULL;
#ifdef HAVE_THREADS
runloop->thread_inited = false;
runloop->alive = false;
#endif
runloop->inited = true;
return runloop;
}
void rarch_main_data_clear_state(void)
{
rarch_main_data_deinit();
rarch_main_data_free();
g_data_runloop = rarch_main_data_new();
if (!g_data_runloop)
return;
rarch_main_data_nbio_init();
#ifdef HAVE_NETWORKING
rarch_main_data_http_init();
#endif
#ifdef HAVE_LIBRETRODB
rarch_main_data_db_init();
#endif
}
void rarch_main_data_init_queues(void)
{
#ifdef HAVE_NETWORKING
rarch_main_data_http_init_msg_queue();
#endif
rarch_main_data_nbio_init_msg_queue();
#ifdef HAVE_LIBRETRODB
rarch_main_data_db_init_msg_queue();
#endif
}
void rarch_main_data_msg_queue_push(unsigned type,
const char *msg, const char *msg2,
unsigned prio, unsigned duration, bool flush)
{
char new_msg[PATH_MAX_LENGTH] = {0};
msg_queue_t *queue = NULL;
switch(type)
{
case DATA_TYPE_NONE:
break;
case DATA_TYPE_FILE:
queue = rarch_main_data_nbio_get_msg_queue_ptr();
if (!queue)
return;
snprintf(new_msg, sizeof(new_msg), "%s|%s", msg, msg2);
break;
case DATA_TYPE_IMAGE:
queue = rarch_main_data_nbio_image_get_msg_queue_ptr();
if (!queue)
return;
snprintf(new_msg, sizeof(new_msg), "%s|%s", msg, msg2);
break;
#ifdef HAVE_NETWORKING
case DATA_TYPE_HTTP:
queue = rarch_main_data_http_get_msg_queue_ptr();
snprintf(new_msg, sizeof(new_msg), "%s|%s", msg, msg2);
break;
#endif
#ifdef HAVE_OVERLAY
case DATA_TYPE_OVERLAY:
snprintf(new_msg, sizeof(new_msg), "%s|%s", msg, msg2);
break;
#endif
#ifdef HAVE_LIBRETRODB
case DATA_TYPE_DB:
queue = rarch_main_data_db_get_msg_queue_ptr();
snprintf(new_msg, sizeof(new_msg), "%s|%s", msg, msg2);
break;
#endif
}
if (!queue)
return;
if (flush)
msg_queue_clear(queue);
msg_queue_push(queue, new_msg, prio, duration);
}
void data_runloop_osd_msg(const char *msg, size_t len)
{
strlcpy(data_runloop_msg, msg, len);
}