1 // The MIT License (MIT)
2 //
3 // 	Copyright (c) 2015 Sergey Makeev, Vadim Slyusarev
4 //
5 // 	Permission is hereby granted, free of charge, to any person obtaining a copy
6 // 	of this software and associated documentation files (the "Software"), to deal
7 // 	in the Software without restriction, including without limitation the rights
8 // 	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // 	copies of the Software, and to permit persons to whom the Software is
10 // 	furnished to do so, subject to the following conditions:
11 //
12 //  The above copyright notice and this permission notice shall be included in
13 // 	all copies or substantial portions of the Software.
14 //
15 // 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // 	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // 	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // 	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // 	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // 	THE SOFTWARE.
22 
23 #pragma once
24 
25 #ifndef __MT_THREAD__
26 #define __MT_THREAD__
27 
28 #include <MTConfig.h>
29 #include <pthread.h>
30 #include <unistd.h>
31 #include <time.h>
32 #include <limits.h>
33 #include <stdlib.h>
34 
35 #if MT_PLATFORM_OSX
36 #include <thread>
37 #endif
38 
39 #include <sys/mman.h>
40 
41 #ifndef MAP_ANONYMOUS
42     #define MAP_ANONYMOUS MAP_ANON
43 #endif
44 
45 #ifndef MAP_STACK
46     #define MAP_STACK (0)
47 #endif
48 
49 #include <Platform/Common/MTThread.h>
50 #include <MTAppInterop.h>
51 
52 namespace MT
53 {
54 	class ThreadId
55 	{
56 		pthread_t id;
57 		Atomic32<uint32> isInitialized;
58 
59 	public:
60 
61 		MT_NOCOPYABLE(ThreadId);
62 
63 		ThreadId()
64 		{
65 			isInitialized.Store(0);
66 		}
67 
68 		void SetAsCurrentThread()
69 		{
70 			id = pthread_self();
71 			isInitialized.Store(1);
72 		}
73 
74 		bool IsCurrentThread() const
75 		{
76 			if (isInitialized.Load() == 0)
77 			{
78 				return false;
79 			}
80 
81 			pthread_t callThread = pthread_self();
82 			if (pthread_equal(callThread, id))
83 			{
84 				return true;
85 			}
86 			return false;
87 		}
88 	};
89 
90 	class Thread : public ThreadBase
91 	{
92 		pthread_t thread;
93 		pthread_attr_t threadAttr;
94 
95 		Memory::StackDesc stackDesc;
96 
97 		size_t stackSize;
98 
99 		bool isStarted;
100 
101 		static void* ThreadFuncInternal(void* pThread)
102 		{
103 			Thread* self = (Thread *)pThread;
104 			self->func(self->funcData);
105 			return nullptr;
106 		}
107 
108 #if MT_PLATFORM_OSX
109 		//TODO: support OSX priority and bind to processors
110 #else
111 		static void GetAffinityMask(cpu_set_t & cpu_mask, uint32 cpuCore)
112 		{
113 			CPU_ZERO(&cpu_mask);
114 
115 			if (cpuCore == MT_CPUCORE_ANY)
116 			{
117 				uint32 threadsCount = (uint32)GetNumberOfHardwareThreads();
118 				for(uint32 i = 0; i < threadsCount; i++)
119 				{
120 					CPU_SET(i, &cpu_mask);
121 				}
122 			} else
123 			{
124 				CPU_SET(cpuCore, &cpu_mask);
125 			}
126 		}
127 
128 
129 		static int GetPriority(ThreadPriority::Type priority)
130 		{
131 			int min_prio = sched_get_priority_min (SCHED_FIFO);
132 			int max_prio = sched_get_priority_max (SCHED_FIFO);
133 			int default_prio = (max_prio - min_prio) / 2;
134 
135 			switch(priority)
136 			{
137 			case ThreadPriority::DEFAULT:
138 				return default_prio;
139 			case ThreadPriority::HIGH:
140 				return max_prio;
141 			case ThreadPriority::LOW:
142 				return min_prio;
143 			default:
144 				MT_REPORT_ASSERT("Invalid thread priority");
145 			}
146 
147 			return default_prio;
148 		}
149 #endif
150 
151 
152 	public:
153 
154 		Thread()
155 			: stackSize(0)
156 			, isStarted(false)
157 		{
158 		}
159 
160 		void* GetStackBottom()
161 		{
162 			return stackDesc.stackBottom;
163 		}
164 
165 		size_t GetStackSize()
166 		{
167 			return stackSize;
168 		}
169 
170 
171 		void Start(size_t _stackSize, TThreadEntryPoint entryPoint, void* userData, uint32 cpuCore = MT_CPUCORE_ANY, ThreadPriority::Type priority = ThreadPriority::DEFAULT)
172 		{
173 			MT_ASSERT(!isStarted, "Thread already stared");
174 
175 			MT_ASSERT(func == nullptr, "Thread already started");
176 
177 			func = entryPoint;
178 			funcData = userData;
179 
180 			stackDesc = Memory::AllocStack(_stackSize);
181 			stackSize = stackDesc.GetStackSize();
182 
183 			MT_ASSERT(stackSize >= PTHREAD_STACK_MIN, "Thread stack to small");
184 
185 			int err = pthread_attr_init(&threadAttr);
186 			MT_USED_IN_ASSERT(err);
187 			MT_ASSERT(err == 0, "pthread_attr_init - error");
188 
189 			err = pthread_attr_setstack(&threadAttr, stackDesc.stackBottom, stackSize);
190 			MT_USED_IN_ASSERT(err);
191 			MT_ASSERT(err == 0, "pthread_attr_setstack - error");
192 
193 			err = pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_JOINABLE);
194 			MT_USED_IN_ASSERT(err);
195 			MT_ASSERT(err == 0, "pthread_attr_setdetachstate - error");
196 
197 #if MT_PLATFORM_OSX
198 			MT_UNUSED(cpuCore);
199 			MT_UNUSED(priority);
200 
201 			//TODO: support OSX priority and bind to processors
202 #else
203 			err = pthread_attr_setinheritsched(&threadAttr, PTHREAD_EXPLICIT_SCHED);
204 			MT_USED_IN_ASSERT(err);
205 			MT_ASSERT(err == 0, "pthread_attr_setinheritsched - error");
206 
207 			cpu_set_t cpu_mask;
208 			GetAffinityMask(cpu_mask, cpuCore);
209 			err = pthread_attr_setaffinity_np(&threadAttr, sizeof(cpu_mask), &cpu_mask);
210 			MT_USED_IN_ASSERT(err);
211 			MT_ASSERT(err == 0, "pthread_attr_setaffinity_np - error");
212 
213 			struct sched_param params;
214 			params.sched_priority = GetPriority(priority);
215 			err = pthread_attr_setschedparam(&threadAttr, &params);
216 			MT_USED_IN_ASSERT(err);
217 			MT_ASSERT(err == 0, "pthread_attr_setschedparam - error");
218 #endif
219 
220 			isStarted = true;
221 
222 			err = pthread_create(&thread, &threadAttr, ThreadFuncInternal, this);
223 			MT_USED_IN_ASSERT(err);
224 			MT_ASSERT(err == 0, "pthread_create - error");
225 		}
226 
227 		void Join()
228 		{
229 			MT_ASSERT(isStarted, "Thread is not started");
230 
231 			if (func == nullptr)
232 			{
233 				return;
234 			}
235 
236 			void *threadStatus = nullptr;
237 			int err = pthread_join(thread, &threadStatus);
238 			MT_USED_IN_ASSERT(err);
239 			MT_ASSERT(err == 0, "pthread_join - error");
240 
241 			err = pthread_attr_destroy(&threadAttr);
242 			MT_USED_IN_ASSERT(err);
243 			MT_ASSERT(err == 0, "pthread_attr_destroy - error");
244 
245 			func = nullptr;
246 			funcData = nullptr;
247 
248 			if (stackDesc.stackMemory != nullptr)
249 			{
250 				Memory::FreeStack(stackDesc);
251 			}
252 
253 			stackSize = 0;
254 			isStarted = false;
255 		}
256 
257 
258 		static int GetNumberOfHardwareThreads()
259 		{
260 #if MT_PLATFORM_OSX
261             return std::thread::hardware_concurrency();
262 #else
263 			long numberOfProcessors = sysconf( _SC_NPROCESSORS_ONLN );
264 			return (int)numberOfProcessors;
265 #endif
266 		}
267 
268 #ifdef MT_INSTRUMENTED_BUILD
269 		static void SetThreadName(const char* threadName)
270 		{
271 			pthread_t callThread = pthread_self();
272 			pthread_setname_np(callThread, threadName);
273 		}
274 #endif
275 
276 		static void SetThreadSchedulingPolicy(uint32 cpuCore, ThreadPriority::Type priority = ThreadPriority::DEFAULT)
277 		{
278 #if MT_PLATFORM_OSX
279 			MT_UNUSED(cpuCore);
280 			MT_UNUSED(priority);
281 
282 			//TODO: support OSX priority and bind to processors
283 #else
284 			pthread_t callThread = pthread_self();
285 
286 			int sched_priority = GetPriority(priority);
287 			int err = pthread_setschedprio(callThread, sched_priority);
288 			MT_USED_IN_ASSERT(err);
289 			MT_ASSERT(err == 0, "pthread_setschedprio - error");
290 
291 			cpu_set_t cpu_mask;
292 			GetAffinityMask(cpu_mask, cpuCore);
293 			err = pthread_setaffinity_np(callThread, sizeof(cpu_mask), &cpu_mask);
294 			MT_USED_IN_ASSERT(err);
295 			MT_ASSERT(err == 0, "pthread_setaffinity_np - error");
296 #endif
297 		}
298 
299 
300 		static void Sleep(uint32 milliseconds)
301 		{
302 			struct timespec req;
303 			time_t sec = (int)(milliseconds/1000);
304 			milliseconds = milliseconds - (sec*1000);
305 			req.tv_sec = sec;
306 			req.tv_nsec = milliseconds * 1000000L;
307 			while (nanosleep(&req,&req) == -1 )
308 			{
309 				continue;
310 			}
311 		}
312 
313 	};
314 
315 
316 }
317 
318 
319 #endif
320