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
23namespace MT
24{
25
26	namespace internal
27	{
28		template<class T>
29		inline internal::GroupedTask GetGroupedTask(TaskGroup::Type group, T * src)
30		{
31			internal::TaskDesc desc(T::TaskEntryPoint, (void*)(src));
32#ifdef MT_INSTRUMENTED_BUILD
33			desc.debugID = T::GetDebugID();
34			desc.colorIndex = T::GetDebugColorIndex();
35#endif
36			return internal::GroupedTask(desc, group);
37		}
38
39		//template specialization for FiberContext*
40		template<>
41		inline internal::GroupedTask GetGroupedTask(TaskGroup::Type group, FiberContext ** src)
42		{
43			MT_ASSERT(group == TaskGroup::GROUP_UNDEFINED, "Group must be GROUP_UNDEFINED");
44			FiberContext * fiberContext = *src;
45			internal::GroupedTask groupedTask(fiberContext->currentTask, fiberContext->currentGroup);
46			groupedTask.awaitingFiber = fiberContext;
47			return groupedTask;
48		}
49
50
51		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
52		// Distributes task to threads:
53		// | Task1 | Task2 | Task3 | Task4 | Task5 | Task6 |
54		// ThreadCount = 4
55		// Thread0: Task1, Task5
56		// Thread1: Task2, Task6
57		// Thread2: Task3
58		// Thread3: Task4
59		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
60		template<class TTask>
61		inline bool DistibuteDescriptions(TaskGroup::Type group, TTask* taskArray, WrapperArray<internal::GroupedTask>& descriptions, WrapperArray<internal::TaskBucket>& buckets)
62		{
63			size_t index = 0;
64
65			for (size_t bucketIndex = 0; (bucketIndex < buckets.Size()) && (index < descriptions.Size()); ++bucketIndex)
66			{
67				size_t bucketStartIndex = index;
68
69				for (size_t i = bucketIndex; i < descriptions.Size(); i += buckets.Size())
70				{
71					descriptions[index] = GetGroupedTask(group, &taskArray[i]);
72					index++;
73				}
74
75				buckets[bucketIndex] = internal::TaskBucket(&descriptions[bucketStartIndex], index - bucketStartIndex);
76			}
77
78			MT_ASSERT(index == descriptions.Size(), "Sanity check");
79			return index > 0;
80		}
81
82	}
83
84
85
86
87
88	template<class TTask>
89	void TaskScheduler::RunAsync(TaskGroup::Type group, TTask* taskArray, uint32 taskCount)
90	{
91		MT_ASSERT(!IsWorkerThread(), "Can't use RunAsync inside Task. Use FiberContext.RunAsync() instead.");
92
93		WrapperArray<internal::GroupedTask> buffer(MT_ALLOCATE_ON_STACK(sizeof(internal::GroupedTask) * taskCount), taskCount);
94
95		size_t bucketCount = MT::Min(threadsCount, taskCount);
96		WrapperArray<internal::TaskBucket> buckets(MT_ALLOCATE_ON_STACK(sizeof(internal::TaskBucket) * bucketCount), bucketCount);
97
98		internal::DistibuteDescriptions(group, taskArray, buffer, buckets);
99		RunTasksImpl(buckets, nullptr, false);
100	}
101
102}
103