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 #define _DARWIN_C_SOURCE
40 #include <sys/mman.h>
41 
42 #ifndef MAP_ANONYMOUS
43     #define MAP_ANONYMOUS MAP_ANON
44 #endif
45 
46 #ifndef MAP_STACK
47     #define MAP_STACK (0)
48 #endif
49 
50 #include <Platform/Common/MTThread.h>
51 #include <MTAppInterop.h>
52 
53 namespace MT
54 {
55 	class _Fiber;
56 
57 	class Thread : public ThreadBase
58 	{
59 		pthread_t thread;
60 		pthread_attr_t threadAttr;
61 
62 		Memory::StackDesc stackDesc;
63 
64     size_t stackSize;
65 
66 		bool isStarted;
67 
68 		static void* ThreadFuncInternal(void* pThread)
69 		{
70 			Thread* self = (Thread *)pThread;
71 			self->func(self->funcData);
72 			return nullptr;
73 		}
74 
75 	public:
76 
77 		Thread()
78 			: stackSize(0)
79 			, isStarted(false)
80 		{
81 		}
82 
83 		void* GetStackBottom()
84 		{
85 			return stackDesc.stackBottom;
86 		}
87 
88 		size_t GetStackSize()
89 		{
90 			return stackSize;
91 		}
92 
93 
94 		void Start(size_t _stackSize, TThreadEntryPoint entryPoint, void* userData)
95 		{
96 			MT_ASSERT(!isStarted, "Thread already stared");
97 
98 			MT_ASSERT(func == nullptr, "Thread already started");
99 
100 			func = entryPoint;
101 			funcData = userData;
102 
103 			stackDesc = Memory::AllocStack(_stackSize);
104 			stackSize = stackDesc.GetStackSize();
105 
106 			MT_ASSERT(stackSize >= PTHREAD_STACK_MIN, "Thread stack to small");
107 
108 			int err = pthread_attr_init(&threadAttr);
109 			MT_USED_IN_ASSERT(err);
110 			MT_ASSERT(err == 0, "pthread_attr_init - error");
111 
112 			err = pthread_attr_setstack(&threadAttr, stackDesc.stackBottom, stackSize);
113 			MT_USED_IN_ASSERT(err);
114 			MT_ASSERT(err == 0, "pthread_attr_setstack - error");
115 
116 			err = pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_JOINABLE);
117 			MT_USED_IN_ASSERT(err);
118 			MT_ASSERT(err == 0, "pthread_attr_setdetachstate - error");
119 
120 			isStarted = true;
121 
122 			err = pthread_create(&thread, &threadAttr, ThreadFuncInternal, this);
123 			MT_USED_IN_ASSERT(err);
124 			MT_ASSERT(err == 0, "pthread_create - error");
125 		}
126 
127 		void Join()
128 		{
129 			MT_ASSERT(isStarted, "Thread is not started");
130 
131 			if (func == nullptr)
132 			{
133 				return;
134 			}
135 
136 			void *threadStatus = nullptr;
137 			int err = pthread_join(thread, &threadStatus);
138 			MT_USED_IN_ASSERT(err);
139 			MT_ASSERT(err == 0, "pthread_join - error");
140 
141 			err = pthread_attr_destroy(&threadAttr);
142 			MT_USED_IN_ASSERT(err);
143 			MT_ASSERT(err == 0, "pthread_attr_destroy - error");
144 
145 			func = nullptr;
146 			funcData = nullptr;
147 
148 			if (stackDesc.stackMemory != nullptr)
149 			{
150 				Memory::FreeStack(stackDesc);
151 			}
152 
153 			stackSize = 0;
154 			isStarted = false;
155 		}
156 
157 		bool IsCurrentThread() const
158 		{
159 			if(!isStarted)
160 			{
161 				return false;
162 			}
163 
164 			pthread_t callThread = pthread_self();
165 			if (pthread_equal(callThread, thread))
166 			{
167 					return true;
168 			}
169 			return false;
170 		}
171 
172 		static int GetNumberOfHardwareThreads()
173 		{
174 #if MT_PLATFORM_OSX
175             return std::thread::hardware_concurrency();
176 #else
177 			long numberOfProcessors = sysconf( _SC_NPROCESSORS_ONLN );
178 			return (int)numberOfProcessors;
179 #endif
180 		}
181 
182 		static void Sleep(uint32 milliseconds)
183 		{
184 			struct timespec req;
185 			time_t sec = (int)(milliseconds/1000);
186 			milliseconds = milliseconds - (sec*1000);
187 			req.tv_sec = sec;
188 			req.tv_nsec = milliseconds * 1000000L;
189 			while (nanosleep(&req,&req) == -1 )
190 			{
191 				continue;
192 			}
193 		}
194 
195 	};
196 
197 
198 }
199 
200 
201 #endif
202