all repos — mgba @ e1325b03735fb0fcec8539485cc42a6322c05403

mGBA Game Boy Advance Emulator

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	void* userData;
 38	void (*run)(struct mCoreThread*);
 39
 40	struct mCoreThreadInternal* impl;
 41};
 42
 43#ifndef OPAQUE_THREADING
 44#include <mgba/core/rewind.h>
 45#include <mgba/core/sync.h>
 46#include <mgba-util/threading.h>
 47
 48enum mCoreThreadState {
 49	THREAD_INITIALIZED = -1,
 50	THREAD_RUNNING = 0,
 51	THREAD_REWINDING,
 52	THREAD_MAX_RUNNING = THREAD_REWINDING,
 53	THREAD_INTERRUPTED,
 54	THREAD_INTERRUPTING,
 55	THREAD_PAUSED,
 56	THREAD_PAUSING,
 57	THREAD_RUN_ON,
 58	THREAD_WAITING,
 59	THREAD_RESETING,
 60	THREAD_EXITING,
 61	THREAD_SHUTDOWN,
 62	THREAD_CRASHED
 63};
 64
 65struct mCoreThreadInternal {
 66	Thread thread;
 67	enum mCoreThreadState state;
 68
 69	Mutex stateMutex;
 70	Condition stateCond;
 71	enum mCoreThreadState savedState;
 72	int interruptDepth;
 73	bool frameWasOn;
 74
 75	struct mCoreSync sync;
 76	struct mCoreRewindContext rewind;
 77};
 78
 79#endif
 80
 81bool mCoreThreadStart(struct mCoreThread* threadContext);
 82bool mCoreThreadHasStarted(struct mCoreThread* threadContext);
 83bool mCoreThreadHasExited(struct mCoreThread* threadContext);
 84bool mCoreThreadHasCrashed(struct mCoreThread* threadContext);
 85void mCoreThreadMarkCrashed(struct mCoreThread* threadContext);
 86void mCoreThreadEnd(struct mCoreThread* threadContext);
 87void mCoreThreadReset(struct mCoreThread* threadContext);
 88void mCoreThreadJoin(struct mCoreThread* threadContext);
 89
 90bool mCoreThreadIsActive(struct mCoreThread* threadContext);
 91void mCoreThreadInterrupt(struct mCoreThread* threadContext);
 92void mCoreThreadInterruptFromThread(struct mCoreThread* threadContext);
 93void mCoreThreadContinue(struct mCoreThread* threadContext);
 94
 95void mCoreThreadRunFunction(struct mCoreThread* threadContext, void (*run)(struct mCoreThread*));
 96
 97void mCoreThreadPause(struct mCoreThread* threadContext);
 98void mCoreThreadUnpause(struct mCoreThread* threadContext);
 99bool mCoreThreadIsPaused(struct mCoreThread* threadContext);
100void mCoreThreadTogglePause(struct mCoreThread* threadContext);
101void mCoreThreadPauseFromThread(struct mCoreThread* threadContext);
102void mCoreThreadWaitFromThread(struct mCoreThread* threadContext);
103void mCoreThreadStopWaiting(struct mCoreThread* threadContext);
104
105void mCoreThreadSetRewinding(struct mCoreThread* threadContext, bool);
106
107struct mCoreThread* mCoreThreadGet(void);
108struct mLogger* mCoreThreadLogger(void);
109
110CXX_GUARD_END
111
112#endif