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 <pthread.h> 26 #include <unistd.h> 27 #include <time.h> 28 #include <limits.h> 29 #include <stdlib.h> 30 31 #include <Platform/Common/MTThread.h> 32 33 namespace MT 34 { 35 class _Fiber; 36 37 class Thread : public ThreadBase 38 { 39 pthread_t thread; 40 pthread_attr_t threadAttr; 41 42 void * stackBase; 43 size_t stackSize; 44 45 bool isStarted; 46 47 static void* ThreadFuncInternal(void *pThread) 48 { 49 Thread * self = (Thread *)pThread; 50 51 self->func(self->funcData); 52 53 pthread_exit(nullptr); 54 } 55 56 public: 57 58 Thread() 59 : stackBase(nullptr) 60 , stackSize(0) 61 , isStarted(false) 62 { 63 } 64 65 void* GetStackBase() 66 { 67 return stackBase; 68 } 69 70 size_t GetStackSize() 71 { 72 return stackSize; 73 } 74 75 76 void Start(size_t _stackSize, TThreadEntryPoint entryPoint, void *userData) 77 { 78 ASSERT(!isStarted, "Thread already stared"); 79 80 ASSERT(func == nullptr, "Thread already started"); 81 82 func = entryPoint; 83 funcData = userData; 84 85 stackSize = _stackSize; 86 87 ASSERT(stackSize >= PTHREAD_STACK_MIN, "Thread stack to small"); 88 89 stackBase = (void *)malloc(stackSize); 90 91 int err = pthread_attr_init(&threadAttr); 92 ASSERT(err == 0, "pthread_attr_init - error"); 93 94 err = pthread_attr_setstack(&threadAttr, stackBase, stackSize); 95 ASSERT(err == 0, "pthread_attr_setstack - error"); 96 97 err = pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_JOINABLE); 98 ASSERT(err == 0, "pthread_attr_setdetachstate - error"); 99 100 err = pthread_create(&thread, &threadAttr, ThreadFuncInternal, this); 101 ASSERT(err == 0, "pthread_create - error"); 102 103 isStarted = true; 104 } 105 106 void Stop() 107 { 108 ASSERT(isStarted, "Thread is not started"); 109 110 if (func == nullptr) 111 { 112 return; 113 } 114 115 void *threadStatus = nullptr; 116 int err = pthread_join(thread, &threadStatus); 117 ASSERT(err == 0, "pthread_join - error"); 118 119 err = pthread_attr_destroy(&threadAttr); 120 ASSERT(err == 0, "pthread_attr_destroy - error"); 121 122 func = nullptr; 123 funcData = nullptr; 124 125 if (stackBase) 126 { 127 free(stackBase); 128 stackBase = nullptr; 129 } 130 stackSize = 0; 131 132 isStarted = false; 133 } 134 135 bool IsCurrentThread() const 136 { 137 if(!isStarted) 138 { 139 return false; 140 } 141 142 pthread_t callThread = pthread_self(); 143 if (pthread_equal(callThread, thread)) 144 { 145 return true; 146 } 147 return false; 148 } 149 150 static int GetNumberOfHardwareThreads() 151 { 152 long numberOfProcessors = sysconf( _SC_NPROCESSORS_ONLN ); 153 return (int)numberOfProcessors; 154 } 155 156 static void Sleep(uint32 milliseconds) 157 { 158 struct timespec req = {0}; 159 time_t sec = (int)(milliseconds/1000); 160 milliseconds = milliseconds - (sec*1000); 161 req.tv_sec = sec; 162 req.tv_nsec = milliseconds * 1000000L; 163 while (nanosleep(&req,&req) == -1 ) 164 { 165 continue; 166 } 167 } 168 169 }; 170 171 172 } 173 174 175