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