all repos — mgba @ 3edb2da58dbb4a4f08be5657b40384d87a2f7018

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