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