2009-12-02 20:33:06 +01:00
|
|
|
// 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/>.
|
|
|
|
*/
|
|
|
|
|
2010-04-27 21:27:52 +02:00
|
|
|
#include "pch.h"
|
2010-04-25 01:24:07 +02:00
|
|
|
#include "../bson/util/atomic_int.h"
|
2010-05-26 19:35:01 +02:00
|
|
|
#include "../util/concurrency/mvar.h"
|
|
|
|
#include "../util/concurrency/thread_pool.h"
|
2009-12-02 20:33:06 +01:00
|
|
|
#include <boost/thread.hpp>
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
|
|
|
#include "dbtests.h"
|
|
|
|
|
|
|
|
namespace ThreadedTests {
|
|
|
|
|
|
|
|
template <int nthreads_param=10>
|
2011-01-04 06:40:41 +01:00
|
|
|
class ThreadedTest {
|
|
|
|
public:
|
|
|
|
virtual void setup() {} //optional
|
|
|
|
virtual void subthread() = 0;
|
|
|
|
virtual void validate() = 0;
|
2009-12-02 20:33:06 +01:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
static const int nthreads = nthreads_param;
|
2009-12-02 20:33:06 +01:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
void run() {
|
|
|
|
setup();
|
|
|
|
launch_subthreads(nthreads);
|
|
|
|
validate();
|
|
|
|
}
|
2009-12-02 20:33:06 +01:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
virtual ~ThreadedTest() {}; // not necessary, but makes compilers happy
|
2009-12-02 21:15:24 +01:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
private:
|
|
|
|
void launch_subthreads(int remaining) {
|
|
|
|
if (!remaining) return;
|
2009-12-02 21:15:24 +01:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
boost::thread athread(boost::bind(&ThreadedTest::subthread, this));
|
2009-12-02 21:15:24 +01:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
launch_subthreads(remaining - 1);
|
2009-12-02 21:15:24 +01:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
athread.join();
|
|
|
|
}
|
2009-12-02 20:33:06 +01:00
|
|
|
};
|
|
|
|
|
2011-01-05 05:03:19 +01:00
|
|
|
class MongoMutexTest : public ThreadedTest<135> {
|
2011-01-04 23:45:28 +01:00
|
|
|
enum { N = 100000 };
|
2011-01-04 22:14:31 +01:00
|
|
|
MongoMutex *mm;
|
|
|
|
virtual void setup() {
|
|
|
|
mm = new MongoMutex("MongoMutexTest");
|
|
|
|
}
|
|
|
|
virtual void subthread() {
|
|
|
|
Client::initThread("mongomutextest");
|
|
|
|
sleepmillis(0);
|
|
|
|
for( int i = 0; i < N; i++ ) {
|
|
|
|
if( i % 20000 == 0 )
|
|
|
|
log() << i << endl;
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-12-02 21:15:24 +01:00
|
|
|
// Tested with up to 30k threads
|
2010-01-26 01:32:00 +01:00
|
|
|
class IsAtomicUIntAtomic : public ThreadedTest<> {
|
2009-12-02 20:33:06 +01:00
|
|
|
static const int iterations = 1000000;
|
2010-01-26 01:32:00 +01:00
|
|
|
AtomicUInt target;
|
2009-12-02 20:33:06 +01:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
void subthread() {
|
|
|
|
for(int i=0; i < iterations; i++) {
|
2009-12-02 20:33:06 +01:00
|
|
|
//target.x++; // verified to fail with this version
|
2010-01-26 01:32:00 +01:00
|
|
|
target++;
|
2009-12-02 20:33:06 +01:00
|
|
|
}
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
void validate() {
|
2009-12-02 20:33:06 +01:00
|
|
|
ASSERT_EQUALS(target.x , unsigned(nthreads * iterations));
|
2010-01-26 01:32:00 +01:00
|
|
|
|
|
|
|
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);
|
2009-12-02 20:33:06 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-12-16 20:35:33 +01:00
|
|
|
class MVarTest : public ThreadedTest<> {
|
|
|
|
static const int iterations = 10000;
|
|
|
|
MVar<int> target;
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
public:
|
2009-12-16 20:35:33 +01:00
|
|
|
MVarTest() : target(0) {}
|
2011-01-04 06:40:41 +01:00
|
|
|
void subthread() {
|
|
|
|
for(int i=0; i < iterations; i++) {
|
2009-12-16 20:35:33 +01:00
|
|
|
int val = target.take();
|
|
|
|
#if BOOST_VERSION >= 103500
|
|
|
|
//increase chances of catching failure
|
|
|
|
boost::this_thread::yield();
|
|
|
|
#endif
|
|
|
|
target.put(val+1);
|
|
|
|
}
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
void validate() {
|
2009-12-16 20:35:33 +01:00
|
|
|
ASSERT_EQUALS(target.take() , nthreads * iterations);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
class ThreadPoolTest {
|
2009-12-22 18:13:40 +01:00
|
|
|
static const int iterations = 10000;
|
|
|
|
static const int nThreads = 8;
|
|
|
|
|
2010-01-26 01:32:00 +01:00
|
|
|
AtomicUInt counter;
|
2011-01-04 06:40:41 +01:00
|
|
|
void increment(int n) {
|
|
|
|
for (int i=0; i<n; i++) {
|
2010-01-26 01:32:00 +01:00
|
|
|
counter++;
|
2009-12-22 18:13:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
public:
|
|
|
|
void run() {
|
2009-12-22 18:13:40 +01:00
|
|
|
ThreadPool tp(nThreads);
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
for (int i=0; i < iterations; i++) {
|
2009-12-22 18:13:40 +01:00
|
|
|
tp.schedule(&ThreadPoolTest::increment, this, 2);
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2009-12-22 18:13:40 +01:00
|
|
|
tp.join();
|
|
|
|
|
2010-01-26 01:32:00 +01:00
|
|
|
ASSERT(counter == (unsigned)(iterations * 2));
|
2009-12-22 18:13:40 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-06-10 16:40:34 +02:00
|
|
|
class LockTest {
|
|
|
|
public:
|
2011-01-04 06:40:41 +01:00
|
|
|
void run() {
|
2010-07-23 15:58:43 +02:00
|
|
|
// quick atomicint wrap test
|
|
|
|
// MSGID likely assumes this semantic
|
|
|
|
AtomicUInt counter = 0xffffffff;
|
|
|
|
counter++;
|
|
|
|
ASSERT( counter == 0 );
|
|
|
|
|
2010-06-10 16:40:34 +02:00
|
|
|
writelocktry lk( "" , 0 );
|
|
|
|
ASSERT( lk.got() );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-12-02 20:33:06 +01:00
|
|
|
class All : public Suite {
|
|
|
|
public:
|
2011-01-04 06:40:41 +01:00
|
|
|
All() : Suite( "threading" ) {
|
2009-12-02 20:33:06 +01:00
|
|
|
}
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
void setupTests() {
|
2010-01-26 01:32:00 +01:00
|
|
|
add< IsAtomicUIntAtomic >();
|
2009-12-16 20:35:33 +01:00
|
|
|
add< MVarTest >();
|
2009-12-22 18:13:40 +01:00
|
|
|
add< ThreadPoolTest >();
|
2010-06-10 16:40:34 +02:00
|
|
|
add< LockTest >();
|
2011-01-04 22:14:31 +01:00
|
|
|
add< MongoMutexTest >();
|
2009-12-02 20:33:06 +01:00
|
|
|
}
|
|
|
|
} myall;
|
|
|
|
}
|