-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThread.h
236 lines (189 loc) · 7.5 KB
/
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/***************************************************************************
Thread.h - description
-------------------
begin : Sun May 9 2010
copyright : (C) 2010 by Knut-Helge Vik
email : knuthelge@vik.gs
***************************************************************************/
#pragma once
#include <string>
#include <iostream>
#include <limits.h>
#include <BaseLib/Templates/Synchronization.h>
#include"BaseLib/CommonDefines.h"
#include"BaseLib/MutexBare.h"
#include"BaseLib/MutexLocker.h"
#include"BaseLib/WaitCondition.h"
#include"BaseLib/Export.h"
namespace BaseLib
{
class ThreadManager;
class DLL_STATE ThreadDetails
{
public:
enum Priority
{
IdlePriority = 0,
LowestPriority,
LowPriority,
NormalPriority,
HighPriority,
HighestPriority,
TimeCriticalPriority,
InheritPriority
};
enum State
{
NoState = 0,
// states set by the thread itself
RunningState,
RunningMutexedState,
SleepingState,
WaitingState,
LockingState,
UnlockingState,
MonitoringState,
// states set by thread itself or other threads
// to enable ThreadManager to detect faulty threads
StopState,
CancelState,
SystemEndState,
PrintState,
CheckState
};
friend std::ostream& operator<<(std::ostream& ostr, const ThreadDetails::State &status);
public:
ThreadDetails(std::string name = std::string(""))
: threadId_(0)
, humanReadableThreadId_(0)
, threadRunning_(false)
, threadPriority_(ThreadDetails::InheritPriority)
, threadState_(ThreadDetails::NoState)
, threadName_(name)
, mutex_(MutexPolicy::No())
, interruptedFlag_(false)
{ }
~ThreadDetails()
{ }
#ifdef USE_GCC
pthread_attr_t& threadAttributes() { MutexTypeLocker<MutexBare> lock(&mutex_); return threadAttributes_; }
#elif _WIN32
HANDLE threadAttributes() { MutexTypeLocker<MutexBare> lock(&mutex_); return threadHandle_; }
#endif
int64 threadId() const { MutexTypeLocker<MutexBare> lock(&mutex_); return threadId_; }
int64 humanReadableId() const { MutexTypeLocker<MutexBare> lock(&mutex_); return humanReadableThreadId_; }
bool threadRunning() const { MutexTypeLocker<MutexBare> lock(&mutex_); return threadRunning_; }
ThreadDetails::Priority threadPriority() const { MutexTypeLocker<MutexBare> lock(&mutex_); return threadPriority_; }
ThreadDetails::State threadState() const { MutexTypeLocker<MutexBare> lock(&mutex_); return threadState_; }
const std::string& threadName() const { MutexTypeLocker<MutexBare> lock(&mutex_); return threadName_; }
bool isInterrupted() const { return interruptedFlag_; }
#ifdef USE_GCC
void threadAttributes(const pthread_attr_t &attr) { threadAttributes_ = attr; }
#elif _WIN32
void threadAttributes(HANDLE handle) { threadHandle_ = handle; }
#endif
void threadId(int64 id) { MutexTypeLocker<MutexBare> lock(&mutex_); threadId_ = id; }
void humanReadableId(int64 id) { MutexTypeLocker<MutexBare> lock(&mutex_); humanReadableThreadId_ = id; }
void threadRunning(bool run) { MutexTypeLocker<MutexBare> lock(&mutex_); threadRunning_ = run; }
void threadPriority(ThreadDetails::Priority p) { MutexTypeLocker<MutexBare> lock(&mutex_); threadPriority_ = p; }
void threadState(ThreadDetails::State s) { MutexTypeLocker<MutexBare> lock(&mutex_); threadState_ = s; }
void threadName(std::string name) { MutexTypeLocker<MutexBare> lock(&mutex_); threadName_ = name; }
void setInterruptFlag(bool flag) { interruptedFlag_ = flag; }
private:
#ifdef USE_GCC
pthread_attr_t threadAttributes_;
#elif _WIN32
HANDLE threadHandle_;
#endif
int64 threadId_;
int64 humanReadableThreadId_;
bool threadRunning_;
Priority threadPriority_;
State threadState_;
std::string threadName_;
mutable MutexBare mutex_;
std::atomic<bool> interruptedFlag_;
private:
ThreadDetails(const ThreadDetails &) {}
ThreadDetails& operator=(const ThreadDetails&) { return *this; }
};
std::ostream& operator<<(std::ostream& ostr, const ThreadDetails &details);
//-----------------------------------------------------------------------
// class Thread
//-----------------------------------------------------------------------
class DLL_STATE Thread
{
public:
Thread(std::string name = std::string(""));
Thread(std::string name, bool debug);
virtual ~Thread();
friend class ThreadManager;
#ifdef USE_GCC
static void* dispatch(void *thread_obj);
#elif _WIN32
static unsigned int __stdcall dispatch(void *thread_obj);
#endif
virtual bool start(ThreadDetails::Priority priority = ThreadDetails::InheritPriority);
virtual void exit();
//void quit();
virtual void terminate();
virtual bool wait(int64 msecs = LONG_MAX);
virtual void run() = 0;
bool isFinished() const;
bool isRunning() const;
ThreadDetails::Priority priority() const;
const std::string& threadName() const;
int64 threadId() const;
int64 humanReadableId() const;
void humanReadableId(int64 id);
void setPriority(ThreadDetails::Priority priority);
void setThreadName(const std::string &name);
public:
inline void setThreadState(ThreadDetails::State state) { threadDetails_.threadState(state); }
inline ThreadDetails::State getThreadState() const { return threadDetails_.threadState(); }
inline ThreadDetails& getThreadDetails() { return threadDetails_; }
inline bool isDebug() const { return debug_; }
public:
static Thread* currentThread();
static int64 currentThreadId();
static int idealThreadCount();
static void yieldCurrentThread();
static bool checkThreadState();
static int processId();
public:
void interrupt();
static bool isInterrupted();
static void unsetInterrupt();
Templates::Notifyable<Thread, WaitCondition, Mutex>& interruptible();
Templates::Notifyable<Thread, WaitCondition, ReadWriteLock>& interruptibleReadWrite();
public:
static void sleep(int secs);
static void msleep(int msecs);
static void usleep(int usecs);
static void nsleep(uint64 nsecs);
//static void setTerminationEnabled(bool enabled = true);
private:
void cleanUp();
private:
Templates::Notifyable<Thread, WaitCondition, Mutex> interruptible_ = Templates::Notifyable<Thread, WaitCondition, Mutex>(MutexPolicy::No());
Templates::Notifyable<Thread, WaitCondition, ReadWriteLock> interruptibleReadWrite_ = Templates::Notifyable<Thread, WaitCondition, ReadWriteLock>(MutexPolicy::No());
private:
ThreadDetails threadDetails_;
mutable MutexBare threadMutex_;
bool debug_;
private:
Thread(const Thread &) {}
Thread& operator=(const Thread&) { return *this; }
};
DLL_STATE std::ostream& operator<<(std::ostream &ostr, const Thread &thread);
DLL_STATE std::ostream& operator<<(std::ostream &ostr, const Thread *const thread);
class ThreadNoOp : public Thread
{
public:
ThreadNoOp(const std::string& name, bool debug) : Thread(name, debug)
{ }
virtual ~ThreadNoOp()
{ }
virtual void run() { ASSERT(0); }
};
}