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 	//
34 	// Fibers implementation using system fibers
35 	// Beware! Windows Fibers are wasteful use of Virtual Memory space for the stack. ( 1Mb reserved for each Fiber )
36 	//
37 	class Fiber
38 	{
39 		void* funcData;
40 		TThreadEntryPoint func;
41 
42 		void* fiber;
43 
44 		static void __stdcall FiberFuncInternal(void* pFiber)
45 		{
46 			Fiber* self = (Fiber*)pFiber;
47 			self->func(self->funcData);
48 		}
49 
50 	public:
51 
52 		MT_NOCOPYABLE(Fiber);
53 
54 		Fiber()
55 			: fiber(nullptr)
56 		{
57 		}
58 
59 		~Fiber()
60 		{
61 			if (fiber)
62 			{
63 				if (func)
64 				{
65 					DeleteFiber(fiber);
66 				}
67 				fiber = nullptr;
68 			}
69 		}
70 
71 
72 		void CreateFromCurrentThreadAndRun(TThreadEntryPoint entryPoint, void *userData)
73 		{
74 			MT_ASSERT(fiber == nullptr, "Fiber already created");
75 
76 			func = nullptr;
77 			funcData = nullptr;
78 
79 			fiber = ::ConvertThreadToFiberEx(nullptr, MW_FIBER_FLAG_FLOAT_SWITCH);
80 			MT_ASSERT(fiber != nullptr, "Can't create fiber");
81 
82 			entryPoint(userData);
83 		}
84 
85 
86 		void Create(size_t stackSize, TThreadEntryPoint entryPoint, void* userData)
87 		{
88 			MT_ASSERT(fiber == nullptr, "Fiber already created");
89 
90 			func = entryPoint;
91 			funcData = userData;
92 			fiber = ::CreateFiber( stackSize, FiberFuncInternal, this );
93 			MT_ASSERT(fiber != nullptr, "Can't create fiber");
94 		}
95 
96 #ifdef MT_INSTRUMENTED_BUILD
97 		void SetName(const char* fiberName)
98 		{
99 			MT_UNUSED(fiberName);
100 		}
101 #endif
102 
103 		static void SwitchTo(Fiber & from, Fiber & to)
104 		{
105 			MT_USED_IN_ASSERT(from);
106 
107 			HardwareFullMemoryBarrier();
108 
109 			MT_ASSERT(from.fiber != nullptr, "Invalid from fiber");
110 			MT_ASSERT(to.fiber != nullptr, "Invalid to fiber");
111 
112 			::SwitchToFiber( (void*)to.fiber );
113 		}
114 
115 
116 	};
117 
118 }
119 
120 #endif
121