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_RESETING,
32 THREAD_EXITING,
33 THREAD_SHUTDOWN,
34 THREAD_CRASHED
35};
36
37struct mCoreThread;
38struct mThreadLogger {
39 struct mLogger d;
40 struct mCoreThread* p;
41};
42
43struct mCoreThread {
44 // Input
45 struct mCore* core;
46
47 // Threading state
48 Thread thread;
49 enum mCoreThreadState state;
50
51 Mutex stateMutex;
52 Condition stateCond;
53 enum mCoreThreadState savedState;
54 int interruptDepth;
55 bool frameWasOn;
56
57 struct mThreadLogger logger;
58 enum mLogLevel logLevel;
59 ThreadCallback startCallback;
60 ThreadCallback resetCallback;
61 ThreadCallback cleanCallback;
62 ThreadCallback frameCallback;
63 void* userData;
64 void (*run)(struct mCoreThread*);
65
66 struct mCoreSync sync;
67 struct mCoreRewindContext rewind;
68};
69
70bool mCoreThreadStart(struct mCoreThread* threadContext);
71bool mCoreThreadHasStarted(struct mCoreThread* threadContext);
72bool mCoreThreadHasExited(struct mCoreThread* threadContext);
73bool mCoreThreadHasCrashed(struct mCoreThread* threadContext);
74void mCoreThreadEnd(struct mCoreThread* threadContext);
75void mCoreThreadReset(struct mCoreThread* threadContext);
76void mCoreThreadJoin(struct mCoreThread* threadContext);
77
78bool mCoreThreadIsActive(struct mCoreThread* threadContext);
79void mCoreThreadInterrupt(struct mCoreThread* threadContext);
80void mCoreThreadInterruptFromThread(struct mCoreThread* threadContext);
81void mCoreThreadContinue(struct mCoreThread* threadContext);
82
83void mCoreThreadRunFunction(struct mCoreThread* threadContext, void (*run)(struct mCoreThread*));
84
85void mCoreThreadPause(struct mCoreThread* threadContext);
86void mCoreThreadUnpause(struct mCoreThread* threadContext);
87bool mCoreThreadIsPaused(struct mCoreThread* threadContext);
88void mCoreThreadTogglePause(struct mCoreThread* threadContext);
89void mCoreThreadPauseFromThread(struct mCoreThread* threadContext);
90
91void mCoreThreadSetRewinding(struct mCoreThread* threadContext, bool);
92
93struct mCoreThread* mCoreThreadGet(void);
94
95void mCoreThreadFrameStarted(struct mCoreThread*);
96void mCoreThreadFrameEnded(struct mCoreThread*);
97
98struct mLogger* mCoreThreadLogger(void);
99
100#endif