-
Notifications
You must be signed in to change notification settings - Fork 0
/
OgreThread.cpp
149 lines (126 loc) · 2.44 KB
/
OgreThread.cpp
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
#include "OgreThread.h"
#include <time.h>
#ifdef _USE_PTHREAD_
#else
#include <Windows.h>
#include <process.h>
#endif
namespace Ogre
{
#ifdef _USE_PTHREAD_
static void* ThreadMainFunc(void *pdata)
#else
static unsigned int WINAPI ThreadMainFunc(void *pdata)
#endif
{
OSThread *pthread = (OSThread *)pdata;
if(pthread == NULL) return 0;
pthread->_onThreadStart();
srand((unsigned)time(NULL)); //!!!
for(;;)
{
pthread->m_ThreadState = OSThread::RUN_CONTINUE;
int ret = pthread->_run();
pthread->m_ThreadState = (OSThread::RUN_RETTYPE)ret;
if(ret == OSThread::RUN_EXIT)
{
pthread->m_Shutdown = 0;
break;
}
else if(ret == OSThread::RUN_WAIT)
{
if(1 == pthread->m_Shutdown)
{
pthread->m_Shutdown = 0;
break;
}
pthread->m_WaitEvent.wait(pthread->getDefaultWaitTime());
}
/*
if(1 == pthread->m_Shutdown)
{
pthread->m_Shutdown = 2;
}
else if (2 == pthread->m_Shutdown)
{
pthread->m_Shutdown = 0;
return 0;
}*/
}
pthread->_onThreadEnd();
return 0;
}
#ifndef _USE_PTHREAD_
OSThread::OSThread() : m_hThread(0), m_Shutdown(0)
{
}
OSThread::~OSThread()
{
shutdown();
}
void OSThread::start()
{
unsigned int threadid;
m_Shutdown = 0;
m_ThreadState = RUN_CONTINUE;
m_hThread = (void *)_beginthreadex(NULL, 0, ThreadMainFunc, this, 0, &threadid);
}
void OSThread::shutdown()
{
if(m_hThread)
{
m_Shutdown = 1;
while(m_Shutdown>0 && m_ThreadState!=RUN_EXIT)
{
::Sleep(0);
m_WaitEvent.trigger();
}
CloseHandle((HANDLE)m_hThread);
m_hThread = 0;
}
}
#else
OSThread::OSThread() : m_hThread(0), m_Shutdown(0)
{
}
OSThread::~OSThread()
{
shutdown();
}
void OSThread::start()
{
//ĎúťŮÉĎŇť´ÎľÄĎßłĚ×ĘÔ´
if(m_hThread)
{
void* ret;
pthread_join(m_hThread, &ret);
m_hThread = 0;
}
m_Shutdown = 0;
m_ThreadState = RUN_CONTINUE;
pthread_attr_t attributes;
pthread_attr_init(&attributes);
pthread_attr_setstacksize(&attributes, 1048576*2);
if(pthread_create(&m_hThread, &attributes, ThreadMainFunc, this))
{
m_ThreadState = RUN_EXIT;
printf("pthread_create failed\n");
assert(0);
}
#if OGRE_PLATFORM == OGRE_PLATFORM_ANDROID
//pthread_setname_np(pthread_self(), "ogre_thread");
#endif
}
void OSThread::shutdown()
{
if(m_hThread)
{
m_Shutdown = 1;
m_WaitEvent.trigger();
void* ret;
pthread_join(m_hThread, &ret);
m_hThread = 0;
}
}
#endif
}