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__ 26 #define __MT_FIBER__ 27 28 //#define MT_USE_BOOST_CONTEXT (1) 29 30 31 32 #if MT_USE_BOOST_CONTEXT 33 34 #include <fcontext.h> 35 36 //TODO 37 38 #else 39 40 41 #ifndef _XOPEN_SOURCE 42 #define _XOPEN_SOURCE 43 #endif 44 45 #include <ucontext.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <pthread.h> 49 #include <sys/mman.h> 50 51 #ifndef MAP_ANONYMOUS 52 #define MAP_ANONYMOUS MAP_ANON 53 #endif 54 55 #ifndef MAP_STACK 56 #define MAP_STACK (0) 57 #endif 58 59 #include <MTAppInterop.h> 60 #include "MTAtomic.h" 61 62 #endif 63 64 65 namespace MT 66 { 67 68 // 69 // 70 // 71 class Fiber 72 { 73 void* funcData; 74 TThreadEntryPoint func; 75 76 Memory::StackDesc stackDesc; 77 78 ucontext_t fiberContext; 79 bool isInitialized; 80 FiberFuncInternal(void * pFiber)81 static void FiberFuncInternal(void* pFiber) 82 { 83 MT_ASSERT(pFiber != nullptr, "Invalid fiber"); 84 Fiber* self = (Fiber*)pFiber; 85 86 MT_ASSERT(self->isInitialized == true, "Using non initialized fiber"); 87 88 MT_ASSERT(self->func != nullptr, "Invalid fiber func"); 89 self->func(self->funcData); 90 } 91 CleanUp()92 void CleanUp() 93 { 94 if (isInitialized) 95 { 96 // if func != null than we have stack memory ownership 97 if (func != nullptr) 98 { 99 Memory::FreeStack(stackDesc); 100 } 101 102 isInitialized = false; 103 } 104 } 105 106 public: 107 108 MT_NOCOPYABLE(Fiber); 109 Fiber()110 Fiber() 111 : funcData(nullptr) 112 , func(nullptr) 113 , isInitialized(false) 114 { 115 memset(&fiberContext, 0, sizeof(ucontext_t)); 116 } 117 ~Fiber()118 ~Fiber() 119 { 120 CleanUp(); 121 } 122 123 CreateFromCurrentThreadAndRun(TThreadEntryPoint entryPoint,void * userData)124 void CreateFromCurrentThreadAndRun(TThreadEntryPoint entryPoint, void *userData) 125 { 126 MT_ASSERT(!isInitialized, "Already initialized"); 127 128 func = nullptr; 129 funcData = nullptr; 130 131 // get execution context 132 int res = getcontext(&fiberContext); 133 MT_USED_IN_ASSERT(res); 134 MT_ASSERT(res == 0, "getcontext - failed"); 135 136 isInitialized = true; 137 138 entryPoint(userData); 139 140 141 CleanUp(); 142 } 143 144 Create(size_t stackSize,TThreadEntryPoint entryPoint,void * userData)145 void Create(size_t stackSize, TThreadEntryPoint entryPoint, void *userData) 146 { 147 MT_ASSERT(!isInitialized, "Already initialized"); 148 MT_ASSERT(stackSize >= PTHREAD_STACK_MIN, "Stack to small"); 149 150 func = entryPoint; 151 funcData = userData; 152 153 int res = getcontext(&fiberContext); 154 MT_USED_IN_ASSERT(res); 155 MT_ASSERT(res == 0, "getcontext - failed"); 156 157 stackDesc = Memory::AllocStack(stackSize); 158 159 fiberContext.uc_link = nullptr; 160 fiberContext.uc_stack.ss_sp = stackDesc.stackBottom; 161 fiberContext.uc_stack.ss_size = stackDesc.GetStackSize(); 162 fiberContext.uc_stack.ss_flags = 0; 163 164 makecontext(&fiberContext, (void(*)())&FiberFuncInternal, 1, (void *)this); 165 166 isInitialized = true; 167 } 168 169 #ifdef MT_INSTRUMENTED_BUILD SetName(const char * fiberName)170 void SetName(const char* fiberName) 171 { 172 MT_UNUSED(fiberName); 173 } 174 #endif 175 SwitchTo(Fiber & from,Fiber & to)176 static void SwitchTo(Fiber & from, Fiber & to) 177 { 178 HardwareFullMemoryBarrier(); 179 180 MT_ASSERT(from.isInitialized, "Invalid from fiber"); 181 MT_ASSERT(to.isInitialized, "Invalid to fiber"); 182 183 int res = swapcontext(&from.fiberContext, &to.fiberContext); 184 MT_USED_IN_ASSERT(res); 185 MT_ASSERT(res == 0, "setcontext - failed"); 186 187 } 188 189 190 191 }; 192 193 194 } 195 196 197 #endif 198