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 #include <MTScheduler.h>
24 #include <string.h> // for memset
25 
26 namespace MT
27 {
28 
29 #ifdef MT_INSTRUMENTED_BUILD
30 	TaskScheduler::TaskScheduler(uint32 workerThreadsCount, IProfilerEventListener* listener)
31 #else
32 	TaskScheduler::TaskScheduler(uint32 workerThreadsCount)
33 #endif
34 		: roundRobinThreadIndex(0)
35 		, startedThreadsCount(0)
36 	{
37 
38 #ifdef MT_INSTRUMENTED_BUILD
39 		profilerEventListener = listener;
40 #endif
41 
42 		if (workerThreadsCount != 0)
43 		{
44 			threadsCount = MT::Clamp(workerThreadsCount, (uint32)1, (uint32)MT_MAX_THREAD_COUNT);
45 		} else
46 		{
47 			//query number of processor
48 			threadsCount = (uint32)MT::Clamp(Thread::GetNumberOfHardwareThreads() - 2, 1, (int)MT_MAX_THREAD_COUNT);
49 		}
50 
51 		// create fiber pool
52 		for (uint32 i = 0; i < MT_MAX_FIBERS_COUNT; i++)
53 		{
54 			FiberContext& context = fiberContext[i];
55 			context.fiber.Create(MT_FIBER_STACK_SIZE, FiberMain, &context);
56 			availableFibers.Push( &context );
57 		}
58 
59 		for (uint32 i = 0; i < TaskGroup::MT_MAX_GROUPS_COUNT; i++)
60 		{
61 			if (i != TaskGroup::DEFAULT)
62 			{
63 				availableGroups.Push( TaskGroup(i) );
64 			}
65 		}
66 
67 		groupStats[TaskGroup::DEFAULT].debugIsFree = false;
68 
69 		// create worker thread pool
70 		for (uint32 i = 0; i < threadsCount; i++)
71 		{
72 			threadContext[i].SetThreadIndex(i);
73 			threadContext[i].taskScheduler = this;
74 			threadContext[i].thread.Start( MT_SCHEDULER_STACK_SIZE, ThreadMain, &threadContext[i] );
75 		}
76 	}
77 
78 	TaskScheduler::~TaskScheduler()
79 	{
80 		for (uint32 i = 0; i < threadsCount; i++)
81 		{
82 			threadContext[i].state.Store(internal::ThreadState::EXIT);
83 			threadContext[i].hasNewTasksEvent.Signal();
84 		}
85 
86 		for (uint32 i = 0; i < threadsCount; i++)
87 		{
88 			threadContext[i].thread.Stop();
89 		}
90 	}
91 
92 	FiberContext* TaskScheduler::RequestFiberContext(internal::GroupedTask& task)
93 	{
94 		FiberContext *fiberContext = task.awaitingFiber;
95 		if (fiberContext)
96 		{
97 			task.awaitingFiber = nullptr;
98 			return fiberContext;
99 		}
100 
101 		if (!availableFibers.TryPopBack(fiberContext))
102 		{
103 			MT_REPORT_ASSERT("Fibers pool is empty. Too many fibers running simultaneously.");
104 		}
105 
106 		fiberContext->currentTask = task.desc;
107 		fiberContext->currentGroup = task.group;
108 		fiberContext->parentFiber = task.parentFiber;
109 		return fiberContext;
110 	}
111 
112 	void TaskScheduler::ReleaseFiberContext(FiberContext* fiberContext)
113 	{
114 		MT_ASSERT(fiberContext != nullptr, "Can't release nullptr Fiber");
115 		fiberContext->Reset();
116 		availableFibers.Push(fiberContext);
117 	}
118 
119 	FiberContext* TaskScheduler::ExecuteTask(internal::ThreadContext& threadContext, FiberContext* fiberContext)
120 	{
121 		MT_ASSERT(threadContext.thread.IsCurrentThread(), "Thread context sanity check failed");
122 
123 		MT_ASSERT(fiberContext, "Invalid fiber context");
124 		MT_ASSERT(fiberContext->currentTask.IsValid(), "Invalid task");
125 
126 		// Set actual thread context to fiber
127 		fiberContext->SetThreadContext(&threadContext);
128 
129 		// Update task status
130 		fiberContext->SetStatus(FiberTaskStatus::RUNNED);
131 
132 		MT_ASSERT(fiberContext->GetThreadContext()->thread.IsCurrentThread(), "Thread context sanity check failed");
133 
134 		void * poolUserData = fiberContext->currentTask.userData;
135 		TPoolTaskDestroy poolDestroyFunc = fiberContext->currentTask.poolDestroyFunc;
136 
137 		// Run current task code
138 		Fiber::SwitchTo(threadContext.schedulerFiber, fiberContext->fiber);
139 
140 		// If task was done
141 		FiberTaskStatus::Type taskStatus = fiberContext->GetStatus();
142 		if (taskStatus == FiberTaskStatus::FINISHED)
143 		{
144 			//destroy task (call dtor) for "fire and forget" type of task from TaskPool
145 			if (poolDestroyFunc != nullptr)
146 			{
147 				poolDestroyFunc(poolUserData);
148 			}
149 
150 
151 			TaskGroup taskGroup = fiberContext->currentGroup;
152 
153 			TaskScheduler::TaskGroupDescription  & groupDesc = threadContext.taskScheduler->GetGroupDesc(taskGroup);
154 
155 			// Update group status
156 			int groupTaskCount = groupDesc.Dec();
157 			MT_ASSERT(groupTaskCount >= 0, "Sanity check failed!");
158 			if (groupTaskCount == 0)
159 			{
160 				// Restore awaiting tasks
161 				threadContext.RestoreAwaitingTasks(taskGroup);
162 
163 				// All restored tasks can be already finished on this line.
164 				// That's why you can't release groups from worker threads, if worker thread release group, than you can't Signal to released group.
165 
166 				// Signal pending threads that group work is finished. Group can be destroyed after this call.
167 				groupDesc.Signal();
168 
169 				fiberContext->currentGroup = TaskGroup::INVALID;
170 			}
171 
172 			// Update total task count
173 			int allGroupTaskCount = threadContext.taskScheduler->allGroups.Dec();
174 			MT_ASSERT(allGroupTaskCount >= 0, "Sanity check failed!");
175 			if (allGroupTaskCount == 0)
176 			{
177 				// Notify all tasks in all group finished
178 				threadContext.taskScheduler->allGroups.Signal();
179 			}
180 
181 			FiberContext* parentFiberContext = fiberContext->parentFiber;
182 			if (parentFiberContext != nullptr)
183 			{
184 				int childrenFibersCount = parentFiberContext->childrenFibersCount.DecFetch();
185 				MT_ASSERT(childrenFibersCount >= 0, "Sanity check failed!");
186 
187 				if (childrenFibersCount == 0)
188 				{
189 					// This is a last subtask. Restore parent task
190 					MT_ASSERT(threadContext.thread.IsCurrentThread(), "Thread context sanity check failed");
191 					MT_ASSERT(parentFiberContext->GetThreadContext() == nullptr, "Inactive parent should not have a valid thread context");
192 
193 					// WARNING!! Thread context can changed here! Set actual current thread context.
194 					parentFiberContext->SetThreadContext(&threadContext);
195 
196 					MT_ASSERT(parentFiberContext->GetThreadContext()->thread.IsCurrentThread(), "Thread context sanity check failed");
197 
198 					// All subtasks is done.
199 					// Exiting and return parent fiber to scheduler
200 					return parentFiberContext;
201 				} else
202 				{
203 					// Other subtasks still exist
204 					// Exiting
205 					return nullptr;
206 				}
207 			} else
208 			{
209 				// Task is finished and no parent task
210 				// Exiting
211 				return nullptr;
212 			}
213 		}
214 
215 		MT_ASSERT(taskStatus != FiberTaskStatus::RUNNED, "Incorrect task status")
216 		return nullptr;
217 	}
218 
219 
220 	void TaskScheduler::FiberMain(void* userData)
221 	{
222 		FiberContext& fiberContext = *(FiberContext*)(userData);
223 		for(;;)
224 		{
225 			MT_ASSERT(fiberContext.currentTask.IsValid(), "Invalid task in fiber context");
226 			MT_ASSERT(fiberContext.GetThreadContext(), "Invalid thread context");
227 			MT_ASSERT(fiberContext.GetThreadContext()->thread.IsCurrentThread(), "Thread context sanity check failed");
228 
229 			fiberContext.currentTask.taskFunc( fiberContext, fiberContext.currentTask.userData );
230 
231 			fiberContext.SetStatus(FiberTaskStatus::FINISHED);
232 
233 #ifdef MT_INSTRUMENTED_BUILD
234 			fiberContext.GetThreadContext()->NotifyTaskFinished(fiberContext.currentTask);
235 #endif
236 
237 			Fiber::SwitchTo(fiberContext.fiber, fiberContext.GetThreadContext()->schedulerFiber);
238 		}
239 
240 	}
241 
242 
243 	bool TaskScheduler::TryStealTask(internal::ThreadContext& threadContext, internal::GroupedTask & task, uint32 workersCount)
244 	{
245 		if (workersCount <= 1)
246 		{
247 			return false;
248 		}
249 
250 		uint32 victimIndex = threadContext.random.Get();
251 
252 		for (uint32 attempt = 0; attempt < workersCount; attempt++)
253 		{
254 			uint32 index = victimIndex % workersCount;
255 			if (index == threadContext.workerIndex)
256 			{
257 				victimIndex++;
258 				index = victimIndex % workersCount;
259 			}
260 
261 			internal::ThreadContext& victimContext = threadContext.taskScheduler->threadContext[index];
262 			if (victimContext.queue.TryPopFront(task))
263 			{
264 				return true;
265 			}
266 
267 			victimIndex++;
268 		}
269 		return false;
270 	}
271 
272 	void TaskScheduler::ThreadMain( void* userData )
273 	{
274 		internal::ThreadContext& context = *(internal::ThreadContext*)(userData);
275 		MT_ASSERT(context.taskScheduler, "Task scheduler must be not null!");
276 
277 #ifdef MT_INSTRUMENTED_BUILD
278 		context.NotifyThreadCreate(context.workerIndex);
279 #endif
280 
281 		context.schedulerFiber.CreateFromThread(context.thread);
282 
283 		uint32 workersCount = context.taskScheduler->GetWorkerCount();
284 
285 		context.taskScheduler->startedThreadsCount.IncFetch();
286 
287 		//Spinlock until all threads started and initialized
288 		for(;;)
289 		{
290 			if (context.taskScheduler->startedThreadsCount.Load() == (int)context.taskScheduler->threadsCount)
291 			{
292 				break;
293 			}
294 			Thread::Sleep(1);
295 		}
296 
297 
298 #ifdef MT_INSTRUMENTED_BUILD
299 		context.NotifyThreadStart(context.workerIndex);
300 #endif
301 
302 		while(context.state.Load() != internal::ThreadState::EXIT)
303 		{
304 			internal::GroupedTask task;
305 			if (context.queue.TryPopBack(task) || TryStealTask(context, task, workersCount) )
306 			{
307 				// There is a new task
308 				FiberContext* fiberContext = context.taskScheduler->RequestFiberContext(task);
309 				MT_ASSERT(fiberContext, "Can't get execution context from pool");
310 				MT_ASSERT(fiberContext->currentTask.IsValid(), "Sanity check failed");
311 
312 				while(fiberContext)
313 				{
314 #ifdef MT_INSTRUMENTED_BUILD
315 					context.NotifyTaskResumed(fiberContext->currentTask);
316 #endif
317 
318 					// prevent invalid fiber resume from child tasks, before ExecuteTask is done
319 					fiberContext->childrenFibersCount.IncFetch();
320 
321 					FiberContext* parentFiber = ExecuteTask(context, fiberContext);
322 
323 					FiberTaskStatus::Type taskStatus = fiberContext->GetStatus();
324 
325 					//release guard
326 					int childrenFibersCount = fiberContext->childrenFibersCount.DecFetch();
327 
328 					// Can drop fiber context - task is finished
329 					if (taskStatus == FiberTaskStatus::FINISHED)
330 					{
331 						MT_ASSERT( childrenFibersCount == 0, "Sanity check failed");
332 						context.taskScheduler->ReleaseFiberContext(fiberContext);
333 
334 						// If parent fiber is exist transfer flow control to parent fiber, if parent fiber is null, exit
335 						fiberContext = parentFiber;
336 					} else
337 					{
338 						MT_ASSERT( childrenFibersCount >= 0, "Sanity check failed");
339 
340 						// No subtasks here and status is not finished, this mean all subtasks already finished before parent return from ExecuteTask
341 						if (childrenFibersCount == 0)
342 						{
343 							MT_ASSERT(parentFiber == nullptr, "Sanity check failed");
344 						} else
345 						{
346 							// If subtasks still exist, drop current task execution. task will be resumed when last subtask finished
347 							break;
348 						}
349 
350 						// If task is in await state drop execution. task will be resumed when RestoreAwaitingTasks called
351 						if (taskStatus == FiberTaskStatus::AWAITING_GROUP)
352 						{
353 							break;
354 						}
355 					}
356 				} //while(fiberContext)
357 
358 			} else
359 			{
360 #ifdef MT_INSTRUMENTED_BUILD
361 				context.NotifyThreadIdleBegin(context.workerIndex);
362 #endif
363 
364 				// Queue is empty and stealing attempt failed
365 				// Wait new events
366 				context.hasNewTasksEvent.Wait(2000);
367 
368 #ifdef MT_INSTRUMENTED_BUILD
369 				context.NotifyThreadIdleEnd(context.workerIndex);
370 #endif
371 
372 			}
373 
374 		} // main thread loop
375 
376 #ifdef MT_INSTRUMENTED_BUILD
377 		context.NotifyThreadStop(context.workerIndex);
378 #endif
379 
380 	}
381 
382 	void TaskScheduler::RunTasksImpl(ArrayView<internal::TaskBucket>& buckets, FiberContext * parentFiber, bool restoredFromAwaitState)
383 	{
384 		// This storage is necessary to calculate how many tasks we add to different groups
385 		int newTaskCountInGroup[TaskGroup::MT_MAX_GROUPS_COUNT];
386 
387 		// Default value is 0
388 		memset(&newTaskCountInGroup[0], 0, sizeof(newTaskCountInGroup));
389 
390 		// Set parent fiber pointer
391 		// Calculate the number of tasks per group
392 		// Calculate total number of tasks
393 		size_t count = 0;
394 		for (size_t i = 0; i < buckets.Size(); ++i)
395 		{
396 			internal::TaskBucket& bucket = buckets[i];
397 			for (size_t taskIndex = 0; taskIndex < bucket.count; taskIndex++)
398 			{
399 				internal::GroupedTask & task = bucket.tasks[taskIndex];
400 
401 				task.parentFiber = parentFiber;
402 
403 				int idx = task.group.GetValidIndex();
404 				MT_ASSERT(idx >= 0 && idx < TaskGroup::MT_MAX_GROUPS_COUNT, "Invalid index");
405 				newTaskCountInGroup[idx]++;
406 			}
407 
408 			count += bucket.count;
409 		}
410 
411 		// Increments child fibers count on parent fiber
412 		if (parentFiber)
413 		{
414 			parentFiber->childrenFibersCount.AddFetch((int)count);
415 		}
416 
417 		if (restoredFromAwaitState == false)
418 		{
419 			// Increase the number of active tasks in the group using data from temporary storage
420 			for (size_t i = 0; i < TaskGroup::MT_MAX_GROUPS_COUNT; i++)
421 			{
422 				int groupNewTaskCount = newTaskCountInGroup[i];
423 				if (groupNewTaskCount > 0)
424 				{
425 					groupStats[i].Reset();
426 					groupStats[i].Add((uint32)groupNewTaskCount);
427 				}
428 			}
429 
430 			// Increments all task in progress counter
431 			allGroups.Reset();
432 			allGroups.Add((uint32)count);
433 		} else
434 		{
435 			// If task's restored from await state, counters already in correct state
436 		}
437 
438 		// Add to thread queue
439 		for (size_t i = 0; i < buckets.Size(); ++i)
440 		{
441 			int bucketIndex = roundRobinThreadIndex.IncFetch() % threadsCount;
442 			internal::ThreadContext & context = threadContext[bucketIndex];
443 
444 			internal::TaskBucket& bucket = buckets[i];
445 
446 			context.queue.PushRange(bucket.tasks, bucket.count);
447 			context.hasNewTasksEvent.Signal();
448 		}
449 	}
450 
451 	void TaskScheduler::RunAsync(TaskGroup group, TaskHandle* taskHandleArray, uint32 taskHandleCount)
452 	{
453 		MT_ASSERT(!IsWorkerThread(), "Can't use RunAsync inside Task. Use FiberContext.RunAsync() instead.");
454 
455 		ArrayView<internal::GroupedTask> buffer(MT_ALLOCATE_ON_STACK(sizeof(internal::GroupedTask) * taskHandleCount), taskHandleCount);
456 
457 		size_t bucketCount = MT::Min(threadsCount, taskHandleCount);
458 		ArrayView<internal::TaskBucket> buckets(MT_ALLOCATE_ON_STACK(sizeof(internal::TaskBucket) * bucketCount), bucketCount);
459 
460 		internal::DistibuteDescriptions(group, taskHandleArray, buffer, buckets);
461 		RunTasksImpl(buckets, nullptr, false);
462 	}
463 
464 	bool TaskScheduler::WaitGroup(TaskGroup group, uint32 milliseconds)
465 	{
466 		MT_VERIFY(IsWorkerThread() == false, "Can't use WaitGroup inside Task. Use FiberContext.WaitGroupAndYield() instead.", return false);
467 
468 		TaskScheduler::TaskGroupDescription  & groupDesc = GetGroupDesc(group);
469 		return groupDesc.Wait(milliseconds);
470 	}
471 
472 	bool TaskScheduler::WaitAll(uint32 milliseconds)
473 	{
474 		MT_VERIFY(IsWorkerThread() == false, "Can't use WaitAll inside Task.", return false);
475 
476 		return allGroups.Wait(milliseconds);
477 	}
478 
479 	bool TaskScheduler::IsEmpty()
480 	{
481 		for (uint32 i = 0; i < MT_MAX_THREAD_COUNT; i++)
482 		{
483 			if (!threadContext[i].queue.IsEmpty())
484 			{
485 				return false;
486 			}
487 		}
488 		return true;
489 	}
490 
491 	uint32 TaskScheduler::GetWorkerCount() const
492 	{
493 		return threadsCount;
494 	}
495 
496 	bool TaskScheduler::IsWorkerThread() const
497 	{
498 		for (uint32 i = 0; i < MT_MAX_THREAD_COUNT; i++)
499 		{
500 			if (threadContext[i].thread.IsCurrentThread())
501 			{
502 				return true;
503 			}
504 		}
505 		return false;
506 	}
507 
508 	TaskGroup TaskScheduler::CreateGroup()
509 	{
510 		MT_ASSERT(IsWorkerThread() == false, "Can't use CreateGroup inside Task.");
511 
512 		TaskGroup group;
513 		if (!availableGroups.TryPopBack(group))
514 		{
515 			MT_REPORT_ASSERT("Group pool is empty");
516 		}
517 
518 		int idx = group.GetValidIndex();
519 
520 		MT_ASSERT(groupStats[idx].debugIsFree == true, "Bad logic!");
521 		groupStats[idx].debugIsFree = false;
522 
523 		return group;
524 	}
525 
526 	void TaskScheduler::ReleaseGroup(TaskGroup group)
527 	{
528 		MT_ASSERT(IsWorkerThread() == false, "Can't use ReleaseGroup inside Task.");
529 		MT_ASSERT(group.IsValid(), "Invalid group ID");
530 
531 		int idx = group.GetValidIndex();
532 
533 		MT_ASSERT(groupStats[idx].debugIsFree == false, "Group already released");
534 		groupStats[idx].debugIsFree = true;
535 
536 		availableGroups.Push(group);
537 	}
538 
539 	TaskScheduler::TaskGroupDescription & TaskScheduler::GetGroupDesc(TaskGroup group)
540 	{
541 		MT_ASSERT(group.IsValid(), "Invalid group ID");
542 
543 		int idx = group.GetValidIndex();
544 		TaskScheduler::TaskGroupDescription & groupDesc = groupStats[idx];
545 
546 		MT_ASSERT(groupDesc.debugIsFree == false, "Invalid group");
547 		return groupDesc;
548 	}
549 
550 }
551