src/gba/gba-thread.c (view raw)
1#include "gba-thread.h"
2
3#include "arm.h"
4#include "debugger.h"
5#include "gba.h"
6
7#include <signal.h>
8
9static void* _GBAThreadRun(void* context) {
10 struct ARMDebugger debugger;
11 struct GBA gba;
12 struct GBAThread* threadContext = context;
13
14 sigset_t signals;
15 sigfillset(&signals);
16 pthread_sigmask(SIG_UNBLOCK, &signals, 0);
17
18 GBAInit(&gba);
19 if (threadContext->renderer) {
20 GBAVideoAssociateRenderer(&gba.video, threadContext->renderer);
21 }
22
23 threadContext->gba = &gba;
24 threadContext->debugger = &debugger;
25 if (threadContext->fd >= 0) {
26 GBALoadROM(&gba, threadContext->fd);
27 }
28 GBAAttachDebugger(&gba, &debugger);
29 gba.keySource = &threadContext->activeKeys;
30
31 threadContext->started = 1;
32 pthread_mutex_lock(&threadContext->mutex);
33 pthread_cond_broadcast(&threadContext->cond);
34 pthread_mutex_unlock(&threadContext->mutex);
35
36 ARMDebuggerRun(&debugger);
37 threadContext->started = 0;
38 GBADeinit(&gba);
39
40 return 0;
41}
42
43int GBAThreadStart(struct GBAThread* threadContext) {
44 // TODO: error check
45 {
46 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
47 threadContext->mutex = mutex;
48 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
49 threadContext->cond = cond;
50 }
51 pthread_mutex_init(&threadContext->mutex, 0);
52 pthread_cond_init(&threadContext->cond, 0);
53
54 pthread_mutex_lock(&threadContext->mutex);
55 threadContext->activeKeys = 0;
56 threadContext->started = 0;
57 pthread_create(&threadContext->thread, 0, _GBAThreadRun, threadContext);
58 pthread_cond_wait(&threadContext->cond, &threadContext->mutex);
59 pthread_mutex_unlock(&threadContext->mutex);
60
61 return 0;
62}
63
64void GBAThreadJoin(struct GBAThread* threadContext) {
65 pthread_join(threadContext->thread, 0);
66
67 pthread_mutex_destroy(&threadContext->mutex);
68 pthread_cond_destroy(&threadContext->cond);
69}