-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexCommonThread.cpp
More file actions
176 lines (161 loc) · 4.07 KB
/
HexCommonThread.cpp
File metadata and controls
176 lines (161 loc) · 4.07 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
#include "HexCommonThread.h"
#ifdef _WIN32
#else
#include <unistd.h>
//#include <malloc/malloc.h>
#include <stdlib.h>
#endif
using namespace DataStructures;
#if defined _WIN32 || defined _WIN64
const int HexCommonThread::P_ABOVE_NORMAL = THREAD_PRIORITY_ABOVE_NORMAL;
const int HexCommonThread::P_BELOW_NORMAL = THREAD_PRIORITY_BELOW_NORMAL;
const int HexCommonThread::P_HIGHEST = THREAD_PRIORITY_HIGHEST;
const int HexCommonThread::P_IDLE = THREAD_PRIORITY_IDLE;
const int HexCommonThread::P_LOWEST = THREAD_PRIORITY_LOWEST;
const int HexCommonThread::P_NORMAL = THREAD_PRIORITY_NORMAL;
const int HexCommonThread::P_CRITICAL = THREAD_PRIORITY_TIME_CRITICAL;
#else
//roc todo
const int HexCommonThread::P_ABOVE_NORMAL = 10;
const int HexCommonThread::P_BELOW_NORMAL = 7;
const int HexCommonThread::P_HIGHEST = 11;
const int HexCommonThread::P_IDLE = 1;
const int HexCommonThread::P_LOWEST = 6;
const int HexCommonThread::P_NORMAL = 9;
const int HexCommonThread::P_CRITICAL = 11;
#endif
unsigned int HexCommonThreadProc( void* param )
{
HexCommonThread* tp = (HexCommonThread*)param;
tp->Run();
return 0;
}
void HexCommonThread::ThreadSleep(long ms)
{
#ifdef _WIN32
Sleep(ms);
#else
usleep((unsigned int)(ms*1000));
#endif
}
void HexCommonThread::Start()
{
if (ThreadStarted())
return;
#ifdef _WIN32
DWORD tid = 0;
m_hThread = (unsigned long*)CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)HexCommonThreadProc,(HexCommonThread*)this,0,&tid);
#else
if (token==NULL)
token = (sem_private) malloc(sizeof(sem_private_struct)+1);
int rc0 = pthread_mutex_init(&(token->mutex), NULL);
if(rc0)
{
free(token);
//printf("Mutex Error :%s\n",strerror(errno));
// exit();
}
int rc1=pthread_cond_init(&(token->condition), NULL);
if (rc1)
{
pthread_mutex_destroy( &(token->mutex) );
free(token);
//printf("Cond Error :%s\n",strerror(errno));
}
token->semCount = 0;
// Create thread linux
pthread_attr_t attr;
//struct sched_param param;
pthread_attr_init( &attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
int rc2= pthread_create( &token->p_hThread, &attr, (void*(*)(void*))HexCommonThreadProc, (void *)this);
if (rc2)
{
pthread_mutex_destroy( &(token->mutex) );
pthread_cond_destroy(&(token->condition));
free(token);
//printf("Pthread Error :%s\n",strerror(errno));
}
#endif
#ifdef _WIN32
EnterCriticalSection(&m_CS_TerminationMask);
#else
pthread_mutex_lock(&m_CS_TerminationMask);
#endif
m_ternimationMask = false;
#ifdef _WIN32
LeaveCriticalSection(&m_CS_TerminationMask);
#else
pthread_mutex_unlock(&m_CS_TerminationMask);
#endif
SetPriority( HexCommonThread::P_NORMAL );
}
void HexCommonThread::Kill()
{
#ifdef _WIN32
TerminateThread( m_hThread, 0 );
#else
pthread_cancel(token->p_hThread);
#endif
}
void HexCommonThread::Stop()
{
TryTerminateThread();
#ifdef _WIN32
if (m_hThread == NULL)
return;
bool started = ThreadStarted();
if (started)
WaitForSingleObject( m_hThread, INFINITE );
CloseHandle( m_hThread );
m_hThread = NULL;
#else
if (token==(sem_private)NULL)
return;
// pthread_join(token->p_hThread,NULL);
pthread_mutex_destroy(&(token->mutex));
pthread_cond_destroy(&(token->condition));
free(token);
token=NULL;
#endif
}
#ifdef _WIN32
void HexCommonThread::SetPriority( int tp )
{
SetThreadPriority( m_hThread, tp );
}
#else
void HexCommonThread::SetPriority(int tp )
{
//roc todo
//pthread_setschedprio(pthread_self(),tp);
struct sched_param sched;
sched.sched_priority = tp;
pthread_setschedparam(pthread_self(), SCHED_RR, &sched);
}
#endif
void HexCommonThread::Suspend()
{
#ifdef _WIN32
SuspendThread( m_hThread );
#else
pthread_mutex_lock (&token->mutex);
while(token->semCount==0)
pthread_cond_wait( &token->condition, &token->mutex);
token->semCount--;
pthread_mutex_unlock (&token->mutex);
#endif
}
void HexCommonThread::Resume()
{
#ifdef _WIN32
ResumeThread( m_hThread );
#else
pthread_mutex_lock (&token->mutex);
if(token->semCount==0)
pthread_cond_signal( &token->condition);
token->semCount++;
pthread_mutex_unlock (&token->mutex);
#endif
}