summaryrefslogtreecommitdiff
path: root/Runtime/Threads/Thread.h
blob: 2be0d8a0afc99ce87960ea4fd10fcbe48bd44cdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef THREAD_H
#define THREAD_H

enum ThreadPriority { kLowPriority = 0, kBelowNormalPriority = 1, kNormalPriority = 2, kHighPriority = 4 };

#if SUPPORT_THREADS

#define ASSERT_RUNNING_ON_MAIN_THREAD Assert(Thread::EqualsCurrentThreadIDForAssert(Thread::mainThreadId));

#if !UNITY_PLUGIN && !UNITY_EXTERNAL_TOOL
#include "Configuration/UnityConfigure.h"
#endif

#if UNITY_WIN || UNITY_XENON
#	include "Winapi/PlatformThread.h"
#elif UNITY_OSX || UNITY_PS3 || UNITY_IPHONE || UNITY_ANDROID || UNITY_PEPPER || UNITY_LINUX || UNITY_BB10 || UNITY_TIZEN
#	include "Posix/PlatformThread.h"
#else
#	include "PlatformThread.h"
#endif


#ifndef DEFAULT_UNITY_THREAD_STACK_SIZE
#define DEFAULT_UNITY_THREAD_STACK_SIZE 0
#endif

#ifndef DEFAULT_UNITY_THREAD_PROCESSOR
#define DEFAULT_UNITY_THREAD_PROCESSOR -1
#endif

#include "Runtime/Utilities/NonCopyable.h"

/**
 *  A thread.
 */
class EXPORT_COREMODULE Thread : public NonCopyable
{
	friend class ThreadHelper;
	friend class PlatformThread;

public:
	typedef PlatformThread::ThreadID ThreadID;

public:
	Thread();
	~Thread(); 

	void Run (void* (*entry_point) (void*), void* data, const UInt32 stackSize = DEFAULT_UNITY_THREAD_STACK_SIZE, int processor = DEFAULT_UNITY_THREAD_PROCESSOR);
	bool IsRunning() const { return m_Running; }
	
	// Was the thread told to stop running?
	bool IsQuitSignaled () const { return m_ShouldQuit; }
	// Tells the thread to stop running, the thread main loop is responsible for checking this variable
	void SignalQuit () { m_ShouldQuit = true; }
	
	// Signals quit and waits until the thread main function is exited!
	void WaitForExit(bool signalQuit = true);

	void SetName (const char* name) { m_Name = name; }

	static void Sleep (double seconds);
	
	void SetPriority (ThreadPriority prior);
	ThreadPriority GetPriority () const { return m_Priority; }
	
	static void SetCurrentThreadProcessor(int processor);
	
	static ThreadID GetCurrentThreadID();
	static bool EqualsCurrentThreadID(ThreadID thread) { return GetCurrentThreadID() == thread; }
	static bool EqualsCurrentThreadIDForAssert(ThreadID thread) { return EqualsCurrentThreadID(thread); }
	static bool EqualsThreadID (Thread::ThreadID lhs, Thread::ThreadID rhs) { return lhs == rhs; }
	
	void ExternalEarlySetRunningFalse() { m_Running = false; }
	
	static double GetThreadRunningTime(ThreadID thread);
	
	static ThreadID mainThreadId;
	
	static bool CurrentThreadIsMainThread() { return EqualsCurrentThreadID(mainThreadId); }
	
private:
	PlatformThread	m_Thread;

	void*     m_UserData;
	void* (*m_EntryPoint) (void*);

	volatile bool	m_Running;
	volatile bool	m_ShouldQuit;
	ThreadPriority m_Priority;
	const char*	m_Name;

	static UNITY_THREAD_FUNCTION_RETURN_SIGNATURE RunThreadWrapper (void* ptr);
};


#else // SUPPORT_THREADS


// No threads in this platform, stub minimal functionality

#define ASSERT_RUNNING_ON_MAIN_THREAD

class Thread {
public:
	static void Sleep (double t) { }
	static bool CurrentThreadIsMainThread() { return true; }
};


#endif //SUPPORT_THREADS

#endif