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 <MTTypes.h> 26 #include <MTDebug.h> 27 28 typedef void (*TThreadEntryPoint)(void* userData); 29 30 #define MT_ARRAY_SIZE( arr ) ( sizeof( arr ) / sizeof( (arr)[0] ) ) 31 32 namespace MT 33 { 34 namespace EventReset 35 { 36 enum Type 37 { 38 AUTOMATIC = 0, 39 MANUAL = 1, 40 }; 41 } 42 } 43 44 45 inline bool IsPointerAligned( const volatile void* p, const uint32 align ) 46 { 47 return !((uintptr_t)p & (align - 1)); 48 } 49 50 #ifdef _WIN32 51 #include <Platform/Windows/MTCommon.h> 52 #else 53 #include <Platform/Posix/MTCommon.h> 54 #endif 55 56 57 namespace MT 58 { 59 //compile time POD type check 60 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 61 static_assert(std::is_pod<AtomicInt32Base>::value == true, "AtomicInt32Base must be a POD (plain old data type)"); 62 63 64 // 65 // Atomic int (type with constructor) 66 // 67 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 68 class AtomicInt32 : public AtomicInt32Base 69 { 70 71 public: 72 73 AtomicInt32() 74 { 75 _value = 0; 76 static_assert(sizeof(AtomicInt32) == 4, "Invalid type size"); 77 MT_ASSERT(IsPointerAligned(&_value, 4), "Invalid atomic int alignment"); 78 } 79 80 explicit AtomicInt32(int v) 81 { 82 _value = v; 83 static_assert(sizeof(AtomicInt32) == 4, "Invalid type size"); 84 MT_ASSERT(IsPointerAligned(&_value, 4), "Invalid atomic int alignment"); 85 } 86 }; 87 88 89 //compile time POD type check 90 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 91 static_assert(std::is_pod<AtomicPtrBase>::value == true, "AtomicPtrBase must be a POD (plain old data type)"); 92 93 94 // 95 // Atomic pointer (type with constructor) 96 // 97 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 98 template<typename T> 99 class AtomicPtr : protected AtomicPtrBase 100 { 101 public: 102 AtomicPtr() 103 { 104 _value = nullptr; 105 MT_ASSERT(IsPointerAligned(&_value, sizeof(void*)), "Invalid atomic ptr alignment"); 106 } 107 108 explicit AtomicPtr(T* v) 109 { 110 _value = v; 111 MT_ASSERT(IsPointerAligned(&_value, sizeof(void*)), "Invalid atomic ptr alignment"); 112 } 113 114 T* Load() const 115 { 116 return (T*)AtomicPtrBase::Load(); 117 } 118 119 T* Store(T* val) 120 { 121 return (T*)AtomicPtrBase::Store(val); 122 } 123 124 125 T* CompareAndSwap(T* compareValue, T* newValue) 126 { 127 return (T*)AtomicPtrBase::CompareAndSwap(compareValue, newValue); 128 } 129 130 T* LoadRelaxed() const 131 { 132 return (T*)AtomicPtrBase::LoadRelaxed(); 133 } 134 135 void StoreRelaxed(T* val) 136 { 137 AtomicPtrBase::StoreRelaxed(val); 138 } 139 }; 140 141 142 143 144 // 145 // 146 // 147 class ScopedGuard 148 { 149 MT::Mutex & mutex; 150 151 ScopedGuard( const ScopedGuard & ) : mutex(*((MT::Mutex*)nullptr)) {} 152 void operator=( const ScopedGuard &) {} 153 154 public: 155 156 ScopedGuard(MT::Mutex & _mutex) : mutex(_mutex) 157 { 158 mutex.Lock(); 159 } 160 161 ~ScopedGuard() 162 { 163 mutex.Unlock(); 164 } 165 }; 166 167 // 168 // Simple Linear congruential generator 169 // 170 class LcgRandom 171 { 172 uint32 state; 173 174 public: 175 176 LcgRandom() 177 : state(2578432553) 178 { 179 } 180 181 void SetSeed(uint32 seed) 182 { 183 state = seed; 184 } 185 186 uint16 Get() 187 { 188 state = 214013 * state + 2531011; 189 uint16 rnd = (state >> 16); 190 return rnd; 191 } 192 193 194 }; 195 196 } 197 198