upgradeable prep (no use yet) and some slight cleaning and a writelock test

This commit is contained in:
Dwight
2011-07-11 15:50:27 -04:00
parent 830a73f1ac
commit eecddcd7de
2 changed files with 80 additions and 41 deletions

View File

@@ -464,7 +464,7 @@ namespace ThreadedTests {
}
};
class UpgradableTest : public ThreadedTest<5> {
class UpgradableTest : public ThreadedTest<7> {
RWLock m;
public:
UpgradableTest() : m("utest") {}
@@ -477,11 +477,15 @@ namespace ThreadedTests {
R = get a read lock and we expect it to be fast
w = write lock
*/
const char *what = " RURwR";
// /-- verify upgrade can be done instantly while in a read lock already
// | /-- verify upgrade acquisition isn't greedy
// | | /-- verify writes aren't greedy while in upgradable
// v v v
const char *what = " RURuRwR";
sleepmillis(100*x);
log() << x << " " << what[x] << endl;
log() << x << what[x] << " request" << endl;
switch( what[x] ) {
case 'w':
{
@@ -497,13 +501,20 @@ namespace ThreadedTests {
{
Timer t;
m.lockAsUpgradable();
log() << x << " U got\n" << endl;
log() << x << " U got" << endl;
if( what[x] == 'U' ) {
ASSERT( t.millis() < 15 );
if( t.millis() > 20 ) {
DEV {
// a _DEBUG buildbot might be slow, try to avoid false positives
log() << "warning lock upgrade was slow " << t.millis() << endl;
}
else {
ASSERT( false );
}
}
}
sleepsecs(5);
cout << endl;
log() << x << " U unlock\n" << endl;
sleepsecs(1);
log() << x << " U unlock" << endl;
m.unlockFromUpgradable();
}
break;
@@ -518,25 +529,61 @@ namespace ThreadedTests {
log() << "warning: when in upgradable write locks are still greedy on this platform" << endl;
}
}
sleepmillis(100);
sleepmillis(200);
log() << x << " R unlock" << endl;
m.unlock_shared();
}
break;
default:
log() << "default? " << x << endl;
ASSERT(false);
}
cc().shutdown();
}
};
class WriteLocksAreGreedy : public ThreadedTest<3> {
public:
WriteLocksAreGreedy() : m("gtest") {}
private:
RWLock m;
virtual void validate() { }
virtual void subthread(int x) {
Client::initThread("utest");
if( x == 1 ) {
cout << mongo::curTimeMillis64() % 10000 << " 1" << endl;
rwlock_shared lk(m);
sleepmillis(300);
cout << mongo::curTimeMillis64() % 10000 << " 1x" << endl;
}
if( x == 2 ) {
sleepmillis(100);
cout << mongo::curTimeMillis64() % 10000 << " 2" << endl;
rwlock lk(m, true);
//m._lock();
cout << mongo::curTimeMillis64() % 10000 << " 2x" << endl;
//m.unlock();
}
if( x == 3 ) {
sleepmillis(200);
Timer t;
cout << mongo::curTimeMillis64() % 10000 << " 3" << endl;
rwlock_shared lk(m);
cout << mongo::curTimeMillis64() % 10000 << " 3x" << endl;
cout << t.millis() << endl;
ASSERT( t.millis() > 50 );
}
cc().shutdown();
}
};
class All : public Suite {
public:
All() : Suite( "threading" ) { }
void setupTests() {
add< UpgradableTest >();
add< WriteLocksAreGreedy >();
//add< UpgradableTest >();
add< List1Test >();
add< List1Test2 >();