all repos — mgba @ 9c1ec8c96d0d3b6abd775cbe3501cf9f1fb46cae

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	threadContext->gba->cpu->halted = false;
361	ConditionWake(&threadContext->stateCond);
362	MutexUnlock(&threadContext->stateMutex);
363	MutexLock(&threadContext->sync.audioBufferMutex);
364	threadContext->sync.audioWait = 0;
365	ConditionWake(&threadContext->sync.audioRequiredCond);
366	MutexUnlock(&threadContext->sync.audioBufferMutex);
367
368	MutexLock(&threadContext->sync.videoFrameMutex);
369	threadContext->sync.videoFrameWait = false;
370	threadContext->sync.videoFrameOn = false;
371	ConditionWake(&threadContext->sync.videoFrameRequiredCond);
372	ConditionWake(&threadContext->sync.videoFrameAvailableCond);
373	MutexUnlock(&threadContext->sync.videoFrameMutex);
374}
375
376void GBAThreadReset(struct GBAThread* threadContext) {
377	MutexLock(&threadContext->stateMutex);
378	_waitOnInterrupt(threadContext);
379	threadContext->state = THREAD_RESETING;
380	ConditionWake(&threadContext->stateCond);
381	MutexUnlock(&threadContext->stateMutex);
382}
383
384void GBAThreadJoin(struct GBAThread* threadContext) {
385	ThreadJoin(threadContext->thread);
386
387	MutexDeinit(&threadContext->stateMutex);
388	ConditionDeinit(&threadContext->stateCond);
389
390	MutexDeinit(&threadContext->sync.videoFrameMutex);
391	ConditionWake(&threadContext->sync.videoFrameAvailableCond);
392	ConditionDeinit(&threadContext->sync.videoFrameAvailableCond);
393	ConditionWake(&threadContext->sync.videoFrameRequiredCond);
394	ConditionDeinit(&threadContext->sync.videoFrameRequiredCond);
395
396	ConditionWake(&threadContext->sync.audioRequiredCond);
397	ConditionDeinit(&threadContext->sync.audioRequiredCond);
398	MutexDeinit(&threadContext->sync.audioBufferMutex);
399
400	int i;
401	for (i = 0; i < threadContext->rewindBufferCapacity; ++i) {
402		if (threadContext->rewindBuffer[i]) {
403			GBADeallocateState(threadContext->rewindBuffer[i]);
404		}
405	}
406	free(threadContext->rewindBuffer);
407
408	if (threadContext->rom) {
409		threadContext->rom->close(threadContext->rom);
410		threadContext->rom = 0;
411	}
412
413	if (threadContext->save) {
414		threadContext->save->close(threadContext->save);
415		threadContext->save = 0;
416	}
417
418	if (threadContext->bios) {
419		threadContext->bios->close(threadContext->bios);
420		threadContext->bios = 0;
421	}
422
423	if (threadContext->patch) {
424		threadContext->patch->close(threadContext->patch);
425		threadContext->patch = 0;
426	}
427
428	if (threadContext->gameDir) {
429		if (threadContext->stateDir == threadContext->gameDir) {
430			threadContext->stateDir = 0;
431		}
432		threadContext->gameDir->close(threadContext->gameDir);
433		threadContext->gameDir = 0;
434	}
435
436	if (threadContext->stateDir) {
437		threadContext->stateDir->close(threadContext->stateDir);
438		threadContext->stateDir = 0;
439	}
440}
441
442bool GBAThreadIsActive(struct GBAThread* threadContext) {
443	return threadContext->state >= THREAD_RUNNING && threadContext->state < THREAD_EXITING;
444}
445
446void GBAThreadInterrupt(struct GBAThread* threadContext) {
447	MutexLock(&threadContext->stateMutex);
448	++threadContext->interruptDepth;
449	if (threadContext->interruptDepth > 1 || !GBAThreadIsActive(threadContext)) {
450		MutexUnlock(&threadContext->stateMutex);
451		return;
452	}
453	threadContext->savedState = threadContext->state;
454	_waitOnInterrupt(threadContext);
455	threadContext->state = THREAD_INTERRUPTING;
456	if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
457		threadContext->debugger->state = DEBUGGER_EXITING;
458	}
459	ConditionWake(&threadContext->stateCond);
460	_waitUntilNotState(threadContext, THREAD_INTERRUPTING);
461	MutexUnlock(&threadContext->stateMutex);
462}
463
464void GBAThreadContinue(struct GBAThread* threadContext) {
465	MutexLock(&threadContext->stateMutex);
466	--threadContext->interruptDepth;
467	if (threadContext->interruptDepth < 1 && GBAThreadIsActive(threadContext)) {
468		threadContext->state = threadContext->savedState;
469		ConditionWake(&threadContext->stateCond);
470	}
471	MutexUnlock(&threadContext->stateMutex);
472}
473
474void GBAThreadPause(struct GBAThread* threadContext) {
475	bool frameOn = true;
476	MutexLock(&threadContext->stateMutex);
477	_waitOnInterrupt(threadContext);
478	if (threadContext->state == THREAD_RUNNING) {
479		_pauseThread(threadContext, false);
480		frameOn = false;
481	}
482	MutexUnlock(&threadContext->stateMutex);
483
484	_changeVideoSync(&threadContext->sync, frameOn);
485}
486
487void GBAThreadUnpause(struct GBAThread* threadContext) {
488	MutexLock(&threadContext->stateMutex);
489	_waitOnInterrupt(threadContext);
490	if (threadContext->state == THREAD_PAUSED || threadContext->state == THREAD_PAUSING) {
491		threadContext->state = THREAD_RUNNING;
492		ConditionWake(&threadContext->stateCond);
493	}
494	MutexUnlock(&threadContext->stateMutex);
495
496	_changeVideoSync(&threadContext->sync, true);
497}
498
499bool GBAThreadIsPaused(struct GBAThread* threadContext) {
500	bool isPaused;
501	MutexLock(&threadContext->stateMutex);
502	_waitOnInterrupt(threadContext);
503	isPaused = threadContext->state == THREAD_PAUSED;
504	MutexUnlock(&threadContext->stateMutex);
505	return isPaused;
506}
507
508void GBAThreadTogglePause(struct GBAThread* threadContext) {
509	bool frameOn = true;
510	MutexLock(&threadContext->stateMutex);
511	_waitOnInterrupt(threadContext);
512	if (threadContext->state == THREAD_PAUSED || threadContext->state == THREAD_PAUSING) {
513		threadContext->state = THREAD_RUNNING;
514		ConditionWake(&threadContext->stateCond);
515	} else if (threadContext->state == THREAD_RUNNING) {
516		_pauseThread(threadContext, false);
517		frameOn = false;
518	}
519	MutexUnlock(&threadContext->stateMutex);
520
521	_changeVideoSync(&threadContext->sync, frameOn);
522}
523
524void GBAThreadPauseFromThread(struct GBAThread* threadContext) {
525	bool frameOn = true;
526	MutexLock(&threadContext->stateMutex);
527	_waitOnInterrupt(threadContext);
528	if (threadContext->state == THREAD_RUNNING) {
529		_pauseThread(threadContext, true);
530		frameOn = false;
531	}
532	MutexUnlock(&threadContext->stateMutex);
533
534	_changeVideoSync(&threadContext->sync, frameOn);
535}
536
537#ifdef USE_PTHREADS
538struct GBAThread* GBAThreadGetContext(void) {
539	pthread_once(&_contextOnce, _createTLS);
540	return pthread_getspecific(_contextKey);
541}
542#elif _WIN32
543struct GBAThread* GBAThreadGetContext(void) {
544	InitOnceExecuteOnce(&_contextOnce, _createTLS, NULL, 0);
545	return TlsGetValue(_contextKey);
546}
547#endif
548
549#ifdef USE_PNG
550void GBAThreadTakeScreenshot(struct GBAThread* threadContext) {
551	unsigned stride;
552	void* pixels = 0;
553	struct VFile* vf = VDirOptionalOpenIncrementFile(threadContext->stateDir, threadContext->gba->activeFile, "screenshot", "-", ".png", O_CREAT | O_TRUNC | O_WRONLY);
554	threadContext->gba->video.renderer->getPixels(threadContext->gba->video.renderer, &stride, &pixels);
555	png_structp png = PNGWriteOpen(vf);
556	png_infop info = PNGWriteHeader(png, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
557	PNGWritePixels(png, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, stride, pixels);
558	PNGWriteClose(png, info);
559	vf->close(vf);
560}
561#endif
562
563#else
564struct GBAThread* GBAThreadGetContext(void) {
565	return 0;
566}
567#endif
568
569void GBASyncPostFrame(struct GBASync* sync) {
570	if (!sync) {
571		return;
572	}
573
574	MutexLock(&sync->videoFrameMutex);
575	++sync->videoFramePending;
576	--sync->videoFrameSkip;
577	if (sync->videoFrameSkip < 0) {
578		do {
579			ConditionWake(&sync->videoFrameAvailableCond);
580			if (sync->videoFrameWait) {
581				ConditionWait(&sync->videoFrameRequiredCond, &sync->videoFrameMutex);
582			}
583		} while (sync->videoFrameWait && sync->videoFramePending);
584	}
585	MutexUnlock(&sync->videoFrameMutex);
586
587	struct GBAThread* thread = GBAThreadGetContext();
588	if (!thread) {
589		return;
590	}
591
592	if (thread->rewindBuffer) {
593		--thread->rewindBufferNext;
594		if (thread->rewindBufferNext <= 0) {
595			thread->rewindBufferNext = thread->rewindBufferInterval;
596			GBARecordFrame(thread);
597		}
598	}
599	if (thread->stream) {
600		thread->stream->postVideoFrame(thread->stream, thread->renderer);
601	}
602	if (thread->frameCallback) {
603		thread->frameCallback(thread);
604	}
605}
606
607bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip) {
608	if (!sync) {
609		return true;
610	}
611
612	MutexLock(&sync->videoFrameMutex);
613	ConditionWake(&sync->videoFrameRequiredCond);
614	if (!sync->videoFrameOn && !sync->videoFramePending) {
615		return false;
616	}
617	if (sync->videoFrameOn) {
618		ConditionWait(&sync->videoFrameAvailableCond, &sync->videoFrameMutex);
619	}
620	sync->videoFramePending = 0;
621	sync->videoFrameSkip = frameskip;
622	return true;
623}
624
625void GBASyncWaitFrameEnd(struct GBASync* sync) {
626	if (!sync) {
627		return;
628	}
629
630	MutexUnlock(&sync->videoFrameMutex);
631}
632
633bool GBASyncDrawingFrame(struct GBASync* sync) {
634	if (!sync) {
635		return true;
636	}
637
638	return sync->videoFrameSkip <= 0;
639}
640
641void GBASyncSuspendDrawing(struct GBASync* sync) {
642	if (!sync) {
643		return;
644	}
645
646	_changeVideoSync(sync, false);
647}
648
649void GBASyncResumeDrawing(struct GBASync* sync) {
650	if (!sync) {
651		return;
652	}
653
654	_changeVideoSync(sync, true);
655}
656
657void GBASyncProduceAudio(struct GBASync* sync, bool wait) {
658	if (!sync) {
659		return;
660	}
661
662	if (sync->audioWait && wait) {
663		// TODO loop properly in event of spurious wakeups
664		ConditionWait(&sync->audioRequiredCond, &sync->audioBufferMutex);
665	}
666	MutexUnlock(&sync->audioBufferMutex);
667}
668
669void GBASyncLockAudio(struct GBASync* sync) {
670	if (!sync) {
671		return;
672	}
673
674	MutexLock(&sync->audioBufferMutex);
675}
676
677void GBASyncUnlockAudio(struct GBASync* sync) {
678	if (!sync) {
679		return;
680	}
681
682	MutexUnlock(&sync->audioBufferMutex);
683}
684
685void GBASyncConsumeAudio(struct GBASync* sync) {
686	if (!sync) {
687		return;
688	}
689
690	ConditionWake(&sync->audioRequiredCond);
691	MutexUnlock(&sync->audioBufferMutex);
692}