all repos — mgba @ a72c77a3df79975cdb40a5fbd7e88df7b3934c46

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	}
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
259int GBAThreadIsPaused(struct GBAThread* threadContext) {
260	int isPaused;
261	MutexLock(&threadContext->stateMutex);
262	isPaused = threadContext->state == THREAD_PAUSED;
263	MutexUnlock(&threadContext->stateMutex);
264	return isPaused;
265}
266
267void GBAThreadTogglePause(struct GBAThread* threadContext) {
268	int frameOn = 1;
269	MutexLock(&threadContext->stateMutex);
270	if (threadContext->state == THREAD_PAUSED) {
271		threadContext->state = THREAD_RUNNING;
272		ConditionWake(&threadContext->stateCond);
273	} else if (threadContext->state == THREAD_RUNNING) {
274		if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
275			threadContext->debugger->state = DEBUGGER_EXITING;
276		}
277		threadContext->state = THREAD_PAUSED;
278		frameOn = 0;
279	}
280	MutexUnlock(&threadContext->stateMutex);
281	MutexLock(&threadContext->sync.videoFrameMutex);
282	if (frameOn != threadContext->sync.videoFrameOn) {
283		threadContext->sync.videoFrameOn = frameOn;
284		ConditionWake(&threadContext->sync.videoFrameAvailableCond);
285	}
286	MutexUnlock(&threadContext->sync.videoFrameMutex);
287}
288
289#ifdef USE_PTHREADS
290struct GBAThread* GBAThreadGetContext(void) {
291	pthread_once(&_contextOnce, _createTLS);
292	return pthread_getspecific(_contextKey);
293}
294#else
295struct GBAThread* GBAThreadGetContext(void) {
296	InitOnceExecuteOnce(&_contextOnce, _createTLS, NULL, 0);
297	return TlsGetValue(_contextKey);
298}
299#endif
300
301void GBASyncPostFrame(struct GBASync* sync) {
302	if (!sync) {
303		return;
304	}
305
306	MutexLock(&sync->videoFrameMutex);
307	++sync->videoFramePending;
308	--sync->videoFrameSkip;
309	if (sync->videoFrameSkip < 0) {
310		ConditionWake(&sync->videoFrameAvailableCond);
311		while (sync->videoFrameWait && sync->videoFramePending) {
312			ConditionWait(&sync->videoFrameRequiredCond, &sync->videoFrameMutex);
313		}
314	}
315	MutexUnlock(&sync->videoFrameMutex);
316
317	struct GBAThread* thread = GBAThreadGetContext();
318	if (thread->rewindBuffer) {
319		--thread->rewindBufferNext;
320		if (thread->rewindBufferNext <= 0) {
321			thread->rewindBufferNext = thread->rewindBufferInterval;
322			GBARecordFrame(thread);
323		}
324	}
325	if (thread->frameCallback) {
326		thread->frameCallback(thread);
327	}
328}
329
330int GBASyncWaitFrameStart(struct GBASync* sync, int frameskip) {
331	if (!sync) {
332		return 1;
333	}
334
335	MutexLock(&sync->videoFrameMutex);
336	ConditionWake(&sync->videoFrameRequiredCond);
337	if (!sync->videoFrameOn) {
338		return 0;
339	}
340	ConditionWait(&sync->videoFrameAvailableCond, &sync->videoFrameMutex);
341	sync->videoFramePending = 0;
342	sync->videoFrameSkip = frameskip;
343	return 1;
344}
345
346void GBASyncWaitFrameEnd(struct GBASync* sync) {
347	if (!sync) {
348		return;
349	}
350
351	MutexUnlock(&sync->videoFrameMutex);
352}
353
354int GBASyncDrawingFrame(struct GBASync* sync) {
355	return sync->videoFrameSkip <= 0;
356}
357
358void GBASyncProduceAudio(struct GBASync* sync, int wait) {
359	if (sync->audioWait && wait) {
360		// TODO loop properly in event of spurious wakeups
361		ConditionWait(&sync->audioRequiredCond, &sync->audioBufferMutex);
362	}
363	MutexUnlock(&sync->audioBufferMutex);
364}
365
366void GBASyncLockAudio(struct GBASync* sync) {
367	MutexLock(&sync->audioBufferMutex);
368}
369
370void GBASyncConsumeAudio(struct GBASync* sync) {
371	ConditionWake(&sync->audioRequiredCond);
372	MutexUnlock(&sync->audioBufferMutex);
373}