summaryrefslogtreecommitdiff
path: root/Runtime/Threads/Thread.cpp
blob: c9b008c3b9f8c2a28373e2f631c273e761015a5a (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
113
114
115
116
117
118
119
120
121
122
123
#include "UnityPrefix.h"

#if SUPPORT_THREADS
#include "Thread.h"
#include "ThreadHelper.h"
#include "ThreadUtility.h"
#include "Runtime/Utilities/Word.h"
#include "Runtime/Utilities/Utility.h"
#include "Runtime/Allocator/MemoryManager.h"

#if !UNITY_EXTERNAL_TOOL
#include "Runtime/Utilities/LogAssert.h"
#include "Runtime/Allocator/MemoryMacros.h" 
#endif

#if SUPPORT_ERROR_EXIT
#include "Runtime/Utilities/ErrorExit.h"
#endif

Thread::ThreadID Thread::mainThreadId = Thread::GetCurrentThreadID();

UNITY_THREAD_FUNCTION_RETURN_SIGNATURE Thread::RunThreadWrapper (void* ptr)
{
	Thread* thread = (Thread*)ptr;
	
	#if ENABLE_MEMORY_MANAGER
	GetMemoryManager().ThreadInitialize();
	#endif

	thread->m_Thread.Enter(thread);				// PlatformThread (Posix/Winapi/Custom)

	ThreadHelper::SetThreadName(thread);

	void *result = NULL;
#if SUPPORT_ERROR_EXIT
	ERROR_EXIT_THREAD_ENTRY();
	result = thread->m_EntryPoint(thread->m_UserData);
	ERROR_EXIT_THREAD_EXIT();
#else // SUPPORT_ERROR_EXIT
	result = thread->m_EntryPoint(thread->m_UserData);
#endif // SUPPORT_ERROR_EXIT

	// NOTE: code below will not execute if thread is terminated
	thread->m_Running = false;
	UnityMemoryBarrier();
	
	#if ENABLE_MEMORY_MANAGER
	GetMemoryManager().ThreadCleanup();
	#endif

	thread->m_Thread.Exit(thread, result);				// PlatformThread (Posix/Winapi/Custom)

	return reinterpret_cast<UNITY_THREAD_FUNCTION_RETURNTYPE>(result);
}

Thread::Thread ()
:	m_UserData(NULL)
,	m_EntryPoint(NULL)
,	m_Running(false)
,	m_ShouldQuit(false)
,	m_Priority(kNormalPriority)
,	m_Name(NULL)
{
}

Thread::~Thread()
{
	AssertMsg(!m_Running, "***Thread '%s' is still running!***", m_Name);
}

void Thread::Run(void* (*entry_point) (void*), void* data, const UInt32 stackSize, int processor)
{
	Assert(!m_Running);

	m_ShouldQuit = false;
	m_UserData = data;
	m_EntryPoint = entry_point;
	m_Running = true;

	m_Thread.Create(this, stackSize, processor);
}

void Thread::WaitForExit(bool signalQuit)
{
	if (m_Running && signalQuit)
			SignalQuit();

	m_Thread.Join(this);				// PlatformThread (Posix/Winapi/Custom)

	Assert(!m_Running && "Thread shouldn't be running anymore");
	m_Running = false;
}

Thread::ThreadID Thread::GetCurrentThreadID()	
{
	return PlatformThread::GetCurrentThreadID();
}

void Thread::Sleep (double time)
{
	ThreadHelper::Sleep(time);
}

void Thread::SetPriority(ThreadPriority prio)
{
	if (!m_Running || m_Priority == prio)
		return;

	m_Priority = prio;
	m_Thread.UpdatePriority(this);				// PlatformThread (Posix/Winapi/Custom)
}

void Thread::SetCurrentThreadProcessor(int processor)
{
	ThreadHelper::SetThreadProcessor(NULL, processor);
}

double Thread::GetThreadRunningTime(ThreadID threadId)
{
	return ThreadHelper::GetThreadRunningTime(threadId);
}

#endif