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 <MTTaskBucket.h>
29 
30 
31 namespace MT
32 {
33 	class TaskHandle;
34 
35 	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
36 	// Fiber task status
37 	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
38 	// Task can be completed for several reasons.
39 	// For example task was done or someone call Yield from the Task body.
40 	namespace FiberTaskStatus
41 	{
42 		enum Type
43 		{
44 			UNKNOWN = 0,
45 			RUNNED = 1,
46 			FINISHED = 2,
47 			AWAITING_GROUP = 3,
48 			AWAITING_CHILD = 4,
49 		};
50 	}
51 
52 	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
53 	// Fiber context
54 	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
55 	// Context passed to fiber main function
56 	class FiberContext
57 	{
58 	private:
59 
60 		void RunSubtasksAndYieldImpl(ArrayView<internal::TaskBucket>& buckets);
61 
62 	public:
63 
64 		FiberContext();
65 
66 		template<class TTask>
67 		void RunSubtasksAndYield(TaskGroup taskGroup, const TTask* taskArray, size_t taskCount);
68 
69 		template<class TTask>
70 		void RunAsync(TaskGroup taskGroup, const TTask* taskArray, size_t taskCount);
71 
72 		//
73 		void RunAsync(TaskGroup taskGroup, const TaskHandle* taskHandleArray, uint32 taskHandleCount);
74 		void RunSubtasksAndYield(TaskGroup taskGroup, const TaskHandle* taskHandleArray, uint32 taskHandleCount);
75 
76 		void WaitGroupAndYield(TaskGroup group);
77 
78 		void Reset();
79 
80 		void SetThreadContext(internal::ThreadContext * _threadContext);
81 		internal::ThreadContext* GetThreadContext();
82 
83 		void SetStatus(FiberTaskStatus::Type _taskStatus);
84 		FiberTaskStatus::Type GetStatus() const;
85 
86 	private:
87 
88 		// Active thread context (null if fiber context is not executing now)
89 		internal::ThreadContext * threadContext;
90 
91 		// Active task status
92 		FiberTaskStatus::Type taskStatus;
93 
94 	public:
95 
96 		// Active task attached to this fiber
97 		internal::TaskDesc currentTask;
98 
99 		// Active task group
100 		TaskGroup currentGroup;
101 
102 		// Requirements for stack
103 		StackRequirements::Type stackRequirements;
104 
105 		// Number of children fibers
106 		AtomicInt32 childrenFibersCount;
107 
108 		// Parent fiber
109 		FiberContext* parentFiber;
110 
111 		// System fiber
112 		Fiber fiber;
113 
114 		// Prevent false sharing between threads
115 		uint8 cacheline[64];
116 	};
117 
118 
119 }
120