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 #ifndef __MT_FIBER_DEFAULT__
26 #define __MT_FIBER_DEFAULT__
27 
28 #include "MTAtomic.h"
29 
30 namespace MT
31 {
32 
33 	inline void* FiberGetSelf()
34 	{
35 		MW_BOOL isThreadAFiber = ::IsThreadAFiber();
36 		if  (isThreadAFiber == 0)
37 		{
38 			// GetCurrentFiber() return invalid values is current thread is not fiber.
39 			return nullptr;
40 		}
41 
42 		//This function is equal to GetCurrentFiber() macro
43 		void* pFiber = (void*)ReadTeb(MW_CURRENT_FIBER_OFFSET);
44 		return pFiber;
45 	}
46 
47 
48 	//
49 	// Fibers implementation using system fibers
50 	// Beware! Windows Fibers are wasteful use of Virtual Memory space for the stack. ( 1Mb reserved for each Fiber )
51 	//
52 	class Fiber
53 	{
54 		void* funcData;
55 		TThreadEntryPoint func;
56 
57 		void* fiber;
58 
59 		static void __stdcall FiberFuncInternal(void* pFiber)
60 		{
61 			Fiber* self = (Fiber*)pFiber;
62 			self->func(self->funcData);
63 		}
64 
65 		void CleanUp()
66 		{
67 			if (fiber)
68 			{
69 				// Do not destroy fibers created using ::ConvertThreadToFiberEx
70 				if (func != nullptr)
71 				{
72 					::DeleteFiber(fiber);
73 				}
74 				fiber = nullptr;
75 			}
76 		}
77 
78 	public:
79 
80 		MT_NOCOPYABLE(Fiber);
81 
82 		Fiber()
83 			: fiber(nullptr)
84 		{
85 		}
86 
87 		~Fiber()
88 		{
89 			CleanUp();
90 		}
91 
92 
93 		void CreateFromCurrentThreadAndRun(TThreadEntryPoint entryPoint, void *userData)
94 		{
95 			MT_ASSERT(fiber == nullptr, "Fiber already created");
96 
97 			func = nullptr;
98 			funcData = nullptr;
99 
100 			void* fiberSelf = FiberGetSelf();
101 			if (fiberSelf != nullptr)
102 			{
103 				fiber = fiberSelf;
104 			} else
105 			{
106 				fiber = ::ConvertThreadToFiberEx(nullptr, MW_FIBER_FLAG_FLOAT_SWITCH);
107 				MT_ASSERT(fiber != nullptr, "Can't create fiber");
108 			}
109 
110 			entryPoint(userData);
111 
112 			CleanUp();
113 		}
114 
115 
116 		void Create(size_t stackSize, TThreadEntryPoint entryPoint, void* userData)
117 		{
118 			MT_ASSERT(fiber == nullptr, "Fiber already created");
119 
120 			func = entryPoint;
121 			funcData = userData;
122 			fiber = ::CreateFiber( stackSize, FiberFuncInternal, this );
123 			MT_ASSERT(fiber != nullptr, "Can't create fiber");
124 		}
125 
126 #ifdef MT_INSTRUMENTED_BUILD
127 		void SetName(const char* fiberName)
128 		{
129 			MT_UNUSED(fiberName);
130 		}
131 #endif
132 
133 		static void SwitchTo(Fiber & from, Fiber & to)
134 		{
135 			MT_USED_IN_ASSERT(from);
136 
137 			HardwareFullMemoryBarrier();
138 
139 			MT_ASSERT(from.fiber != nullptr, "Invalid from fiber");
140 			MT_ASSERT(to.fiber != nullptr, "Invalid to fiber");
141 
142 			::SwitchToFiber( (void*)to.fiber );
143 		}
144 
145 
146 	};
147 
148 }
149 
150 #endif
151