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 <MTTaskQueue.h> 28 #include <MTConcurrentRingBuffer.h> 29 #include <MTGroupedTask.h> 30 31 32 #ifdef MT_INSTRUMENTED_BUILD 33 34 #define MT_SYSTEM_TASK_COLOR (MT::Color::Yellow) 35 #define MT_SYSTEM_TASK_NAME "SchedulerTask" 36 #define MT_SYSTEM_TASK_FIBER_NAME "IdleFiber" 37 38 #endif 39 40 41 namespace MT 42 { 43 class FiberContext; 44 class TaskScheduler; 45 46 47 #ifdef MT_INSTRUMENTED_BUILD 48 namespace TaskExecuteState 49 { 50 enum Type 51 { 52 START = 0, 53 STOP = 1, 54 RESUME = 2, 55 SUSPEND = 3, 56 }; 57 } 58 59 #endif 60 61 namespace internal 62 { 63 static const size_t TASK_BUFFER_CAPACITY = 4096; 64 65 66 namespace ThreadState 67 { 68 const uint32 ALIVE = 0; 69 const uint32 EXIT = 1; 70 }; 71 72 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 73 // Thread (Scheduler fiber) context 74 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 75 struct ThreadContext 76 { 77 FiberContext* lastActiveFiberContext; 78 79 // pointer to task manager 80 TaskScheduler* taskScheduler; 81 82 // thread 83 Thread thread; 84 85 // scheduler fiber 86 Fiber schedulerFiber; 87 88 // task queue awaiting execution 89 TaskQueue<internal::GroupedTask> queue; 90 91 // new task has arrived to queue event 92 Event hasNewTasksEvent; 93 94 // thread is alive or not 95 Atomic32<int32> state; 96 97 // Temporary buffer, fixed size = TASK_BUFFER_CAPACITY 98 void* descBuffer; 99 100 // Thread index 101 uint32 workerIndex; 102 103 // Thread random number generator 104 LcgRandom random; 105 106 // prevent false cache sharing between threads 107 uint8 cacheline[64]; 108 109 ThreadContext(); 110 ~ThreadContext(); 111 112 void SetThreadIndex(uint32 threadIndex); 113 114 #ifdef MT_INSTRUMENTED_BUILD 115 116 void NotifyThreadCreate(uint32 threadIndex); 117 void NotifyThreadStart(uint32 threadIndex); 118 void NotifyThreadStop(uint32 threadIndex); 119 120 void NotifyTaskExecuteStateChanged(MT::Color::Type debugColor, const mt_char* debugID, TaskExecuteState::Type type); 121 122 void NotifyThreadIdleBegin(uint32 threadIndex); 123 void NotifyThreadIdleEnd(uint32 threadIndex); 124 125 #endif 126 }; 127 128 } 129 130 } 131