forked from abcdls0905/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathios_mutex.h
290 lines (234 loc) · 3.86 KB
/
ios_mutex.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
285
286
287
288
289
290
//--------------------------------------------------------------------
// 文件名: ios_mutex.h
// 内 容:
// 说 明:
// 创建日期: 2010年12月15日
// 创建人: 陆利民
// 版权所有: 苏州蜗牛电子有限公司
//--------------------------------------------------------------------
#ifndef _SYSTEM_IOS_MUTEX_H
#define _SYSTEM_IOS_MUTEX_H
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
// 进程互斥锁
class CMutex
{
public:
// 保证文件存在
static bool ConfirmFile(const char* name)
{
int fd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
if (fd < 0)
{
if (errno != EEXIST)
{
return false;
}
}
else
{
close(fd);
}
return true;
}
// 是否存在指定名字的互斥锁
static bool Exists(const char* name)
{
// 共享文件
if (!ConfirmFile(name))
{
return false;
}
key_t key = ftok(name, 0);
if (-1 == key)
{
return false;
}
if (semget(key, 0, 0) == -1)
{
return false;
}
return true;
}
// 保证唯一
static bool Exclusive(const char* name)
{
// 防止重复创建文件
if (!ConfirmFile(name))
{
return false;
}
// 打开互斥文件
int fd = open(name, O_RDWR, 0666);
if (fd < 0)
{
return false;
}
// 获得文件锁状态
struct flock fl;
memset(&fl, 0, sizeof(fl));
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
int res = fcntl(fd, F_GETLK, &fl);
if (res < 0)
{
close(fd);
return false;
}
if (fl.l_type != F_UNLCK)
{
// 已被其他进程锁定
close(fd);
return false;
}
// 锁定文件
memset(&fl, 0, sizeof(fl));
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
res = fcntl(fd, F_SETLK, &fl);
if (res < 0)
{
close(fd);
return false;
}
// 为了保证锁定不可关闭此文件
return true;
}
public:
CMutex()
{
m_pName = NULL;
m_sem_id = -1;
}
~CMutex()
{
Destroy();
}
// 获得名字
const char* GetName() const
{
if (NULL == m_pName)
{
return "";
}
return m_pName;
}
// 是否有效
bool IsValid() const
{
return -1 != m_sem_id;
}
// 创建或获得锁
bool Create(const char* name, bool* exists = NULL)
{
size_t name_size = strlen(name) + 1;
char* pName = NEW char[name_size];
memcpy(pName, name, name_size);
if (m_pName)
{
delete[] m_pName;
}
m_pName = pName;
if (exists)
{
*exists = false;
}
// 共享文件
if (!ConfirmFile(name))
{
return false;
}
key_t key = ftok(m_pName, 0);
if (-1 == key)
{
return false;
}
m_sem_id = semget(key, 1, IPC_CREAT | IPC_EXCL | 00666);
if (-1 == m_sem_id)
{
if (errno == EEXIST)
{
// 已存在
if (exists)
{
*exists = true;
}
m_sem_id = semget(key, 1, IPC_CREAT | 00666);
if (-1 == m_sem_id)
{
return false;
}
}
else
{
return false;
}
}
else
{
// 新建,设置信号量的初始值
union semu { int val; };
semu arg;
arg.val = 1;
if (semctl(m_sem_id, 0, SETVAL, arg) == -1)
{
// 失败
semctl(m_sem_id, 0, IPC_RMID, NULL);
m_sem_id = -1;
return false;
}
}
return true;
}
// 删除锁
bool Destroy()
{
if (m_sem_id != -1)
{
semctl(m_sem_id, 0, IPC_RMID, NULL);
m_sem_id = -1;
}
if (m_pName)
{
delete[] m_pName;
m_pName = NULL;
}
return true;
}
// 加锁
void Lock()
{
struct sembuf lock;
lock.sem_num = 0;
lock.sem_op = -1;
lock.sem_flg = SEM_UNDO;
semop(m_sem_id, &lock, 1);
}
// 解锁
void Unlock()
{
struct sembuf unlock;
unlock.sem_num = 0;
unlock.sem_op = 1;
unlock.sem_flg = SEM_UNDO;
semop(m_sem_id, &unlock, 1);
}
private:
CMutex(const CMutex&);
CMutex& operator=(const CMutex&);
private:
char* m_pName;
int m_sem_id;
};
#endif // _SYSTEM_IOS_MUTEX_H