mirror of
https://github.com/mongodb/mongo.git
synced 2024-12-01 09:32:32 +01:00
Test for atomicIncrement() and ThreadedTest helper
This commit is contained in:
parent
f610e9f657
commit
c58d0d3123
79
dbtests/threadedtests.cpp
Normal file
79
dbtests/threadedtests.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
// 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 "stdafx.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() = 0;
|
||||
virtual void validate() = 0;
|
||||
|
||||
static const int nthreads = nthreads_param;
|
||||
|
||||
void run(){
|
||||
setup();
|
||||
|
||||
boost::thread threads[nthreads];
|
||||
for(int i=0; i < nthreads; i++){
|
||||
boost::thread athread(boost::bind(&ThreadedTest::subthread, this));
|
||||
threads[i].swap(athread);
|
||||
}
|
||||
|
||||
for(int i=0; i < nthreads; i++)
|
||||
threads[i].join();
|
||||
|
||||
validate();
|
||||
}
|
||||
|
||||
virtual ~ThreadedTest() {}; // not necessary, but makes compilers happy
|
||||
};
|
||||
|
||||
class IsWrappingIntAtomic : public ThreadedTest<> {
|
||||
static const int iterations = 1000000;
|
||||
WrappingInt target;
|
||||
|
||||
void subthread(){
|
||||
for(int i=0; i < iterations; i++){
|
||||
//target.x++; // verified to fail with this version
|
||||
target.atomicIncrement();
|
||||
}
|
||||
}
|
||||
void validate(){
|
||||
ASSERT_EQUALS(target.x , unsigned(nthreads * iterations));
|
||||
}
|
||||
};
|
||||
|
||||
class All : public Suite {
|
||||
public:
|
||||
All() : Suite( "threading" ){
|
||||
}
|
||||
|
||||
void setupTests(){
|
||||
add< IsWrappingIntAtomic >();
|
||||
}
|
||||
} myall;
|
||||
}
|
Loading…
Reference in New Issue
Block a user