all repos — mgba @ a38beac307b7b42dbc5ad5806a92b91396bea555

mGBA Game Boy Advance Emulator

src/gba/supervisor/thread.c (view raw)

  1/* Copyright (c) 2013-2015 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "thread.h"
  7
  8#include "arm.h"
  9#include "gba/gba.h"
 10#include "gba/cheats.h"
 11#include "gba/serialize.h"
 12#include "gba/context/config.h"
 13#include "gba/rr/mgm.h"
 14#include "gba/rr/vbm.h"
 15
 16#include "debugger/debugger.h"
 17
 18#include "util/patch.h"
 19#include "util/vfs.h"
 20
 21#include "platform/commandline.h"
 22
 23#include <signal.h>
 24
 25static const float _defaultFPSTarget = 60.f;
 26
 27#ifndef DISABLE_THREADING
 28
 29static bool _reloadDirectories(struct GBAThread* threadContext) {
 30	GBADirectorySetDetachBase(&threadContext->dirs);
 31
 32	char basename[PATH_MAX];
 33	if (threadContext->fname) {
 34		char dirname[PATH_MAX];
 35		separatePath(threadContext->fname, dirname, basename, 0);
 36		GBADirectorySetAttachBase(&threadContext->dirs, VDirOpen(dirname));
 37	} else {
 38		return false;
 39	}
 40
 41	char path[PATH_MAX];
 42	snprintf(path, sizeof(path), "%s.sav", basename);
 43	threadContext->save = threadContext->dirs.save->openFile(threadContext->dirs.save, path, O_CREAT | O_RDWR);
 44
 45	if (!threadContext->patch) {
 46		snprintf(path, sizeof(path), "%s.ups", basename);
 47		threadContext->patch = threadContext->dirs.patch->openFile(threadContext->dirs.patch, path, O_RDONLY);
 48	}
 49	if (!threadContext->patch) {
 50		snprintf(path, sizeof(path), "%s.ips", basename);
 51		threadContext->patch = threadContext->dirs.patch->openFile(threadContext->dirs.patch, path, O_RDONLY);
 52	}
 53	if (!threadContext->patch) {
 54		snprintf(path, sizeof(path), "%s.bps", basename);
 55		threadContext->patch = threadContext->dirs.patch->openFile(threadContext->dirs.patch, path, O_RDONLY);
 56	}
 57	return true;
 58}
 59
 60#ifdef USE_PTHREADS
 61static pthread_key_t _contextKey;
 62static pthread_once_t _contextOnce = PTHREAD_ONCE_INIT;
 63
 64static void _createTLS(void) {
 65	pthread_key_create(&_contextKey, 0);
 66}
 67#elif _WIN32
 68static DWORD _contextKey;
 69static INIT_ONCE _contextOnce = INIT_ONCE_STATIC_INIT;
 70
 71static BOOL CALLBACK _createTLS(PINIT_ONCE once, PVOID param, PVOID* context) {
 72	UNUSED(once);
 73	UNUSED(param);
 74	UNUSED(context);
 75	_contextKey = TlsAlloc();
 76	return TRUE;
 77}
 78#endif
 79
 80static void _changeState(struct GBAThread* threadContext, enum ThreadState newState, bool broadcast) {
 81	MutexLock(&threadContext->stateMutex);
 82	threadContext->state = newState;
 83	if (broadcast) {
 84		ConditionWake(&threadContext->stateCond);
 85	}
 86	MutexUnlock(&threadContext->stateMutex);
 87}
 88
 89static void _waitOnInterrupt(struct GBAThread* threadContext) {
 90	while (threadContext->state == THREAD_INTERRUPTED) {
 91		ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
 92	}
 93}
 94
 95static void _waitUntilNotState(struct GBAThread* threadContext, enum ThreadState oldState) {
 96	MutexLock(&threadContext->sync.videoFrameMutex);
 97	bool videoFrameWait = threadContext->sync.videoFrameWait;
 98	threadContext->sync.videoFrameWait = false;
 99	MutexUnlock(&threadContext->sync.videoFrameMutex);
100
101	while (threadContext->state == oldState) {
102		MutexUnlock(&threadContext->stateMutex);
103
104		if (!MutexTryLock(&threadContext->sync.videoFrameMutex)) {
105			ConditionWake(&threadContext->sync.videoFrameRequiredCond);
106			MutexUnlock(&threadContext->sync.videoFrameMutex);
107		}
108
109		if (!MutexTryLock(&threadContext->sync.audioBufferMutex)) {
110			ConditionWake(&threadContext->sync.audioRequiredCond);
111			MutexUnlock(&threadContext->sync.audioBufferMutex);
112		}
113
114		MutexLock(&threadContext->stateMutex);
115		ConditionWake(&threadContext->stateCond);
116	}
117
118	MutexLock(&threadContext->sync.videoFrameMutex);
119	threadContext->sync.videoFrameWait = videoFrameWait;
120	MutexUnlock(&threadContext->sync.videoFrameMutex);
121}
122
123static void _pauseThread(struct GBAThread* threadContext, bool onThread) {
124	threadContext->state = THREAD_PAUSING;
125	if (!onThread) {
126		_waitUntilNotState(threadContext, THREAD_PAUSING);
127	}
128}
129
130struct GBAThreadStop {
131	struct GBAStopCallback d;
132	struct GBAThread* p;
133};
134
135static void _stopCallback(struct GBAStopCallback* stop) {
136	struct GBAThreadStop* callback = (struct GBAThreadStop*) stop;
137	if (callback->p->stopCallback(callback->p)) {
138		_changeState(callback->p, THREAD_EXITING, false);
139	}
140}
141
142static THREAD_ENTRY _GBAThreadRun(void* context) {
143#ifdef USE_PTHREADS
144	pthread_once(&_contextOnce, _createTLS);
145#elif _WIN32
146	InitOnceExecuteOnce(&_contextOnce, _createTLS, NULL, 0);
147#endif
148
149	struct GBA gba;
150	struct ARMCore cpu;
151	struct Patch patch;
152	struct GBACheatDevice cheatDevice;
153	struct GBAThread* threadContext = context;
154	struct ARMComponent* components[GBA_COMPONENT_MAX] = { 0 };
155	struct GBARRContext* movie = 0;
156	int numComponents = GBA_COMPONENT_MAX;
157
158	ThreadSetName("CPU Thread");
159
160#if !defined(_WIN32) && defined(USE_PTHREADS)
161	sigset_t signals;
162	sigemptyset(&signals);
163	pthread_sigmask(SIG_SETMASK, &signals, 0);
164#endif
165
166	GBACreate(&gba);
167	ARMSetComponents(&cpu, &gba.d, numComponents, components);
168	ARMInit(&cpu);
169	gba.sync = &threadContext->sync;
170	threadContext->gba = &gba;
171	threadContext->cpu = &cpu;
172	gba.logLevel = threadContext->logLevel;
173	gba.logHandler = threadContext->logHandler;
174	gba.stream = threadContext->stream;
175	gba.video.frameskip = threadContext->frameskip;
176
177	struct GBAThreadStop stop;
178	if (threadContext->stopCallback) {
179		stop.d.stop = _stopCallback;
180		stop.p = threadContext;
181		gba.stopCallback = &stop.d;
182	}
183
184	gba.idleOptimization = threadContext->idleOptimization;
185#ifdef USE_PTHREADS
186	pthread_setspecific(_contextKey, threadContext);
187#elif _WIN32
188	TlsSetValue(_contextKey, threadContext);
189#endif
190
191	if (threadContext->audioBuffers) {
192		GBAAudioResizeBuffer(&gba.audio, threadContext->audioBuffers);
193	} else {
194		threadContext->audioBuffers = GBA_AUDIO_SAMPLES;
195	}
196
197	if (threadContext->renderer) {
198		GBAVideoAssociateRenderer(&gba.video, threadContext->renderer);
199	}
200
201	if (threadContext->rom) {
202		if (GBAIsMB(threadContext->rom)) {
203			GBALoadMB(&gba, threadContext->rom, threadContext->fname);
204		} else {
205			GBALoadROM(&gba, threadContext->rom, threadContext->save, threadContext->fname);
206		}
207
208		struct GBACartridgeOverride override;
209		const struct GBACartridge* cart = (const struct GBACartridge*) gba.pristineRom;
210		memcpy(override.id, &cart->id, sizeof(override.id));
211		if (GBAOverrideFind(threadContext->overrides, &override)) {
212			GBAOverrideApply(&gba, &override);
213		}
214		if (threadContext->hasOverride) {
215			GBAOverrideApply(&gba, &threadContext->override);
216		}
217
218		if (threadContext->patch && loadPatch(threadContext->patch, &patch)) {
219			GBAApplyPatch(&gba, &patch);
220		}
221	}
222
223	if (threadContext->bios && GBAIsBIOS(threadContext->bios)) {
224		GBALoadBIOS(&gba, threadContext->bios);
225	}
226
227	if (threadContext->movie) {
228		struct VDir* movieDir = VDirOpen(threadContext->movie);
229		if (!movieDir) {
230			movieDir = VDirOpenArchive(threadContext->movie);
231		}
232		if (movieDir) {
233			struct GBAMGMContext* mgm = malloc(sizeof(*mgm));
234			GBAMGMContextCreate(mgm);
235			if (!GBAMGMSetStream(mgm, movieDir)) {
236				mgm->d.destroy(&mgm->d);
237			} else {
238				movie = &mgm->d;
239			}
240		} else {
241			struct VFile* movieFile = VFileOpen(threadContext->movie, O_RDONLY);
242			if (movieFile) {
243				struct GBAVBMContext* vbm = malloc(sizeof(*vbm));
244				GBAVBMContextCreate(vbm);
245				if (!GBAVBMSetStream(vbm, movieFile)) {
246					vbm->d.destroy(&vbm->d);
247				} else {
248					movie = &vbm->d;
249				}
250			}
251		}
252	}
253
254	ARMReset(&cpu);
255
256	if (movie) {
257		gba.rr = movie;
258		movie->startPlaying(movie, false);
259		GBARRInitPlay(&gba);
260	}
261
262	if (threadContext->skipBios && gba.pristineRom) {
263		GBASkipBIOS(&gba);
264	}
265
266	if (!threadContext->cheats) {
267		GBACheatDeviceCreate(&cheatDevice);
268		threadContext->cheats = &cheatDevice;
269	}
270	if (threadContext->cheatsFile) {
271		GBACheatParseFile(threadContext->cheats, threadContext->cheatsFile);
272	}
273	GBACheatAttachDevice(&gba, threadContext->cheats);
274
275	if (threadContext->debugger) {
276		threadContext->debugger->log = GBADebuggerLogShim;
277		GBAAttachDebugger(&gba, threadContext->debugger);
278		ARMDebuggerEnter(threadContext->debugger, DEBUGGER_ENTER_ATTACHED, 0);
279	}
280
281	GBASIOSetDriverSet(&gba.sio, &threadContext->sioDrivers);
282
283	if (threadContext->volume == 0) {
284		threadContext->volume = GBA_AUDIO_VOLUME_MAX;
285	}
286	if (threadContext->mute) {
287		gba.audio.masterVolume = 0;
288	} else {
289		gba.audio.masterVolume = threadContext->volume;
290	}
291
292	gba.keySource = &threadContext->activeKeys;
293
294	if (threadContext->startCallback) {
295		threadContext->startCallback(threadContext);
296	}
297
298	_changeState(threadContext, THREAD_RUNNING, true);
299
300	while (threadContext->state < THREAD_EXITING) {
301		if (threadContext->debugger) {
302			struct ARMDebugger* debugger = threadContext->debugger;
303			ARMDebuggerRun(debugger);
304			if (debugger->state == DEBUGGER_SHUTDOWN) {
305				_changeState(threadContext, THREAD_EXITING, false);
306			}
307		} else {
308			while (threadContext->state == THREAD_RUNNING) {
309				ARMRunLoop(&cpu);
310			}
311		}
312
313		int resetScheduled = 0;
314		MutexLock(&threadContext->stateMutex);
315		while (threadContext->state > THREAD_RUNNING && threadContext->state < THREAD_EXITING) {
316			if (threadContext->state == THREAD_PAUSING) {
317				threadContext->state = THREAD_PAUSED;
318				ConditionWake(&threadContext->stateCond);
319			}
320			if (threadContext->state == THREAD_INTERRUPTING) {
321				threadContext->state = THREAD_INTERRUPTED;
322				ConditionWake(&threadContext->stateCond);
323			}
324			if (threadContext->state == THREAD_RUN_ON) {
325				if (threadContext->run) {
326					threadContext->run(threadContext);
327				}
328				threadContext->state = threadContext->savedState;
329				ConditionWake(&threadContext->stateCond);
330			}
331			if (threadContext->state == THREAD_RESETING) {
332				threadContext->state = THREAD_RUNNING;
333				resetScheduled = 1;
334			}
335			while (threadContext->state == THREAD_PAUSED || threadContext->state == THREAD_INTERRUPTED) {
336				ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
337			}
338		}
339		MutexUnlock(&threadContext->stateMutex);
340		if (resetScheduled) {
341			ARMReset(&cpu);
342			if (threadContext->skipBios && gba.pristineRom) {
343				GBASkipBIOS(&gba);
344			}
345		}
346	}
347
348	while (threadContext->state < THREAD_SHUTDOWN) {
349		_changeState(threadContext, THREAD_SHUTDOWN, false);
350	}
351
352	if (threadContext->cleanCallback) {
353		threadContext->cleanCallback(threadContext);
354	}
355
356	threadContext->gba = 0;
357	ARMDeinit(&cpu);
358	GBADestroy(&gba);
359	if (&cheatDevice == threadContext->cheats) {
360		GBACheatDeviceDestroy(&cheatDevice);
361	}
362
363	if (movie) {
364		movie->destroy(movie);
365		free(movie);
366	}
367
368	threadContext->sync.videoFrameOn = false;
369	ConditionWake(&threadContext->sync.videoFrameAvailableCond);
370	ConditionWake(&threadContext->sync.audioRequiredCond);
371
372	return 0;
373}
374
375void GBAMapOptionsToContext(const struct GBAOptions* opts, struct GBAThread* threadContext) {
376	if (opts->useBios) {
377		threadContext->bios = VFileOpen(opts->bios, O_RDONLY);
378	} else {
379		threadContext->bios = 0;
380	}
381	threadContext->frameskip = opts->frameskip;
382	threadContext->volume = opts->volume;
383	threadContext->mute = opts->mute;
384	threadContext->logLevel = opts->logLevel;
385	if (opts->rewindEnable) {
386		threadContext->rewindBufferCapacity = opts->rewindBufferCapacity;
387		threadContext->rewindBufferInterval = opts->rewindBufferInterval;
388	} else {
389		threadContext->rewindBufferCapacity = 0;
390	}
391	threadContext->skipBios = opts->skipBios;
392	threadContext->sync.audioWait = opts->audioSync;
393	threadContext->sync.videoFrameWait = opts->videoSync;
394
395	if (opts->fpsTarget) {
396		threadContext->fpsTarget = opts->fpsTarget;
397	}
398
399	if (opts->audioBuffers) {
400		threadContext->audioBuffers = opts->audioBuffers;
401	}
402
403	threadContext->idleOptimization = opts->idleOptimization;
404
405	GBADirectorySetMapOptions(&threadContext->dirs, opts);
406}
407
408void GBAMapArgumentsToContext(const struct GBAArguments* args, struct GBAThread* threadContext) {
409	GBAThreadLoadROM(threadContext, args->fname);
410	threadContext->fname = args->fname;
411	threadContext->patch = VFileOpen(args->patch, O_RDONLY);
412	threadContext->cheatsFile = VFileOpen(args->cheatsFile, O_RDONLY);
413	threadContext->movie = args->movie;
414}
415
416bool GBAThreadStart(struct GBAThread* threadContext) {
417	// TODO: error check
418	threadContext->activeKeys = 0;
419	threadContext->state = THREAD_INITIALIZED;
420	threadContext->sync.videoFrameOn = true;
421
422	threadContext->rewindBuffer = 0;
423	threadContext->rewindScreenBuffer = 0;
424	int newCapacity = threadContext->rewindBufferCapacity;
425	int newInterval = threadContext->rewindBufferInterval;
426	threadContext->rewindBufferCapacity = 0;
427	threadContext->rewindBufferInterval = 0;
428	GBARewindSettingsChanged(threadContext, newCapacity, newInterval);
429
430	if (!threadContext->fpsTarget) {
431		threadContext->fpsTarget = _defaultFPSTarget;
432	}
433
434	bool bootBios = threadContext->bootBios && threadContext->bios;
435
436	if (threadContext->rom && (!GBAIsROM(threadContext->rom) || bootBios)) {
437		threadContext->rom->close(threadContext->rom);
438		threadContext->rom = 0;
439	}
440
441	if (!threadContext->rom && !bootBios) {
442		threadContext->state = THREAD_SHUTDOWN;
443		return false;
444	}
445
446	_reloadDirectories(threadContext);
447
448	MutexInit(&threadContext->stateMutex);
449	ConditionInit(&threadContext->stateCond);
450
451	MutexInit(&threadContext->sync.videoFrameMutex);
452	ConditionInit(&threadContext->sync.videoFrameAvailableCond);
453	ConditionInit(&threadContext->sync.videoFrameRequiredCond);
454	MutexInit(&threadContext->sync.audioBufferMutex);
455	ConditionInit(&threadContext->sync.audioRequiredCond);
456
457	threadContext->interruptDepth = 0;
458
459#ifdef USE_PTHREADS
460	sigset_t signals;
461	sigemptyset(&signals);
462	sigaddset(&signals, SIGINT);
463	sigaddset(&signals, SIGTRAP);
464	pthread_sigmask(SIG_BLOCK, &signals, 0);
465#endif
466
467	MutexLock(&threadContext->stateMutex);
468	ThreadCreate(&threadContext->thread, _GBAThreadRun, threadContext);
469	while (threadContext->state < THREAD_RUNNING) {
470		ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
471	}
472	MutexUnlock(&threadContext->stateMutex);
473
474	return true;
475}
476
477bool GBAThreadHasStarted(struct GBAThread* threadContext) {
478	bool hasStarted;
479	MutexLock(&threadContext->stateMutex);
480	hasStarted = threadContext->state > THREAD_INITIALIZED;
481	MutexUnlock(&threadContext->stateMutex);
482	return hasStarted;
483}
484
485bool GBAThreadHasExited(struct GBAThread* threadContext) {
486	bool hasExited;
487	MutexLock(&threadContext->stateMutex);
488	hasExited = threadContext->state > THREAD_EXITING;
489	MutexUnlock(&threadContext->stateMutex);
490	return hasExited;
491}
492
493bool GBAThreadHasCrashed(struct GBAThread* threadContext) {
494	bool hasExited;
495	MutexLock(&threadContext->stateMutex);
496	hasExited = threadContext->state == THREAD_CRASHED;
497	MutexUnlock(&threadContext->stateMutex);
498	return hasExited;
499}
500
501void GBAThreadEnd(struct GBAThread* threadContext) {
502	MutexLock(&threadContext->stateMutex);
503	_waitOnInterrupt(threadContext);
504	threadContext->state = THREAD_EXITING;
505	if (threadContext->gba) {
506		threadContext->gba->cpu->halted = false;
507	}
508	ConditionWake(&threadContext->stateCond);
509	MutexUnlock(&threadContext->stateMutex);
510	MutexLock(&threadContext->sync.audioBufferMutex);
511	threadContext->sync.audioWait = 0;
512	ConditionWake(&threadContext->sync.audioRequiredCond);
513	MutexUnlock(&threadContext->sync.audioBufferMutex);
514
515	MutexLock(&threadContext->sync.videoFrameMutex);
516	threadContext->sync.videoFrameWait = false;
517	threadContext->sync.videoFrameOn = false;
518	ConditionWake(&threadContext->sync.videoFrameRequiredCond);
519	ConditionWake(&threadContext->sync.videoFrameAvailableCond);
520	MutexUnlock(&threadContext->sync.videoFrameMutex);
521}
522
523void GBAThreadReset(struct GBAThread* threadContext) {
524	MutexLock(&threadContext->stateMutex);
525	_waitOnInterrupt(threadContext);
526	threadContext->state = THREAD_RESETING;
527	ConditionWake(&threadContext->stateCond);
528	MutexUnlock(&threadContext->stateMutex);
529}
530
531void GBAThreadJoin(struct GBAThread* threadContext) {
532	ThreadJoin(threadContext->thread);
533
534	MutexDeinit(&threadContext->stateMutex);
535	ConditionDeinit(&threadContext->stateCond);
536
537	MutexDeinit(&threadContext->sync.videoFrameMutex);
538	ConditionWake(&threadContext->sync.videoFrameAvailableCond);
539	ConditionDeinit(&threadContext->sync.videoFrameAvailableCond);
540	ConditionWake(&threadContext->sync.videoFrameRequiredCond);
541	ConditionDeinit(&threadContext->sync.videoFrameRequiredCond);
542
543	ConditionWake(&threadContext->sync.audioRequiredCond);
544	ConditionDeinit(&threadContext->sync.audioRequiredCond);
545	MutexDeinit(&threadContext->sync.audioBufferMutex);
546
547	int i;
548	for (i = 0; i < threadContext->rewindBufferCapacity; ++i) {
549		if (threadContext->rewindBuffer[i]) {
550			GBADeallocateState(threadContext->rewindBuffer[i]);
551		}
552	}
553	free(threadContext->rewindBuffer);
554	free(threadContext->rewindScreenBuffer);
555
556	if (threadContext->rom) {
557		threadContext->rom->close(threadContext->rom);
558		threadContext->rom = 0;
559	}
560
561	if (threadContext->save) {
562		threadContext->save->close(threadContext->save);
563		threadContext->save = 0;
564	}
565
566	if (threadContext->bios) {
567		threadContext->bios->close(threadContext->bios);
568		threadContext->bios = 0;
569	}
570
571	if (threadContext->patch) {
572		threadContext->patch->close(threadContext->patch);
573		threadContext->patch = 0;
574	}
575}
576
577bool GBAThreadIsActive(struct GBAThread* threadContext) {
578	return threadContext->state >= THREAD_RUNNING && threadContext->state < THREAD_EXITING;
579}
580
581void GBAThreadInterrupt(struct GBAThread* threadContext) {
582	MutexLock(&threadContext->stateMutex);
583	++threadContext->interruptDepth;
584	if (threadContext->interruptDepth > 1 || !GBAThreadIsActive(threadContext)) {
585		MutexUnlock(&threadContext->stateMutex);
586		return;
587	}
588	threadContext->savedState = threadContext->state;
589	_waitOnInterrupt(threadContext);
590	threadContext->state = THREAD_INTERRUPTING;
591	threadContext->gba->cpu->nextEvent = 0;
592	ConditionWake(&threadContext->stateCond);
593	_waitUntilNotState(threadContext, THREAD_INTERRUPTING);
594	MutexUnlock(&threadContext->stateMutex);
595}
596
597void GBAThreadContinue(struct GBAThread* threadContext) {
598	MutexLock(&threadContext->stateMutex);
599	--threadContext->interruptDepth;
600	if (threadContext->interruptDepth < 1 && GBAThreadIsActive(threadContext)) {
601		threadContext->state = threadContext->savedState;
602		ConditionWake(&threadContext->stateCond);
603	}
604	MutexUnlock(&threadContext->stateMutex);
605}
606
607void GBARunOnThread(struct GBAThread* threadContext, void (*run)(struct GBAThread*)) {
608	MutexLock(&threadContext->stateMutex);
609	threadContext->run = run;
610	_waitOnInterrupt(threadContext);
611	threadContext->savedState = threadContext->state;
612	threadContext->state = THREAD_RUN_ON;
613	threadContext->gba->cpu->nextEvent = 0;
614	ConditionWake(&threadContext->stateCond);
615	_waitUntilNotState(threadContext, THREAD_RUN_ON);
616	MutexUnlock(&threadContext->stateMutex);
617}
618
619void GBAThreadPause(struct GBAThread* threadContext) {
620	bool frameOn = threadContext->sync.videoFrameOn;
621	MutexLock(&threadContext->stateMutex);
622	_waitOnInterrupt(threadContext);
623	if (threadContext->state == THREAD_RUNNING) {
624		_pauseThread(threadContext, false);
625		threadContext->frameWasOn = frameOn;
626		frameOn = false;
627	}
628	MutexUnlock(&threadContext->stateMutex);
629
630	GBASyncSetVideoSync(&threadContext->sync, frameOn);
631}
632
633void GBAThreadUnpause(struct GBAThread* threadContext) {
634	bool frameOn = threadContext->sync.videoFrameOn;
635	MutexLock(&threadContext->stateMutex);
636	_waitOnInterrupt(threadContext);
637	if (threadContext->state == THREAD_PAUSED || threadContext->state == THREAD_PAUSING) {
638		threadContext->state = THREAD_RUNNING;
639		ConditionWake(&threadContext->stateCond);
640		frameOn = threadContext->frameWasOn;
641	}
642	MutexUnlock(&threadContext->stateMutex);
643
644	GBASyncSetVideoSync(&threadContext->sync, frameOn);
645}
646
647bool GBAThreadIsPaused(struct GBAThread* threadContext) {
648	bool isPaused;
649	MutexLock(&threadContext->stateMutex);
650	_waitOnInterrupt(threadContext);
651	isPaused = threadContext->state == THREAD_PAUSED;
652	MutexUnlock(&threadContext->stateMutex);
653	return isPaused;
654}
655
656void GBAThreadTogglePause(struct GBAThread* threadContext) {
657	bool frameOn = threadContext->sync.videoFrameOn;
658	MutexLock(&threadContext->stateMutex);
659	_waitOnInterrupt(threadContext);
660	if (threadContext->state == THREAD_PAUSED || threadContext->state == THREAD_PAUSING) {
661		threadContext->state = THREAD_RUNNING;
662		ConditionWake(&threadContext->stateCond);
663		frameOn = threadContext->frameWasOn;
664	} else if (threadContext->state == THREAD_RUNNING) {
665		_pauseThread(threadContext, false);
666		threadContext->frameWasOn = frameOn;
667		frameOn = false;
668	}
669	MutexUnlock(&threadContext->stateMutex);
670
671	GBASyncSetVideoSync(&threadContext->sync, frameOn);
672}
673
674void GBAThreadPauseFromThread(struct GBAThread* threadContext) {
675	bool frameOn = true;
676	MutexLock(&threadContext->stateMutex);
677	_waitOnInterrupt(threadContext);
678	if (threadContext->state == THREAD_RUNNING) {
679		_pauseThread(threadContext, true);
680		frameOn = false;
681	}
682	MutexUnlock(&threadContext->stateMutex);
683
684	GBASyncSetVideoSync(&threadContext->sync, frameOn);
685}
686
687void GBAThreadLoadROM(struct GBAThread* threadContext, const char* fname) {
688	threadContext->rom = GBADirectorySetOpenPath(&threadContext->dirs, fname, GBAIsROM);
689}
690
691void GBAThreadReplaceROM(struct GBAThread* threadContext, const char* fname) {
692	GBAUnloadROM(threadContext->gba);
693
694	if (threadContext->rom) {
695		threadContext->rom->close(threadContext->rom);
696		threadContext->rom = 0;
697	}
698
699	if (threadContext->save) {
700		threadContext->save->close(threadContext->save);
701		threadContext->save = 0;
702	}
703
704	if (threadContext->dirs.archive) {
705		threadContext->dirs.archive->close(threadContext->dirs.archive);
706		threadContext->dirs.archive = 0;
707	}
708
709	GBAThreadLoadROM(threadContext, fname);
710
711	threadContext->fname = fname;
712	_reloadDirectories(threadContext);
713
714	GBARaiseIRQ(threadContext->gba, IRQ_GAMEPAK);
715	GBALoadROM(threadContext->gba, threadContext->rom, threadContext->save, threadContext->fname);
716}
717
718#ifdef USE_PTHREADS
719struct GBAThread* GBAThreadGetContext(void) {
720	pthread_once(&_contextOnce, _createTLS);
721	return pthread_getspecific(_contextKey);
722}
723#elif _WIN32
724struct GBAThread* GBAThreadGetContext(void) {
725	InitOnceExecuteOnce(&_contextOnce, _createTLS, NULL, 0);
726	return TlsGetValue(_contextKey);
727}
728#endif
729
730void GBAThreadTakeScreenshot(struct GBAThread* threadContext) {
731	GBATakeScreenshot(threadContext->gba, threadContext->dirs.screenshot);
732}
733
734#else
735struct GBAThread* GBAThreadGetContext(void) {
736	return 0;
737}
738#endif