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 const char* fname;
50 int activeKeys;
51 int frameskip;
52
53 // Threading state
54 Thread thread;
55
56 Mutex stateMutex;
57 Condition stateCond;
58 enum ThreadState savedState;
59
60 GBALogHandler logHandler;
61 ThreadCallback startCallback;
62 ThreadCallback cleanCallback;
63 ThreadCallback frameCallback;
64 void* userData;
65
66 struct GBASync sync;
67
68 int rewindBufferSize;
69 int rewindBufferCapacity;
70 int rewindBufferInterval;
71 int rewindBufferNext;
72 struct GBASerializedState** rewindBuffer;
73 int rewindBufferWriteOffset;
74};
75
76void GBAMapOptionsToContext(struct StartupOptions*, struct GBAThread*);
77
78int GBAThreadStart(struct GBAThread* threadContext);
79int GBAThreadHasStarted(struct GBAThread* threadContext);
80void GBAThreadEnd(struct GBAThread* threadContext);
81void GBAThreadJoin(struct GBAThread* threadContext);
82
83void GBAThreadInterrupt(struct GBAThread* threadContext);
84void GBAThreadContinue(struct GBAThread* threadContext);
85
86void GBAThreadPause(struct GBAThread* threadContext);
87void GBAThreadUnpause(struct GBAThread* threadContext);
88int GBAThreadIsPaused(struct GBAThread* threadContext);
89void GBAThreadTogglePause(struct GBAThread* threadContext);
90struct GBAThread* GBAThreadGetContext(void);
91
92void GBASyncPostFrame(struct GBASync* sync);
93int GBASyncWaitFrameStart(struct GBASync* sync, int frameskip);
94void GBASyncWaitFrameEnd(struct GBASync* sync);
95int GBASyncDrawingFrame(struct GBASync* sync);
96
97void GBASyncProduceAudio(struct GBASync* sync, int wait);
98void GBASyncLockAudio(struct GBASync* sync);
99void GBASyncConsumeAudio(struct GBASync* sync);
100
101#endif