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 #include <string.h> 28 29 #define _DARWIN_C_SOURCE 30 #include <sys/mman.h> 31 32 #ifndef MAP_ANONYMOUS 33 #define MAP_ANONYMOUS MAP_ANON 34 #endif 35 36 #ifndef MAP_STACK 37 #define MAP_STACK (0) 38 #endif 39 40 #include <MTAllocator.h> 41 42 namespace MT 43 { 44 45 // 46 // 47 // 48 class Fiber 49 { 50 void * funcData; 51 TThreadEntryPoint func; 52 53 Memory::StackDesc stackDesc; 54 55 ucontext_t fiberContext; 56 bool isInitialized; 57 58 static void FiberFuncInternal(void *pFiber) 59 { 60 MT_ASSERT(pFiber != nullptr, "Invalid fiber"); 61 Fiber* self = (Fiber*)pFiber; 62 63 MT_ASSERT(self->isInitialized == true, "Using non initialized fiber"); 64 65 MT_ASSERT(self->func != nullptr, "Invalid fiber func"); 66 self->func(self->funcData); 67 } 68 69 private: 70 71 Fiber(const Fiber &) {} 72 void operator=(const Fiber &) {} 73 74 public: 75 76 Fiber() 77 : funcData(nullptr) 78 , func(nullptr) 79 , isInitialized(false) 80 { 81 memset(&fiberContext, 0, sizeof(ucontext_t)); 82 } 83 84 ~Fiber() 85 { 86 if (isInitialized) 87 { 88 // if func != null than we have stack memory ownership 89 if (func != nullptr) 90 { 91 Memory::FreeStack(stackDesc); 92 } 93 94 isInitialized = false; 95 } 96 } 97 98 99 void CreateFromThread(Thread & thread) 100 { 101 MT_ASSERT(!isInitialized, "Already initialized"); 102 MT_ASSERT(thread.IsCurrentThread(), "ERROR: Can create fiber only from current thread!"); 103 104 int res = getcontext(&fiberContext); 105 MT_ASSERT(res == 0, "getcontext - failed"); 106 107 fiberContext.uc_link = nullptr; 108 fiberContext.uc_stack.ss_sp = thread.GetStackBottom(); 109 fiberContext.uc_stack.ss_size = thread.GetStackSize(); 110 fiberContext.uc_stack.ss_flags = 0; 111 112 func = nullptr; 113 funcData = nullptr; 114 115 isInitialized = true; 116 } 117 118 119 void Create(size_t stackSize, TThreadEntryPoint entryPoint, void *userData) 120 { 121 MT_ASSERT(!isInitialized, "Already initialized"); 122 MT_ASSERT(stackSize >= PTHREAD_STACK_MIN, "Stack to small"); 123 124 func = entryPoint; 125 funcData = userData; 126 127 int res = getcontext(&fiberContext); 128 MT_ASSERT(res == 0, "getcontext - failed"); 129 130 stackDesc = Memory::AllocStack(stackSize); 131 132 fiberContext.uc_link = nullptr; 133 fiberContext.uc_stack.ss_sp = stackDesc.stackBottom; 134 fiberContext.uc_stack.ss_size = stackDesc.GetStackSize(); 135 fiberContext.uc_stack.ss_flags = 0; 136 137 makecontext(&fiberContext, (void(*)())&FiberFuncInternal, 1, (void *)this); 138 139 isInitialized = true; 140 } 141 142 static void SwitchTo(Fiber & from, Fiber & to) 143 { 144 __sync_synchronize(); 145 146 MT_ASSERT(from.isInitialized, "Invalid from fiber"); 147 MT_ASSERT(to.isInitialized, "Invalid to fiber"); 148 149 int res = swapcontext(&from.fiberContext, &to.fiberContext); 150 MT_ASSERT(res == 0, "setcontext - failed"); 151 152 } 153 154 155 156 }; 157 158 159 } 160 161 162