-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy paththread.h
284 lines (246 loc) · 5.1 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#ifndef SERVER_FRAMEWORK_THREAD_H
#define SERVER_FRAMEWORK_THREAD_H
#include "util.h"
#include <functional>
#include <memory>
#include <pthread.h>
#include <semaphore.h>
#include <cstdint>
#include <sys/types.h>
#include <unistd.h>
namespace zjl
{
/**
* @brief 对 semaphore.h 信号量的简单封装
*/
class Semaphore : public noncopyable
{
public:
explicit Semaphore(uint32_t count);
~Semaphore();
// -1,值为零时阻塞
void wait();
// +1
void notify();
private:
sem_t m_semaphore;
};
/**
* @brief 作用域线程锁包装器
* T 需要实现 lock() 与 unlock() 方法
*/
template <typename T>
class ScopedLockImpl
{
public:
explicit ScopedLockImpl(T* mutex)
: m_mutex(mutex)
{
m_mutex->lock();
m_locked = true;
}
~ScopedLockImpl() { unlock(); }
void lock()
{
if (!m_locked)
{
m_mutex->lock();
m_locked = true;
}
}
void unlock()
{
if (m_locked)
{
m_locked = false;
m_mutex->unlock();
}
}
private:
T* m_mutex;
bool m_locked;
};
/**
* @brief 作用域读写锁包装器
* T 需要实现 readLock() 与 unlock() 方法
*/
template <typename T>
class ReadScopedLockImpl
{
public:
explicit ReadScopedLockImpl(T* mutex)
: m_mutex(mutex)
{
m_mutex->readLock();
m_locked = true;
}
~ReadScopedLockImpl() { unlock(); }
void lock()
{
if (!m_locked)
{
m_mutex->readLock();
m_locked = true;
}
}
void unlock()
{
if (m_locked)
{
m_locked = false;
m_mutex->unlock();
}
}
private:
T* m_mutex;
bool m_locked;
};
/**
* @brief 作用域读写锁包装器
* T 需要实现 writeLock() 与 unlock() 方法
*/
template <typename T>
class WriteScopedLockImpl
{
public:
explicit WriteScopedLockImpl(T* mutex)
: m_mutex(mutex)
{
m_mutex->writeLock();
m_locked = true;
}
~WriteScopedLockImpl() { unlock(); }
void lock()
{
if (!m_locked)
{
m_mutex->writeLock();
m_locked = true;
}
}
void unlock()
{
if (m_locked)
{
m_locked = false;
m_mutex->unlock();
}
}
private:
T* m_mutex;
bool m_locked;
};
/**
* @brief pthread 互斥量的封装
*/
class Mutex
{
public:
Mutex()
{
pthread_mutex_init(&m_mutex, nullptr);
}
~Mutex()
{
pthread_mutex_destroy(&m_mutex);
}
int lock()
{
return pthread_mutex_lock(&m_mutex);
}
int unlock()
{
return pthread_mutex_unlock(&m_mutex);
}
private:
pthread_mutex_t m_mutex{};
};
/**
* @brief 互斥量的 RAII
*/
using ScopedLock = ScopedLockImpl<Mutex>;
/**
* @brief pthread 读写锁的封装
*/
class RWLock
{
public:
RWLock()
{
pthread_rwlock_init(&m_lock, nullptr);
}
~RWLock()
{
pthread_rwlock_destroy(&m_lock);
}
int readLock()
{
return pthread_rwlock_rdlock(&m_lock);
}
int writeLock()
{
return pthread_rwlock_wrlock(&m_lock);
}
int unlock()
{
return pthread_rwlock_unlock(&m_lock);
}
private:
pthread_rwlock_t m_lock{};
};
/**
* @brief 读写锁针对读操作的作用域 RAII 实现
*/
using ReadScopedLock = ReadScopedLockImpl<RWLock>;
/**
* @brief 读写锁针对写操作的作用域 RAII 实现
*/
using WriteScopedLock = WriteScopedLockImpl<RWLock>;
/**
* @brief 线程类
* 基于 pthread 封装的
*/
class Thread : public noncopyable
{
public:
typedef std::shared_ptr<Thread> ptr;
typedef std::unique_ptr<Thread> uptr;
typedef std::function<void()> ThreadFunc;
Thread(ThreadFunc callback, const std::string& name);
~Thread();
// 获取线程 id
pid_t getId() const;
// 获取线程名称
const std::string& getName() const;
// 设置线程名称
void setName(const std::string& name);
// 将线程并入主线程
int join();
public:
// 获取当前线程
// static Thread* GetThis();
// 获取当前线程的系统线程 id
static pid_t GetThisId();
// 获取当前运行线程的名称
static const std::string& GetThisThreadName();
// 设置当前运行线程的名称
static void SetThisThreadName(const std::string& name);
// 启动线程, 接收 Thread*
static void* Run(void* arg);
private:
// 系统线程 id, 通过 syscall() 获取
pid_t m_id;
// 线程名称
std::string m_name;
// pthread 线程 id
pthread_t m_thread;
// 线程执行的函数
ThreadFunc m_callback;
// 控制线程启动的信号量
Semaphore m_semaphore;
// 线程状态
bool m_started;
bool m_joined;
};
}
#endif