diff options
Diffstat (limited to 'util/concurrency')
-rw-r--r-- | util/concurrency/spin_lock.h | 10 | ||||
-rw-r--r-- | util/concurrency/value.h | 35 | ||||
-rw-r--r-- | util/concurrency/vars.cpp | 2 |
3 files changed, 31 insertions, 16 deletions
diff --git a/util/concurrency/spin_lock.h b/util/concurrency/spin_lock.h index d5360f7..02a8797 100644 --- a/util/concurrency/spin_lock.h +++ b/util/concurrency/spin_lock.h @@ -49,6 +49,16 @@ namespace mongo { SpinLock(SpinLock&); SpinLock& operator=(SpinLock&); }; + + struct scoped_spinlock { + scoped_spinlock( SpinLock& l ) : _l(l){ + _l.lock(); + } + ~scoped_spinlock() { + _l.unlock(); + } + SpinLock& _l; + }; } // namespace mongo diff --git a/util/concurrency/value.h b/util/concurrency/value.h index 08d5306..0a0ef85 100644 --- a/util/concurrency/value.h +++ b/util/concurrency/value.h @@ -60,28 +60,31 @@ namespace mongo { }; }; - /** 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; + string _s; + static mutex m; public: - DiagStr() { - memset(buf1, 0, 256); - memset(buf2, 0, 256); - p = buf1; + DiagStr(const DiagStr& r) : _s(r.get()) { } + DiagStr() { } + bool empty() const { + mutex::scoped_lock lk(m); + return _s.empty(); + } + string get() const { + mutex::scoped_lock lk(m); + return _s; } - - const char * get() const { return p; } void set(const char *s) { - char *q = (p==buf1) ? buf2 : buf1; - strncpy(q, s, 255); - p = q; + mutex::scoped_lock lk(m); + _s = s; + } + void set(const string& s) { + mutex::scoped_lock lk(m); + _s = s; } + operator string() const { return get(); } + void operator=(const string& s) { set(s); } }; } diff --git a/util/concurrency/vars.cpp b/util/concurrency/vars.cpp index 3d057a4..19b58eb 100644 --- a/util/concurrency/vars.cpp +++ b/util/concurrency/vars.cpp @@ -22,6 +22,8 @@ namespace mongo { + mutex DiagStr::m("diags"); + mongo::mutex _atomicMutex("_atomicMutex"); // intentional leak. otherwise destructor orders can be problematic at termination. |