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
15struct mCoreThread;
16struct mCore;
17
18typedef void (*ThreadCallback)(struct mCoreThread* threadContext);
19
20struct mCoreThread;
21struct mThreadLogger {
22 struct mLogger d;
23 struct mCoreThread* p;
24};
25
26struct mCoreThreadInternal;
27struct mCoreThread {
28 // Input
29 struct mCore* core;
30
31 struct mThreadLogger logger;
32 ThreadCallback startCallback;
33 ThreadCallback resetCallback;
34 ThreadCallback cleanCallback;
35 ThreadCallback frameCallback;
36 ThreadCallback sleepCallback;
37 ThreadCallback pauseCallback;
38 ThreadCallback unpauseCallback;
39 void* userData;
40 void (*run)(struct mCoreThread*);
41
42 struct mCoreThreadInternal* impl;
43};
44
45#ifndef OPAQUE_THREADING
46#include <mgba/core/rewind.h>
47#include <mgba/core/sync.h>
48#include <mgba-util/threading.h>
49
50enum mCoreThreadState {
51 THREAD_INITIALIZED = -1,
52 THREAD_RUNNING = 0,
53 THREAD_REWINDING,
54 THREAD_MAX_RUNNING = THREAD_REWINDING,
55
56 THREAD_WAITING,
57 THREAD_INTERRUPTED,
58 THREAD_PAUSED,
59 THREAD_MAX_WAITING = THREAD_PAUSED,
60
61 THREAD_PAUSING,
62 THREAD_RUN_ON,
63 THREAD_RESETING,
64 THREAD_MIN_DEFERRED = THREAD_PAUSING,
65 THREAD_MAX_DEFERRED = THREAD_RESETING,
66
67 THREAD_INTERRUPTING,
68 THREAD_EXITING,
69 THREAD_SHUTDOWN,
70 THREAD_CRASHED
71};
72
73struct mCoreThreadInternal {
74 Thread thread;
75 enum mCoreThreadState state;
76
77 Mutex stateMutex;
78 Condition stateCond;
79 enum mCoreThreadState savedState;
80 int interruptDepth;
81 bool frameWasOn;
82
83 struct mCoreSync sync;
84 struct mCoreRewindContext rewind;
85};
86
87#endif
88
89bool mCoreThreadStart(struct mCoreThread* threadContext);
90bool mCoreThreadHasStarted(struct mCoreThread* threadContext);
91bool mCoreThreadHasExited(struct mCoreThread* threadContext);
92bool mCoreThreadHasCrashed(struct mCoreThread* threadContext);
93void mCoreThreadMarkCrashed(struct mCoreThread* threadContext);
94void mCoreThreadEnd(struct mCoreThread* threadContext);
95void mCoreThreadReset(struct mCoreThread* threadContext);
96void mCoreThreadJoin(struct mCoreThread* threadContext);
97
98bool mCoreThreadIsActive(struct mCoreThread* threadContext);
99void mCoreThreadInterrupt(struct mCoreThread* threadContext);
100void mCoreThreadInterruptFromThread(struct mCoreThread* threadContext);
101void mCoreThreadContinue(struct mCoreThread* threadContext);
102
103void mCoreThreadRunFunction(struct mCoreThread* threadContext, void (*run)(struct mCoreThread*));
104
105void mCoreThreadPause(struct mCoreThread* threadContext);
106void mCoreThreadUnpause(struct mCoreThread* threadContext);
107bool mCoreThreadIsPaused(struct mCoreThread* threadContext);
108void mCoreThreadTogglePause(struct mCoreThread* threadContext);
109void mCoreThreadPauseFromThread(struct mCoreThread* threadContext);
110void mCoreThreadWaitFromThread(struct mCoreThread* threadContext);
111void mCoreThreadStopWaiting(struct mCoreThread* threadContext);
112
113void mCoreThreadSetRewinding(struct mCoreThread* threadContext, bool);
114void mCoreThreadRewindParamsChanged(struct mCoreThread* threadContext);
115
116struct mCoreThread* mCoreThreadGet(void);
117struct mLogger* mCoreThreadLogger(void);
118
119CXX_GUARD_END
120
121#endif