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
|
/* @file value.h
concurrency helpers DiagStr, Guarded
*/
/**
* 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
#include "mutex.h"
#include "spin_lock.h"
namespace mongo {
/** declare that a variable that is "guarded" by a mutex.
The decl documents the rule. For example "counta and countb are guarded by xyzMutex":
Guarded<int, xyzMutex> counta;
Guarded<int, xyzMutex> countb;
Upon use, specify the scoped_lock object. This makes it hard for someone
later to forget to be in the lock. Check is made that it is the right lock in _DEBUG
builds at runtime.
*/
template <typename T, mutex& BY>
class Guarded : boost::noncopyable {
T _val;
public:
T& ref(const scoped_lock& lk) {
dassert( lk._mut == &BY );
return _val;
}
};
class DiagStr {
string _s;
mutable SpinLock m;
public:
DiagStr(const DiagStr& r) : _s(r.get()) { }
DiagStr() { }
bool empty() const {
scoped_spinlock lk(m);
return _s.empty();
}
string get() const {
scoped_spinlock lk(m);
return _s;
}
void set(const char *s) {
scoped_spinlock lk(m);
_s = s;
}
void set(const string& s) {
scoped_spinlock lk(m);
_s = s;
}
operator string() const { return get(); }
void operator=(const string& s) { set(s); }
void operator=(const DiagStr& rhs) {
scoped_spinlock lk(m);
_s = rhs.get();
}
};
#if 0 // not including in 2.0
/** Thread safe map.
Be careful not to use this too much or it could make things slow;
if not a hot code path no problem.
Examples:
mapsf<int,int> mp;
int x = mp.get();
map<int,int> two;
mp.swap(two);
{
mapsf<int,int>::ref r(mp);
r[9] = 1;
map<int,int>::iterator i = r.r.begin();
}
*/
template< class K, class V >
struct mapsf : boost::noncopyable {
SimpleMutex m;
map<K,V> val;
friend struct ref;
public:
mapsf() : m("mapsf") { }
void swap(map<K,V>& rhs) {
SimpleMutex::scoped_lock lk(m);
val.swap(rhs);
}
// safe as we pass by value:
V get(K k) {
SimpleMutex::scoped_lock lk(m);
map<K,V>::iterator i = val.find(k);
if( i == val.end() )
return K();
return i->second;
}
// think about deadlocks when using ref. the other methods
// above will always be safe as they are "leaf" operations.
struct ref {
SimpleMutex::scoped_lock lk;
public:
map<K,V> const &r;
ref(mapsf<K,V> &m) : lk(m.m), r(m.val) { }
V& operator[](const K& k) { return r[k]; }
};
};
#endif
}
|