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,
18 THREAD_INTERRUPTING,
19 THREAD_PAUSED,
20 THREAD_PAUSING,
21 THREAD_RESETING,
22 THREAD_EXITING,
23 THREAD_SHUTDOWN
24};
25
26struct GBASync {
27 int videoFramePending;
28 int videoFrameWait;
29 int videoFrameSkip;
30 bool videoFrameOn;
31 Mutex videoFrameMutex;
32 Condition videoFrameAvailableCond;
33 Condition videoFrameRequiredCond;
34
35 int audioWait;
36 Condition audioRequiredCond;
37 Mutex audioBufferMutex;
38};
39
40struct GBAThread {
41 // Output
42 enum ThreadState state;
43 struct GBA* gba;
44 struct ARMCore* cpu;
45
46 // Input
47 struct GBAVideoRenderer* renderer;
48 struct GBASIODriverSet sioDrivers;
49 struct ARMDebugger* debugger;
50 struct VDir* gameDir;
51 struct VDir* stateDir;
52 struct VFile* rom;
53 struct VFile* save;
54 struct VFile* bios;
55 struct VFile* patch;
56 const char* fname;
57 int activeKeys;
58 int frameskip;
59 float fpsTarget;
60 size_t audioBuffers;
61
62 // Threading state
63 Thread thread;
64
65 Mutex stateMutex;
66 Condition stateCond;
67 enum ThreadState savedState;
68
69 GBALogHandler logHandler;
70 int logLevel;
71 ThreadCallback startCallback;
72 ThreadCallback cleanCallback;
73 ThreadCallback frameCallback;
74 void* userData;
75
76 struct GBASync sync;
77
78 int rewindBufferSize;
79 int rewindBufferCapacity;
80 int rewindBufferInterval;
81 int rewindBufferNext;
82 struct GBASerializedState** rewindBuffer;
83 int rewindBufferWriteOffset;
84};
85
86void GBAMapOptionsToContext(struct StartupOptions*, struct GBAThread*);
87
88bool GBAThreadStart(struct GBAThread* threadContext);
89bool GBAThreadHasStarted(struct GBAThread* threadContext);
90void GBAThreadEnd(struct GBAThread* threadContext);
91void GBAThreadReset(struct GBAThread* threadContext);
92void GBAThreadJoin(struct GBAThread* threadContext);
93
94void GBAThreadInterrupt(struct GBAThread* threadContext);
95void GBAThreadContinue(struct GBAThread* threadContext);
96
97void GBAThreadPause(struct GBAThread* threadContext);
98void GBAThreadUnpause(struct GBAThread* threadContext);
99bool GBAThreadIsPaused(struct GBAThread* threadContext);
100void GBAThreadTogglePause(struct GBAThread* threadContext);
101struct GBAThread* GBAThreadGetContext(void);
102
103void GBASyncPostFrame(struct GBASync* sync);
104bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip);
105void GBASyncWaitFrameEnd(struct GBASync* sync);
106bool GBASyncDrawingFrame(struct GBASync* sync);
107
108void GBASyncProduceAudio(struct GBASync* sync, int wait);
109void GBASyncLockAudio(struct GBASync* sync);
110void GBASyncUnlockAudio(struct GBASync* sync);
111void GBASyncConsumeAudio(struct GBASync* sync);
112
113#endif