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