include/mgba/core/thread.h (view raw)
1/* Copyright (c) 2013-2016 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#ifndef M_CORE_THREAD_H
7#define M_CORE_THREAD_H
8
9#include <mgba-util/common.h>
10
11CXX_GUARD_START
12
13#include <mgba/core/log.h>
14#include <mgba/core/rewind.h>
15#include <mgba/core/sync.h>
16#include <mgba-util/threading.h>
17
18struct mCoreThread;
19struct mCore;
20
21typedef void (*ThreadCallback)(struct mCoreThread* threadContext);
22
23enum mCoreThreadState {
24 THREAD_INITIALIZED = -1,
25 THREAD_RUNNING = 0,
26 THREAD_REWINDING,
27 THREAD_MAX_RUNNING = THREAD_REWINDING,
28 THREAD_INTERRUPTED,
29 THREAD_INTERRUPTING,
30 THREAD_PAUSED,
31 THREAD_PAUSING,
32 THREAD_RUN_ON,
33 THREAD_WAITING,
34 THREAD_RESETING,
35 THREAD_EXITING,
36 THREAD_SHUTDOWN,
37 THREAD_CRASHED
38};
39
40struct mCoreThread;
41struct mThreadLogger {
42 struct mLogger d;
43 struct mCoreThread* p;
44};
45
46struct mCoreThread {
47 // Input
48 struct mCore* core;
49
50 // Threading state
51 Thread thread;
52 enum mCoreThreadState state;
53
54 Mutex stateMutex;
55 Condition stateCond;
56 enum mCoreThreadState savedState;
57 int interruptDepth;
58 bool frameWasOn;
59
60 struct mThreadLogger logger;
61 ThreadCallback startCallback;
62 ThreadCallback resetCallback;
63 ThreadCallback cleanCallback;
64 ThreadCallback frameCallback;
65 void* userData;
66 void (*run)(struct mCoreThread*);
67
68 struct mCoreSync sync;
69 struct mCoreRewindContext rewind;
70};
71
72bool mCoreThreadStart(struct mCoreThread* threadContext);
73bool mCoreThreadHasStarted(struct mCoreThread* threadContext);
74bool mCoreThreadHasExited(struct mCoreThread* threadContext);
75bool mCoreThreadHasCrashed(struct mCoreThread* threadContext);
76void mCoreThreadMarkCrashed(struct mCoreThread* threadContext);
77void mCoreThreadEnd(struct mCoreThread* threadContext);
78void mCoreThreadReset(struct mCoreThread* threadContext);
79void mCoreThreadJoin(struct mCoreThread* threadContext);
80
81bool mCoreThreadIsActive(struct mCoreThread* threadContext);
82void mCoreThreadInterrupt(struct mCoreThread* threadContext);
83void mCoreThreadInterruptFromThread(struct mCoreThread* threadContext);
84void mCoreThreadContinue(struct mCoreThread* threadContext);
85
86void mCoreThreadRunFunction(struct mCoreThread* threadContext, void (*run)(struct mCoreThread*));
87
88void mCoreThreadPause(struct mCoreThread* threadContext);
89void mCoreThreadUnpause(struct mCoreThread* threadContext);
90bool mCoreThreadIsPaused(struct mCoreThread* threadContext);
91void mCoreThreadTogglePause(struct mCoreThread* threadContext);
92void mCoreThreadPauseFromThread(struct mCoreThread* threadContext);
93void mCoreThreadWaitFromThread(struct mCoreThread* threadContext);
94void mCoreThreadStopWaiting(struct mCoreThread* threadContext);
95
96void mCoreThreadSetRewinding(struct mCoreThread* threadContext, bool);
97
98struct mCoreThread* mCoreThreadGet(void);
99struct mLogger* mCoreThreadLogger(void);
100
101CXX_GUARD_END
102
103#endif