-
Notifications
You must be signed in to change notification settings - Fork 0
/
retro_emu_thread.c
181 lines (148 loc) · 4.29 KB
/
retro_emu_thread.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
// This is copyrighted software. More information is at the end of this file.
#include "retro_emu_thread.h"
#include <pthread.h>
static pthread_t main_thread;
static pthread_t emu_thread;
static pthread_mutex_t emu_mutex;
static pthread_mutex_t main_mutex;
static pthread_cond_t emu_cv;
static pthread_cond_t main_cv;
static bool emu_keep_waiting = true;
static bool main_keep_waiting = true;
static bool emu_has_exited = false;
static bool emu_thread_canceled = false;
static bool emu_thread_initialized = false;
static void* retro_run_emulator(void *args)
{
char *args_str = (char *)args;
bool dynarec = (*args_str++ == 1) ? true : false;
//printf("************ dynarec=%d, args[0]=%d\n", dynarec, args_str[0]);
u32 cycles = strtol(args_str, NULL, 10);
emu_has_exited = false;
emu_thread_canceled = false;
#if defined(HAVE_DYNAREC)
if (dynarec){
printf("DYNAREC ENABLED\n");
execute_arm_translate(cycles);
}
#endif
printf("NO DYNAREC\n");
execute_arm(cycles);
emu_has_exited = true;
return NULL;
}
static void retro_switch_to_emu_thread()
{
pthread_mutex_lock(&emu_mutex);
emu_keep_waiting = false;
pthread_mutex_unlock(&emu_mutex);
pthread_mutex_lock(&main_mutex);
pthread_cond_signal(&emu_cv);
main_keep_waiting = true;
while (main_keep_waiting)
{
pthread_cond_wait(&main_cv, &main_mutex);
}
pthread_mutex_unlock(&main_mutex);
}
static void retro_switch_to_main_thread()
{
pthread_mutex_lock(&main_mutex);
main_keep_waiting = false;
pthread_mutex_unlock(&main_mutex);
pthread_mutex_lock(&emu_mutex);
pthread_cond_signal(&main_cv);
emu_keep_waiting = true;
while (emu_keep_waiting)
{
pthread_cond_wait(&emu_cv, &emu_mutex);
}
pthread_mutex_unlock(&emu_mutex);
}
void retro_switch_thread()
{
if (pthread_self() == main_thread)
retro_switch_to_emu_thread();
else
retro_switch_to_main_thread();
}
bool retro_init_emu_thread(bool dynarec, u32 cycles)
{
static volatile char args[256];
args[0] = '\0';
if (emu_thread_initialized)
return true;
/* Keep this very simple:
* - First character: dynarec, 0/1
* - Remaining characters: cycles */
snprintf(args, sizeof(args), " %u", cycles);
args[0] = dynarec ? 1 : 0;
main_thread = pthread_self();
if (pthread_mutex_init(&main_mutex, NULL))
goto main_mutex_error;
if (pthread_mutex_init(&emu_mutex, NULL))
goto emu_mutex_error;
if (pthread_cond_init(&main_cv, NULL))
goto main_cv_error;
if (pthread_cond_init(&emu_cv, NULL))
goto emu_cv_error;
if (pthread_create(&emu_thread, NULL, retro_run_emulator, args))
goto emu_thread_error;
emu_thread_initialized = true;
return true;
emu_thread_error:
pthread_cond_destroy(&emu_cv);
emu_cv_error:
pthread_cond_destroy(&main_cv);
main_cv_error:
pthread_mutex_destroy(&emu_mutex);
emu_mutex_error:
pthread_mutex_destroy(&main_mutex);
main_mutex_error:
return false;
}
void retro_deinit_emu_thread()
{
if (!emu_thread_initialized)
return;
pthread_mutex_destroy(&main_mutex);
pthread_mutex_destroy(&emu_mutex);
pthread_cond_destroy(&main_cv);
pthread_cond_destroy(&emu_cv);
emu_thread_initialized = false;
}
bool retro_is_emu_thread_initialized()
{
return emu_thread_initialized;
}
void retro_join_emu_thread()
{
static bool is_joined = false;
if (is_joined)
return;
pthread_join(emu_thread, NULL);
is_joined = true;
}
void retro_cancel_emu_thread()
{
if (emu_thread_canceled)
return;
pthread_cancel(emu_thread);
emu_thread_canceled = true;
}
bool retro_emu_thread_exited()
{
return emu_has_exited;
}
/*
Copyright (C) 2020 Nikos Chantziaras <[email protected]>
This program 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
Foundation, either version 2 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 General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/