-
Notifications
You must be signed in to change notification settings - Fork 0
/
V2Thread.h
195 lines (141 loc) · 3.04 KB
/
V2Thread.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* @file: V2Windows.h
* @author: Ondrej Hirjak, 2006 - 2007
* @description: Windows includes.
*/
#ifndef V2Thread_h
#define V2Thread_h
namespace V2
{
/*
* =============================================
* Trieda implementuje mutex
* =============================================
*/
class Mutex
{
#if WIN
private:
HANDLE mutexHandle;
public:
~Mutex()
{
CloseHandle(mutexHandle);
}
Mutex()
{
mutexHandle = CreateMutex(NULL, FALSE, NULL);
}
void Acquire()
{
WaitForSingleObject(mutexHandle, INFINITE);
}
bool TryAcquire(void)
{
return (WaitForSingleObject(mutexHandle, 0) != WAIT_FAILED);
}
void Release()
{
ReleaseMutex(mutexHandle);
}
#elif LINUX || MACOS
private:
pthread_mutex_t mutex;
public:
~Mutex()
{
pthread_mutex_destroy(&mutex);
}
Mutex()
{
pthread_mutex_init(&mutex, NULL);
}
void Acquire(void)
{
pthread_mutex_lock(&mutex);
}
bool TryAcquire(void)
{
return (pthread_mutex_trylock(&mutex) == 0);
}
void Release(void)
{
pthread_mutex_unlock(&mutex);
}
#endif
};
enum
{
kThreadStackSize = 131072
};
/*
* =============================================
* Vysledky chyb pri praci s vlaknom
* =============================================
*/
enum ThreadResult
{
kThreadOkay,
kThreadInitFailed
};
/*
* =============================================
* Vynimka pri praci s vlaknom
* =============================================
*/
class ThreadException : public Exception
{
private:
ThreadResult threadResult;
public:
ThreadException(ThreadResult result) : threadResult(result) {}
const String GetTypeString() const;
const String GetErrorString() const;
};
/*
* =============================================
* Trieda implementujuca vlakno
* =============================================
*/
class Thread
{
public:
typedef void (*ThreadRoutineType)(const Thread *, void *);
private:
ThreadRoutineType threadRoutine;
void *threadData;
#if WIN
HANDLE threadHandle;
static DWORD WINAPI ThreadProc(LPVOID lpParameter);
#elif LINUX || MACOS
pthread_t thread;
static void * ThreadProc(void *data);
#endif
public:
~Thread();
Thread(ThreadRoutineType routine, void *data, unsigned long stackSize = kThreadStackSize);
void SetThreadAffinity(int index) const;
static int GetProcessorNumber() {
#if WIN
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
return systemInfo.dwNumberOfProcessors;
#elif LINUX || MACOS
return sysconf(_SC_NPROCESSORS_ONLN);
#endif
}
};
/*
* =============================================
* Hlavna trieda spravy vlaken, TODO
* =============================================
*/
class ThreadPool : public Singleton<ThreadPool>
{
public:
~ThreadPool();
ThreadPool();
};
extern ThreadPool *TheThreadPool;
}
#endif // V2Thread_h