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 <ucontext.h>
26 #include <stdlib.h>
27 
28 namespace MT
29 {
30 
31 	//
32 	//
33 	//
34 	class Fiber
35 	{
36 		void * funcData;
37 		TThreadEntryPoint func;
38 
39 		ucontext_t fiberContext;
40 		bool isInitialized;
41 
42 		static void FiberFuncInternal(void *pFiber)
43 		{
44 			Fiber* self = (Fiber*)pFiber;
45 			self->func(self->funcData);
46 		}
47 
48 	private:
49 
50 		Fiber(const Fiber &) {}
51 		void operator=(const Fiber &) {}
52 
53 	public:
54 
55 		Fiber()
56 			: isInitialized(false)
57 		{
58 		}
59 
60 		~Fiber()
61 		{
62 			if (isInitialized)
63 			{
64 				if (func != nullptr)
65 				{
66 					free(fiberContext.uc_stack.ss_sp);
67 				}
68 				isInitialized = false;
69 			}
70 		}
71 
72 
73 		void CreateFromThread(Thread & thread)
74 		{
75 			ASSERT(!isInitialized, "Already initialized");
76 			ASSERT(thread.IsCurrentThread(), "Can't create fiber from this thread");
77 
78 			ucontext_t m;
79 			fiberContext.uc_link = &m;
80 
81 			int res = getcontext(&fiberContext);
82 			ASSERT(res == 0, "getcontext - failed");
83 
84 			fiberContext.uc_link = nullptr;
85 			fiberContext.uc_stack.ss_sp = thread.GetStackBase();
86 			fiberContext.uc_stack.ss_size = thread.GetStackSize();
87 			fiberContext.uc_stack.ss_flags = 0;
88 
89 			func = nullptr;
90 			funcData = nullptr;
91 
92 			isInitialized = true;
93 		}
94 
95 
96 		void Create(size_t stackSize, TThreadEntryPoint entryPoint, void *userData)
97 		{
98 			ASSERT(!isInitialized, "Already initialized");
99 
100 			func = entryPoint;
101 			funcData = userData;
102 
103 			int res = getcontext(&fiberContext);
104 			ASSERT(res == 0, "getcontext - failed");
105 
106 			fiberContext.uc_link = nullptr;
107 			fiberContext.uc_stack.ss_sp = malloc(stackSize);
108 			fiberContext.uc_stack.ss_size = stackSize;
109 			fiberContext.uc_stack.ss_flags = 0;
110 			makecontext(&fiberContext, (void(*)())&FiberFuncInternal, 1, this);
111 
112 			isInitialized = true;
113 		}
114 
115 		static void SwitchTo(Fiber & from, Fiber & to)
116 		{
117 			 __sync_synchronize();
118 
119 			ASSERT(from.isInitialized, "Invalid from fiber");
120 			ASSERT(to.isInitialized, "Invalid to fiber");
121 			int res = swapcontext(&from.fiberContext, &to.fiberContext);
122 			ASSERT(res == 0, "setcontext - failed");
123 		}
124 
125 
126 
127 	};
128 
129 
130 }
131 
132 
133