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 
23 #include <MTScheduler.h>
24 
25 
26 
27 
28 
29 
30 
31 namespace MT
32 {
33 	namespace internal
34 	{
35 		static const size_t TASK_BUFFER_CAPACITY = 4096;
36 
37 		// Prime numbers for linear congruential generator seed
38 		static const uint32 primeNumbers[] = {
39 			128473, 135349, 159499, 173839, 209213, 241603, 292709, 314723,
40 			343943, 389299, 419473, 465169, 518327, 649921, 748271, 851087,
41 			862171, 974551, 1002973, 1034639, 1096289, 1153123, 1251037, 1299269,
42 			1272941, 1252151, 1231091, 1206761, 1185469, 1169933, 1141351, 1011583 };
43 
44 		uint32 GetPrimeNumber(uint32 index)
45 		{
46 			return primeNumbers[index % ARRAY_SIZE(primeNumbers)];
47 		}
48 
49 
50 
51 		ThreadContext::ThreadContext()
52 			: lastActiveFiberContext(nullptr)
53 			, taskScheduler(nullptr)
54 			, hasNewTasksEvent(EventReset::AUTOMATIC, true)
55 			, state(ThreadState::ALIVE)
56 			, descBuffer(TASK_BUFFER_CAPACITY)
57 			, workerIndex(0)
58 		{
59 		}
60 
61 		ThreadContext::~ThreadContext()
62 		{
63 		}
64 
65 		void ThreadContext::SetThreadIndex(uint32 threadIndex)
66 		{
67 			workerIndex = threadIndex;
68 			random.SetSeed( GetPrimeNumber(threadIndex) );
69 		}
70 
71 		void ThreadContext::RestoreAwaitingTasks(TaskGroup::Type taskGroup)
72 		{
73 			ASSERT(taskScheduler, "Invalid Task Scheduler");
74 			ASSERT(taskScheduler->IsWorkerThread(), "Can't use RunAsync outside Task. Use TaskScheduler.RunAsync() instead.");
75 
76 			ConcurrentQueueLIFO<FiberContext*> & groupQueue = taskScheduler->waitTaskQueues[taskGroup];
77 
78 			if (groupQueue.IsEmpty())
79 			{
80 				return;
81 			}
82 
83 			//copy awaiting tasks list to stack
84 			StackArray<FiberContext*, MT_MAX_FIBERS_COUNT> groupQueueCopy(MT_MAX_FIBERS_COUNT, nullptr);
85 			size_t taskCount = groupQueue.PopAll(groupQueueCopy.Begin(), groupQueueCopy.Size());
86 
87 			WrapperArray<internal::GroupedTask> buffer(&descBuffer.front(), taskCount);
88 
89 			TaskScheduler & scheduler = *(taskScheduler);
90 			size_t bucketCount = Min((size_t)scheduler.GetWorkerCount(), taskCount);
91 			WrapperArray<internal::TaskBucket>	buckets(ALLOCATE_ON_STACK(sizeof(internal::TaskBucket) * bucketCount), bucketCount);
92 
93 			internal::DistibuteDescriptions(TaskGroup::GROUP_UNDEFINED, groupQueueCopy.Begin(), buffer, buckets);
94 			scheduler.RunTasksImpl(buckets, nullptr, true);
95 		}
96 
97 	}
98 
99 }
100