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 #include <MTScheduler.h> 24 25 namespace MT 26 { 27 namespace internal 28 { 29 // Prime numbers for linear congruential generator seed 30 static const uint32 primeNumbers[] = { 31 128473, 135349, 159499, 173839, 209213, 241603, 292709, 314723, 32 343943, 389299, 419473, 465169, 518327, 649921, 748271, 851087, 33 862171, 974551, 1002973, 1034639, 1096289, 1153123, 1251037, 1299269, 34 1272941, 1252151, 1231091, 1206761, 1185469, 1169933, 1141351, 1011583 }; 35 36 uint32 GetPrimeNumber(uint32 index) 37 { 38 return primeNumbers[index % MT_ARRAY_SIZE(primeNumbers)]; 39 } 40 41 42 43 ThreadContext::ThreadContext() 44 : lastActiveFiberContext(nullptr) 45 , taskScheduler(nullptr) 46 , hasNewTasksEvent(EventReset::AUTOMATIC, true) 47 , state(ThreadState::ALIVE) 48 , workerIndex(0) 49 { 50 descBuffer = Memory::Alloc( sizeof(internal::GroupedTask) * TASK_BUFFER_CAPACITY ); 51 } 52 53 ThreadContext::~ThreadContext() 54 { 55 Memory::Free(descBuffer); 56 descBuffer = nullptr; 57 } 58 59 void ThreadContext::SetThreadIndex(uint32 threadIndex) 60 { 61 workerIndex = threadIndex; 62 random.SetSeed( GetPrimeNumber(threadIndex) ); 63 } 64 65 #ifdef MT_INSTRUMENTED_BUILD 66 67 void ThreadContext::NotifyTaskBeginExecute(MT::Color::Type debugColor, const mt_char* debugID) 68 { 69 if (IProfilerEventListener* eventListener = taskScheduler->GetProfilerEventListener()) 70 { 71 eventListener->OnTaskBeginExecute(debugColor, debugID); 72 } 73 } 74 75 void ThreadContext::NotifyTaskEndExecute(MT::Color::Type debugColor, const mt_char* debugID) 76 { 77 if (IProfilerEventListener* eventListener = taskScheduler->GetProfilerEventListener()) 78 { 79 eventListener->OnTaskEndExecute(debugColor, debugID); 80 } 81 } 82 83 void ThreadContext::NotifyThreadCreate(uint32 threadIndex) 84 { 85 if (IProfilerEventListener* eventListener = taskScheduler->GetProfilerEventListener()) 86 { 87 eventListener->OnThreadCreated(threadIndex); 88 } 89 } 90 91 void ThreadContext::NotifyThreadStart(uint32 threadIndex) 92 { 93 if (IProfilerEventListener* eventListener = taskScheduler->GetProfilerEventListener()) 94 { 95 eventListener->OnThreadStarted(threadIndex); 96 } 97 } 98 99 void ThreadContext::NotifyThreadStop(uint32 threadIndex) 100 { 101 if (IProfilerEventListener* eventListener = taskScheduler->GetProfilerEventListener()) 102 { 103 eventListener->OnThreadStoped(threadIndex); 104 } 105 } 106 107 void ThreadContext::NotifyThreadIdleBegin(uint32 threadIndex) 108 { 109 if (IProfilerEventListener* eventListener = taskScheduler->GetProfilerEventListener()) 110 { 111 eventListener->OnThreadIdleBegin(threadIndex); 112 } 113 } 114 115 void ThreadContext::NotifyThreadIdleEnd(uint32 threadIndex) 116 { 117 if (IProfilerEventListener* eventListener = taskScheduler->GetProfilerEventListener()) 118 { 119 eventListener->OnThreadIdleEnd(threadIndex); 120 } 121 } 122 123 #endif 124 125 } 126 127 } 128