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