summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEliot Horowitz <eliot@10gen.com>2009-12-02 15:14:43 -0500
committerEliot Horowitz <eliot@10gen.com>2009-12-02 15:14:43 -0500
commitac1f4db189a9e0d810026d862151ba860209d7c1 (patch)
tree4bc21219757106d73a526cb3349032069481f777
parent9ba5360e8bd3cb7741c0dfcad9d7453a340d2a98 (diff)
parentc58d0d3123e754b9816cf48ef4754d924eec9def (diff)
downloadmongo-ac1f4db189a9e0d810026d862151ba860209d7c1.tar.gz
Merge branch 'master' of git@github.com:mongodb/mongo
-rw-r--r--dbtests/threadedtests.cpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/dbtests/threadedtests.cpp b/dbtests/threadedtests.cpp
new file mode 100644
index 00000000000..a3e8562c8f8
--- /dev/null
+++ b/dbtests/threadedtests.cpp
@@ -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;
+}