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