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 <MTArrayView.h>
30 #include <MTThreadContext.h>
31 #include <MTFiberContext.h>
32 #include <MTTaskBase.h>
33 #include <MTAllocator.h>
34 #include <MTTaskPool.h>
35 
36 #ifdef MT_INSTRUMENTED_BUILD
37 #include <MTMicroWebSrv.h>
38 #include <MTProfilerEventListener.h>
39 #endif
40 
41 namespace MT
42 {
43 	const uint32 MT_MAX_THREAD_COUNT = 64;
44 	const uint32 MT_MAX_FIBERS_COUNT = 256;
45 	const uint32 MT_SCHEDULER_STACK_SIZE = 1048576;
46 	const uint32 MT_FIBER_STACK_SIZE = 65536;
47 
48 	namespace internal
49 	{
50 		struct ThreadContext;
51 	}
52 
53 	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
54 	// Task scheduler
55 	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
56 	class TaskScheduler
57 	{
58 		friend class FiberContext;
59 		friend struct internal::ThreadContext;
60 
61 
62 
63 		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
64 		// Task group description
65 		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
66 		// Application can assign task group to task and later wait until group was finished.
67 		class TaskGroupDescription
68 		{
69 			AtomicInt inProgressTaskCount;
70 			Event allDoneEvent;
71 
72 			//Tasks awaiting group through FiberContext::WaitGroupAndYield call
73 			ConcurrentQueueLIFO<FiberContext*> waitTasksQueue;
74 
75 		public:
76 
77 			bool debugIsFree;
78 
79 
80 		private:
81 
82 			TaskGroupDescription(TaskGroupDescription& ) {}
83 			void operator=(const TaskGroupDescription&) {}
84 
85 		public:
86 
87 			TaskGroupDescription()
88 			{
89 				inProgressTaskCount.Set(0);
90 				allDoneEvent.Create( EventReset::MANUAL, true );
91 				debugIsFree = true;
92 			}
93 
94 			int GetTaskCount() const { return inProgressTaskCount.Get(); }
95 			ConcurrentQueueLIFO<FiberContext*> & GetWaitQueue() { return waitTasksQueue; }
96 			int Dec() { return inProgressTaskCount.Dec(); }
97 			int Inc() { return inProgressTaskCount.Inc(); }
98 			int Add(int sum) { return inProgressTaskCount.Add(sum); }
99 			void Signal() { allDoneEvent.Signal(); }
100 			void Reset() { allDoneEvent.Reset(); }
101 			bool Wait(uint32 milliseconds) { return allDoneEvent.Wait(milliseconds); }
102 		};
103 
104 
105 		// Thread index for new task
106 		AtomicInt roundRobinThreadIndex;
107 
108 		// Started threads count
109 		AtomicInt startedThreadsCount;
110 
111 		// Threads created by task manager
112 		volatile uint32 threadsCount;
113 		internal::ThreadContext threadContext[MT_MAX_THREAD_COUNT];
114 
115 		// All groups task statistic
116 		TaskGroupDescription allGroups;
117 
118 		// Groups pool
119 		ConcurrentQueueLIFO<TaskGroup> availableGroups;
120 
121 		//
122 		TaskGroupDescription groupStats[TaskGroup::MT_MAX_GROUPS_COUNT];
123 
124 		// Fibers pool
125 		ConcurrentQueueLIFO<FiberContext*> availableFibers;
126 
127 		// Fibers context
128 		FiberContext fiberContext[MT_MAX_FIBERS_COUNT];
129 
130 #ifdef MT_INSTRUMENTED_BUILD
131 		IProfilerEventListener * profilerEventListener;
132 		int64 startTime;
133 		profile::MicroWebServer profilerWebServer;
134 		int32 webServerPort;
135 #endif
136 
137 		FiberContext* RequestFiberContext(internal::GroupedTask& task);
138 		void ReleaseFiberContext(FiberContext* fiberExecutionContext);
139 		void RunTasksImpl(ArrayView<internal::TaskBucket>& buckets, FiberContext * parentFiber, bool restoredFromAwaitState);
140 		TaskGroupDescription & GetGroupDesc(TaskGroup group);
141 
142 		static void ThreadMain( void* userData );
143 		static void FiberMain( void* userData );
144 		static bool TryStealTask(internal::ThreadContext& threadContext, internal::GroupedTask & task, uint32 workersCount);
145 
146 		static FiberContext* ExecuteTask (internal::ThreadContext& threadContext, FiberContext* fiberContext);
147 
148 	public:
149 
150 		/// \brief Initializes a new instance of the TaskScheduler class.
151 		/// \param workerThreadsCount Worker threads count. Automatically determines the required number of threads if workerThreadsCount set to 0
152 #ifdef MT_INSTRUMENTED_BUILD
153 		TaskScheduler(uint32 workerThreadsCount = 0, IProfilerEventListener* listener = nullptr);
154 #else
155 		TaskScheduler(uint32 workerThreadsCount = 0);
156 #endif
157 
158 
159 		~TaskScheduler();
160 
161 		template<class TTask>
162 		void RunAsync(TaskGroup group, TTask* taskArray, uint32 taskCount);
163 
164 		void RunAsync(TaskGroup group, TaskHandle* taskHandleArray, uint32 taskHandleCount);
165 
166 
167 		bool WaitGroup(TaskGroup group, uint32 milliseconds);
168 		bool WaitAll(uint32 milliseconds);
169 
170 		TaskGroup CreateGroup();
171 		void ReleaseGroup(TaskGroup group);
172 
173 		bool IsEmpty();
174 
175 		uint32 GetWorkerCount() const;
176 
177 		bool IsWorkerThread() const;
178 
179 #ifdef MT_INSTRUMENTED_BUILD
180 
181 		size_t GetProfilerEvents(uint32 workerIndex, MT::ProfileEventDesc * dstBuffer, size_t dstBufferSize);
182 		void UpdateProfiler();
183 		int32 GetWebServerPort() const;
184 
185 		inline int64 GetStartTime() const
186 		{
187 			return startTime;
188 		}
189 
190 		inline uint64 GetTimeStamp() const
191 		{
192 			return MT::GetTimeMicroSeconds() - startTime;
193 		}
194 
195 		inline IProfilerEventListener* GetProfilerEventListener()
196 		{
197 			return profilerEventListener;
198 		}
199 
200 #endif
201 	};
202 }
203 
204 #include "MTScheduler.inl"
205 #include "MTFiberContext.inl"
206