all repos — mgba @ e739e4000b74c8629b23936aded80f92427d093f

mGBA Game Boy Advance Emulator

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