all repos — mgba @ df3f2796f7dca510480755b3952f2033dc1d2097

mGBA Game Boy Advance Emulator

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

  1/* Copyright (c) 2013-2014 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 "gba-thread.h"
  7
  8#include "arm.h"
  9#include "gba.h"
 10#include "gba-config.h"
 11#include "gba-serialize.h"
 12
 13#include "debugger/debugger.h"
 14
 15#include "util/patch.h"
 16#include "util/png-io.h"
 17#include "util/vfs.h"
 18
 19#include "platform/commandline.h"
 20
 21#include <signal.h>
 22
 23static const float _defaultFPSTarget = 60.f;
 24
 25#ifdef USE_PTHREADS
 26static pthread_key_t _contextKey;
 27static pthread_once_t _contextOnce = PTHREAD_ONCE_INIT;
 28
 29static void _createTLS(void) {
 30	pthread_key_create(&_contextKey, 0);
 31}
 32#elif _WIN32
 33static DWORD _contextKey;
 34static INIT_ONCE _contextOnce = INIT_ONCE_STATIC_INIT;
 35
 36static BOOL CALLBACK _createTLS(PINIT_ONCE once, PVOID param, PVOID* context) {
 37	UNUSED(once);
 38	UNUSED(param);
 39	UNUSED(context);
 40	_contextKey = TlsAlloc();
 41	return TRUE;
 42}
 43#endif
 44
 45#ifndef DISABLE_THREADING
 46static void _changeState(struct GBAThread* threadContext, enum ThreadState newState, bool broadcast) {
 47	MutexLock(&threadContext->stateMutex);
 48	threadContext->state = newState;
 49	if (broadcast) {
 50		ConditionWake(&threadContext->stateCond);
 51	}
 52	MutexUnlock(&threadContext->stateMutex);
 53}
 54
 55static void _waitOnInterrupt(struct GBAThread* threadContext) {
 56	while (threadContext->state == THREAD_INTERRUPTED) {
 57		ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
 58	}
 59}
 60
 61static void _waitUntilNotState(struct GBAThread* threadContext, enum ThreadState oldState) {
 62	while (threadContext->state == oldState) {
 63		MutexUnlock(&threadContext->stateMutex);
 64
 65		MutexLock(&threadContext->sync.videoFrameMutex);
 66		ConditionWake(&threadContext->sync.videoFrameRequiredCond);
 67		MutexUnlock(&threadContext->sync.videoFrameMutex);
 68
 69		MutexLock(&threadContext->sync.audioBufferMutex);
 70		ConditionWake(&threadContext->sync.audioRequiredCond);
 71		MutexUnlock(&threadContext->sync.audioBufferMutex);
 72
 73		MutexLock(&threadContext->stateMutex);
 74		ConditionWake(&threadContext->stateCond);
 75	}
 76}
 77
 78static void _pauseThread(struct GBAThread* threadContext, bool onThread) {
 79	if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
 80		threadContext->debugger->state = DEBUGGER_EXITING;
 81	}
 82	threadContext->state = THREAD_PAUSING;
 83	if (!onThread) {
 84		_waitUntilNotState(threadContext, THREAD_PAUSING);
 85	}
 86}
 87#endif
 88
 89static void _changeVideoSync(struct GBASync* sync, bool frameOn) {
 90	// Make sure the video thread can process events while the GBA thread is paused
 91	MutexLock(&sync->videoFrameMutex);
 92	if (frameOn != sync->videoFrameOn) {
 93		sync->videoFrameOn = frameOn;
 94		ConditionWake(&sync->videoFrameAvailableCond);
 95	}
 96	MutexUnlock(&sync->videoFrameMutex);
 97}
 98
 99#ifndef DISABLE_THREADING
100static THREAD_ENTRY _GBAThreadRun(void* context) {
101#ifdef USE_PTHREADS
102	pthread_once(&_contextOnce, _createTLS);
103#else
104	InitOnceExecuteOnce(&_contextOnce, _createTLS, NULL, 0);
105#endif
106
107	struct GBA gba;
108	struct ARMCore cpu;
109	struct Patch patch;
110	struct GBAThread* threadContext = context;
111	struct ARMComponent* components[1] = {};
112	int numComponents = 0;
113
114	if (threadContext->debugger) {
115		components[numComponents] = &threadContext->debugger->d;
116		++numComponents;
117	}
118
119#if !defined(_WIN32) && defined(USE_PTHREADS)
120	sigset_t signals;
121	sigemptyset(&signals);
122	pthread_sigmask(SIG_SETMASK, &signals, 0);
123#endif
124
125	GBACreate(&gba);
126	ARMSetComponents(&cpu, &gba.d, numComponents, components);
127	ARMInit(&cpu);
128	gba.sync = &threadContext->sync;
129	threadContext->gba = &gba;
130	gba.logLevel = threadContext->logLevel;
131#ifdef USE_PTHREADS
132	pthread_setspecific(_contextKey, threadContext);
133#else
134	TlsSetValue(_contextKey, threadContext);
135#endif
136
137	if (threadContext->audioBuffers) {
138		GBAAudioResizeBuffer(&gba.audio, threadContext->audioBuffers);
139	} else {
140		threadContext->audioBuffers = GBA_AUDIO_SAMPLES;
141	}
142
143	if (threadContext->renderer) {
144		GBAVideoAssociateRenderer(&gba.video, threadContext->renderer);
145	}
146
147	if (threadContext->rom) {
148		GBALoadROM(&gba, threadContext->rom, threadContext->save, threadContext->fname);
149		if (threadContext->bios) {
150			GBALoadBIOS(&gba, threadContext->bios);
151		}
152
153		if (threadContext->patch && loadPatch(threadContext->patch, &patch)) {
154			GBAApplyPatch(&gba, &patch);
155		}
156	}
157
158	ARMReset(&cpu);
159
160	if (threadContext->debugger) {
161		threadContext->debugger->log = GBADebuggerLogShim;
162		GBAAttachDebugger(&gba, threadContext->debugger);
163		ARMDebuggerEnter(threadContext->debugger, DEBUGGER_ENTER_ATTACHED);
164	}
165
166	GBASIOSetDriverSet(&gba.sio, &threadContext->sioDrivers);
167
168	gba.keySource = &threadContext->activeKeys;
169
170	if (threadContext->startCallback) {
171		threadContext->startCallback(threadContext);
172	}
173
174	_changeState(threadContext, THREAD_RUNNING, true);
175
176	while (threadContext->state < THREAD_EXITING) {
177		if (threadContext->debugger) {
178			struct ARMDebugger* debugger = threadContext->debugger;
179			ARMDebuggerRun(debugger);
180			if (debugger->state == DEBUGGER_SHUTDOWN) {
181				_changeState(threadContext, THREAD_EXITING, false);
182			}
183		} else {
184			while (threadContext->state == THREAD_RUNNING) {
185				ARMRunLoop(&cpu);
186			}
187		}
188
189		int resetScheduled = 0;
190		MutexLock(&threadContext->stateMutex);
191		while (threadContext->state > THREAD_RUNNING && threadContext->state < THREAD_EXITING) {
192			if (threadContext->state == THREAD_PAUSING) {
193				threadContext->state = THREAD_PAUSED;
194				ConditionWake(&threadContext->stateCond);
195			}
196			if (threadContext->state == THREAD_INTERRUPTING) {
197				threadContext->state = THREAD_INTERRUPTED;
198				ConditionWake(&threadContext->stateCond);
199			}
200			if (threadContext->state == THREAD_RESETING) {
201				threadContext->state = THREAD_RUNNING;
202				resetScheduled = 1;
203			}
204			while (threadContext->state == THREAD_PAUSED || threadContext->state == THREAD_INTERRUPTED) {
205				ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
206			}
207		}
208		MutexUnlock(&threadContext->stateMutex);
209		if (resetScheduled) {
210			ARMReset(&cpu);
211		}
212	}
213
214	while (threadContext->state != THREAD_SHUTDOWN) {
215		_changeState(threadContext, THREAD_SHUTDOWN, false);
216	}
217
218	if (threadContext->cleanCallback) {
219		threadContext->cleanCallback(threadContext);
220	}
221
222	threadContext->gba = 0;
223	ARMDeinit(&cpu);
224	GBADestroy(&gba);
225
226	threadContext->sync.videoFrameOn = false;
227	ConditionWake(&threadContext->sync.videoFrameAvailableCond);
228	ConditionWake(&threadContext->sync.audioRequiredCond);
229
230	return 0;
231}
232
233void GBAMapOptionsToContext(const struct GBAOptions* opts, struct GBAThread* threadContext) {
234	threadContext->bios = VFileOpen(opts->bios, O_RDONLY);
235	threadContext->frameskip = opts->frameskip;
236	threadContext->logLevel = opts->logLevel;
237	threadContext->rewindBufferCapacity = opts->rewindBufferCapacity;
238	threadContext->rewindBufferInterval = opts->rewindBufferInterval;
239	threadContext->sync.audioWait = opts->audioSync;
240	threadContext->sync.videoFrameWait = opts->videoSync;
241
242	if (opts->fpsTarget) {
243		threadContext->fpsTarget = opts->fpsTarget;
244	}
245
246	if (opts->audioBuffers) {
247		threadContext->audioBuffers = opts->audioBuffers;
248	}
249}
250
251void GBAMapArgumentsToContext(const struct GBAArguments* args, struct GBAThread* threadContext) {
252	if (args->dirmode) {
253		threadContext->gameDir = VDirOpen(args->fname);
254		threadContext->stateDir = threadContext->gameDir;
255	} else {
256		threadContext->rom = VFileOpen(args->fname, O_RDONLY);
257#if ENABLE_LIBZIP
258		threadContext->gameDir = VDirOpenZip(args->fname, 0);
259#endif
260	}
261	threadContext->fname = args->fname;
262	threadContext->patch = VFileOpen(args->patch, O_RDONLY);
263}
264
265bool GBAThreadStart(struct GBAThread* threadContext) {
266	// TODO: error check
267	threadContext->activeKeys = 0;
268	threadContext->state = THREAD_INITIALIZED;
269	threadContext->sync.videoFrameOn = true;
270	threadContext->sync.videoFrameSkip = 0;
271
272	threadContext->rewindBufferNext = threadContext->rewindBufferInterval;
273	threadContext->rewindBufferSize = 0;
274	if (threadContext->rewindBufferCapacity) {
275		threadContext->rewindBuffer = calloc(threadContext->rewindBufferCapacity, sizeof(void*));
276	} else {
277		threadContext->rewindBuffer = 0;
278	}
279
280	if (!threadContext->fpsTarget) {
281		threadContext->fpsTarget = _defaultFPSTarget;
282	}
283
284	if (threadContext->rom && !GBAIsROM(threadContext->rom)) {
285		threadContext->rom->close(threadContext->rom);
286		threadContext->rom = 0;
287	}
288
289	if (threadContext->gameDir) {
290		threadContext->gameDir->rewind(threadContext->gameDir);
291		struct VDirEntry* dirent = threadContext->gameDir->listNext(threadContext->gameDir);
292		while (dirent) {
293			struct Patch patchTemp;
294			struct VFile* vf = threadContext->gameDir->openFile(threadContext->gameDir, dirent->name(dirent), O_RDONLY);
295			if (!vf) {
296				continue;
297			}
298			if (!threadContext->rom && GBAIsROM(vf)) {
299				threadContext->rom = vf;
300			} else if (!threadContext->patch && loadPatch(vf, &patchTemp)) {
301				threadContext->patch = vf;
302			} else {
303				vf->close(vf);
304			}
305			dirent = threadContext->gameDir->listNext(threadContext->gameDir);
306		}
307
308	}
309
310	if (!threadContext->rom) {
311		threadContext->state = THREAD_SHUTDOWN;
312		return false;
313	}
314
315	threadContext->save = VDirOptionalOpenFile(threadContext->stateDir, threadContext->fname, "sram", ".sav", O_CREAT | O_RDWR);
316
317	MutexInit(&threadContext->stateMutex);
318	ConditionInit(&threadContext->stateCond);
319
320	MutexInit(&threadContext->sync.videoFrameMutex);
321	ConditionInit(&threadContext->sync.videoFrameAvailableCond);
322	ConditionInit(&threadContext->sync.videoFrameRequiredCond);
323	MutexInit(&threadContext->sync.audioBufferMutex);
324	ConditionInit(&threadContext->sync.audioRequiredCond);
325
326	threadContext->interruptDepth = 0;
327
328#ifndef _WIN32
329	sigset_t signals;
330	sigemptyset(&signals);
331	sigaddset(&signals, SIGINT);
332	sigaddset(&signals, SIGTRAP);
333	pthread_sigmask(SIG_BLOCK, &signals, 0);
334#endif
335
336	MutexLock(&threadContext->stateMutex);
337	ThreadCreate(&threadContext->thread, _GBAThreadRun, threadContext);
338	while (threadContext->state < THREAD_RUNNING) {
339		ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
340	}
341	MutexUnlock(&threadContext->stateMutex);
342
343	return true;
344}
345
346bool GBAThreadHasStarted(struct GBAThread* threadContext) {
347	bool hasStarted;
348	MutexLock(&threadContext->stateMutex);
349	hasStarted = threadContext->state > THREAD_INITIALIZED;
350	MutexUnlock(&threadContext->stateMutex);
351	return hasStarted;
352}
353
354void GBAThreadEnd(struct GBAThread* threadContext) {
355	MutexLock(&threadContext->stateMutex);
356	if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
357		threadContext->debugger->state = DEBUGGER_EXITING;
358	}
359	threadContext->state = THREAD_EXITING;
360	ConditionWake(&threadContext->stateCond);
361	MutexUnlock(&threadContext->stateMutex);
362	MutexLock(&threadContext->sync.audioBufferMutex);
363	threadContext->sync.audioWait = 0;
364	ConditionWake(&threadContext->sync.audioRequiredCond);
365	MutexUnlock(&threadContext->sync.audioBufferMutex);
366}
367
368void GBAThreadReset(struct GBAThread* threadContext) {
369	MutexLock(&threadContext->stateMutex);
370	_waitOnInterrupt(threadContext);
371	threadContext->state = THREAD_RESETING;
372	ConditionWake(&threadContext->stateCond);
373	MutexUnlock(&threadContext->stateMutex);
374}
375
376void GBAThreadJoin(struct GBAThread* threadContext) {
377	MutexLock(&threadContext->sync.videoFrameMutex);
378	threadContext->sync.videoFrameWait = 0;
379	ConditionWake(&threadContext->sync.videoFrameRequiredCond);
380	MutexUnlock(&threadContext->sync.videoFrameMutex);
381
382	ThreadJoin(threadContext->thread);
383
384	MutexDeinit(&threadContext->stateMutex);
385	ConditionDeinit(&threadContext->stateCond);
386
387	MutexDeinit(&threadContext->sync.videoFrameMutex);
388	ConditionWake(&threadContext->sync.videoFrameAvailableCond);
389	ConditionDeinit(&threadContext->sync.videoFrameAvailableCond);
390	ConditionWake(&threadContext->sync.videoFrameRequiredCond);
391	ConditionDeinit(&threadContext->sync.videoFrameRequiredCond);
392
393	ConditionWake(&threadContext->sync.audioRequiredCond);
394	ConditionDeinit(&threadContext->sync.audioRequiredCond);
395	MutexDeinit(&threadContext->sync.audioBufferMutex);
396
397	int i;
398	for (i = 0; i < threadContext->rewindBufferCapacity; ++i) {
399		if (threadContext->rewindBuffer[i]) {
400			GBADeallocateState(threadContext->rewindBuffer[i]);
401		}
402	}
403	free(threadContext->rewindBuffer);
404
405	if (threadContext->rom) {
406		threadContext->rom->close(threadContext->rom);
407		threadContext->rom = 0;
408	}
409
410	if (threadContext->save) {
411		threadContext->save->close(threadContext->save);
412		threadContext->save = 0;
413	}
414
415	if (threadContext->bios) {
416		threadContext->bios->close(threadContext->bios);
417		threadContext->bios = 0;
418	}
419
420	if (threadContext->patch) {
421		threadContext->patch->close(threadContext->patch);
422		threadContext->patch = 0;
423	}
424
425	if (threadContext->gameDir) {
426		if (threadContext->stateDir == threadContext->gameDir) {
427			threadContext->stateDir = 0;
428		}
429		threadContext->gameDir->close(threadContext->gameDir);
430		threadContext->gameDir = 0;
431	}
432
433	if (threadContext->stateDir) {
434		threadContext->stateDir->close(threadContext->stateDir);
435		threadContext->stateDir = 0;
436	}
437}
438
439bool GBAThreadIsActive(struct GBAThread* threadContext) {
440	return threadContext->state >= THREAD_RUNNING && threadContext->state < THREAD_EXITING;
441}
442
443void GBAThreadInterrupt(struct GBAThread* threadContext) {
444	MutexLock(&threadContext->stateMutex);
445	++threadContext->interruptDepth;
446	if (threadContext->interruptDepth > 1 || !GBAThreadIsActive(threadContext)) {
447		MutexUnlock(&threadContext->stateMutex);
448		return;
449	}
450	threadContext->savedState = threadContext->state;
451	_waitOnInterrupt(threadContext);
452	threadContext->state = THREAD_INTERRUPTING;
453	if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
454		threadContext->debugger->state = DEBUGGER_EXITING;
455	}
456	ConditionWake(&threadContext->stateCond);
457	_waitUntilNotState(threadContext, THREAD_INTERRUPTING);
458	MutexUnlock(&threadContext->stateMutex);
459}
460
461void GBAThreadContinue(struct GBAThread* threadContext) {
462	MutexLock(&threadContext->stateMutex);
463	--threadContext->interruptDepth;
464	if (threadContext->interruptDepth < 1 && GBAThreadIsActive(threadContext)) {
465		threadContext->state = threadContext->savedState;
466		ConditionWake(&threadContext->stateCond);
467	}
468	MutexUnlock(&threadContext->stateMutex);
469}
470
471void GBAThreadPause(struct GBAThread* threadContext) {
472	bool frameOn = true;
473	MutexLock(&threadContext->stateMutex);
474	_waitOnInterrupt(threadContext);
475	if (threadContext->state == THREAD_RUNNING) {
476		_pauseThread(threadContext, false);
477		frameOn = false;
478	}
479	MutexUnlock(&threadContext->stateMutex);
480
481	_changeVideoSync(&threadContext->sync, frameOn);
482}
483
484void GBAThreadUnpause(struct GBAThread* threadContext) {
485	MutexLock(&threadContext->stateMutex);
486	_waitOnInterrupt(threadContext);
487	if (threadContext->state == THREAD_PAUSED || threadContext->state == THREAD_PAUSING) {
488		threadContext->state = THREAD_RUNNING;
489		ConditionWake(&threadContext->stateCond);
490	}
491	MutexUnlock(&threadContext->stateMutex);
492
493	_changeVideoSync(&threadContext->sync, true);
494}
495
496bool GBAThreadIsPaused(struct GBAThread* threadContext) {
497	bool isPaused;
498	MutexLock(&threadContext->stateMutex);
499	_waitOnInterrupt(threadContext);
500	isPaused = threadContext->state == THREAD_PAUSED;
501	MutexUnlock(&threadContext->stateMutex);
502	return isPaused;
503}
504
505void GBAThreadTogglePause(struct GBAThread* threadContext) {
506	bool frameOn = true;
507	MutexLock(&threadContext->stateMutex);
508	_waitOnInterrupt(threadContext);
509	if (threadContext->state == THREAD_PAUSED || threadContext->state == THREAD_PAUSING) {
510		threadContext->state = THREAD_RUNNING;
511		ConditionWake(&threadContext->stateCond);
512	} else if (threadContext->state == THREAD_RUNNING) {
513		_pauseThread(threadContext, false);
514		frameOn = false;
515	}
516	MutexUnlock(&threadContext->stateMutex);
517
518	_changeVideoSync(&threadContext->sync, frameOn);
519}
520
521void GBAThreadPauseFromThread(struct GBAThread* threadContext) {
522	bool frameOn = true;
523	MutexLock(&threadContext->stateMutex);
524	_waitOnInterrupt(threadContext);
525	if (threadContext->state == THREAD_RUNNING) {
526		_pauseThread(threadContext, true);
527		frameOn = false;
528	}
529	MutexUnlock(&threadContext->stateMutex);
530
531	_changeVideoSync(&threadContext->sync, frameOn);
532}
533
534#ifdef USE_PTHREADS
535struct GBAThread* GBAThreadGetContext(void) {
536	pthread_once(&_contextOnce, _createTLS);
537	return pthread_getspecific(_contextKey);
538}
539#elif _WIN32
540struct GBAThread* GBAThreadGetContext(void) {
541	InitOnceExecuteOnce(&_contextOnce, _createTLS, NULL, 0);
542	return TlsGetValue(_contextKey);
543}
544#endif
545
546#ifdef USE_PNG
547void GBAThreadTakeScreenshot(struct GBAThread* threadContext) {
548	unsigned stride;
549	void* pixels = 0;
550	struct VFile* vf = VDirOptionalOpenIncrementFile(threadContext->stateDir, threadContext->gba->activeFile, "screenshot", "-", ".png", O_CREAT | O_TRUNC | O_WRONLY);
551	threadContext->gba->video.renderer->getPixels(threadContext->gba->video.renderer, &stride, &pixels);
552	png_structp png = PNGWriteOpen(vf);
553	png_infop info = PNGWriteHeader(png, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
554	PNGWritePixels(png, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, stride, pixels);
555	PNGWriteClose(png, info);
556	vf->close(vf);
557}
558#endif
559
560#else
561struct GBAThread* GBAThreadGetContext(void) {
562	return 0;
563}
564#endif
565
566void GBASyncPostFrame(struct GBASync* sync) {
567	if (!sync) {
568		return;
569	}
570
571	MutexLock(&sync->videoFrameMutex);
572	++sync->videoFramePending;
573	--sync->videoFrameSkip;
574	if (sync->videoFrameSkip < 0) {
575		do {
576			ConditionWake(&sync->videoFrameAvailableCond);
577			if (sync->videoFrameWait) {
578				ConditionWait(&sync->videoFrameRequiredCond, &sync->videoFrameMutex);
579			}
580		} while (sync->videoFrameWait && sync->videoFramePending);
581	}
582	MutexUnlock(&sync->videoFrameMutex);
583
584	struct GBAThread* thread = GBAThreadGetContext();
585	if (!thread) {
586		return;
587	}
588
589	if (thread->rewindBuffer) {
590		--thread->rewindBufferNext;
591		if (thread->rewindBufferNext <= 0) {
592			thread->rewindBufferNext = thread->rewindBufferInterval;
593			GBARecordFrame(thread);
594		}
595	}
596	if (thread->stream) {
597		thread->stream->postVideoFrame(thread->stream, thread->renderer);
598	}
599	if (thread->frameCallback) {
600		thread->frameCallback(thread);
601	}
602}
603
604bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip) {
605	if (!sync) {
606		return true;
607	}
608
609	MutexLock(&sync->videoFrameMutex);
610	ConditionWake(&sync->videoFrameRequiredCond);
611	if (!sync->videoFrameOn && !sync->videoFramePending) {
612		return false;
613	}
614	if (sync->videoFrameOn && !sync->videoFramePending) {
615		ConditionWait(&sync->videoFrameAvailableCond, &sync->videoFrameMutex);
616	}
617	sync->videoFramePending = 0;
618	sync->videoFrameSkip = frameskip;
619	return true;
620}
621
622void GBASyncWaitFrameEnd(struct GBASync* sync) {
623	if (!sync) {
624		return;
625	}
626
627	MutexUnlock(&sync->videoFrameMutex);
628}
629
630bool GBASyncDrawingFrame(struct GBASync* sync) {
631	if (!sync) {
632		return true;
633	}
634
635	return sync->videoFrameSkip <= 0;
636}
637
638void GBASyncSuspendDrawing(struct GBASync* sync) {
639	if (!sync) {
640		return;
641	}
642
643	_changeVideoSync(sync, false);
644}
645
646void GBASyncResumeDrawing(struct GBASync* sync) {
647	if (!sync) {
648		return;
649	}
650
651	_changeVideoSync(sync, true);
652}
653
654void GBASyncProduceAudio(struct GBASync* sync, bool wait) {
655	if (!sync) {
656		return;
657	}
658
659	if (sync->audioWait && wait) {
660		// TODO loop properly in event of spurious wakeups
661		ConditionWait(&sync->audioRequiredCond, &sync->audioBufferMutex);
662	}
663	MutexUnlock(&sync->audioBufferMutex);
664}
665
666void GBASyncLockAudio(struct GBASync* sync) {
667	if (!sync) {
668		return;
669	}
670
671	MutexLock(&sync->audioBufferMutex);
672}
673
674void GBASyncUnlockAudio(struct GBASync* sync) {
675	if (!sync) {
676		return;
677	}
678
679	MutexUnlock(&sync->audioBufferMutex);
680}
681
682void GBASyncConsumeAudio(struct GBASync* sync) {
683	if (!sync) {
684		return;
685	}
686
687	ConditionWake(&sync->audioRequiredCond);
688	MutexUnlock(&sync->audioBufferMutex);
689}