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