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 static_assert (sizeof(void*) == sizeof(size_t), "Can't cast pointer to size_t, different type size"); 48 return !(size_t(p) & (align - 1)); 49 } 50 51 #ifdef _WIN32 52 #include <Platform/Windows/MTCommon.h> 53 #else 54 #include <Platform/Posix/MTCommon.h> 55 #endif 56 57 58 namespace MT 59 { 60 61 // 62 // 63 // 64 class ScopedGuard 65 { 66 MT::Mutex & mutex; 67 68 ScopedGuard( const ScopedGuard & ) : mutex(*((MT::Mutex*)nullptr)) {} 69 void operator=( const ScopedGuard &) {} 70 71 public: 72 73 ScopedGuard(MT::Mutex & _mutex) : mutex(_mutex) 74 { 75 mutex.Lock(); 76 } 77 78 ~ScopedGuard() 79 { 80 mutex.Unlock(); 81 } 82 }; 83 84 // 85 // Simple Linear congruential generator 86 // 87 class LcgRandom 88 { 89 uint32 state; 90 91 public: 92 93 LcgRandom() 94 : state(2578432553) 95 { 96 } 97 98 void SetSeed(uint32 seed) 99 { 100 state = seed; 101 } 102 103 uint16 Get() 104 { 105 state = 214013 * state + 2531011; 106 uint16 rnd = (state >> 16); 107 return rnd; 108 } 109 110 111 }; 112 113 } 114 115