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 #include <Platform/Common/MTAtomic.h>
57 
58 namespace MT
59 {
60 	//
61 	//
62 	//
63 	class ScopedGuard
64 	{
65 		MT::Mutex & mutex;
66 
67 		ScopedGuard( const ScopedGuard & ) : mutex(*((MT::Mutex*)nullptr)) {}
68 		void operator=( const ScopedGuard &) {}
69 
70 	public:
71 
72 		ScopedGuard(MT::Mutex & _mutex) : mutex(_mutex)
73 		{
74 			mutex.Lock();
75 		}
76 
77 		~ScopedGuard()
78 		{
79 			mutex.Unlock();
80 		}
81 	};
82 
83 	//
84 	// Simple Linear congruential generator
85 	//
86 	class LcgRandom
87 	{
88 		uint32 state;
89 
90 	public:
91 
92 		LcgRandom()
93 			: state(2578432553)
94 		{
95 		}
96 
97 		void SetSeed(uint32 seed)
98 		{
99 			state = seed;
100 		}
101 
102 		uint16 Get()
103 		{
104 			state = 214013 * state + 2531011;
105 			uint16 rnd = (state >> 16);
106 			return rnd;
107 		}
108 
109 
110 	};
111 
112 }
113 
114 
115 #ifdef __GNUC__
116 
117 #define mt_thread_local __thread
118 
119 #elif __STDC_VERSION__ >= 201112L
120 
121 #define mt_thread_local _Thread_local
122 
123 #elif defined(_MSC_VER)
124 
125 #define mt_thread_local __declspec(thread)
126 
127 #else
128 
129 #error Can not define mt_thread_local. Unknown platform.
130 
131 #endif
132 
133 
134 
135 
136 #if (defined(__SSE__) || defined(_M_IX86) || defined(_M_X64))
137 
138 #define MT_SSE_INTRINSICS (1)
139 
140 #endif
141