-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexCommonThread.h
More file actions
137 lines (119 loc) · 2.81 KB
/
HexCommonThread.h
File metadata and controls
137 lines (119 loc) · 2.81 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
#ifndef __HEX_COMMON_THREAD__
#define __HEX_COMMON_THREAD__
#if defined _WIN32 || defined _WIN64
#include <windows.h>
#else
#include <pthread.h>
//#include "gnuclib.h"
#include <string.h>
#include <pthread.h>
#endif
namespace DataStructures
{
class HexCommonThread
{
private:
#ifdef _WIN32
CRITICAL_SECTION m_CS_TerminationMask;
unsigned long* m_hThread;
#else
pthread_mutex_t m_CS_TerminationMask;
typedef struct
{
pthread_mutex_t mutex;
pthread_cond_t condition;
pthread_t p_hThread;
int semCount;
}sem_private_struct, *sem_private;
sem_private token;
#endif
bool m_ternimationMask;
protected:
inline bool ShouldTerminated();
inline void TryTerminateThread();
public:
inline HexCommonThread();
inline virtual ~HexCommonThread();
inline bool ThreadStarted();
#ifdef _WIN32
inline unsigned long* Handle() { return m_hThread; }
#else
inline sem_private Handle() { return token; }
#endif
void Start();
virtual void Run() {};
void Suspend();
void Resume();
void Kill();
void Stop();
static void ThreadSleep(long ms);
void SetPriority(int p);
static const int P_ABOVE_NORMAL;
static const int P_BELOW_NORMAL;
static const int P_HIGHEST;
static const int P_IDLE;
static const int P_LOWEST;
static const int P_NORMAL;
static const int P_CRITICAL;
};
//---------------------------------------------------- inline functions --------------------------------------------------
HexCommonThread::HexCommonThread() : m_ternimationMask(false)
{
#ifdef _WIN32
m_hThread = NULL;
InitializeCriticalSection( &m_CS_TerminationMask );
#else
token=(sem_private)NULL;
pthread_mutex_init(&m_CS_TerminationMask, NULL);
#endif
}
HexCommonThread::~HexCommonThread()
{
Stop();
#ifdef _WIN32
DeleteCriticalSection( &m_CS_TerminationMask );
#else
pthread_mutex_destroy(&m_CS_TerminationMask);
#endif
}
bool HexCommonThread::ThreadStarted()
{
bool shouldBe = ShouldTerminated();
#ifdef _WIN32
return !shouldBe && (m_hThread != NULL);
#else
return !shouldBe && (token!=(sem_private)NULL);
#endif
}
bool HexCommonThread::ShouldTerminated()
{
#ifdef _WIN32
EnterCriticalSection(&m_CS_TerminationMask);
#else
pthread_mutex_lock(&m_CS_TerminationMask);
#endif
bool shouldBe = m_ternimationMask;
#ifdef _WIN32
LeaveCriticalSection(&m_CS_TerminationMask);
#else
pthread_mutex_unlock(&m_CS_TerminationMask);
#endif
return shouldBe;
}
void HexCommonThread::TryTerminateThread()
{
#ifdef _WIN32
EnterCriticalSection(&m_CS_TerminationMask);
#else
pthread_mutex_lock(&m_CS_TerminationMask);
#endif
m_ternimationMask = true;
#ifdef _WIN32
LeaveCriticalSection(&m_CS_TerminationMask);
#else
pthread_mutex_unlock(&m_CS_TerminationMask);
#endif
}
}
extern "C" { unsigned int HexCommonThreadProc(void* param); }
#endif