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