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 FiberContext::FiberContext() 28 : threadContext(nullptr) 29 , taskStatus(FiberTaskStatus::UNKNOWN) 30 , currentGroup(TaskGroup::GROUP_UNDEFINED) 31 , childrenFibersCount(0) 32 , parentFiber(nullptr) 33 { 34 } 35 36 void FiberContext::SetStatus(FiberTaskStatus::Type _taskStatus) 37 { 38 ASSERT(threadContext, "Sanity check failed"); 39 ASSERT(threadContext->thread.IsCurrentThread(), "You can change task status only from owner thread"); 40 taskStatus = _taskStatus; 41 } 42 43 FiberTaskStatus::Type FiberContext::GetStatus() const 44 { 45 return taskStatus; 46 } 47 48 void FiberContext::SetThreadContext(internal::ThreadContext * _threadContext) 49 { 50 if (_threadContext) 51 { 52 _threadContext->lastActiveFiberContext = this; 53 } 54 55 threadContext = _threadContext; 56 } 57 58 internal::ThreadContext* FiberContext::GetThreadContext() 59 { 60 return threadContext; 61 } 62 63 void FiberContext::Reset() 64 { 65 ASSERT(childrenFibersCount.Get() == 0, "Can't release fiber with active children fibers"); 66 67 currentGroup = TaskGroup::GROUP_UNDEFINED; 68 currentTask = internal::TaskDesc(); 69 parentFiber = nullptr; 70 threadContext = nullptr; 71 } 72 73 void FiberContext::WaitGroupAndYield(TaskGroup::Type group) 74 { 75 ASSERT(threadContext, "Sanity check failed!"); 76 ASSERT(threadContext->taskScheduler->IsWorkerThread(), "Can't use WaitGroupAndYield outside Task. Use TaskScheduler.WaitGroup() instead."); 77 ASSERT(threadContext->thread.IsCurrentThread(), "Thread context sanity check failed"); 78 79 VERIFY(group != currentGroup, "Can't wait the same group. Deadlock detected!", return); 80 VERIFY(group < TaskGroup::COUNT, "Invalid group!", return); 81 82 ConcurrentQueueLIFO<FiberContext*> & groupQueue = threadContext->taskScheduler->waitTaskQueues[group]; 83 84 // Change status 85 taskStatus = FiberTaskStatus::AWAITING_GROUP; 86 87 // Add current fiber to awaiting queue 88 groupQueue.Push(this); 89 90 Fiber & schedulerFiber = threadContext->schedulerFiber; 91 92 // Yielding, so reset thread context 93 threadContext = nullptr; 94 95 //switch to scheduler 96 Fiber::SwitchTo(fiber, schedulerFiber); 97 } 98 99 void FiberContext::RunSubtasksAndYieldImpl(fixed_array<internal::TaskBucket>& buckets) 100 { 101 ASSERT(threadContext, "Sanity check failed!"); 102 ASSERT(threadContext->taskScheduler->IsWorkerThread(), "Can't use RunSubtasksAndYield outside Task. Use TaskScheduler.WaitGroup() instead."); 103 ASSERT(threadContext->thread.IsCurrentThread(), "Thread context sanity check failed"); 104 105 // add to scheduler 106 threadContext->taskScheduler->RunTasksImpl(buckets, this, false); 107 108 // 109 ASSERT(threadContext->thread.IsCurrentThread(), "Thread context sanity check failed"); 110 111 // Change status 112 taskStatus = FiberTaskStatus::AWAITING_CHILD; 113 114 Fiber & schedulerFiber = threadContext->schedulerFiber; 115 116 // Yielding, so reset thread context 117 threadContext = nullptr; 118 119 //switch to scheduler 120 Fiber::SwitchTo(fiber, schedulerFiber); 121 } 122 123 124 } 125