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 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 group, FiberContext ** src)
42		{
43			MT_ASSERT(group == TaskGroup::ASSIGN_FROM_CONTEXT, "Group must be assigned from context");
44			FiberContext * fiberContext = *src;
45			internal::GroupedTask groupedTask(fiberContext->currentTask, fiberContext->currentGroup);
46			groupedTask.awaitingFiber = fiberContext;
47			return groupedTask;
48		}
49
50		//template specialization for TaskHandle*
51		template<>
52		inline internal::GroupedTask GetGroupedTask(TaskGroup group, MT::TaskHandle * src)
53		{
54			MT_ASSERT(src->IsValid(), "Invalid task handle!");
55
56			const internal::TaskDesc & desc = src->GetDesc();
57			return internal::GroupedTask(desc, group);
58		}
59
60
61
62		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
63		// Distributes task to threads:
64		// | Task1 | Task2 | Task3 | Task4 | Task5 | Task6 |
65		// ThreadCount = 4
66		// Thread0: Task1, Task5
67		// Thread1: Task2, Task6
68		// Thread2: Task3
69		// Thread3: Task4
70		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
71		template<class TTask>
72		inline bool DistibuteDescriptions(TaskGroup group, TTask* taskArray, ArrayView<internal::GroupedTask>& descriptions, ArrayView<internal::TaskBucket>& buckets)
73		{
74			size_t index = 0;
75
76			for (size_t bucketIndex = 0; (bucketIndex < buckets.Size()) && (index < descriptions.Size()); ++bucketIndex)
77			{
78				size_t bucketStartIndex = index;
79
80				for (size_t i = bucketIndex; i < descriptions.Size(); i += buckets.Size())
81				{
82					descriptions[index] = GetGroupedTask(group, &taskArray[i]);
83					index++;
84				}
85
86				buckets[bucketIndex] = internal::TaskBucket(&descriptions[bucketStartIndex], index - bucketStartIndex);
87			}
88
89			MT_ASSERT(index == descriptions.Size(), "Sanity check");
90			return index > 0;
91		}
92
93	}
94
95
96
97
98
99	template<class TTask>
100	void TaskScheduler::RunAsync(TaskGroup group, TTask* taskArray, uint32 taskCount)
101	{
102		MT_ASSERT(!IsWorkerThread(), "Can't use RunAsync inside Task. Use FiberContext.RunAsync() instead.");
103
104		ArrayView<internal::GroupedTask> buffer(MT_ALLOCATE_ON_STACK(sizeof(internal::GroupedTask) * taskCount), taskCount);
105
106		size_t bucketCount = MT::Min(threadsCount, taskCount);
107		ArrayView<internal::TaskBucket> buckets(MT_ALLOCATE_ON_STACK(sizeof(internal::TaskBucket) * bucketCount), bucketCount);
108
109		internal::DistibuteDescriptions(group, taskArray, buffer, buckets);
110		RunTasksImpl(buckets, nullptr, false);
111	}
112
113}
114