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