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