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
|
// threadedtests.cpp - Tests for threaded code
//
/**
* Copyright (C) 2008 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/>.
*/
#include "pch.h"
#include "../bson/util/atomic_int.h"
#include "../util/concurrency/mvar.h"
#include "../util/concurrency/thread_pool.h"
#include "../util/timer.h"
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include "dbtests.h"
namespace ThreadedTests {
template <int nthreads_param=10>
class ThreadedTest {
public:
virtual void setup() {} //optional
virtual void subthread(int remaining) = 0; // each thread whatever test work you want done
virtual void validate() = 0; // after work is done
static const int nthreads = nthreads_param;
void run() {
setup();
launch_subthreads(nthreads);
validate();
}
virtual ~ThreadedTest() {}; // not necessary, but makes compilers happy
private:
void launch_subthreads(int remaining) {
if (!remaining)
return;
boost::thread athread(boost::bind(&ThreadedTest::subthread, this, remaining));
launch_subthreads(remaining - 1);
athread.join();
}
};
class MongoMutexTest : public ThreadedTest<135> {
#if defined(_DEBUG)
enum { N = 5000 };
#else
enum { N = 40000 };
#endif
MongoMutex *mm;
public:
void run() {
Timer t;
cout << "MongoMutexTest N:" << N << endl;
ThreadedTest<135>::run();
cout << "MongoMutexTest " << t.millis() << "ms" << endl;
}
private:
virtual void setup() {
mm = new MongoMutex("MongoMutexTest");
}
virtual void subthread(int) {
Client::initThread("mongomutextest");
sleepmillis(0);
for( int i = 0; i < N; i++ ) {
if( i % 7 == 0 ) {
mm->lock_shared();
mm->lock_shared();
mm->unlock_shared();
mm->unlock_shared();
}
else if( i % 7 == 1 ) {
mm->lock_shared();
ASSERT( mm->atLeastReadLocked() );
mm->unlock_shared();
}
else if( i % 7 == 2 ) {
mm->lock();
ASSERT( mm->isWriteLocked() );
mm->unlock();
}
else if( i % 7 == 3 ) {
mm->lock();
mm->lock_shared();
ASSERT( mm->isWriteLocked() );
mm->unlock_shared();
mm->unlock();
}
else if( i % 7 == 4 ) {
mm->lock();
mm->releaseEarly();
mm->unlock();
}
else if( i % 7 == 5 ) {
if( mm->lock_try(1) ) {
mm->unlock();
}
}
else if( i % 7 == 6 ) {
if( mm->lock_shared_try(0) ) {
mm->unlock_shared();
}
}
else {
mm->lock_shared();
mm->unlock_shared();
}
}
cc().shutdown();
}
virtual void validate() {
ASSERT( !mm->atLeastReadLocked() );
mm->lock();
mm->unlock();
mm->lock_shared();
mm->unlock_shared();
}
};
// Tested with up to 30k threads
class IsAtomicUIntAtomic : public ThreadedTest<> {
static const int iterations = 1000000;
AtomicUInt target;
void subthread(int) {
for(int i=0; i < iterations; i++) {
//target.x++; // verified to fail with this version
target++;
}
}
void validate() {
ASSERT_EQUALS(target.x , unsigned(nthreads * iterations));
AtomicUInt u;
ASSERT_EQUALS(0u, u);
ASSERT_EQUALS(0u, u++);
ASSERT_EQUALS(2u, ++u);
ASSERT_EQUALS(2u, u--);
ASSERT_EQUALS(0u, --u);
ASSERT_EQUALS(0u, u);
u++;
ASSERT( u > 0 );
u--;
ASSERT( ! ( u > 0 ) );
}
};
class MVarTest : public ThreadedTest<> {
static const int iterations = 10000;
MVar<int> target;
public:
MVarTest() : target(0) {}
void subthread(int) {
for(int i=0; i < iterations; i++) {
int val = target.take();
#if BOOST_VERSION >= 103500
//increase chances of catching failure
boost::this_thread::yield();
#endif
target.put(val+1);
}
}
void validate() {
ASSERT_EQUALS(target.take() , nthreads * iterations);
}
};
class ThreadPoolTest {
static const int iterations = 10000;
static const int nThreads = 8;
AtomicUInt counter;
void increment(int n) {
for (int i=0; i<n; i++) {
counter++;
}
}
public:
void run() {
ThreadPool tp(nThreads);
for (int i=0; i < iterations; i++) {
tp.schedule(&ThreadPoolTest::increment, this, 2);
}
tp.join();
ASSERT(counter == (unsigned)(iterations * 2));
}
};
class LockTest {
public:
void run() {
// quick atomicint wrap test
// MSGID likely assumes this semantic
AtomicUInt counter = 0xffffffff;
counter++;
ASSERT( counter == 0 );
writelocktry lk( "" , 0 );
ASSERT( lk.got() );
}
};
class RWLockTest1 {
public:
void run() {
RWLock lk( "eliot" );
{
rwlock r( lk , true , false , 1000 );
}
}
};
class RWLockTest2 {
public:
static void worker1( const RWLock * lk , AtomicUInt * x ) {
(*x)++; // 1
cout << "lock b try" << endl;
rwlock b( *lk , true );
cout << "lock b got" << endl;
(*x)++; // 2
}
static void worker2( const RWLock * lk , AtomicUInt * x ) {
cout << "lock c try" << endl;
rwlock c( *lk , false );
(*x)++;
cout << "lock c got" << endl;
}
void run() {
/**
* note: this test will deadlock if the code breaks
*/
RWLock lk( "eliot2" , 120 * 1000 );
auto_ptr<rwlock> a( new rwlock( lk , false ) );
AtomicUInt x1 = 0;
cout << "A : " << &x1 << endl;
boost::thread t1( boost::bind( worker1 , &lk , &x1 ) );
while ( ! x1 );
assert( x1 == 1 );
sleepmillis( 500 );
assert( x1 == 1 );
AtomicUInt x2 = 0;
boost::thread t2( boost::bind( worker2, &lk , &x2 ) );
t2.join();
assert( x2 == 1 );
a.reset();
for ( int i=0; i<2000; i++ ) {
if ( x1 == 2 )
break;
sleepmillis(1);
}
assert( x1 == 2 );
t1.join();
}
};
class All : public Suite {
public:
All() : Suite( "threading" ) {
}
void setupTests() {
add< IsAtomicUIntAtomic >();
add< MVarTest >();
add< ThreadPoolTest >();
add< LockTest >();
add< RWLockTest1 >();
add< RWLockTest2 >();
add< MongoMutexTest >();
}
} myall;
}
|