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