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 {
35 // Input
36 struct mCore* core;
37
38 // Threading state
39 Thread thread;
40 enum mCoreThreadState state;
41
42 Mutex stateMutex;
43 Condition stateCond;
44 enum mCoreThreadState savedState;
45 int interruptDepth;
46 bool frameWasOn;
47
48 struct mLogger logger;
49 enum mLogLevel logLevel;
50 ThreadCallback startCallback;
51 ThreadCallback cleanCallback;
52 ThreadCallback frameCallback;
53 void* userData;
54 void (*run)(struct mCoreThread*);
55
56 struct mCoreSync sync;
57};
58
59bool mCoreThreadStart(struct mCoreThread* threadContext);
60bool mCoreThreadHasStarted(struct mCoreThread* threadContext);
61bool mCoreThreadHasExited(struct mCoreThread* threadContext);
62bool mCoreThreadHasCrashed(struct mCoreThread* threadContext);
63void mCoreThreadEnd(struct mCoreThread* threadContext);
64void mCoreThreadReset(struct mCoreThread* threadContext);
65void mCoreThreadJoin(struct mCoreThread* threadContext);
66
67bool mCoreThreadIsActive(struct mCoreThread* threadContext);
68void mCoreThreadInterrupt(struct mCoreThread* threadContext);
69void mCoreThreadContinue(struct mCoreThread* threadContext);
70
71void mCoreThreadRunFunction(struct mCoreThread* threadContext, void (*run)(struct mCoreThread*));
72
73void mCoreThreadPause(struct mCoreThread* threadContext);
74void mCoreThreadUnpause(struct mCoreThread* threadContext);
75bool mCoreThreadIsPaused(struct mCoreThread* threadContext);
76void mCoreThreadTogglePause(struct mCoreThread* threadContext);
77void mCoreThreadPauseFromThread(struct mCoreThread* threadContext);
78
79struct mCoreThread* mCoreThreadGet(void);
80struct mLogger* mCoreThreadLogger(void);
81
82#endif