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 #include <MTConfig.h> 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 46 #if MT_PLATFORM_WINDOWS 47 #include <Platform/Windows/MTCommon.h> 48 #elif MT_PLATFORM_POSIX || MT_PLATFORM_OSX 49 #include <Platform/Posix/MTCommon.h> 50 #else 51 #error Platfrom is not supported 52 #endif 53 54 #include <Platform/Common/MTAtomic.h> 55 #include <Platform/Common/MTSpinWait.h> 56 57 namespace MT 58 { 59 // 60 // 61 // 62 class ScopedGuard 63 { 64 MT::Mutex & mutex; 65 66 ScopedGuard( const ScopedGuard & ); 67 void operator=( const ScopedGuard &); 68 69 public: 70 ScopedGuard(MT::Mutex & _mutex)71 ScopedGuard(MT::Mutex & _mutex) : mutex(_mutex) 72 { 73 mutex.Lock(); 74 } 75 ~ScopedGuard()76 ~ScopedGuard() 77 { 78 mutex.Unlock(); 79 } 80 }; 81 82 // 83 // Simple Linear congruential generator 84 // 85 class LcgRandom 86 { 87 uint32 state; 88 89 public: 90 LcgRandom()91 LcgRandom() 92 : state(2578432553) 93 { 94 } 95 SetSeed(uint32 seed)96 void SetSeed(uint32 seed) 97 { 98 state = seed; 99 } 100 Get()101 uint16 Get() 102 { 103 state = 214013 * state + 2531011; 104 uint16 rnd = (state >> 16); 105 return rnd; 106 } 107 108 109 }; 110 111 } 112 113 114 #if MT_CPP11_SUPPORTED 115 #define mt_thread_local _Thread_local 116 #elif MT_GCC_COMPILER_FAMILY 117 #define mt_thread_local __thread 118 #elif MT_MSVC_COMPILER_FAMILY 119 #define mt_thread_local __declspec(thread) 120 #else 121 #error Can not define mt_thread_local. Unknown platform. 122 #endif 123 124 125