src/gba/gba-thread.h (view raw)
1#ifndef GBA_THREAD_H
2#define GBA_THREAD_H
3
4#include "common.h"
5
6#include "gba.h"
7
8#include "util/threading.h"
9#include "platform/commandline.h"
10
11struct GBAThread;
12typedef void (*ThreadCallback)(struct GBAThread* threadContext);
13
14enum ThreadState {
15 THREAD_INITIALIZED = -1,
16 THREAD_RUNNING = 0,
17 THREAD_INTERRUPTED = 1,
18 THREAD_PAUSED = 2,
19 THREAD_EXITING = 3,
20 THREAD_SHUTDOWN = 4
21};
22
23struct GBASync {
24 int videoFramePending;
25 int videoFrameWait;
26 int videoFrameSkip;
27 int videoFrameOn;
28 Mutex videoFrameMutex;
29 Condition videoFrameAvailableCond;
30 Condition videoFrameRequiredCond;
31
32 int audioWait;
33 Condition audioRequiredCond;
34 Mutex audioBufferMutex;
35};
36
37struct GBAThread {
38 // Output
39 enum ThreadState state;
40 struct GBA* gba;
41 struct ARMCore* cpu;
42
43 // Input
44 struct GBAVideoRenderer* renderer;
45 struct GBASIODriverSet sioDrivers;
46 struct ARMDebugger* debugger;
47 int fd;
48 int biosFd;
49 int patchFd;
50 const char* fname;
51 int activeKeys;
52 int frameskip;
53
54 // Threading state
55 Thread thread;
56
57 Mutex stateMutex;
58 Condition stateCond;
59 enum ThreadState savedState;
60
61 GBALogHandler logHandler;
62 int logLevel;
63 ThreadCallback startCallback;
64 ThreadCallback cleanCallback;
65 ThreadCallback frameCallback;
66 void* userData;
67
68 struct GBASync sync;
69
70 int rewindBufferSize;
71 int rewindBufferCapacity;
72 int rewindBufferInterval;
73 int rewindBufferNext;
74 struct GBASerializedState** rewindBuffer;
75 int rewindBufferWriteOffset;
76};
77
78void GBAMapOptionsToContext(struct StartupOptions*, struct GBAThread*);
79
80int GBAThreadStart(struct GBAThread* threadContext);
81int GBAThreadHasStarted(struct GBAThread* threadContext);
82void GBAThreadEnd(struct GBAThread* threadContext);
83void GBAThreadJoin(struct GBAThread* threadContext);
84
85void GBAThreadTryPause(struct GBAThread* threadContext);
86void GBAThreadContinue(struct GBAThread* threadContext);
87
88void GBAThreadPause(struct GBAThread* threadContext);
89void GBAThreadUnpause(struct GBAThread* threadContext);
90int GBAThreadIsPaused(struct GBAThread* threadContext);
91void GBAThreadTogglePause(struct GBAThread* threadContext);
92struct GBAThread* GBAThreadGetContext(void);
93
94void GBASyncPostFrame(struct GBASync* sync);
95int GBASyncWaitFrameStart(struct GBASync* sync, int frameskip);
96void GBASyncWaitFrameEnd(struct GBASync* sync);
97int GBASyncDrawingFrame(struct GBASync* sync);
98
99void GBASyncProduceAudio(struct GBASync* sync, int wait);
100void GBASyncLockAudio(struct GBASync* sync);
101void GBASyncConsumeAudio(struct GBASync* sync);
102
103#endif