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