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 #include <MTConfig.h>
26 #include <MTColorTable.h>
27 #include <MTTools.h>
28 #include <MTPlatform.h>
29 #include <MTQueueMPMC.h>
30 #include <MTStackArray.h>
31 #include <MTArrayView.h>
32 #include <MTThreadContext.h>
33 #include <MTFiberContext.h>
34 #include <MTAppInterop.h>
35 #include <MTTaskPool.h>
36 #include <MTStackRequirements.h>
37 #include <Scopes/MTScopes.h>
38 
39 
40 namespace MT
41 {
42 
43 	template<typename CLASS_TYPE, typename MACRO_TYPE>
44 	struct CheckType
45 	{
46 		static_assert(std::is_same<CLASS_TYPE, MACRO_TYPE>::value, "Invalid type in MT_DECLARE_TASK macro. See CheckType template instantiation params to details.");
47 	};
48 
49 	struct TypeChecker
50 	{
51 		template <typename T>
52 		static T QueryThisType(T thisPtr)
53 		{
54 			MT_UNUSED(thisPtr);
55 			return (T)nullptr;
56 		}
57 	};
58 
59 
60 	template <typename T>
61 	inline void CallDtor(T* p)
62 	{
63 		MT_UNUSED(p);
64 		p->~T();
65 	}
66 
67 }
68 
69 #if MT_MSVC_COMPILER_FAMILY
70 
71 // Visual Studio compile time check
72 #define MT_COMPILE_TIME_TYPE_CHECK(TYPE) \
73 	void CompileTimeCheckMethod() \
74 	{ \
75 		MT::CheckType< typename std::remove_pointer< decltype(MT::TypeChecker::QueryThisType(this)) >::type, typename TYPE > compileTypeTypesCheck; \
76 		compileTypeTypesCheck; \
77 	}
78 
79 #elif MT_GCC_COMPILER_FAMILY
80 
81 // GCC, Clang and other compilers compile time check
82 #define MT_COMPILE_TIME_TYPE_CHECK(TYPE) \
83 	void CompileTimeCheckMethod() \
84 	{ \
85 		/* query this pointer type */ \
86 		typedef decltype(MT::TypeChecker::QueryThisType(this)) THIS_PTR_TYPE; \
87 		/* query class type from this pointer type */ \
88 		typedef typename std::remove_pointer<THIS_PTR_TYPE>::type CPP_TYPE; \
89 		/* define macro type */ \
90 		typedef TYPE MACRO_TYPE; \
91 		/* compile time checking that is same types */ \
92 		MT::CheckType< CPP_TYPE, MACRO_TYPE > compileTypeTypesCheck; \
93 		/* remove unused variable warning */ \
94 		MT_UNUSED(compileTypeTypesCheck); \
95 	}
96 
97 #else
98 
99 #error Platform is not supported.
100 
101 #endif
102 
103 
104 #define MT_DECLARE_TASK_IMPL(TYPE, STACK_REQUIREMENTS) \
105 	\
106 	MT_COMPILE_TIME_TYPE_CHECK(TYPE) \
107 	\
108 	static void TaskEntryPoint(MT::FiberContext& fiberContext, const void* userData) \
109 	{ \
110 		/* C style cast */ \
111 		TYPE * task = (TYPE *)(userData); \
112 		task->Do(fiberContext); \
113 	} \
114 	\
115 	static void PoolTaskDestroy(const void* userData) \
116 	{ \
117 		/* C style cast */ \
118 		TYPE * task = (TYPE *)(userData); \
119 		MT::CallDtor( task ); \
120 		/* Find task pool header */ \
121 		MT::PoolElementHeader * poolHeader = (MT::PoolElementHeader *)((char*)userData - sizeof(MT::PoolElementHeader)); \
122 		/* Fixup pool header, mark task as unused */ \
123 		poolHeader->id.Store(MT::TaskID::UNUSED); \
124 	} \
125 	\
126 	static MT::StackRequirements::Type GetStackRequirements() \
127 	{ \
128 		return STACK_REQUIREMENTS; \
129 	} \
130 
131 
132 
133 #ifdef MT_INSTRUMENTED_BUILD
134 #include <MTProfilerEventListener.h>
135 
136 #define MT_DECLARE_TASK(TYPE, STACK_REQUIREMENTS, DEBUG_COLOR) \
137 	static const mt_char* GetDebugID() \
138 	{ \
139 		return MT_TEXT( #TYPE ); \
140 	} \
141 	\
142 	static MT::Color::Type GetDebugColor() \
143 	{ \
144 		return DEBUG_COLOR; \
145 	} \
146 	\
147 	MT_DECLARE_TASK_IMPL(TYPE, STACK_REQUIREMENTS);
148 
149 
150 #else
151 
152 #define MT_DECLARE_TASK(TYPE, STACK_REQUIREMENTS, DEBUG_COLOR) \
153 	MT_DECLARE_TASK_IMPL(TYPE, STACK_REQUIREMENTS);
154 
155 #endif
156 
157 
158 
159 
160 #if defined(MT_DEBUG) || defined(MT_INSTRUMENTED_BUILD)
161 #define MT_GROUP_DEBUG (1)
162 #endif
163 
164 
165 
166 namespace MT
167 {
168 	const uint32 MT_MAX_THREAD_COUNT = 64;
169 	const uint32 MT_SCHEDULER_STACK_SIZE = 1048576; // 1Mb
170 
171 	const uint32 MT_MAX_STANDART_FIBERS_COUNT = 256;
172 	const uint32 MT_STANDART_FIBER_STACK_SIZE = 32768; //32Kb
173 
174 	const uint32 MT_MAX_EXTENDED_FIBERS_COUNT = 8;
175 	const uint32 MT_EXTENDED_FIBER_STACK_SIZE = 1048576; // 1Mb
176 
177 	namespace internal
178 	{
179 		struct ThreadContext;
180 	}
181 
182 	struct WorkerThreadParams
183 	{
184 		uint32 core;
185 		ThreadPriority::Type priority;
186 
187 		WorkerThreadParams()
188 			: core(MT_CPUCORE_ANY)
189 			, priority(ThreadPriority::DEFAULT)
190 		{
191 		}
192 	};
193 
194 	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
195 	// Task scheduler
196 	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
197 	class TaskScheduler
198 	{
199 		friend class FiberContext;
200 		friend struct internal::ThreadContext;
201 
202 
203 
204 		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
205 		// Task group description
206 		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
207 		// Application can assign task group to task and later wait until group was finished.
208 		class TaskGroupDescription
209 		{
210 			Atomic32<int32> inProgressTaskCount;
211 			Event allDoneEvent;
212 
213 #if MT_GROUP_DEBUG
214 			bool debugIsFree;
215 #endif
216 
217 		public:
218 
219 			MT_NOCOPYABLE(TaskGroupDescription);
220 
221 			TaskGroupDescription()
222 			{
223 				inProgressTaskCount.Store(0);
224 				allDoneEvent.Create( EventReset::MANUAL, true );
225 
226 #if MT_GROUP_DEBUG
227 				debugIsFree = true;
228 #endif
229 			}
230 
231 			int GetTaskCount() const
232 			{
233 				return inProgressTaskCount.Load();
234 			}
235 
236 			int Dec()
237 			{
238 				return inProgressTaskCount.DecFetch();
239 			}
240 
241 			int Inc()
242 			{
243 				return inProgressTaskCount.IncFetch();
244 			}
245 
246 			int Add(int sum)
247 			{
248 				return inProgressTaskCount.AddFetch(sum);
249 			}
250 
251 			void Signal()
252 			{
253 				allDoneEvent.Signal();
254 			}
255 
256 			void Reset()
257 			{
258 				allDoneEvent.Reset();
259 			}
260 
261 			bool Wait(uint32 milliseconds)
262 			{
263 				return allDoneEvent.Wait(milliseconds);
264 			}
265 
266 #if MT_GROUP_DEBUG
267 			void SetDebugIsFree(bool _debugIsFree)
268 			{
269 				debugIsFree = _debugIsFree;
270 			}
271 
272 			bool GetDebugIsFree() const
273 			{
274 				return debugIsFree;
275 			}
276 #endif
277 		};
278 
279 
280 		// Thread index for new task
281 		Atomic32<int32> roundRobinThreadIndex;
282 
283 		// Started threads count
284 		Atomic32<int32> startedThreadsCount;
285 
286 		// Threads created by task manager
287 		Atomic32<int32> threadsCount;
288 		internal::ThreadContext threadContext[MT_MAX_THREAD_COUNT];
289 
290 		// All groups task statistic
291 		TaskGroupDescription allGroups;
292 
293 		// Groups pool
294 		LockFreeQueueMPMC<TaskGroup, TaskGroup::MT_MAX_GROUPS_COUNT * 2> availableGroups;
295 
296 		//
297 		TaskGroupDescription groupStats[TaskGroup::MT_MAX_GROUPS_COUNT];
298 
299 		// Fibers context
300 		FiberContext standartFiberContexts[MT_MAX_STANDART_FIBERS_COUNT];
301 		FiberContext extendedFiberContexts[MT_MAX_EXTENDED_FIBERS_COUNT];
302 
303 		// Fibers pool
304 		LockFreeQueueMPMC<FiberContext*, MT_MAX_STANDART_FIBERS_COUNT * 2> standartFibersAvailable;
305 		LockFreeQueueMPMC<FiberContext*, MT_MAX_EXTENDED_FIBERS_COUNT * 2> extendedFibersAvailable;
306 
307 #ifdef MT_INSTRUMENTED_BUILD
308 		IProfilerEventListener * profilerEventListener;
309 #endif
310 
311 		FiberContext* RequestFiberContext(internal::GroupedTask& task);
312 		void ReleaseFiberContext(FiberContext*&& fiberExecutionContext);
313 		void RunTasksImpl(ArrayView<internal::TaskBucket>& buckets, FiberContext * parentFiber, bool restoredFromAwaitState);
314 		TaskGroupDescription & GetGroupDesc(TaskGroup group);
315 
316 		static void WorkerThreadMain( void* userData );
317 		static void SchedulerFiberMain( void* userData );
318 		static void FiberMain( void* userData );
319 		static bool TryStealTask(internal::ThreadContext& threadContext, internal::GroupedTask & task, uint32 workersCount);
320 
321 		static FiberContext* ExecuteTask (internal::ThreadContext& threadContext, FiberContext* fiberContext);
322 
323 	public:
324 
325 		/// \brief Initializes a new instance of the TaskScheduler class.
326 		/// \param workerThreadsCount Worker threads count. Automatically determines the required number of threads if workerThreadsCount set to 0
327 #ifdef MT_INSTRUMENTED_BUILD
328 		TaskScheduler(uint32 workerThreadsCount = 0, WorkerThreadParams* workerParameters = nullptr, IProfilerEventListener* listener = nullptr);
329 #else
330 		TaskScheduler(uint32 workerThreadsCount = 0, WorkerThreadParams* workerParameters = nullptr);
331 #endif
332 
333 
334 		~TaskScheduler();
335 
336 		template<class TTask>
337 		void RunAsync(TaskGroup group, const TTask* taskArray, uint32 taskCount);
338 
339 		void RunAsync(TaskGroup group, const TaskHandle* taskHandleArray, uint32 taskHandleCount);
340 
341 		/// \brief Wait while no more tasks in specific group.
342 		/// \return true - if no more tasks in specific group. false - if timeout in milliseconds has reached and group still has some tasks.
343 		bool WaitGroup(TaskGroup group, uint32 milliseconds);
344 
345 		bool WaitAll(uint32 milliseconds);
346 
347 		TaskGroup CreateGroup();
348 		void ReleaseGroup(TaskGroup group);
349 
350 		bool IsEmpty();
351 
352 		int32 GetWorkersCount() const;
353 
354 		bool IsWorkerThread() const;
355 
356 #ifdef MT_INSTRUMENTED_BUILD
357 
358 		inline IProfilerEventListener* GetProfilerEventListener()
359 		{
360 			return profilerEventListener;
361 		}
362 
363 #endif
364 	};
365 }
366 
367 #include "MTScheduler.inl"
368 #include "MTFiberContext.inl"
369