src/gba/gba-thread.c (view raw)
1#include "gba-thread.h"
2
3#include "arm.h"
4#include "debugger.h"
5#include "gba.h"
6#include "gba-serialize.h"
7
8#include <stdlib.h>
9#include <signal.h>
10
11#ifdef USE_PTHREADS
12static pthread_key_t _contextKey;
13static pthread_once_t _contextOnce = PTHREAD_ONCE_INIT;
14
15static void _createTLS(void) {
16 pthread_key_create(&_contextKey, 0);
17}
18#else
19static DWORD _contextKey;
20static INIT_ONCE _contextOnce = INIT_ONCE_STATIC_INIT;
21
22static BOOL CALLBACK _createTLS(PINIT_ONCE once, PVOID param, PVOID* context) {
23 (void) (once);
24 (void) (param);
25 (void) (context);
26 _contextKey = TlsAlloc();
27 return TRUE;
28}
29#endif
30
31static void _changeState(struct GBAThread* threadContext, enum ThreadState newState, int broadcast) {
32 MutexLock(&threadContext->stateMutex);
33 threadContext->state = newState;
34 if (broadcast) {
35 ConditionWake(&threadContext->stateCond);
36 }
37 MutexUnlock(&threadContext->stateMutex);
38}
39
40static THREAD_ENTRY _GBAThreadRun(void* context) {
41#ifdef USE_PTHREADS
42 pthread_once(&_contextOnce, _createTLS);
43#else
44 InitOnceExecuteOnce(&_contextOnce, _createTLS, NULL, 0);
45#endif
46
47#ifdef USE_DEBUGGER
48 struct ARMDebugger debugger;
49#endif
50 struct GBA gba;
51 struct GBAThread* threadContext = context;
52 char* savedata = 0;
53
54#if !defined(_WIN32) && defined(USE_PTHREADS)
55 sigset_t signals;
56 sigemptyset(&signals);
57 pthread_sigmask(SIG_SETMASK, &signals, 0);
58#endif
59
60 GBAInit(&gba);
61 threadContext->gba = &gba;
62 gba.sync = &threadContext->sync;
63#ifdef USE_PTHREADS
64 pthread_setspecific(_contextKey, threadContext);
65#else
66 TlsSetValue(_contextKey, threadContext);
67#endif
68 if (threadContext->renderer) {
69 GBAVideoAssociateRenderer(&gba.video, threadContext->renderer);
70 }
71
72 if (threadContext->fd >= 0) {
73 if (threadContext->fname) {
74 char* dotPoint = strrchr(threadContext->fname, '.');
75 if (dotPoint > strrchr(threadContext->fname, '/') && dotPoint[1] && dotPoint[2] && dotPoint[3]) {
76 savedata = strdup(threadContext->fname);
77 dotPoint = strrchr(savedata, '.');
78 dotPoint[1] = 's';
79 dotPoint[2] = 'a';
80 dotPoint[3] = 'v';
81 dotPoint[4] = '\0';
82 } else if (dotPoint) {
83 savedata = malloc((dotPoint - threadContext->fname + 5) * sizeof(char));
84 strncpy(savedata, threadContext->fname, dotPoint - threadContext->fname + 1);
85 strcat(savedata, "sav");
86 } else {
87 savedata = malloc(strlen(threadContext->fname + 5));
88 strcpy(savedata, threadContext->fname);
89 strcat(savedata, "sav");
90 }
91 }
92 gba.savefile = savedata;
93 GBALoadROM(&gba, threadContext->fd, threadContext->fname);
94 }
95
96#ifdef USE_DEBUGGER
97 if (threadContext->useDebugger) {
98 threadContext->debugger = &debugger;
99 GBAAttachDebugger(&gba, &debugger);
100 } else {
101 threadContext->debugger = 0;
102 }
103#else
104 threadContext->debugger = 0;
105#endif
106
107 gba.keySource = &threadContext->activeKeys;
108
109 if (threadContext->startCallback) {
110 threadContext->startCallback(threadContext);
111 }
112
113 _changeState(threadContext, THREAD_RUNNING, 1);
114
115 while (threadContext->state < THREAD_EXITING) {
116#ifdef USE_DEBUGGER
117 if (threadContext->useDebugger) {
118 ARMDebuggerRun(&debugger);
119 if (debugger.state == DEBUGGER_SHUTDOWN) {
120 _changeState(threadContext, THREAD_EXITING, 0);
121 }
122 } else {
123#endif
124 while (threadContext->state == THREAD_RUNNING) {
125 ARMRun(&gba.cpu);
126 }
127#ifdef USE_DEBUGGER
128 }
129#endif
130 MutexLock(&threadContext->stateMutex);
131 while (threadContext->state == THREAD_PAUSED) {
132 ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
133 }
134 MutexUnlock(&threadContext->stateMutex);
135 }
136
137 while (threadContext->state != THREAD_SHUTDOWN) {
138 _changeState(threadContext, THREAD_SHUTDOWN, 0);
139 }
140
141 if (threadContext->cleanCallback) {
142 threadContext->cleanCallback(threadContext);
143 }
144
145 GBADeinit(&gba);
146
147 ConditionWake(&threadContext->sync.videoFrameAvailableCond);
148 ConditionWake(&threadContext->sync.audioRequiredCond);
149 free(savedata);
150
151 return 0;
152}
153
154int GBAThreadStart(struct GBAThread* threadContext) {
155 // TODO: error check
156 threadContext->activeKeys = 0;
157 threadContext->state = THREAD_INITIALIZED;
158 threadContext->sync.videoFrameOn = 1;
159 threadContext->sync.videoFrameSkip = 0;
160
161 threadContext->rewindBufferNext = threadContext->rewindBufferInterval;
162 threadContext->rewindBufferSize = 0;
163 if (threadContext->rewindBufferCapacity) {
164 threadContext->rewindBuffer = calloc(threadContext->rewindBufferCapacity, sizeof(void*));
165 }
166
167 MutexInit(&threadContext->stateMutex);
168 ConditionInit(&threadContext->stateCond);
169
170 MutexInit(&threadContext->sync.videoFrameMutex);
171 ConditionInit(&threadContext->sync.videoFrameAvailableCond);
172 ConditionInit(&threadContext->sync.videoFrameRequiredCond);
173 MutexInit(&threadContext->sync.audioBufferMutex);
174 ConditionInit(&threadContext->sync.audioRequiredCond);
175
176#ifndef _WIN32
177 sigset_t signals;
178 sigemptyset(&signals);
179 sigaddset(&signals, SIGINT);
180 sigaddset(&signals, SIGTRAP);
181 pthread_sigmask(SIG_BLOCK, &signals, 0);
182#endif
183
184 MutexLock(&threadContext->stateMutex);
185 ThreadCreate(&threadContext->thread, _GBAThreadRun, threadContext);
186 while (threadContext->state < THREAD_RUNNING) {
187 ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
188 }
189 MutexUnlock(&threadContext->stateMutex);
190
191 return 0;
192}
193
194void GBAThreadJoin(struct GBAThread* threadContext) {
195 MutexLock(&threadContext->sync.videoFrameMutex);
196 threadContext->sync.videoFrameWait = 0;
197 ConditionWake(&threadContext->sync.videoFrameRequiredCond);
198 MutexUnlock(&threadContext->sync.videoFrameMutex);
199
200 ThreadJoin(threadContext->thread);
201
202 MutexDeinit(&threadContext->stateMutex);
203 ConditionDeinit(&threadContext->stateCond);
204
205 MutexDeinit(&threadContext->sync.videoFrameMutex);
206 ConditionWake(&threadContext->sync.videoFrameAvailableCond);
207 ConditionDeinit(&threadContext->sync.videoFrameAvailableCond);
208 ConditionWake(&threadContext->sync.videoFrameRequiredCond);
209 ConditionDeinit(&threadContext->sync.videoFrameRequiredCond);
210
211 ConditionWake(&threadContext->sync.audioRequiredCond);
212 ConditionDeinit(&threadContext->sync.audioRequiredCond);
213 MutexDeinit(&threadContext->sync.audioBufferMutex);
214
215 int i;
216 for (i = 0; i < threadContext->rewindBufferCapacity; ++i) {
217 if (threadContext->rewindBuffer[i]) {
218 GBADeallocateState(threadContext->rewindBuffer[i]);
219 }
220 }
221 free(threadContext->rewindBuffer);
222}
223
224void GBAThreadPause(struct GBAThread* threadContext) {
225 int frameOn = 1;
226 MutexLock(&threadContext->stateMutex);
227 if (threadContext->state == THREAD_RUNNING) {
228 if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
229 threadContext->debugger->state = DEBUGGER_EXITING;
230 }
231 threadContext->state = THREAD_PAUSED;
232 frameOn = 0;
233 }
234 MutexUnlock(&threadContext->stateMutex);
235 MutexLock(&threadContext->sync.videoFrameMutex);
236 if (frameOn != threadContext->sync.videoFrameOn) {
237 threadContext->sync.videoFrameOn = frameOn;
238 ConditionWake(&threadContext->sync.videoFrameAvailableCond);
239 }
240 MutexUnlock(&threadContext->sync.videoFrameMutex);
241}
242
243void GBAThreadUnpause(struct GBAThread* threadContext) {
244 int frameOn = 1;
245 MutexLock(&threadContext->stateMutex);
246 if (threadContext->state == THREAD_PAUSED) {
247 threadContext->state = THREAD_RUNNING;
248 ConditionWake(&threadContext->stateCond);
249 }
250 MutexUnlock(&threadContext->stateMutex);
251 MutexLock(&threadContext->sync.videoFrameMutex);
252 if (frameOn != threadContext->sync.videoFrameOn) {
253 threadContext->sync.videoFrameOn = frameOn;
254 ConditionWake(&threadContext->sync.videoFrameAvailableCond);
255 }
256 MutexUnlock(&threadContext->sync.videoFrameMutex);
257}
258
259void GBAThreadTogglePause(struct GBAThread* threadContext) {
260 int frameOn = 1;
261 MutexLock(&threadContext->stateMutex);
262 if (threadContext->state == THREAD_PAUSED) {
263 threadContext->state = THREAD_RUNNING;
264 ConditionWake(&threadContext->stateCond);
265 } else if (threadContext->state == THREAD_RUNNING) {
266 if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
267 threadContext->debugger->state = DEBUGGER_EXITING;
268 }
269 threadContext->state = THREAD_PAUSED;
270 frameOn = 0;
271 }
272 MutexUnlock(&threadContext->stateMutex);
273 MutexLock(&threadContext->sync.videoFrameMutex);
274 if (frameOn != threadContext->sync.videoFrameOn) {
275 threadContext->sync.videoFrameOn = frameOn;
276 ConditionWake(&threadContext->sync.videoFrameAvailableCond);
277 }
278 MutexUnlock(&threadContext->sync.videoFrameMutex);
279}
280
281
282#ifdef USE_PTHREADS
283struct GBAThread* GBAThreadGetContext(void) {
284 pthread_once(&_contextOnce, _createTLS);
285 return pthread_getspecific(_contextKey);
286}
287#else
288struct GBAThread* GBAThreadGetContext(void) {
289 InitOnceExecuteOnce(&_contextOnce, _createTLS, NULL, 0);
290 return TlsGetValue(_contextKey);
291}
292#endif
293
294void GBASyncPostFrame(struct GBASync* sync) {
295 if (!sync) {
296 return;
297 }
298
299 MutexLock(&sync->videoFrameMutex);
300 ++sync->videoFramePending;
301 --sync->videoFrameSkip;
302 if (sync->videoFrameSkip < 0) {
303 ConditionWake(&sync->videoFrameAvailableCond);
304 while (sync->videoFrameWait && sync->videoFramePending) {
305 ConditionWait(&sync->videoFrameRequiredCond, &sync->videoFrameMutex);
306 }
307 }
308 MutexUnlock(&sync->videoFrameMutex);
309
310 struct GBAThread* thread = GBAThreadGetContext();
311 if (thread->rewindBuffer) {
312 --thread->rewindBufferNext;
313 if (thread->rewindBufferNext <= 0) {
314 thread->rewindBufferNext = thread->rewindBufferInterval;
315 GBARecordFrame(thread);
316 }
317 }
318 if (thread->frameCallback) {
319 thread->frameCallback(thread);
320 }
321}
322
323int GBASyncWaitFrameStart(struct GBASync* sync, int frameskip) {
324 if (!sync) {
325 return 1;
326 }
327
328 MutexLock(&sync->videoFrameMutex);
329 ConditionWake(&sync->videoFrameRequiredCond);
330 if (!sync->videoFrameOn) {
331 return 0;
332 }
333 ConditionWait(&sync->videoFrameAvailableCond, &sync->videoFrameMutex);
334 sync->videoFramePending = 0;
335 sync->videoFrameSkip = frameskip;
336 return 1;
337}
338
339void GBASyncWaitFrameEnd(struct GBASync* sync) {
340 if (!sync) {
341 return;
342 }
343
344 MutexUnlock(&sync->videoFrameMutex);
345}
346
347int GBASyncDrawingFrame(struct GBASync* sync) {
348 return sync->videoFrameSkip <= 0;
349}
350
351void GBASyncProduceAudio(struct GBASync* sync, int wait) {
352 if (sync->audioWait && wait) {
353 // TODO loop properly in event of spurious wakeups
354 ConditionWait(&sync->audioRequiredCond, &sync->audioBufferMutex);
355 }
356 MutexUnlock(&sync->audioBufferMutex);
357}
358
359void GBASyncLockAudio(struct GBASync* sync) {
360 MutexLock(&sync->audioBufferMutex);
361}
362
363void GBASyncConsumeAudio(struct GBASync* sync) {
364 ConditionWake(&sync->audioRequiredCond);
365 MutexUnlock(&sync->audioBufferMutex);
366}