-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.h
More file actions
197 lines (161 loc) · 4.49 KB
/
Utils.h
File metadata and controls
197 lines (161 loc) · 4.49 KB
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
196
197
//------------------------------------------------------------------------
#ifndef FRAMEPROEXAMPLE_EVENT_H_INCLUDED
#define FRAMEPROEXAMPLE_EVENT_H_INCLUDED
//--------------------------------------------------------------------
#include "FramePro.h"
//------------------------------------------------------------------------
#if FRAMEPRO_WIN_BASED_PLATFORM
#include <Windows.h>
#endif
//------------------------------------------------------------------------
#if !FRAMEPRO_WIN_BASED_PLATFORM
#include <pthread.h>
#include <sys/time.h>
#include <errno.h>
#endif
//------------------------------------------------------------------------
typedef int (*ThreadMain)(void*);
//--------------------------------------------------------------------
class Event
{
public:
//--------------------------------------------------------------------
Event(bool initial_state, bool auto_reset)
{
#if FRAMEPRO_WIN_BASED_PLATFORM
m_Handle = CreateEvent(NULL, !auto_reset, initial_state, NULL);
#else
pthread_cond_init(&m_Cond, NULL);
pthread_mutex_init(&m_Mutex, NULL);
m_Signalled = false;
m_AutoReset = auto_reset;
if (initial_state)
Set();
#endif
}
//--------------------------------------------------------------------
~Event()
{
#if FRAMEPRO_WIN_BASED_PLATFORM
CloseHandle(m_Handle);
#else
pthread_mutex_destroy(&m_Mutex);
pthread_cond_destroy(&m_Cond);
#endif
}
//--------------------------------------------------------------------
void Set() const
{
#if FRAMEPRO_WIN_BASED_PLATFORM
SetEvent(m_Handle);
#else
pthread_mutex_lock(&m_Mutex);
m_Signalled = true;
pthread_mutex_unlock(&m_Mutex);
pthread_cond_signal(&m_Cond);
#endif
}
//--------------------------------------------------------------------
void Reset()
{
#if FRAMEPRO_WIN_BASED_PLATFORM
ResetEvent(m_Handle);
#else
pthread_mutex_lock(&m_Mutex);
m_Signalled = false;
pthread_mutex_unlock(&m_Mutex);
#endif
}
//--------------------------------------------------------------------
int Wait(int timeout = -1) const
{
#if FRAMEPRO_WIN_BASED_PLATFORM
return WaitForSingleObject(m_Handle, timeout) == 0/*WAIT_OBJECT_0*/;
#else
pthread_mutex_lock(&m_Mutex);
if (m_Signalled)
{
m_Signalled = false;
pthread_mutex_unlock(&m_Mutex);
return true;
}
if (timeout == -1)
{
while (!m_Signalled)
pthread_cond_wait(&m_Cond, &m_Mutex);
if (!m_AutoReset)
m_Signalled = false;
pthread_mutex_unlock(&m_Mutex);
return true;
}
else
{
timeval curr;
gettimeofday(&curr, NULL);
timespec time;
time.tv_sec = curr.tv_sec + timeout / 1000;
time.tv_nsec = (curr.tv_usec * 1000) + ((timeout % 1000) * 1000000);
time.tv_sec += time.tv_nsec / 1000000000L;
time.tv_nsec = time.tv_nsec % 1000000000L;
int ret = 0;
do
{
ret = pthread_cond_timedwait(&m_Cond, &m_Mutex, &time);
} while (!m_Signalled && ret != ETIMEDOUT);
if (m_Signalled)
{
if (!m_AutoReset)
m_Signalled = false;
pthread_mutex_unlock(&m_Mutex);
return true;
}
pthread_mutex_unlock(&m_Mutex);
return false;
}
#endif
}
//------------------------------------------------------------------------
// data
private:
#if FRAMEPRO_WIN_BASED_PLATFORM
HANDLE m_Handle;
#else
mutable pthread_cond_t m_Cond;
mutable pthread_mutex_t m_Mutex;
mutable volatile bool m_Signalled;
bool m_AutoReset;
#endif
};
//------------------------------------------------------------------------
typedef int (*ThreadMain)(void*);
//------------------------------------------------------------------------
class Thread
{
public:
Thread();
void CreateThread(ThreadMain p_thread_main, void* p_param=NULL);
bool IsAlive() const { return m_Alive; }
void SetPriority(int priority);
void SetAffinity(int affinity);
void WaitForThreadToTerminate(int timeout);
private:
#if FRAMEPRO_WIN_BASED_PLATFORM
static unsigned long WINAPI PlatformThreadMain(void* p_param);
#else
static void* PlatformThreadMain(void* p_param);
#endif
//------------------------------------------------------------------------
// data
private:
#if FRAMEPRO_WIN_BASED_PLATFORM
mutable HANDLE m_Handle;
#else
mutable pthread_t m_Thread;
#endif
mutable bool m_Alive;
mutable ThreadMain mp_ThreadMain;
mutable void* mp_Param;
Event m_ThreadTerminatedEvent;
};
//------------------------------------------------------------------------
#endif