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