summaryrefslogtreecommitdiff
path: root/src/mongo/db/db.h
blob: 7d2928c25ba498add5edf203e58caf53d8661ae9 (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
/**
*    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/>.
*/

#pragma once

#include "../pch.h"
#include "../util/net/message.h"
#include "mongomutex.h"
#include "pdfile.h"
#include "curop.h"
#include "client.h"
#include "databaseholder.h"

namespace mongo {

    // todo: relocked is being called when there was no unlock below. 
    //       that is weird.

    struct dbtemprelease {
        Client::Context * _context;
        scoped_ptr<Lock::TempRelease> tr;
        dbtemprelease() {
            const Client& c = cc();
            _context = c.getContext();
            verify( Lock::isLocked() );
            if( Lock::nested() ) {
                Lock::nested();
                massert(10298 , "can't temprelease nested lock", false);
            }
            if ( _context ) {
                _context->unlocked();
            }
            tr.reset(new Lock::TempRelease);
            verify( c.curop() );
            c.curop()->yielded();
        }
        ~dbtemprelease() {
            tr.reset();
            if ( _context ) 
                _context->relocked();
        }
    };

    /** must be write locked
        no verify(and no release) if nested write lock 
        a lot like dbtempreleasecond, eliminate?
    */
    struct dbtempreleasewritelock {
        Client::Context * _context;
        int _locktype;
        scoped_ptr<Lock::TempRelease> tr;
        dbtempreleasewritelock() {
            const Client& c = cc();
            _context = c.getContext();
            verify( Lock::isW() );
            if( Lock::nested() )
                return;
            if ( _context ) 
                _context->unlocked();
            tr.reset(new Lock::TempRelease);
            verify( c.curop() );
            c.curop()->yielded();            
        }
        ~dbtempreleasewritelock() {
            tr.reset();
            if ( _context ) 
                _context->relocked();
        }
    };

    /**
       only does a temp release if we're not nested and have a lock
     */
    class dbtempreleasecond : boost::noncopyable {
        dbtemprelease * real;
    public:
        dbtempreleasecond() {
            real = 0;
            if( Lock::isLocked() ) {
                // if nested don't temprelease, and we don't complain either for this class
                if( !Lock::nested() ) {
                    real = new dbtemprelease();
                }
            }
        }
        ~dbtempreleasecond() {
            if ( real ) {
                delete real;
                real = 0;
            }
        }
        bool unlocked() const { return real != 0; }
    };

} // namespace mongo