all repos — mgba @ c8b04a2ca583d0f9f70f7bdff1c4d7d462e1ce4c

mGBA Game Boy Advance Emulator

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	} else {
166		threadContext->rewindBuffer = 0;
167	}
168
169	MutexInit(&threadContext->stateMutex);
170	ConditionInit(&threadContext->stateCond);
171
172	MutexInit(&threadContext->sync.videoFrameMutex);
173	ConditionInit(&threadContext->sync.videoFrameAvailableCond);
174	ConditionInit(&threadContext->sync.videoFrameRequiredCond);
175	MutexInit(&threadContext->sync.audioBufferMutex);
176	ConditionInit(&threadContext->sync.audioRequiredCond);
177
178#ifndef _WIN32
179	sigset_t signals;
180	sigemptyset(&signals);
181	sigaddset(&signals, SIGINT);
182	sigaddset(&signals, SIGTRAP);
183	pthread_sigmask(SIG_BLOCK, &signals, 0);
184#endif
185
186	MutexLock(&threadContext->stateMutex);
187	ThreadCreate(&threadContext->thread, _GBAThreadRun, threadContext);
188	while (threadContext->state < THREAD_RUNNING) {
189		ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
190	}
191	MutexUnlock(&threadContext->stateMutex);
192
193	return 0;
194}
195
196void GBAThreadJoin(struct GBAThread* threadContext) {
197	MutexLock(&threadContext->sync.videoFrameMutex);
198	threadContext->sync.videoFrameWait = 0;
199	ConditionWake(&threadContext->sync.videoFrameRequiredCond);
200	MutexUnlock(&threadContext->sync.videoFrameMutex);
201
202	ThreadJoin(threadContext->thread);
203
204	MutexDeinit(&threadContext->stateMutex);
205	ConditionDeinit(&threadContext->stateCond);
206
207	MutexDeinit(&threadContext->sync.videoFrameMutex);
208	ConditionWake(&threadContext->sync.videoFrameAvailableCond);
209	ConditionDeinit(&threadContext->sync.videoFrameAvailableCond);
210	ConditionWake(&threadContext->sync.videoFrameRequiredCond);
211	ConditionDeinit(&threadContext->sync.videoFrameRequiredCond);
212
213	ConditionWake(&threadContext->sync.audioRequiredCond);
214	ConditionDeinit(&threadContext->sync.audioRequiredCond);
215	MutexDeinit(&threadContext->sync.audioBufferMutex);
216
217	int i;
218	for (i = 0; i < threadContext->rewindBufferCapacity; ++i) {
219		if (threadContext->rewindBuffer[i]) {
220			GBADeallocateState(threadContext->rewindBuffer[i]);
221		}
222	}
223	free(threadContext->rewindBuffer);
224}
225
226void GBAThreadPause(struct GBAThread* threadContext) {
227	int frameOn = 1;
228	MutexLock(&threadContext->stateMutex);
229	if (threadContext->state == THREAD_RUNNING) {
230		if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
231			threadContext->debugger->state = DEBUGGER_EXITING;
232		}
233		threadContext->state = THREAD_PAUSED;
234		frameOn = 0;
235	}
236	MutexUnlock(&threadContext->stateMutex);
237	MutexLock(&threadContext->sync.videoFrameMutex);
238	if (frameOn != threadContext->sync.videoFrameOn) {
239		threadContext->sync.videoFrameOn = frameOn;
240		ConditionWake(&threadContext->sync.videoFrameAvailableCond);
241	}
242	MutexUnlock(&threadContext->sync.videoFrameMutex);
243}
244
245void GBAThreadUnpause(struct GBAThread* threadContext) {
246	int frameOn = 1;
247	MutexLock(&threadContext->stateMutex);
248	if (threadContext->state == THREAD_PAUSED) {
249		threadContext->state = THREAD_RUNNING;
250		ConditionWake(&threadContext->stateCond);
251	}
252	MutexUnlock(&threadContext->stateMutex);
253	MutexLock(&threadContext->sync.videoFrameMutex);
254	if (frameOn != threadContext->sync.videoFrameOn) {
255		threadContext->sync.videoFrameOn = frameOn;
256		ConditionWake(&threadContext->sync.videoFrameAvailableCond);
257	}
258	MutexUnlock(&threadContext->sync.videoFrameMutex);
259}
260
261int GBAThreadIsPaused(struct GBAThread* threadContext) {
262	int isPaused;
263	MutexLock(&threadContext->stateMutex);
264	isPaused = threadContext->state == THREAD_PAUSED;
265	MutexUnlock(&threadContext->stateMutex);
266	return isPaused;
267}
268
269void GBAThreadTogglePause(struct GBAThread* threadContext) {
270	int frameOn = 1;
271	MutexLock(&threadContext->stateMutex);
272	if (threadContext->state == THREAD_PAUSED) {
273		threadContext->state = THREAD_RUNNING;
274		ConditionWake(&threadContext->stateCond);
275	} else if (threadContext->state == THREAD_RUNNING) {
276		if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
277			threadContext->debugger->state = DEBUGGER_EXITING;
278		}
279		threadContext->state = THREAD_PAUSED;
280		frameOn = 0;
281	}
282	MutexUnlock(&threadContext->stateMutex);
283	MutexLock(&threadContext->sync.videoFrameMutex);
284	if (frameOn != threadContext->sync.videoFrameOn) {
285		threadContext->sync.videoFrameOn = frameOn;
286		ConditionWake(&threadContext->sync.videoFrameAvailableCond);
287	}
288	MutexUnlock(&threadContext->sync.videoFrameMutex);
289}
290
291#ifdef USE_PTHREADS
292struct GBAThread* GBAThreadGetContext(void) {
293	pthread_once(&_contextOnce, _createTLS);
294	return pthread_getspecific(_contextKey);
295}
296#else
297struct GBAThread* GBAThreadGetContext(void) {
298	InitOnceExecuteOnce(&_contextOnce, _createTLS, NULL, 0);
299	return TlsGetValue(_contextKey);
300}
301#endif
302
303void GBASyncPostFrame(struct GBASync* sync) {
304	if (!sync) {
305		return;
306	}
307
308	MutexLock(&sync->videoFrameMutex);
309	++sync->videoFramePending;
310	--sync->videoFrameSkip;
311	if (sync->videoFrameSkip < 0) {
312		ConditionWake(&sync->videoFrameAvailableCond);
313		while (sync->videoFrameWait && sync->videoFramePending) {
314			ConditionWait(&sync->videoFrameRequiredCond, &sync->videoFrameMutex);
315		}
316	}
317	MutexUnlock(&sync->videoFrameMutex);
318
319	struct GBAThread* thread = GBAThreadGetContext();
320	if (thread->rewindBuffer) {
321		--thread->rewindBufferNext;
322		if (thread->rewindBufferNext <= 0) {
323			thread->rewindBufferNext = thread->rewindBufferInterval;
324			GBARecordFrame(thread);
325		}
326	}
327	if (thread->frameCallback) {
328		thread->frameCallback(thread);
329	}
330}
331
332int GBASyncWaitFrameStart(struct GBASync* sync, int frameskip) {
333	if (!sync) {
334		return 1;
335	}
336
337	MutexLock(&sync->videoFrameMutex);
338	ConditionWake(&sync->videoFrameRequiredCond);
339	if (!sync->videoFrameOn) {
340		return 0;
341	}
342	ConditionWait(&sync->videoFrameAvailableCond, &sync->videoFrameMutex);
343	sync->videoFramePending = 0;
344	sync->videoFrameSkip = frameskip;
345	return 1;
346}
347
348void GBASyncWaitFrameEnd(struct GBASync* sync) {
349	if (!sync) {
350		return;
351	}
352
353	MutexUnlock(&sync->videoFrameMutex);
354}
355
356int GBASyncDrawingFrame(struct GBASync* sync) {
357	return sync->videoFrameSkip <= 0;
358}
359
360void GBASyncProduceAudio(struct GBASync* sync, int wait) {
361	if (sync->audioWait && wait) {
362		// TODO loop properly in event of spurious wakeups
363		ConditionWait(&sync->audioRequiredCond, &sync->audioBufferMutex);
364	}
365	MutexUnlock(&sync->audioBufferMutex);
366}
367
368void GBASyncLockAudio(struct GBASync* sync) {
369	MutexLock(&sync->audioBufferMutex);
370}
371
372void GBASyncConsumeAudio(struct GBASync* sync) {
373	ConditionWake(&sync->audioRequiredCond);
374	MutexUnlock(&sync->audioBufferMutex);
375}