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_PAUSED = 1,
14 THREAD_EXITING = 2,
15 THREAD_SHUTDOWN = 3
16};
17
18struct GBASync {
19 int videoFramePending;
20 int videoFrameWait;
21 int videoFrameSkip;
22 int videoFrameOn;
23 Mutex videoFrameMutex;
24 Condition videoFrameAvailableCond;
25 Condition videoFrameRequiredCond;
26
27 int audioWait;
28 Condition audioRequiredCond;
29 Mutex audioBufferMutex;
30};
31
32struct GBAThread {
33 // Output
34 enum ThreadState state;
35 int useDebugger;
36 struct GBA* gba;
37 struct ARMDebugger* debugger;
38
39 // Input
40 struct GBAVideoRenderer* renderer;
41 int fd;
42 const char* fname;
43 int activeKeys;
44 int frameskip;
45
46 // Threading state
47 Thread thread;
48
49 Mutex stateMutex;
50 Condition stateCond;
51
52 GBALogHandler logHandler;
53 ThreadCallback startCallback;
54 ThreadCallback cleanCallback;
55 ThreadCallback frameCallback;
56 void* userData;
57
58 struct GBASync sync;
59
60 int rewindBufferSize;
61 int rewindBufferCapacity;
62 int rewindBufferInterval;
63 int rewindBufferNext;
64 struct GBASerializedState** rewindBuffer;
65 int rewindBufferWriteOffset;
66};
67
68int GBAThreadStart(struct GBAThread* threadContext);
69void GBAThreadEnd(struct GBAThread* threadContext);
70void GBAThreadJoin(struct GBAThread* threadContext);
71
72void GBAThreadPause(struct GBAThread* threadContext);
73void GBAThreadUnpause(struct GBAThread* threadContext);
74int GBAThreadIsPaused(struct GBAThread* threadContext);
75void GBAThreadTogglePause(struct GBAThread* threadContext);
76struct GBAThread* GBAThreadGetContext(void);
77
78void GBASyncPostFrame(struct GBASync* sync);
79int GBASyncWaitFrameStart(struct GBASync* sync, int frameskip);
80void GBASyncWaitFrameEnd(struct GBASync* sync);
81int GBASyncDrawingFrame(struct GBASync* sync);
82
83void GBASyncProduceAudio(struct GBASync* sync, int wait);
84void GBASyncLockAudio(struct GBASync* sync);
85void GBASyncConsumeAudio(struct GBASync* sync);
86
87#endif