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 <MTConcurrentRingBuffer.h> 29 #include <MTGroupedTask.h> 30 31 namespace MT 32 { 33 class FiberContext; 34 class TaskScheduler; 35 36 37 #ifdef MT_INSTRUMENTED_BUILD 38 39 namespace ProfileEventType 40 { 41 enum Type 42 { 43 TASK_RESUME = 0, 44 TASK_YIELD = 1, 45 TASK_DONE = 2 46 }; 47 } 48 49 struct ProfileEventDesc 50 { 51 uint64 timeStampMicroSeconds; 52 const mt_char* id; 53 int colorIndex; 54 ProfileEventType::Type type; 55 }; 56 57 #endif 58 59 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 ConcurrentQueueLIFO<internal::GroupedTask> queue; 90 91 // new task was arrived to queue event 92 Event hasNewTasksEvent; 93 94 // whether thread is alive 95 AtomicInt32 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 107 #ifdef MT_INSTRUMENTED_BUILD 108 109 ConcurrentRingBuffer<ProfileEventDesc, 4096> profileEvents; 110 111 #endif 112 113 // prevent false sharing between threads 114 uint8 cacheline[64]; 115 116 ThreadContext(); 117 ~ThreadContext(); 118 119 void RestoreAwaitingTasks(TaskGroup taskGroup); 120 121 void SetThreadIndex(uint32 threadIndex); 122 123 #ifdef MT_INSTRUMENTED_BUILD 124 125 void NotifyThreadCreate(uint32 threadIndex); 126 void NotifyThreadStart(uint32 threadIndex); 127 void NotifyThreadStop(uint32 threadIndex); 128 129 void NotifyTaskFinished(const internal::TaskDesc & desc); 130 void NotifyTaskResumed(const internal::TaskDesc & desc); 131 void NotifyTaskYielded(const internal::TaskDesc & desc); 132 133 void NotifyThreadAwait(uint64 timeStampMicroSecondsFrom, uint64 timeStampMicroSecondsTo, uint32 threadIndex); 134 135 #endif 136 }; 137 138 } 139 140 } 141