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