File: race.h

package info (click to toggle)
mongodb 1%3A2.0.6-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 17,204 kB
  • sloc: cpp: 109,783; ansic: 101,073; python: 2,287; perl: 395; makefile: 370; sh: 242; asm: 46
file content (115 lines) | stat: -rw-r--r-- 2,675 bytes parent folder | download
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
#pragma once

#include "../goodies.h" // printStackTrace

namespace mongo {

    /** some self-testing of synchronization and attempts to catch race conditions.

        use something like:

        CodeBlock myBlock;

        void foo() { 
            CodeBlock::Within w(myBlock);
            ...
        }

        In _DEBUG builds, will (sometimes/maybe) fail if two threads are in the same code block at 
        the same time. Also detects and disallows recursion.
    */

#ifdef _WIN32
    typedef unsigned threadId_t;
#else
    typedef pthread_t threadId_t;
#endif


#if defined(_DEBUG)

    namespace race {

        class CodePoint { 
        public:
            string lastName;
            threadId_t lastTid;
            string file;
            CodePoint(string f) : lastTid(0), file(f) { }
        };
        class Check {
        public:
            Check(CodePoint& p) {
                threadId_t t = GetCurrentThreadId();
                if( p.lastTid == 0 ) {
                    p.lastTid = t;
                    p.lastName = getThreadName();
                }
                else if( t != p.lastTid ) { 
                    log() << "\n\n\n\n\nRACE? error assert\n  " << p.file << '\n' 
                        << "  " << p.lastName
                        << "  " << getThreadName() << "\n\n" << endl;
                    mongoAbort("racecheck");
                }
            };
        };

    }

#define RACECHECK
        // dm TODO - the right code for this file is in a different branch at the moment (merge)
        //#define RACECHECK 
        //static race::CodePoint __cp(__FILE__); 
        //race::Check __ck(__cp);

    class CodeBlock { 
        volatile int n;
        threadId_t tid;
        void fail() { 
            log() << "synchronization (race condition) failure" << endl;
            printStackTrace();
            ::abort();
        }
        void enter() { 
            if( ++n != 1 ) fail();
#if defined(_WIN32)
            tid = GetCurrentThreadId();
#endif
        }
        void leave() {
            if( --n != 0 ) fail();
        }
    public:
        CodeBlock() : n(0) { }

        class Within { 
            CodeBlock& _s;
        public:
            Within(CodeBlock& s) : _s(s) { _s.enter(); }
            ~Within() { _s.leave(); }
        };

        void assertWithin() {
            assert( n == 1 );
#if defined(_WIN32)
            assert( GetCurrentThreadId() == tid );
#endif
        }
    };
    
#else

#define RACECHECK

    class CodeBlock{ 
    public:
        class Within { 
        public:
            Within(CodeBlock&) { }
        };
        void assertWithin() { }
    };

#endif

} // namespace