forked from artyom-beilis/cppcms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposix_util.h
More file actions
331 lines (304 loc) · 7 KB
/
Copy pathposix_util.h
File metadata and controls
331 lines (304 loc) · 7 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef CPPCMS_POSIX_UTIL_H
#define CPPCMS_POSIX_UTIL_H
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <cppcms/cppcms_error.h>
#include <booster/noncopyable.h>
namespace cppcms {
namespace impl {
inline void *mmap_anonymous(size_t size)
{
#if defined(MAP_ANONYMOUS)
void *p=::mmap(0,size,PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
int err=errno;
#elif defined(MAP_ANON)
void *p=::mmap(0,size,PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
int err=errno;
#else
int fd=::open("/dev/null",O_RDWR);
if(fd < 0)
throw cppcms_error(errno,"Failed to open /dev/null");
void *p=::mmap(0,size,PROT_READ | PROT_WRITE,MAP_SHARED, fd, 0);
int err=errno;
::close(fd);
#endif
if(p==MAP_FAILED)
throw cppcms_error(err,"Failed to create shared memory");
return p;
}
inline void create_mutex(pthread_mutex_t *m,bool pshared = false)
{
if(!pshared) {
pthread_mutex_init(m,0);
}
else {
#ifdef CPPCMS_HAS_THREAD_PSHARED
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
try {
int res;
res = pthread_mutexattr_setpshared(&attr,PTHREAD_PROCESS_SHARED);
if(res==0)
res = pthread_mutex_init(m,&attr);
if(res < 0)
throw cppcms_error(errno,"Failed to create process shared mutex");
pthread_mutexattr_destroy(&attr);
}
catch(...) {
pthread_mutexattr_destroy(&attr);
throw;
}
#else
throw cppcms_error("Process shared mutex is not supported");
#endif
}
}
inline void destroy_mutex(pthread_mutex_t *m)
{
pthread_mutex_destroy(m);
}
inline void create_rwlock(pthread_rwlock_t *m,bool pshared=false)
{
if(!pshared) {
pthread_rwlock_init(m,0);
}
else {
#ifdef CPPCMS_HAS_THREAD_PSHARED
pthread_rwlockattr_t attr;
pthread_rwlockattr_init(&attr);
try {
int res;
res = pthread_rwlockattr_setpshared(&attr,PTHREAD_PROCESS_SHARED);
if(res==0)
res = pthread_rwlock_init(m,&attr);
if(res < 0)
throw cppcms_error(errno,"Failed to create process shared mutex");
pthread_rwlockattr_destroy(&attr);
}
catch(...) {
pthread_rwlockattr_destroy(&attr);
throw;
}
#else
throw cppcms_error("Process shared mutex is not supported");
#endif
}
}
inline void destroy_rwlock(pthread_rwlock_t *m)
{
pthread_rwlock_destroy(m);
}
#ifdef CPPCMS_HAS_THREAD_PSHARED
inline bool test_pthread_mutex_pshared_impl()
{
void *memory=mmap_anonymous(sizeof(pthread_mutex_t));
bool res=false;
try {
create_mutex(reinterpret_cast<pthread_mutex_t *>(memory),true);
destroy_mutex(reinterpret_cast<pthread_mutex_t *>(memory));
res=true;
}
catch(cppcms_error const &e) {
res= false;
}
::munmap((char*)memory,sizeof(pthread_mutex_t));
return res;
}
inline bool test_pthread_mutex_pshared()
{
static bool has = test_pthread_mutex_pshared_impl();
return has;
}
#else
inline bool test_pthread_mutex_pshared()
{
return false;
}
#endif
class mutex : public booster::noncopyable {
public:
class guard;
mutex() :
plock_(0),
flock_(0)
{
bool use_pthread=test_pthread_mutex_pshared();
if(use_pthread) {
plock_ =reinterpret_cast<pthread_mutex_t *>(mmap_anonymous(sizeof(pthread_mutex_t)));
create_mutex(plock_,true);
}
else {
plock_=&normal_;
create_mutex(plock_,false);
flock_=tmpfile();
if(!flock_) {
int err=errno;
destroy_mutex(plock_);
throw cppcms_error(err,"Failed to create temporary file");
}
}
}
void lock()
{
pthread_mutex_lock(plock_);
if(flock_) {
struct flock lock;
memset(&lock,0,sizeof(lock));
lock.l_type=F_WRLCK;
lock.l_whence=SEEK_SET;
while(::fcntl(fileno(flock_),F_SETLKW,&lock)!=0 && errno==EINTR)
;
}
}
void unlock()
{
if(flock_) {
struct flock lock;
memset(&lock,0,sizeof(lock));
lock.l_type=F_UNLCK;
lock.l_whence=SEEK_SET;
while(::fcntl(fileno(flock_),F_SETLKW,&lock)!=0 && errno==EINTR)
;
}
pthread_mutex_unlock(plock_);
}
~mutex()
{
if(flock_) ::fclose(flock_);
destroy_mutex(plock_);
if(plock_ != &normal_)
::munmap((char*)plock_,sizeof(pthread_mutex_t));
}
private:
pthread_mutex_t *plock_;
FILE *flock_;
pthread_mutex_t normal_;
};
class mutex::guard : public booster::noncopyable {
public:
guard(mutex &m) : m_(m)
{
m_.lock();
}
~guard()
{
m_.unlock();
}
private:
mutex &m_;
};
class shared_mutex : public booster::noncopyable {
public:
class shared_guard;
class unique_guard;
shared_mutex() :
plock_(0),
flock_(0)
{
bool use_pthread=test_pthread_mutex_pshared();
if(use_pthread) {
plock_ =reinterpret_cast<pthread_rwlock_t *>(mmap_anonymous(sizeof(pthread_rwlock_t)));
create_rwlock(plock_,true);
}
else {
plock_=&normal_;
create_rwlock(plock_,false);
flock_=tmpfile();
if(!flock_) {
int err=errno;
destroy_rwlock(plock_);
throw cppcms_error(err,"Failed to create temporary file");
}
}
}
void wrlock()
{
pthread_rwlock_wrlock(plock_);
if(flock_) {
struct flock lock;
memset(&lock,0,sizeof(lock));
lock.l_type=F_WRLCK;
lock.l_whence=SEEK_SET;
while(::fcntl(fileno(flock_),F_SETLKW,&lock)!=0 && errno==EINTR)
;
}
}
void rdlock()
{
pthread_rwlock_rdlock(plock_);
if(flock_) {
struct flock lock;
memset(&lock,0,sizeof(lock));
lock.l_type=F_RDLCK;
lock.l_whence=SEEK_SET;
while(::fcntl(fileno(flock_),F_SETLKW,&lock)!=0 && errno==EINTR)
;
}
}
void unlock()
{
if(flock_) {
struct flock lock;
memset(&lock,0,sizeof(lock));
lock.l_type=F_UNLCK;
lock.l_whence=SEEK_SET;
while(::fcntl(fileno(flock_),F_SETLKW,&lock)!=0 && errno==EINTR)
;
}
pthread_rwlock_unlock(plock_);
}
~shared_mutex()
{
if(flock_) ::fclose(flock_);
destroy_rwlock(plock_);
if(plock_ != &normal_)
::munmap((char*)plock_,sizeof(pthread_rwlock_t));
}
private:
pthread_rwlock_t *plock_;
FILE *flock_;
pthread_rwlock_t normal_;
};
class shared_mutex::shared_guard : public booster::noncopyable {
public:
shared_guard(shared_mutex &m) : m_(m)
{
m_.rdlock();
}
~shared_guard()
{
m_.unlock();
}
private:
shared_mutex &m_;
};
class shared_mutex::unique_guard : public booster::noncopyable {
public:
unique_guard(shared_mutex &m) : m_(m)
{
m_.wrlock();
}
~unique_guard()
{
m_.unlock();
}
private:
shared_mutex &m_;
};
} // impl
} // cppcms
#endif