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