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 enum mLogLevel logLevel;
62 ThreadCallback startCallback;
63 ThreadCallback resetCallback;
64 ThreadCallback cleanCallback;
65 ThreadCallback frameCallback;
66 void* userData;
67 void (*run)(struct mCoreThread*);
68
69 struct mCoreSync sync;
70 struct mCoreRewindContext rewind;
71};
72
73bool mCoreThreadStart(struct mCoreThread* threadContext);
74bool mCoreThreadHasStarted(struct mCoreThread* threadContext);
75bool mCoreThreadHasExited(struct mCoreThread* threadContext);
76bool mCoreThreadHasCrashed(struct mCoreThread* threadContext);
77void mCoreThreadMarkCrashed(struct mCoreThread* threadContext);
78void mCoreThreadEnd(struct mCoreThread* threadContext);
79void mCoreThreadReset(struct mCoreThread* threadContext);
80void mCoreThreadJoin(struct mCoreThread* threadContext);
81
82bool mCoreThreadIsActive(struct mCoreThread* threadContext);
83void mCoreThreadInterrupt(struct mCoreThread* threadContext);
84void mCoreThreadInterruptFromThread(struct mCoreThread* threadContext);
85void mCoreThreadContinue(struct mCoreThread* threadContext);
86
87void mCoreThreadRunFunction(struct mCoreThread* threadContext, void (*run)(struct mCoreThread*));
88
89void mCoreThreadPause(struct mCoreThread* threadContext);
90void mCoreThreadUnpause(struct mCoreThread* threadContext);
91bool mCoreThreadIsPaused(struct mCoreThread* threadContext);
92void mCoreThreadTogglePause(struct mCoreThread* threadContext);
93void mCoreThreadPauseFromThread(struct mCoreThread* threadContext);
94void mCoreThreadWaitFromThread(struct mCoreThread* threadContext);
95void mCoreThreadStopWaiting(struct mCoreThread* threadContext);
96
97void mCoreThreadSetRewinding(struct mCoreThread* threadContext, bool);
98
99struct mCoreThread* mCoreThreadGet(void);
100struct mLogger* mCoreThreadLogger(void);
101
102CXX_GUARD_END
103
104#endif