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 namespace MT 41 { 42 43 // 44 // 45 // 46 class Fiber 47 { 48 void * funcData; 49 TThreadEntryPoint func; 50 51 char* stackRawMemory; 52 size_t stackRawMemorySize; 53 54 ucontext_t fiberContext; 55 bool isInitialized; 56 57 static void FiberFuncInternal(void *pFiber) 58 { 59 MT_ASSERT(pFiber != nullptr, "Invalid fiber"); 60 Fiber* self = (Fiber*)pFiber; 61 62 MT_ASSERT(self->isInitialized == true, "Using non initialized fiber"); 63 64 MT_ASSERT(self->func != nullptr, "Invalid fiber func"); 65 self->func(self->funcData); 66 } 67 68 private: 69 70 Fiber(const Fiber &) {} 71 void operator=(const Fiber &) {} 72 73 public: 74 75 Fiber() 76 : funcData(nullptr) 77 , func(nullptr) 78 , stackRawMemory(nullptr) 79 , stackRawMemorySize(0) 80 , isInitialized(false) 81 { 82 memset(&fiberContext, 0, sizeof(ucontext_t)); 83 } 84 85 ~Fiber() 86 { 87 if (isInitialized) 88 { 89 // if func != null than we have memory ownership 90 if (func != nullptr) 91 { 92 int res = munmap(stackRawMemory, stackRawMemorySize); 93 MT_ASSERT(res == 0, "Can't free memory"); 94 } 95 96 isInitialized = false; 97 } 98 } 99 100 101 void CreateFromThread(Thread & thread) 102 { 103 MT_ASSERT(!isInitialized, "Already initialized"); 104 MT_ASSERT(thread.IsCurrentThread(), "ERROR: Can create fiber only from current thread!"); 105 106 int res = getcontext(&fiberContext); 107 MT_ASSERT(res == 0, "getcontext - failed"); 108 109 fiberContext.uc_link = nullptr; 110 fiberContext.uc_stack.ss_sp = thread.GetStackBottom(); 111 fiberContext.uc_stack.ss_size = thread.GetStackSize(); 112 fiberContext.uc_stack.ss_flags = 0; 113 114 func = nullptr; 115 funcData = nullptr; 116 117 isInitialized = true; 118 } 119 120 121 void Create(size_t stackSize, TThreadEntryPoint entryPoint, void *userData) 122 { 123 MT_ASSERT(!isInitialized, "Already initialized"); 124 MT_ASSERT(stackSize >= PTHREAD_STACK_MIN, "Stack to small"); 125 126 func = entryPoint; 127 funcData = userData; 128 129 int res = getcontext(&fiberContext); 130 MT_ASSERT(res == 0, "getcontext - failed"); 131 132 133 int pageSize = sysconf(_SC_PAGE_SIZE); 134 int pagesCount = stackSize / pageSize; 135 136 //need additional page for stack tail 137 if ((stackSize % pageSize) > 0) 138 { 139 pagesCount++; 140 } 141 142 //protected guard page 143 pagesCount++; 144 145 stackRawMemorySize = pagesCount * pageSize; 146 147 stackRawMemory = (char*)mmap(NULL, stackRawMemorySize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0); 148 149 MT_ASSERT((void *)stackRawMemory != (void *)-1, "Can't allocate memory"); 150 151 char* stackBottom = stackRawMemory + pageSize; 152 //char* stackTop = stackRawMemory + stackMemorySize; 153 154 res = mprotect(stackRawMemory, pageSize, PROT_NONE); 155 MT_ASSERT(res == 0, "Can't protect memory"); 156 157 fiberContext.uc_link = nullptr; 158 fiberContext.uc_stack.ss_sp = stackBottom; 159 fiberContext.uc_stack.ss_size = stackRawMemorySize - pageSize; 160 fiberContext.uc_stack.ss_flags = 0; 161 162 makecontext(&fiberContext, (void(*)())&FiberFuncInternal, 1, (void *)this); 163 164 isInitialized = true; 165 } 166 167 static void SwitchTo(Fiber & from, Fiber & to) 168 { 169 __sync_synchronize(); 170 171 MT_ASSERT(from.isInitialized, "Invalid from fiber"); 172 MT_ASSERT(to.isInitialized, "Invalid to fiber"); 173 174 int res = swapcontext(&from.fiberContext, &to.fiberContext); 175 MT_ASSERT(res == 0, "setcontext - failed"); 176 177 } 178 179 180 181 }; 182 183 184 } 185 186 187