summaryrefslogtreecommitdiff
path: root/util/concurrency/value.h
blob: dabeb956e430b37a2b6043a5c8bf1903b47914ba (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
/* @file value.h
   concurrency helpers Atomic<T> and DiagStr
*/

/**
*    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,b
*    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

namespace mongo { 

    extern mutex _atomicMutex;

    /** atomic wrapper for a value.  enters a mutex on each access.  must 
        be copyable.
    */
    template<typename T>
    class Atomic : boost::noncopyable {
        T val;
    public:
        Atomic<T>() { }

        void operator=(const T& a) { 
            scoped_lock lk(_atomicMutex);
            val = a; }

        operator T() const { 
            scoped_lock lk(_atomicMutex);
            return val; }
        
        /** example:
              Atomic<int> q;
              ...
              {
                Atomic<int>::tran t(q);
                if( q.ref() > 0 ) 
                    q.ref()--;
              }
        */
        class tran : private scoped_lock {
            Atomic<T>& _a;
        public:
            tran(Atomic<T>& a) : scoped_lock(_atomicMutex), _a(a) { }
            T& ref() { return _a.val; }
        };
    };

    /** this string COULD be mangled but with the double buffering, assuming writes 
    are infrequent, it's unlikely.  thus, this is reasonable for lockless setting of 
    diagnostic strings, where their content isn't critical.
    */
    class DiagStr { 
        char buf1[256];
        char buf2[256];
        char *p;
    public:
        DiagStr() {
            memset(buf1, 0, 256);
            memset(buf2, 0, 256);
            p = buf1;
        }

        const char * get() const { return p; }

        void set(const char *s) {
            char *q = (p==buf1) ? buf2 : buf1;
            strncpy(q, s, 255);
            p = q;
        }
    };

}