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