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