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 ProfileEventType::Type type; 53 }; 54 55 #endif 56 57 58 59 namespace internal 60 { 61 namespace ThreadState 62 { 63 const uint32 ALIVE = 0; 64 const uint32 EXIT = 1; 65 }; 66 67 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 68 // Thread (Scheduler fiber) context 69 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 70 struct ThreadContext 71 { 72 FiberContext* lastActiveFiberContext; 73 74 // pointer to task manager 75 TaskScheduler* taskScheduler; 76 77 // thread 78 Thread thread; 79 80 // scheduler fiber 81 Fiber schedulerFiber; 82 83 // task queue awaiting execution 84 ConcurrentQueueLIFO<internal::GroupedTask> queue; 85 86 // new task was arrived to queue event 87 Event hasNewTasksEvent; 88 89 // whether thread is alive 90 AtomicInt state; 91 92 // Temporary buffer, fixed size = TASK_BUFFER_CAPACITY 93 std::vector<internal::GroupedTask> descBuffer; 94 95 // Thread index 96 uint32 workerIndex; 97 98 // Thread random number generator 99 LcgRandom random; 100 101 102 #ifdef MT_INSTRUMENTED_BUILD 103 104 ConcurrentRingBuffer<ProfileEventDesc, 4096> profileEvents; 105 106 #endif 107 108 // prevent false sharing between threads 109 uint8 cacheline[64]; 110 111 ThreadContext(); 112 ~ThreadContext(); 113 114 void RestoreAwaitingTasks(TaskGroup::Type taskGroup); 115 116 void SetThreadIndex(uint32 threadIndex); 117 118 #ifdef MT_INSTRUMENTED_BUILD 119 120 void NotifyTaskFinished(const internal::TaskDesc & desc); 121 void NotifyTaskResumed(const internal::TaskDesc & desc); 122 void NotifyTaskYielded(const internal::TaskDesc & desc); 123 124 #endif 125 }; 126 127 } 128 129 } 130