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