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