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 mTHREAD_INITIALIZED = -1,
52 mTHREAD_RUNNING = 0,
53 mTHREAD_REQUEST,
54
55 mTHREAD_INTERRUPTED,
56 mTHREAD_PAUSED,
57 mTHREAD_MIN_WAITING = mTHREAD_INTERRUPTED,
58 mTHREAD_MAX_WAITING = mTHREAD_PAUSED,
59
60 mTHREAD_INTERRUPTING,
61 mTHREAD_EXITING,
62
63 mTHREAD_SHUTDOWN,
64 mTHREAD_CRASHED
65};
66
67enum mCoreThreadRequest {
68 mTHREAD_REQ_PAUSE = 1, // User-set pause
69 mTHREAD_REQ_WAIT = 2, // Core-set pause
70 mTHREAD_REQ_RESET = 4,
71 mTHREAD_REQ_RUN_ON = 8,
72};
73
74struct mCoreThreadInternal {
75 Thread thread;
76 enum mCoreThreadState state;
77 bool rewinding;
78 int requested;
79
80 Mutex stateMutex;
81 Condition stateCond;
82 int interruptDepth;
83 bool frameWasOn;
84
85 struct mCoreSync sync;
86 struct mCoreRewindContext rewind;
87};
88
89#endif
90
91bool mCoreThreadStart(struct mCoreThread* threadContext);
92bool mCoreThreadHasStarted(struct mCoreThread* threadContext);
93bool mCoreThreadHasExited(struct mCoreThread* threadContext);
94bool mCoreThreadHasCrashed(struct mCoreThread* threadContext);
95void mCoreThreadMarkCrashed(struct mCoreThread* threadContext);
96void mCoreThreadEnd(struct mCoreThread* threadContext);
97void mCoreThreadReset(struct mCoreThread* threadContext);
98void mCoreThreadJoin(struct mCoreThread* threadContext);
99
100bool mCoreThreadIsActive(struct mCoreThread* threadContext);
101void mCoreThreadInterrupt(struct mCoreThread* threadContext);
102void mCoreThreadInterruptFromThread(struct mCoreThread* threadContext);
103void mCoreThreadContinue(struct mCoreThread* threadContext);
104
105void mCoreThreadRunFunction(struct mCoreThread* threadContext, void (*run)(struct mCoreThread*));
106
107void mCoreThreadPause(struct mCoreThread* threadContext);
108void mCoreThreadUnpause(struct mCoreThread* threadContext);
109bool mCoreThreadIsPaused(struct mCoreThread* threadContext);
110void mCoreThreadTogglePause(struct mCoreThread* threadContext);
111void mCoreThreadPauseFromThread(struct mCoreThread* threadContext);
112void mCoreThreadWaitFromThread(struct mCoreThread* threadContext);
113void mCoreThreadStopWaiting(struct mCoreThread* threadContext);
114
115void mCoreThreadSetRewinding(struct mCoreThread* threadContext, bool);
116void mCoreThreadRewindParamsChanged(struct mCoreThread* threadContext);
117
118struct mCoreThread* mCoreThreadGet(void);
119struct mLogger* mCoreThreadLogger(void);
120
121CXX_GUARD_END
122
123#endif