all repos — mgba @ 31f0e179c028ee0d33cd9891d7bf667b26dfe087

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