all repos — mgba @ 270c1a35d765b833c2b5a2c5f46655f5a31915a9

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
  7#include <stdlib.h>
  8#include <signal.h>
  9
 10static pthread_key_t _contextKey;
 11static pthread_once_t _contextOnce = PTHREAD_ONCE_INIT;
 12
 13static void _createTLS(void) {
 14	pthread_key_create(&_contextKey, 0);
 15}
 16
 17static void* _GBAThreadRun(void* context) {
 18	pthread_once(&_contextOnce, _createTLS);
 19
 20#ifdef USE_DEBUGGER
 21	struct ARMDebugger debugger;
 22#endif
 23	struct GBA gba;
 24	struct GBAThread* threadContext = context;
 25	char* savedata = 0;
 26
 27#ifndef _WIN32
 28	sigset_t signals;
 29	sigfillset(&signals);
 30	pthread_sigmask(SIG_UNBLOCK, &signals, 0);
 31#endif
 32
 33	GBAInit(&gba);
 34	threadContext->gba = &gba;
 35	gba.sync = &threadContext->sync;
 36	pthread_setspecific(_contextKey, threadContext);
 37	if (threadContext->renderer) {
 38		GBAVideoAssociateRenderer(&gba.video, threadContext->renderer);
 39	}
 40
 41	if (threadContext->fd >= 0) {
 42		if (threadContext->fname) {
 43			char* dotPoint = strrchr(threadContext->fname, '.');
 44			if (dotPoint > strrchr(threadContext->fname, '/') && dotPoint[1] && dotPoint[2] && dotPoint[3]) {
 45				savedata = strdup(threadContext->fname);
 46				dotPoint = strrchr(savedata, '.');
 47				dotPoint[1] = 's';
 48				dotPoint[2] = 'a';
 49				dotPoint[3] = 'v';
 50				dotPoint[4] = '\0';
 51			} else if (dotPoint) {
 52				savedata = malloc((dotPoint - threadContext->fname + 5) * sizeof(char));
 53				strncpy(savedata, threadContext->fname, dotPoint - threadContext->fname + 1);
 54				strcat(savedata, "sav");
 55			} else {
 56				savedata = malloc(strlen(threadContext->fname + 5));
 57				strcpy(savedata, threadContext->fname);
 58				strcat(savedata, "sav");
 59			}
 60		}
 61		gba.savefile = savedata;
 62		GBALoadROM(&gba, threadContext->fd, threadContext->fname);
 63	}
 64
 65#ifdef USE_DEBUGGER
 66	if (threadContext->useDebugger) {
 67		threadContext->debugger = &debugger;
 68		GBAAttachDebugger(&gba, &debugger);
 69	} else {
 70		threadContext->debugger = 0;
 71	}
 72#else
 73	threadContext->debugger = 0;
 74#endif
 75
 76	gba.keySource = &threadContext->activeKeys;
 77
 78	if (threadContext->startCallback) {
 79		threadContext->startCallback(threadContext);
 80	}
 81
 82	pthread_mutex_lock(&threadContext->stateMutex);
 83	threadContext->state = THREAD_RUNNING;
 84	pthread_cond_broadcast(&threadContext->stateCond);
 85	pthread_mutex_unlock(&threadContext->stateMutex);
 86
 87	while (threadContext->state < THREAD_EXITING) {
 88#ifdef USE_DEBUGGER
 89		if (threadContext->useDebugger) {
 90			ARMDebuggerRun(&debugger);
 91			if (debugger.state == DEBUGGER_SHUTDOWN) {
 92				pthread_mutex_lock(&threadContext->stateMutex);
 93				threadContext->state = THREAD_EXITING;
 94				pthread_mutex_unlock(&threadContext->stateMutex);
 95			}
 96		} else {
 97#endif
 98			while (threadContext->state == THREAD_RUNNING) {
 99				ARMRun(&gba.cpu);
100			}
101#ifdef USE_DEBUGGER
102		}
103#endif
104		while (threadContext->state == THREAD_PAUSED) {
105			pthread_mutex_lock(&threadContext->stateMutex);
106			pthread_cond_wait(&threadContext->stateCond, &threadContext->stateMutex);
107			pthread_mutex_unlock(&threadContext->stateMutex);
108		}
109	}
110
111	while (threadContext->state != THREAD_SHUTDOWN) {
112		pthread_mutex_lock(&threadContext->stateMutex);
113		threadContext->state = THREAD_SHUTDOWN;
114		pthread_mutex_unlock(&threadContext->stateMutex);
115	}
116
117	if (threadContext->cleanCallback) {
118		threadContext->cleanCallback(threadContext);
119	}
120
121	GBADeinit(&gba);
122
123	pthread_cond_broadcast(&threadContext->sync.videoFrameAvailableCond);
124	pthread_cond_broadcast(&threadContext->sync.audioRequiredCond);
125	free(savedata);
126
127	return 0;
128}
129
130int GBAThreadStart(struct GBAThread* threadContext) {
131	// TODO: error check
132	pthread_mutex_init(&threadContext->stateMutex, 0);
133	pthread_cond_init(&threadContext->stateCond, 0);
134
135	pthread_mutex_init(&threadContext->sync.videoFrameMutex, 0);
136	pthread_cond_init(&threadContext->sync.videoFrameAvailableCond, 0);
137	pthread_cond_init(&threadContext->sync.videoFrameRequiredCond, 0);
138	pthread_cond_init(&threadContext->sync.audioRequiredCond, 0);
139
140	pthread_mutex_lock(&threadContext->stateMutex);
141	threadContext->activeKeys = 0;
142	threadContext->state = THREAD_INITIALIZED;
143	threadContext->sync.videoFrameOn = 1;
144	threadContext->sync.videoFrameSkip = 0;
145	pthread_create(&threadContext->thread, 0, _GBAThreadRun, threadContext);
146	pthread_cond_wait(&threadContext->stateCond, &threadContext->stateMutex);
147	pthread_mutex_unlock(&threadContext->stateMutex);
148
149	return 0;
150}
151
152void GBAThreadJoin(struct GBAThread* threadContext) {
153	pthread_mutex_lock(&threadContext->sync.videoFrameMutex);
154	threadContext->sync.videoFrameWait = 0;
155	pthread_cond_broadcast(&threadContext->sync.videoFrameRequiredCond);
156	pthread_mutex_unlock(&threadContext->sync.videoFrameMutex);
157
158	pthread_join(threadContext->thread, 0);
159
160	pthread_mutex_destroy(&threadContext->stateMutex);
161	pthread_cond_destroy(&threadContext->stateCond);
162
163	pthread_mutex_destroy(&threadContext->sync.videoFrameMutex);
164	pthread_cond_broadcast(&threadContext->sync.videoFrameAvailableCond);
165	pthread_cond_destroy(&threadContext->sync.videoFrameAvailableCond);
166	pthread_cond_broadcast(&threadContext->sync.videoFrameRequiredCond);
167	pthread_cond_destroy(&threadContext->sync.videoFrameRequiredCond);
168
169	pthread_cond_broadcast(&threadContext->sync.audioRequiredCond);
170	pthread_cond_destroy(&threadContext->sync.audioRequiredCond);
171}
172
173void GBAThreadPause(struct GBAThread* threadContext) {
174	int frameOn = 1;
175	pthread_mutex_lock(&threadContext->stateMutex);
176	if (threadContext->state == THREAD_RUNNING) {
177		if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
178			threadContext->debugger->state = DEBUGGER_EXITING;
179		}
180		threadContext->state = THREAD_PAUSED;
181		frameOn = 0;
182	}
183	pthread_mutex_unlock(&threadContext->stateMutex);
184	pthread_mutex_lock(&threadContext->sync.videoFrameMutex);
185	if (frameOn != threadContext->sync.videoFrameOn) {
186		threadContext->sync.videoFrameOn = frameOn;
187		pthread_cond_broadcast(&threadContext->sync.videoFrameAvailableCond);
188	}
189	pthread_mutex_unlock(&threadContext->sync.videoFrameMutex);
190}
191
192void GBAThreadUnpause(struct GBAThread* threadContext) {
193	int frameOn = 1;
194	pthread_mutex_lock(&threadContext->stateMutex);
195	if (threadContext->state == THREAD_PAUSED) {
196		threadContext->state = THREAD_RUNNING;
197		pthread_cond_broadcast(&threadContext->stateCond);
198	}
199	pthread_mutex_unlock(&threadContext->stateMutex);
200	pthread_mutex_lock(&threadContext->sync.videoFrameMutex);
201	if (frameOn != threadContext->sync.videoFrameOn) {
202		threadContext->sync.videoFrameOn = frameOn;
203		pthread_cond_broadcast(&threadContext->sync.videoFrameAvailableCond);
204	}
205	pthread_mutex_unlock(&threadContext->sync.videoFrameMutex);
206}
207
208void GBAThreadTogglePause(struct GBAThread* threadContext) {
209	int frameOn = 1;
210	pthread_mutex_lock(&threadContext->stateMutex);
211	if (threadContext->state == THREAD_PAUSED) {
212		threadContext->state = THREAD_RUNNING;
213		pthread_cond_broadcast(&threadContext->stateCond);
214	} else if (threadContext->state == THREAD_RUNNING) {
215		if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
216			threadContext->debugger->state = DEBUGGER_EXITING;
217		}
218		threadContext->state = THREAD_PAUSED;
219		frameOn = 0;
220	}
221	pthread_mutex_unlock(&threadContext->stateMutex);
222	pthread_mutex_lock(&threadContext->sync.videoFrameMutex);
223	if (frameOn != threadContext->sync.videoFrameOn) {
224		threadContext->sync.videoFrameOn = frameOn;
225		pthread_cond_broadcast(&threadContext->sync.videoFrameAvailableCond);
226	}
227	pthread_mutex_unlock(&threadContext->sync.videoFrameMutex);
228}
229
230struct GBAThread* GBAThreadGetContext(void) {
231	pthread_once(&_contextOnce, _createTLS);
232	return pthread_getspecific(_contextKey);
233}
234
235void GBASyncPostFrame(struct GBASync* sync) {
236	if (!sync) {
237		return;
238	}
239
240	pthread_mutex_lock(&sync->videoFrameMutex);
241	++sync->videoFramePending;
242	--sync->videoFrameSkip;
243	if (sync->videoFrameSkip < 0) {
244		pthread_cond_broadcast(&sync->videoFrameAvailableCond);
245		if (sync->videoFrameWait) {
246			pthread_cond_wait(&sync->videoFrameRequiredCond, &sync->videoFrameMutex);
247		}
248	}
249	pthread_mutex_unlock(&sync->videoFrameMutex);
250}
251
252int GBASyncWaitFrameStart(struct GBASync* sync, int frameskip) {
253	if (!sync) {
254		return 1;
255	}
256
257	pthread_mutex_lock(&sync->videoFrameMutex);
258	pthread_cond_broadcast(&sync->videoFrameRequiredCond);
259	if (!sync->videoFrameOn) {
260		return 0;
261	}
262	pthread_cond_wait(&sync->videoFrameAvailableCond, &sync->videoFrameMutex);
263	sync->videoFramePending = 0;
264	sync->videoFrameSkip = frameskip;
265	return 1;
266}
267
268void GBASyncWaitFrameEnd(struct GBASync* sync) {
269	if (!sync) {
270		return;
271	}
272
273	pthread_mutex_unlock(&sync->videoFrameMutex);
274}
275
276int GBASyncDrawingFrame(struct GBASync* sync) {
277	return sync->videoFrameSkip <= 0;
278}
279
280void GBASyncProduceAudio(struct GBASync* sync, pthread_mutex_t* mutex) {
281	if (&sync->audioWait) {
282		pthread_cond_wait(&sync->audioRequiredCond, mutex);
283	}
284}
285
286void GBASyncConsumeAudio(struct GBASync* sync) {
287	pthread_cond_broadcast(&sync->audioRequiredCond);
288}