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
30 threadContext->started = 1;
31 pthread_mutex_lock(&threadContext->mutex);
32 pthread_cond_broadcast(&threadContext->cond);
33 pthread_mutex_unlock(&threadContext->mutex);
34
35 ARMDebuggerRun(&debugger);
36 GBADeinit(&gba);
37
38 return 0;
39}
40
41int GBAThreadStart(struct GBAThread* threadContext, pthread_t* thread) {
42 // TODO: error check
43 {
44 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
45 threadContext->mutex = mutex;
46 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
47 threadContext->cond = cond;
48 }
49 pthread_mutex_init(&threadContext->mutex, 0);
50 pthread_cond_init(&threadContext->cond, 0);
51
52 pthread_mutex_lock(&threadContext->mutex);
53 threadContext->started = 0;
54 pthread_create(thread, 0, _GBAThreadRun, threadContext);
55 pthread_cond_wait(&threadContext->cond, &threadContext->mutex);
56 pthread_mutex_unlock(&threadContext->mutex);
57
58 return 0;
59}