|
MongoDB
2.1.1-pre-
|
00001 /* @file value.h 00002 concurrency helpers DiagStr, Guarded 00003 */ 00004 00021 #pragma once 00022 00023 #include "spin_lock.h" 00024 00025 namespace mongo { 00026 00038 template <typename T, SimpleMutex& BY> 00039 class Guarded { 00040 T _val; 00041 public: 00042 T& ref(const SimpleMutex::scoped_lock& lk) { 00043 dassert( &lk.m() == &BY ); 00044 return _val; 00045 } 00046 }; 00047 00048 // todo: rename this to ThreadSafeString or something 00052 class DiagStr { 00053 mutable SpinLock m; 00054 string _s; 00055 public: 00056 DiagStr(const DiagStr& r) : _s(r.get()) { } 00057 DiagStr(const string& r) : _s(r) { } 00058 DiagStr() { } 00059 bool empty() const { 00060 scoped_spinlock lk(m); 00061 return _s.empty(); 00062 } 00063 string get() const { 00064 scoped_spinlock lk(m); 00065 return _s; 00066 } 00067 void set(const char *s) { 00068 scoped_spinlock lk(m); 00069 _s = s; 00070 } 00071 void set(const string& s) { 00072 scoped_spinlock lk(m); 00073 _s = s; 00074 } 00075 operator string() const { return get(); } 00076 void operator=(const string& s) { set(s); } 00077 void operator=(const DiagStr& rhs) { 00078 set( rhs.get() ); 00079 } 00080 00081 // == is not defined. use get() == ... instead. done this way so one thinks about if composing multiple operations 00082 bool operator==(const string& s) const; 00083 }; 00084 00085 }
1.8.0