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 int biosFd;
43 const char* fname;
44 int activeKeys;
45 int frameskip;
46
47 // Threading state
48 Thread thread;
49
50 Mutex stateMutex;
51 Condition stateCond;
52
53 GBALogHandler logHandler;
54 ThreadCallback startCallback;
55 ThreadCallback cleanCallback;
56 ThreadCallback frameCallback;
57 void* userData;
58
59 struct GBASync sync;
60
61 int rewindBufferSize;
62 int rewindBufferCapacity;
63 int rewindBufferInterval;
64 int rewindBufferNext;
65 struct GBASerializedState** rewindBuffer;
66 int rewindBufferWriteOffset;
67};
68
69int GBAThreadStart(struct GBAThread* threadContext);
70void GBAThreadEnd(struct GBAThread* threadContext);
71void GBAThreadJoin(struct GBAThread* threadContext);
72
73void GBAThreadPause(struct GBAThread* threadContext);
74void GBAThreadUnpause(struct GBAThread* threadContext);
75int GBAThreadIsPaused(struct GBAThread* threadContext);
76void GBAThreadTogglePause(struct GBAThread* threadContext);
77struct GBAThread* GBAThreadGetContext(void);
78
79void GBASyncPostFrame(struct GBASync* sync);
80int GBASyncWaitFrameStart(struct GBASync* sync, int frameskip);
81void GBASyncWaitFrameEnd(struct GBASync* sync);
82int GBASyncDrawingFrame(struct GBASync* sync);
83
84void GBASyncProduceAudio(struct GBASync* sync, int wait);
85void GBASyncLockAudio(struct GBASync* sync);
86void GBASyncConsumeAudio(struct GBASync* sync);
87
88#endif