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 #pragma once
24 
25 #include <MTTools.h>
26 #include <MTPlatform.h>
27 #include <MTConcurrentQueueLIFO.h>
28 #include <MTStackArray.h>
29 #include <MTWrapperArray.h>
30 #include <MTThreadContext.h>
31 #include <MTFiberContext.h>
32 #include <MTTaskBase.h>
33 
34 namespace MT
35 {
36 	const uint32 MT_MAX_THREAD_COUNT = 32;
37 	const uint32 MT_MAX_FIBERS_COUNT = 128;
38 	const uint32 MT_SCHEDULER_STACK_SIZE = 131072;
39 	const uint32 MT_FIBER_STACK_SIZE = 32768;
40 
41 	namespace internal
42 	{
43 		struct ThreadContext;
44 	}
45 
46 	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
47 	// Task scheduler
48 	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
49 	class TaskScheduler
50 	{
51 		friend class FiberContext;
52 		friend struct internal::ThreadContext;
53 
54 		struct GroupStats
55 		{
56 			AtomicInt inProgressTaskCount;
57 			Event allDoneEvent;
58 
59 			GroupStats()
60 			{
61 				inProgressTaskCount.Set(0);
62 				allDoneEvent.Create( EventReset::MANUAL, true );
63 			}
64 		};
65 
66 		// Thread index for new task
67 		AtomicInt roundRobinThreadIndex;
68 
69 		// Threads created by task manager
70 		uint32 threadsCount;
71 		internal::ThreadContext threadContext[MT_MAX_THREAD_COUNT];
72 
73 		// Per group task statistic
74 		GroupStats groupStats[TaskGroup::COUNT];
75 
76 		// All groups task statistic
77 		GroupStats allGroupStats;
78 
79 
80 		//Task awaiting group through FiberContext::WaitGroupAndYield call
81 		ConcurrentQueueLIFO<FiberContext*> waitTaskQueues[TaskGroup::COUNT];
82 
83 
84 		// Fibers pool
85 		ConcurrentQueueLIFO<FiberContext*> availableFibers;
86 
87 		// Fibers context
88 		FiberContext fiberContext[MT_MAX_FIBERS_COUNT];
89 
90 		FiberContext* RequestFiberContext(internal::GroupedTask& task);
91 		void ReleaseFiberContext(FiberContext* fiberExecutionContext);
92 		void RunTasksImpl(WrapperArray<internal::TaskBucket>& buckets, FiberContext * parentFiber, bool restoredFromAwaitState);
93 
94 		static void ThreadMain( void* userData );
95 		static void FiberMain( void* userData );
96 		static bool StealTask(internal::ThreadContext& threadContext, internal::GroupedTask & task);
97 
98 		static FiberContext* ExecuteTask (internal::ThreadContext& threadContext, FiberContext* fiberContext);
99 
100 	public:
101 
102 		TaskScheduler();
103 		~TaskScheduler();
104 
105 		template<class TTask>
106 		void RunAsync(TaskGroup::Type group, TTask* taskArray, uint32 taskCount);
107 
108 		bool WaitGroup(TaskGroup::Type group, uint32 milliseconds);
109 		bool WaitAll(uint32 milliseconds);
110 
111 		bool IsEmpty();
112 
113 		uint32 GetWorkerCount() const;
114 
115 		bool IsWorkerThread() const;
116 
117 #ifdef MT_INSTRUMENTED_BUILD
118 
119 		size_t GetProfilerEvents(uint32 workerIndex, MT::ProfileEventDesc * dstBuffer, size_t dstBufferSize);
120 
121 #endif
122 	};
123 }
124 
125 #include "MTScheduler.inl"
126 #include "MTFiberContext.inl"
127