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 		, stackRequirements(StackRequirements::INVALID)
31 		, childrenFibersCount(0)
32 		, parentFiber(nullptr)
33 	{
34 	}
35 
36 	void FiberContext::SetStatus(FiberTaskStatus::Type _taskStatus)
37 	{
38 		MT_ASSERT(threadContext, "Sanity check failed");
39 		MT_ASSERT(threadContext->threadId.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 		MT_ASSERT(childrenFibersCount.Load() == 0, "Can't release fiber with active children fibers");
66 		currentTask = internal::TaskDesc();
67 		parentFiber = nullptr;
68 		threadContext = nullptr;
69 		stackRequirements = StackRequirements::INVALID;
70 	}
71 
72 	void FiberContext::Yield()
73 	{
74 		taskStatus = FiberTaskStatus::YIELDED;
75 
76 		Fiber & schedulerFiber = threadContext->schedulerFiber;
77 
78 #ifdef MT_INSTRUMENTED_BUILD
79 		threadContext->NotifyTaskExecuteStateChanged( currentTask.debugColor, currentTask.debugID, TaskExecuteState::SUSPEND );
80 #endif
81 
82 		// Yielding, so reset thread context
83 		threadContext = nullptr;
84 
85 		//switch to scheduler
86 		Fiber::SwitchTo(fiber, schedulerFiber);
87 
88 #ifdef MT_INSTRUMENTED_BUILD
89 		threadContext->NotifyTaskExecuteStateChanged( currentTask.debugColor, currentTask.debugID, TaskExecuteState::RESUME );
90 #endif
91 	}
92 
93 	void FiberContext::RunSubtasksAndYieldImpl(ArrayView<internal::TaskBucket>& buckets)
94 	{
95 		MT_ASSERT(threadContext, "Sanity check failed!");
96 		MT_ASSERT(threadContext->taskScheduler, "Sanity check failed!");
97 		MT_ASSERT(threadContext->taskScheduler->IsWorkerThread(), "Can't use RunSubtasksAndYield outside Task. Use TaskScheduler.WaitGroup() instead.");
98 		MT_ASSERT(threadContext->threadId.IsCurrentThread(), "Thread context sanity check failed");
99 
100 		// add to scheduler
101 		threadContext->taskScheduler->RunTasksImpl(buckets, this, false);
102 
103 		//
104 		MT_ASSERT(threadContext->threadId.IsCurrentThread(), "Thread context sanity check failed");
105 
106 		// Change status
107 		taskStatus = FiberTaskStatus::AWAITING_CHILD;
108 
109 		Fiber & schedulerFiber = threadContext->schedulerFiber;
110 
111 #ifdef MT_INSTRUMENTED_BUILD
112 		threadContext->NotifyTaskExecuteStateChanged( currentTask.debugColor, currentTask.debugID, TaskExecuteState::SUSPEND );
113 #endif
114 
115 		// Yielding, so reset thread context
116 		threadContext = nullptr;
117 
118 		//switch to scheduler
119 		Fiber::SwitchTo(fiber, schedulerFiber);
120 
121 #ifdef MT_INSTRUMENTED_BUILD
122 		threadContext->NotifyTaskExecuteStateChanged( currentTask.debugColor, currentTask.debugID, TaskExecuteState::RESUME );
123 #endif
124 
125 	}
126 
127 
128 	void FiberContext::RunAsync(TaskGroup taskGroup, const TaskHandle* taskHandleArray, uint32 taskHandleCount)
129 	{
130 		MT_ASSERT(threadContext, "ThreadContext is nullptr");
131 		MT_ASSERT(threadContext->taskScheduler, "Sanity check failed!");
132 		MT_ASSERT(threadContext->taskScheduler->IsWorkerThread(), "Can't use RunAsync outside Task. Use TaskScheduler.RunAsync() instead.");
133 
134 		TaskScheduler& scheduler = *(threadContext->taskScheduler);
135 
136 		ArrayView<internal::GroupedTask> buffer(threadContext->descBuffer, taskHandleCount);
137 
138 		uint32 bucketCount = MT::Min((uint32)scheduler.GetWorkersCount(), taskHandleCount);
139 		ArrayView<internal::TaskBucket>	buckets(MT_ALLOCATE_ON_STACK(sizeof(internal::TaskBucket) * bucketCount), bucketCount);
140 
141 		internal::DistibuteDescriptions(taskGroup, taskHandleArray, buffer, buckets);
142 		scheduler.RunTasksImpl(buckets, nullptr, false);
143 	}
144 
145 
146 	void FiberContext::RunSubtasksAndYield(TaskGroup taskGroup, const TaskHandle* taskHandleArray, uint32 taskHandleCount)
147 	{
148 		MT_ASSERT(threadContext, "ThreadContext is nullptr");
149 		MT_ASSERT(threadContext->taskScheduler, "TaskScheduler is nullptr");
150 
151 		MT_ASSERT(taskHandleCount < internal::TASK_BUFFER_CAPACITY, "Buffer overrun!");
152 
153 		TaskScheduler& scheduler = *(threadContext->taskScheduler);
154 
155 		ArrayView<internal::GroupedTask> buffer(threadContext->descBuffer, taskHandleCount);
156 
157 		uint32 bucketCount = MT::Min((uint32)scheduler.GetWorkersCount(), taskHandleCount);
158 		ArrayView<internal::TaskBucket> buckets(MT_ALLOCATE_ON_STACK(sizeof(internal::TaskBucket) * bucketCount), bucketCount);
159 
160 		internal::DistibuteDescriptions(taskGroup, taskHandleArray, buffer, buckets);
161 		RunSubtasksAndYieldImpl(buckets);
162 	}
163 
164 
165 
166 }
167