summaryrefslogtreecommitdiff
path: root/src/mongo/dbtests/threadedtests.cpp
blob: 9b38e12badfb72c6232242c780fbb86480f0bc44 (plain)
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
// @file 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 "../server.h"
#include "../bson/util/atomic_int.h"
#include "../util/concurrency/mvar.h"
#include "../util/concurrency/thread_pool.h"
#include "../util/concurrency/list.h"
#include "../util/timer.h"
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include "../db/d_concurrency.h"
#include "../util/concurrency/synchronization.h"
#include "../util/concurrency/qlock.h"
#include "dbtests.h"

namespace mongo { 
    void testNonGreedy();
}

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();
        }
    };

    const int nthr=135;
    //const int nthr=7;
    class MongoMutexTest : public ThreadedTest<nthr> {
#if defined(_DEBUG)
        enum { N = 2000 };
#else
        enum { N = 4000/*0*/ };
#endif
        ProgressMeter pm;
    public:
        int upgradeWorked, upgradeFailed;
        MongoMutexTest() : pm(N * nthreads) {
            upgradeWorked = upgradeFailed = 0;
        }
        void run() {
            DEV {
                // in _DEBUG builds on linux we mprotect each time a writelock
                // is taken. That can greatly slow down this test if there are
                // many open files
                DBDirectClient db;
                db.simpleCommand("admin", NULL, "closeAllDatabases");
            }

            Timer t;
            cout << "MongoMutexTest N:" << N << endl;
            ThreadedTest<nthr>::run();
            cout << "MongoMutexTest " << t.millis() << "ms" << endl;
        }
    private:
        virtual void setup() {
        }
        virtual void subthread(int tnumber) {
            Client::initThread("mongomutextest");
            sleepmillis(0);
            for( int i = 0; i < N; i++ ) {
                int x = std::rand();
                bool sometimes = (x % 15 == 0);
                if( i % 7 == 0 ) {
                    Lock::GlobalRead r; // nested test
                    Lock::GlobalRead r2;
                    if( sometimes ) {
                        Lock::TempRelease t;
                    }
                }
                else if( i % 7 == 1 ) {
                    Lock::GlobalRead r;
                    ASSERT( d.dbMutex.atLeastReadLocked() );
                    ASSERT( Lock::isLocked() );
                    if( sometimes ) {
                        Lock::TempRelease t;
                    }
                }
                else if( i % 7 == 4 && 
                         tnumber == 1 /*only one upgrader legal*/ ) {
                    Lock::GlobalWrite w;
                    ASSERT( d.dbMutex.isWriteLocked() );
                    ASSERT( Lock::isW() );
                    if( i % 7 == 2 ) {
                        Lock::TempRelease t;
                    }
                    if( sometimes ) { 
                        w.downgrade();
                        sleepmillis(0);
                        bool worked = w.upgrade();
                        if( worked) upgradeWorked++;
                        else upgradeFailed++;
                    }
                }
                else if( i % 7 == 2 ) {
                    Lock::GlobalWrite w;
                    ASSERT( d.dbMutex.isWriteLocked() );
                    ASSERT( Lock::isW() );
                    if( sometimes ) {
                        Lock::TempRelease t;
                    }
                }
                else if( i % 7 == 3 ) {
                    Lock::GlobalWrite w;
                    {
                        Lock::TempRelease t;
                    }
                    Lock::GlobalRead r;
                    ASSERT( d.dbMutex.isWriteLocked() );
                    ASSERT( Lock::isW() );
                    if( sometimes ) {
                        Lock::TempRelease t;
                    }
                }
                else if( i % 7 == 5 ) {
                    {
                        Lock::DBRead r("foo");
                        if( sometimes ) {
                            Lock::TempRelease t;
                        }
                    }
                    {
                        Lock::DBRead r("bar");
                    }
                }
                else if( i % 7 == 6 ) {
                    if( i > N/2 ) { 
                        int q = i % 11;
                        if( q == 0 ) { 
                            char what = Lock::dbLevelLockingEnabled() ? 'r' : 'R';
                            Lock::DBRead r("foo");
                            ASSERT( Lock::isLocked() == what && Lock::atLeastReadLocked("foo") );
                            ASSERT( !Lock::nested() );
                            Lock::DBRead r2("foo");
                            ASSERT( Lock::nested() );
                            ASSERT( Lock::isLocked() == what && Lock::atLeastReadLocked("foo") );
                            Lock::DBRead r3("local");
                            if( sometimes ) {
                                Lock::TempRelease t;
                            }
                            ASSERT( Lock::isLocked() == what && Lock::atLeastReadLocked("foo") );
                            ASSERT( Lock::isLocked() == what && Lock::atLeastReadLocked("local") );
                        }
                        else if( q == 1 ) {
                            // test locking local only -- with no preceeding lock
                            { 
                                Lock::DBRead x("local"); 
                                //Lock::DBRead y("q");
                                if( sometimes ) {
                                    Lock::TempRelease t; // we don't temprelease (cant=true) here thus this is just a check that nothing weird happens...
                                }
                            }
                            { 
                                Lock::DBWrite x("local"); 
                                if( sometimes ) {
                                    Lock::TempRelease t;
                                }
                            }
                        } else if( q == 1 ) {
                            { Lock::DBRead  x("admin"); }
                            { Lock::DBWrite x("admin"); }
                        } else if( q == 2 ) { 
                            /*Lock::DBWrite x("foo");
                            Lock::DBWrite y("admin");
                            { Lock::TempRelease t; }*/
                        }
                        else if( q == 3 ) {
                            Lock::DBWrite x("foo");
                            Lock::DBRead y("admin");
                            { Lock::TempRelease t; }
                        } 
                        else if( q == 4 ) { 
                            Lock::DBRead x("foo2");
                            Lock::DBRead y("admin");
                            { Lock::TempRelease t; }
                        }
                        else { 
                            Lock::DBWrite w("foo");
                            {
                                Lock::TempRelease t;
                            }
                            Lock::DBRead r2("foo");
                            Lock::DBRead r3("local");
                            if( sometimes ) {
                                Lock::TempRelease t;
                            }
                        }
                    }
                    else { 
                        Lock::DBRead r("foo");
                        Lock::DBRead r2("foo");
                        Lock::DBRead r3("local");
                    }
                }
                else {
                    Lock::ThreadSpanningOp::setWLockedNongreedy();
                    Lock::ThreadSpanningOp::unsetW();
                    Lock::ThreadSpanningOp::setWLockedNongreedy();
                    Lock::ThreadSpanningOp::W_to_R();
                    Lock::ThreadSpanningOp::unsetR();
                }
                pm.hit();
            }
            cc().shutdown();
        }
        virtual void validate() {
            log() << "mongomutextest validate" << endl;
            ASSERT( !d.dbMutex.atLeastReadLocked() );
            ASSERT( upgradeWorked > upgradeFailed );
            ASSERT( upgradeWorked > 4 );
            {
                    Lock::GlobalWrite w;
            }
            {
                    Lock::GlobalRead r;
            }
        }
    };

    // 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() );
            ASSERT( d.dbMutex.isWriteLocked() );
        }
    };

    class RWLockTest1 { 
    public:
        void run() { 
            RWLock lk( "eliot" );
            {
                rwlock r( lk , true , 1000 );
            }
        }
    };

    class RWLockTest2 { 
    public:
        static void worker1( RWLockRecursiveNongreedy * lk , AtomicUInt * x ) {
            (*x)++; // 1
            RWLockRecursiveNongreedy::Exclusive b(*lk);
            (*x)++; // 2
        }
        static void worker2( RWLockRecursiveNongreedy * lk , AtomicUInt * x ) {
            RWLockRecursiveNongreedy::Shared c(*lk);
            (*x)++;
        }
        void run() { 
            /**
             * note: this test will deadlock if the code breaks
             */            
            RWLockRecursiveNongreedy lk( "eliot2" , 120 * 1000 );
            cout << "RWLock impl: " << lk.implType() << endl;
            auto_ptr<RWLockRecursiveNongreedy::Shared> a( new RWLockRecursiveNongreedy::Shared(lk) );            
            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 RWLockTest3 { 
    public:        
        static void worker2( RWLockRecursiveNongreedy * lk , AtomicUInt * x ) {
    	    assert( ! lk->__lock_try(0) );
            RWLockRecursiveNongreedy::Shared c( *lk  );
            (*x)++;
        }

        void run() { 
            /**
             * note: this test will deadlock if the code breaks
             */
            
            RWLockRecursiveNongreedy lk( "eliot2" , 120 * 1000 );
            
            auto_ptr<RWLockRecursiveNongreedy::Shared> a( new RWLockRecursiveNongreedy::Shared( lk ) );
            
            AtomicUInt x2 = 0;

            boost::thread t2( boost::bind( worker2, &lk , &x2 ) );
            t2.join();
            assert( x2 == 1 );

            a.reset();            
        }
    };

    class RWLockTest4 { 
    public:
        
#if defined(__linux__) || defined(__APPLE__)
        static void worker1( pthread_rwlock_t * lk , AtomicUInt * x ) {
            (*x)++; // 1
            cout << "lock b try" << endl;
            while ( 1 ) {
                if ( pthread_rwlock_trywrlock( lk ) == 0 )
                    break;
                sleepmillis(10);
            }
            cout << "lock b got" << endl;
            (*x)++; // 2
            pthread_rwlock_unlock( lk );
        }

        static void worker2( pthread_rwlock_t * lk , AtomicUInt * x ) {
            cout << "lock c try" << endl;
            pthread_rwlock_rdlock( lk );
            (*x)++;
            cout << "lock c got" << endl;
            pthread_rwlock_unlock( lk );
        }
#endif
        void run() { 
            /**
             * note: this test will deadlock if the code breaks
             */
      
#if defined(__linux__) || defined(__APPLE__)      
            
            // create
            pthread_rwlock_t lk;
            assert( pthread_rwlock_init( &lk , 0 ) == 0 );
            
            // read lock
            assert( pthread_rwlock_rdlock( &lk ) == 0 );
            
            AtomicUInt x1 = 0;
            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 );

            pthread_rwlock_unlock( &lk );

            for ( int i=0; i<2000; i++ ) {
                if ( x1 == 2 )
                    break;
                sleepmillis(1);
            }

            assert( x1 == 2 );
            t1.join();
#endif            
        }
    };

    class List1Test2 : public ThreadedTest<> {
        static const int iterations = 1000; // note: a lot of iterations will use a lot of memory as List1 leaks on purpose
        class M : public List1<M>::Base {
        public:
            M(int x) : _x(x) { }
            const int _x;
        };
        List1<M> l;
    public:
        void validate() { }
        void subthread(int) {
            for(int i=0; i < iterations; i++) {
                int r = std::rand() % 256;
                if( r == 0 ) {
                    l.orphanAll();
                }
                else if( r < 4 ) { 
                    l.push(new M(r));
                }
                else {
                    M *orph = 0;
                    for( M *m = l.head(); m; m=m->next() ) { 
                        ASSERT( m->_x > 0 && m->_x < 4 );
                        if( r > 192 && std::rand() % 8 == 0 )
                            orph = m;
                    }
                    if( orph ) {
                        try { 
                            l.orphan(orph);
                        }
                        catch(...) { }
                    }
                }
            }
        }
    };

    class List1Test {
    public:
        class M : public List1<M>::Base {
            ~M();
        public:
            M( int x ) {
                num = x;
            }
            int num;
        };

        void run(){
            List1<M> l;
            
            vector<M*> ms;
            for ( int i=0; i<5; i++ ) {
                M * m = new M(i);
                ms.push_back( m );
                l.push( m );
            }
            
            // must assert as the item is missing
            ASSERT_THROWS( l.orphan( new M( -3 ) ) , UserException );
        }
    };

    // we don't use upgrade so that part is not important currently but the other aspects of this test are 
    // interesting; it would be nice to do analogous tests for SimpleRWLock and QLock
    class UpgradableTest : public ThreadedTest<7> {
        RWLock m;
    public:
        UpgradableTest() : m("utest") {}
    private:
        virtual void validate() { }
        virtual void subthread(int x) {
            Client::initThread("utest");

            /* r = get a read lock 
               R = get a read lock and we expect it to be fast
               u = get upgradable 
               U = get upgradable and we expect it to be fast
               w = get a write lock
            */
            //                    /-- 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 (or are they?)
            //                    v  v v
            const char *what = " RURuRwR";

            sleepmillis(100*x);

            int Z = 1;
            log(Z) << x << ' ' << what[x] << " request" << endl;
            char ch = what[x];
            switch( ch ) { 
            case 'w':
                {
                    m.lock();
                    log(Z) << x << " w got" << endl;
                    sleepmillis(100);
                    log(Z) << x << " w unlock" << endl;
                    m.unlock();
                }
                break;
            case 'u':
            case 'U':
                {
                    Timer t;
                    RWLock::Upgradable u(m);
                    log(Z) << x << ' ' << ch << " got" << endl;
                    if( ch == 'U' ) {
#ifdef MONGO_USE_SRW_ON_WINDOWS
                        // SRW locks are neither fair nor FIFO, as per docs
                        if( t.millis() > 2000 ) {
#else
                        if( t.millis() > 20 ) {
#endif
                            DEV {
                                // a _DEBUG buildbot might be slow, try to avoid false positives
                                log() << "warning lock upgrade was slow " << t.millis() << endl;
                            }
                            else {
                                log() << "assertion failure: lock upgrade was too slow: " << t.millis() << endl;
                                ASSERT( false );
                            }
                        }
                    }
                    sleepsecs(1);
                    log(Z) << x << ' ' << ch << " unlock" << endl;
                }
                break;
            case 'r':
            case 'R':
                {
                    Timer t;
                    m.lock_shared();
                    log(Z) << x << ' ' << ch << " got " << endl;
                    if( what[x] == 'R' ) {
                        if( t.millis() > 15 ) { 
                            // commented out for less chatter, we aren't using upgradeable anyway right now: 
                            // log() << x << " info: when in upgradable, write locks are still greedy on this platform" << endl;
                        }
                    }
                    sleepmillis(200);
                    log(Z) << x << ' ' << ch << " unlock" << endl;
                    m.unlock_shared();
                }
                break;
            default:
                ASSERT(false);
            }

            cc().shutdown();
        }
    };

    void sleepalittle() { 
        Timer t;
        while( 1 ) { 
            boost::this_thread::yield();
            if( t.micros() > 8 )
                break;
        }
    }

    int once;

    /* This test is to see how long it takes to get a lock after there has been contention -- the OS 
         will need to reschedule us. if a spinlock, it will be fast of course, but these aren't spin locks.
       Experimenting with different # of threads would be a good idea.
    */
    template <class whichmutex, class scoped>
    class Slack : public ThreadedTest<17> {
    public:
        Slack() : m("slack") {
            k = 0;
            done = false;
            a = b = 0;
            locks = 0;
        }
    private:
        whichmutex m;
        char pad1[128];
        unsigned a, b;
        char pad2[128];
        unsigned locks;
        char pad3[128];
        volatile int k;

        virtual void validate() { 
            if( once++ == 0 ) {
                // <= 1.35 we use a different rwmutex impl so worth noting
                cout << "Boost version : " << BOOST_VERSION << endl;
            }
            cout << typeid(whichmutex).name() <<
             " Slack useful work fraction: " << ((double)a)/b << " locks:" << locks << endl;
        }
        void watch() {
            while( 1 ) { 
                b++;
                //__sync_synchronize();
                if( k ) { 
                    a++;
                }
                sleepmillis(0);
                if( done ) 
                    break;
            }
        }
        volatile bool done;
        virtual void subthread(int x) {
            if( x == 1 ) { 
                watch();
                return;
            }
            Timer t;
            unsigned lks = 0;
            while( 1 ) {
                scoped lk(m);
                k = 1;
                // not very long, we'd like to simulate about 100K locks per second
                sleepalittle();
                lks++;
                if( done ||  t.millis() > 1500 ) {
                    locks += lks;
                    k = 0;
                    break;
                }
                k = 0;
                //__sync_synchronize();
            }
            done = true;
        }
    };

    class CondSlack : public ThreadedTest<17> {
        Notification n;
    public:
        CondSlack() {
            k = 0;
            done = false;
            a = b = 0;
            locks = 0;
        }
    private:
        unsigned a, b;
        virtual void validate() { 
            cout << "CondSlack useful work fraction: " << ((double)a)/b << " locks:" << locks << endl;
        }
        unsigned locks;
        volatile int k;
        void watch() {
            while( 1 ) { 
                b++;
                if( k ) { 
                    a++;
                }
                sleepmillis(0);
                if( done ) 
                    break;
            }
        }
        volatile bool done;
        virtual void subthread(int x) {
            if( x == 1 ) { 
                n.notifyOne();
                watch();
                return;
            }
            Timer t;
            while( 1 ) {
                n.waitToBeNotified();
                assert( k == 0 );
                k = 1;
                // not very long, we'd like to simulate about 100K locks per second
                sleepalittle();
                k = 0; 
                locks++;
                n.notifyOne();
                if( done ||  t.millis() > 1500 )
                    break;
            }
            done = true;
        }
    };

    class WriteLocksAreGreedy : public ThreadedTest<3> {
    public:
        WriteLocksAreGreedy() : m("gtest") {}
    private:
        RWLock m;
        virtual void validate() { }
        virtual void subthread(int x) {
            int Z = 1;
            Client::initThread("utest");
            if( x == 1 ) { 
                cout << mongo::curTimeMillis64() % 10000 << " 1" << endl;
                rwlock_shared lk(m);
                sleepmillis(300);
                log(Z) << mongo::curTimeMillis64() % 10000 << " 1x" << endl;
            }
            if( x == 2 ) {
                sleepmillis(100);
                log(Z) << mongo::curTimeMillis64() % 10000 << " 2" << endl;
                rwlock lk(m, true);
                log(Z) << mongo::curTimeMillis64() % 10000 << " 2x" << endl;
            }
            if( x == 3 ) {
                sleepmillis(200);
                Timer t;
                log(Z) << mongo::curTimeMillis64() % 10000 << " 3" << endl;
                rwlock_shared lk(m);
                log(Z) << mongo::curTimeMillis64() % 10000 << " 3x" << endl;
                log(Z) << t.millis() << endl;
                ASSERT( t.millis() > 50 );
            }
            cc().shutdown();
        }
    };

    static int pass;
    class QLockTest : public ThreadedTest<3> {
    public:
        bool gotW;
        QLockTest() : gotW(false), m() { }
        void setup() { 
            if( pass == 1) { 
                m.stop_greed();
            }
        }
        ~QLockTest() {
            m.start_greed();
        }
    private:
        QLock m;
        virtual void validate() { }
        virtual void subthread(int x) {
            int Z = 1;
            Client::initThread("qtest");
            if( x == 1 ) { 
                log(Z) << mongo::curTimeMillis64() % 10000 << " 1 lock_r()..." << endl;
                m.lock_r();
                log(Z) << mongo::curTimeMillis64() % 10000 << " 1            got" << endl;
                sleepmillis(300);
                m.unlock_r();
                log(Z) << mongo::curTimeMillis64() % 10000 << " 1 unlock_r()" << endl;
            }
            if( x == 2 || x == 4 ) {
                sleepmillis(x*50);
                log(Z) << mongo::curTimeMillis64() % 10000 << " 2 lock_W()..." << endl;
                m.lock_W();
                log(Z) << mongo::curTimeMillis64() % 10000 << " 2            got" << endl;
                gotW = true;
                m.unlock_W();
            }
            if( x == 3 ) {
                sleepmillis(200);

                Timer t;
                log(Z) << mongo::curTimeMillis64() % 10000 << " 3 lock_r()..." << endl;
                m.lock_r();
                assert( gotW );
                log(Z) << mongo::curTimeMillis64() % 10000 << " 3            got" << gotW << endl;
                log(Z) << t.millis() << endl;
                m.unlock_r();
                ASSERT( t.millis() > 50 );
            }
            cc().shutdown();
        }
    };

    // Tests waiting on the TicketHolder by running many more threads than can fit into the "hotel", but only
    // max _nRooms threads should ever get in at once
    class TicketHolderWaits : public ThreadedTest<10> {

        static const int checkIns = 1000;
        static const int rooms = 3;

    public:
        TicketHolderWaits() : _hotel( rooms ), _tickets( _hotel._nRooms ) {}

    private:

        class Hotel {
        public:
            Hotel( int nRooms ) : _frontDesk( "frontDesk" ), _nRooms( nRooms ), _checkedIn( 0 ), _maxRooms( 0 ) {}

            void checkIn(){
                scoped_lock lk( _frontDesk );
                _checkedIn++;
                assert( _checkedIn <= _nRooms );
                if( _checkedIn > _maxRooms ) _maxRooms = _checkedIn;
            }

            void checkOut(){
                scoped_lock lk( _frontDesk );
                _checkedIn--;
                assert( _checkedIn >= 0 );
            }

            mongo::mutex _frontDesk;
            int _nRooms;
            int _checkedIn;
            int _maxRooms;
        };

        Hotel _hotel;
        TicketHolder _tickets;

        virtual void subthread(int x) {

            string threadName = ( str::stream() << "ticketHolder" << x );
            Client::initThread( threadName.c_str() );

            for( int i = 0; i < checkIns; i++ ){

                _tickets.waitForTicket();
                TicketHolderReleaser whenDone( &_tickets );

                _hotel.checkIn();

                sleepalittle();
                if( i == checkIns - 1 ) sleepsecs( 2 );

                _hotel.checkOut();

                if( ( i % ( checkIns / 10 ) ) == 0 )
                    log() << "checked in " << i << " times..." << endl;

            }

            cc().shutdown();

        }

        virtual void validate() {

            // This should always be true, assuming that it takes < 1 sec for the hardware to process a check-out/check-in
            // Time for test is then ~ #threads / _nRooms * 2 seconds
            assert( _hotel._maxRooms == _hotel._nRooms );

        }

    };

    class All : public Suite {
    public:
        All() : Suite( "threading" ) { }

        void setupTests() {
            add< WriteLocksAreGreedy >();
            add< QLockTest >();
            add< QLockTest >();

            // Slack is a test to see how long it takes for another thread to pick up
            // and begin work after another relinquishes the lock.  e.g. a spin lock 
            // would have very little slack.
            add< Slack<mongo::mutex , mongo::mutex::scoped_lock > >();
            add< Slack<SimpleMutex,SimpleMutex::scoped_lock> >();
            add< Slack<SimpleRWLock,SimpleRWLock::Exclusive> >();
            add< CondSlack >();

            add< UpgradableTest >();
            add< List1Test >();
            add< List1Test2 >();

            add< IsAtomicUIntAtomic >();
            add< MVarTest >();
            add< ThreadPoolTest >();
            add< LockTest >();


            add< RWLockTest1 >();
            //add< RWLockTest2 >(); // SERVER-2996
            add< RWLockTest3 >();
            add< RWLockTest4 >();

            add< MongoMutexTest >();
            add< TicketHolderWaits >();
        }
    } myall;
}