forked from abcdls0905/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathios_thread.h
220 lines (175 loc) · 3.69 KB
/
ios_thread.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//--------------------------------------------------------------------
// 文件名: ios_thread.h
// 内 容:
// 说 明:
// 创建日期: 2010年12月15日
// 创建人: 陆利民
// 版权所有: 苏州蜗牛电子有限公司
//--------------------------------------------------------------------
#ifndef _SYSTEM_IOS_THREAD_H
#define _SYSTEM_IOS_THREAD_H
#include <pthread.h>
#include <unistd.h>
#include <sched.h>
#include <time.h>
#include <sys/time.h>
// 线程
class CThread
{
private:
typedef void (__cdecl* LOOP_FUNC)(void*);
typedef bool (__cdecl* INIT_FUNC)(void*);
typedef bool (__cdecl* SHUT_FUNC)(void*);
// 线程函数
static void* WorkerProc(void* lpParameter)
{
CThread* pthis = (CThread*)lpParameter;
LOOP_FUNC loop_func = pthis->m_LoopFunc;
void* context = pthis->m_pContext;
int sleep_ms = pthis->m_nSleep;
if (pthis->m_InitFunc)
{
pthis->m_InitFunc(context);
}
while (!pthis->m_bQuit)
{
loop_func(context);
if (sleep_ms > 0)
{
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = sleep_ms * 1000000;
nanosleep(&ts, NULL);
}
else if (sleep_ms == 0)
{
sched_yield();
}
}
if (pthis->m_ShutFunc)
{
pthis->m_ShutFunc(context);
}
return (void*)0;
}
public:
CThread(LOOP_FUNC loop_func, INIT_FUNC init_func, SHUT_FUNC shut_func,
void* context, int sleep_ms, int stack_size)
{
m_LoopFunc = loop_func;
m_InitFunc = init_func;
m_ShutFunc = shut_func;
m_pContext = context;
m_nSleep = sleep_ms;
m_nStackSize = stack_size;
m_bQuit = false;
m_hThread = 0;
}
~CThread()
{
}
// 是否已退出线程
void SetQuit(bool value) { m_bQuit = value; }
bool GetQuit() const { return m_bQuit; }
// 启动线程
bool Start()
{
m_bQuit = false;
int res = pthread_create(&m_hThread, NULL, WorkerProc, this);
return (0 == res);
}
// 停止线程
bool Stop()
{
m_bQuit = true;
if (m_hThread != 0)
{
pthread_join(m_hThread, NULL);
m_hThread = 0;
}
return true;
}
private:
CThread();
CThread(const CThread&);
CThread& operator=(const CThread&);
private:
LOOP_FUNC m_LoopFunc;
INIT_FUNC m_InitFunc;
SHUT_FUNC m_ShutFunc;
void* m_pContext;
int m_nSleep;
int m_nStackSize;
bool m_bQuit;
pthread_t m_hThread;
};
// 线程等待
class CThreadWaiter
{
public:
CThreadWaiter()
{
pthread_cond_init(&m_cond, NULL);
pthread_mutex_init(&m_mutex, NULL);
}
~CThreadWaiter()
{
pthread_cond_destroy(&m_cond);
pthread_mutex_destroy(&m_mutex);
}
// 等待线程唤醒
bool Wait(int ms)
{
bool result = false;
if (ms < 0)
{
// 无限时间等待
pthread_mutex_lock(&m_mutex);
int res = pthread_cond_wait(&m_cond, &m_mutex);
if (0 == res)
{
result = true;
}
pthread_mutex_unlock(&m_mutex);
}
else
{
struct timespec ts;
struct timeval now;
gettimeofday(&now, NULL);
ts.tv_sec = now.tv_sec;
ts.tv_nsec = now.tv_usec * 1000;
//clock_gettime(CLOCK_REALTIME, &ts);
//clock_serv_t cclock;
//mach_timespec_t mts;
//host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
//clock_get_time(cclock, &mts);
//mach_port_deallocate(mach_task_self(), cclock);
//ts.tv_sec = mts.tv_sec;
//ts.tv_nsec = mts.tv_nsec;
ts.tv_nsec += ms * 1000000;
if (ts.tv_nsec > 999999999)
{
ts.tv_sec += 1;
ts.tv_nsec -= 1000000000;
}
pthread_mutex_lock(&m_mutex);
int res = pthread_cond_timedwait(&m_cond, &m_mutex, &ts);
if (0 == res)
{
result = true;
}
pthread_mutex_unlock(&m_mutex);
}
return result;
}
// 唤醒
bool Signal()
{
return pthread_cond_signal(&m_cond) == 0;
}
private:
pthread_cond_t m_cond;
pthread_mutex_t m_mutex;
};
#endif // _SYSTEM_IOS_THREAD_H