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 threadContext->started = 0;
37 GBADeinit(&gba);
38
39 return 0;
40}
41
42int GBAThreadStart(struct GBAThread* threadContext) {
43 // TODO: error check
44 {
45 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
46 threadContext->mutex = mutex;
47 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
48 threadContext->cond = cond;
49 }
50 pthread_mutex_init(&threadContext->mutex, 0);
51 pthread_cond_init(&threadContext->cond, 0);
52
53 pthread_mutex_lock(&threadContext->mutex);
54 threadContext->started = 0;
55 pthread_create(&threadContext->thread, 0, _GBAThreadRun, threadContext);
56 pthread_cond_wait(&threadContext->cond, &threadContext->mutex);
57 pthread_mutex_unlock(&threadContext->mutex);
58
59 return 0;
60}
61
62void GBAThreadJoin(struct GBAThread* threadContext) {
63 pthread_join(threadContext->thread, 0);
64}