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 = 32;
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 		// Thread index for new task
60 		AtomicInt roundRobinThreadIndex;
61 
62 		// Threads created by task manager
63 		uint32 threadsCount;
64 		internal::ThreadContext threadContext[MT_MAX_THREAD_COUNT];
65 
66 		// All groups task statistic
67 		TaskGroup allGroups;
68 
69 		// Fibers pool
70 		ConcurrentQueueLIFO<FiberContext*> availableFibers;
71 
72 		// Fibers context
73 		FiberContext fiberContext[MT_MAX_FIBERS_COUNT];
74 
75 #ifdef MT_INSTRUMENTED_BUILD
76 		int32 webServerPort;
77 		profile::MicroWebServer profilerWebServer;
78 		int64 startTime;
79 #endif
80 
81 		FiberContext* RequestFiberContext(internal::GroupedTask& task);
82 		void ReleaseFiberContext(FiberContext* fiberExecutionContext);
83 		void RunTasksImpl(ArrayView<internal::TaskBucket>& buckets, FiberContext * parentFiber, bool restoredFromAwaitState);
84 
85 		static void ThreadMain( void* userData );
86 		static void FiberMain( void* userData );
87 		static bool TryStealTask(internal::ThreadContext& threadContext, internal::GroupedTask & task, uint32 workersCount);
88 
89 		static FiberContext* ExecuteTask (internal::ThreadContext& threadContext, FiberContext* fiberContext);
90 
91 	public:
92 
93 		/// \brief Initializes a new instance of the TaskScheduler class.
94 		/// \param workerThreadsCount Worker threads count. Automatically determines the required number of threads if workerThreadsCount set to 0
95 		TaskScheduler(uint32 workerThreadsCount = 0);
96 		~TaskScheduler();
97 
98 		template<class TTask>
99 		void RunAsync(TaskGroup* group, TTask* taskArray, uint32 taskCount);
100 
101 		bool WaitGroup(TaskGroup* group, uint32 milliseconds);
102 		bool WaitAll(uint32 milliseconds);
103 
104 		bool IsEmpty();
105 
106 		uint32 GetWorkerCount() const;
107 
108 		bool IsWorkerThread() const;
109 
110 #ifdef MT_INSTRUMENTED_BUILD
111 
112 		size_t GetProfilerEvents(uint32 workerIndex, MT::ProfileEventDesc * dstBuffer, size_t dstBufferSize);
113 		void UpdateProfiler();
114 		int32 GetWebServerPort() const;
115 
116 		inline int64 GetStartTime() const
117 		{
118 			return startTime;
119 		}
120 
121 #endif
122 	};
123 }
124 
125 #include "MTScheduler.inl"
126 #include "MTFiberContext.inl"
127