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