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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
// @file rwlock.h generic reader-writer lock (cross platform support)
/*
* Copyright (C) 2010 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "mutex.h"
#include "../time_support.h"
// this requires newer windows versions
// it works better than sharable_mutex under high contention
#if defined(_WIN64)
//#define MONGO_USE_SRW_ON_WINDOWS 1
#endif
#if !defined(MONGO_USE_SRW_ON_WINDOWS)
#if BOOST_VERSION >= 103500
# define BOOST_RWLOCK
#else
# if defined(_WIN32)
# error need boost >= 1.35 for windows
# endif
# include <pthread.h>
#endif
#if defined(_WIN32)
# include "shared_mutex_win.hpp"
namespace mongo {
typedef boost::modified_shared_mutex shared_mutex;
}
# undef assert
# define assert MONGO_assert
#elif defined(BOOST_RWLOCK)
# include <boost/thread/shared_mutex.hpp>
# undef assert
# define assert MONGO_assert
#endif
#endif
namespace mongo {
#if defined(MONGO_USE_SRW_ON_WINDOWS) && defined(_WIN32)
// Windows RWLock implementation (requires newer versions of windows thus the above macro)
class RWLock : boost::noncopyable {
public:
RWLock(const char *, int lowPriorityWaitMS=0 ) : _lowPriorityWaitMS(lowPriorityWaitMS)
{ InitializeSRWLock(&_lock); }
~RWLock() { }
const char * implType() const { return "WINSRW"; }
int lowPriorityWaitMS() const { return _lowPriorityWaitMS; }
void lock() { AcquireSRWLockExclusive(&_lock); }
void unlock() { ReleaseSRWLockExclusive(&_lock); }
void lock_shared() { AcquireSRWLockShared(&_lock); }
void unlock_shared() { ReleaseSRWLockShared(&_lock); }
bool lock_shared_try( int millis ) {
if( TryAcquireSRWLockShared(&_lock) )
return true;
if( millis == 0 )
return false;
unsigned long long end = curTimeMicros64() + millis*1000;
while( 1 ) {
Sleep(1);
if( TryAcquireSRWLockShared(&_lock) )
return true;
if( curTimeMicros64() >= end )
break;
}
return false;
}
bool lock_try( int millis = 0 ) {
if( TryAcquireSRWLockExclusive(&_lock) ) // quick check to optimistically avoid calling curTimeMicros64
return true;
if( millis == 0 )
return false;
unsigned long long end = curTimeMicros64() + millis*1000;
do {
Sleep(1);
if( TryAcquireSRWLockExclusive(&_lock) )
return true;
} while( curTimeMicros64() < end );
return false;
}
private:
SRWLOCK _lock;
const int _lowPriorityWaitMS;
};
#elif defined(BOOST_RWLOCK)
// Boost based RWLock implementation
class RWLock : boost::noncopyable {
shared_mutex _m;
const int _lowPriorityWaitMS;
public:
const char * const _name;
RWLock(const char *name, int lowPriorityWait=0) : _lowPriorityWaitMS(lowPriorityWait) , _name(name) { }
const char * implType() const { return "boost"; }
int lowPriorityWaitMS() const { return _lowPriorityWaitMS; }
void lock() {
_m.lock();
DEV mutexDebugger.entering(_name);
}
/*void lock() {
// This sequence gives us the lock semantics we want: specifically that write lock acquisition is
// greedy EXCEPT when someone already is in upgradable state.
lockAsUpgradable();
upgrade();
DEV mutexDebugger.entering(_name);
}*/
void unlock() {
DEV mutexDebugger.leaving(_name);
_m.unlock();
}
void lockAsUpgradable() {
_m.lock_upgrade();
}
void unlockFromUpgradable() { // upgradable -> unlocked
_m.unlock_upgrade();
}
void upgrade() { // upgradable -> exclusive lock
_m.unlock_upgrade_and_lock();
}
void lock_shared() {
_m.lock_shared();
}
void unlock_shared() {
_m.unlock_shared();
}
bool lock_shared_try( int millis ) {
if( _m.timed_lock_shared( boost::posix_time::milliseconds(millis) ) ) {
return true;
}
return false;
}
bool lock_try( int millis = 0 ) {
if( _m.timed_lock( boost::posix_time::milliseconds(millis) ) ) {
DEV mutexDebugger.entering(_name);
return true;
}
return false;
}
};
#else
// Posix RWLock implementation
class RWLock : boost::noncopyable {
pthread_rwlock_t _lock;
const int _lowPriorityWaitMS;
static void check( int x ) {
if( MONGO_likely(x == 0) )
return;
log() << "pthread rwlock failed: " << x << endl;
assert( x == 0 );
}
public:
const char *_name;
RWLock(const char *name, int lowPriorityWaitMS=0) : _lowPriorityWaitMS(lowPriorityWaitMS), _name(name)
{
check( pthread_rwlock_init( &_lock , 0 ) );
}
~RWLock() {
if ( ! StaticObserver::_destroyingStatics ) {
wassert( pthread_rwlock_destroy( &_lock ) == 0 ); // wassert as don't want to throw from a destructor
}
}
const char * implType() const { return "posix"; }
int lowPriorityWaitMS() const { return _lowPriorityWaitMS; }
void lock() {
check( pthread_rwlock_wrlock( &_lock ) );
DEV mutexDebugger.entering(_name);
}
void unlock() {
DEV mutexDebugger.leaving(_name);
check( pthread_rwlock_unlock( &_lock ) );
}
void lock_shared() {
check( pthread_rwlock_rdlock( &_lock ) );
}
void unlock_shared() {
check( pthread_rwlock_unlock( &_lock ) );
}
bool lock_shared_try( int millis ) {
return _try( millis , false );
}
bool lock_try( int millis = 0 ) {
if( _try( millis , true ) ) {
DEV mutexDebugger.entering(_name);
return true;
}
return false;
}
bool _try( int millis , bool write ) {
while ( true ) {
int x = write ?
pthread_rwlock_trywrlock( &_lock ) :
pthread_rwlock_tryrdlock( &_lock );
if ( x <= 0 ) {
return true;
}
if ( millis-- <= 0 )
return false;
if ( x == EBUSY ) {
sleepmillis(1);
continue;
}
check(x);
}
return false;
}
};
#endif
/** throws on failure to acquire in the specified time period. */
class rwlock_try_write : boost::noncopyable {
public:
struct exception { };
rwlock_try_write(RWLock& l, int millis = 0) : _l(l) {
if( !l.lock_try(millis) )
throw exception();
}
~rwlock_try_write() { _l.unlock(); }
private:
RWLock& _l;
};
class rwlock_shared : boost::noncopyable {
public:
rwlock_shared(RWLock& rwlock) : _r(rwlock) {_r.lock_shared(); }
~rwlock_shared() { _r.unlock_shared(); }
private:
RWLock& _r;
};
/* scoped lock for RWLock */
class rwlock : boost::noncopyable {
public:
/**
* @param write acquire write lock if true sharable if false
* @param lowPriority if > 0, will try to get the lock non-greedily for that many ms
*/
rwlock( const RWLock& lock , bool write, /* bool alreadyHaveLock = false , */int lowPriorityWaitMS = 0 )
: _lock( (RWLock&)lock ) , _write( write ) {
{
if ( _write ) {
if ( ! lowPriorityWaitMS && lock.lowPriorityWaitMS() )
lowPriorityWaitMS = lock.lowPriorityWaitMS();
if ( lowPriorityWaitMS ) {
bool got = false;
for ( int i=0; i<lowPriorityWaitMS; i++ ) {
if ( _lock.lock_try(0) ) {
got = true;
break;
}
int sleep = 1;
if ( i > ( lowPriorityWaitMS / 20 ) )
sleep = 10;
sleepmillis(sleep);
i += ( sleep - 1 );
}
if ( ! got ) {
log() << "couldn't get lazy rwlock" << endl;
_lock.lock();
}
}
else {
_lock.lock();
}
}
else {
_lock.lock_shared();
}
}
}
~rwlock() {
if ( _write )
_lock.unlock();
else
_lock.unlock_shared();
}
private:
RWLock& _lock;
const bool _write;
};
/** recursive on shared locks is ok for this implementation */
class RWLockRecursive : boost::noncopyable {
ThreadLocalValue<int> _state;
RWLock _lk;
friend class Exclusive;
public:
/** @param lpwaitms lazy wait */
RWLockRecursive(const char *name, int lpwaitms) : _lk(name, lpwaitms) { }
void assertExclusivelyLocked() {
dassert( _state.get() < 0 );
}
// RWLockRecursive::Exclusive scoped lock
class Exclusive : boost::noncopyable {
RWLockRecursive& _r;
rwlock *_scopedLock;
public:
Exclusive(RWLockRecursive& r) : _r(r), _scopedLock(0) {
int s = _r._state.get();
dassert( s <= 0 );
if( s == 0 )
_scopedLock = new rwlock(_r._lk, true);
_r._state.set(s-1);
}
~Exclusive() {
int s = _r._state.get();
DEV wassert( s < 0 ); // wassert: don't throw from destructors
_r._state.set(s+1);
delete _scopedLock;
}
};
// RWLockRecursive::Shared scoped lock
class Shared : boost::noncopyable {
RWLockRecursive& _r;
bool _alreadyExclusive;
public:
Shared(RWLockRecursive& r) : _r(r) {
int s = _r._state.get();
_alreadyExclusive = s < 0;
if( !_alreadyExclusive ) {
dassert( s >= 0 ); // -1 would mean exclusive
if( s == 0 )
_r._lk.lock_shared();
_r._state.set(s+1);
}
}
~Shared() {
if( _alreadyExclusive ) {
DEV wassert( _r._state.get() < 0 );
}
else {
int s = _r._state.get() - 1;
if( s == 0 )
_r._lk.unlock_shared();
_r._state.set(s);
DEV wassert( s >= 0 );
}
}
};
};
}
|