1//===-- ThreadSafeValue.h ---------------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef liblldb_ThreadSafeValue_h_ 11#define liblldb_ThreadSafeValue_h_ 12 13// C Includes 14// C++ Includes 15// Other libraries and framework includes 16// Project includes 17#include "lldb/Host/Mutex.h" 18 19namespace lldb_private { 20 21template <class T> 22class ThreadSafeValue 23{ 24public: 25 //------------------------------------------------------------------ 26 // Constructors and Destructors 27 //------------------------------------------------------------------ 28 ThreadSafeValue() : 29 m_value (), 30 m_mutex (Mutex::eMutexTypeRecursive) 31 { 32 } 33 34 ThreadSafeValue(const T& value) : 35 m_value (value), 36 m_mutex (Mutex::eMutexTypeRecursive) 37 { 38 } 39 40 ~ThreadSafeValue() 41 { 42 } 43 44 T 45 GetValue () const 46 { 47 T value; 48 { 49 Mutex::Locker locker(m_mutex); 50 value = m_value; 51 } 52 return value; 53 } 54 55 // Call this if you have already manually locked the mutex using the 56 // GetMutex() accessor 57 const T& 58 GetValueNoLock () const 59 { 60 return m_value; 61 } 62 63 void 64 SetValue (const T& value) 65 { 66 Mutex::Locker locker(m_mutex); 67 m_value = value; 68 } 69 70 // Call this if you have already manually locked the mutex using the 71 // GetMutex() accessor 72 void 73 SetValueNoLock (const T& value) 74 { 75 m_value = value; 76 } 77 78 Mutex & 79 GetMutex () 80 { 81 return m_mutex; 82 } 83 84private: 85 T m_value; 86 mutable Mutex m_mutex; 87 88 //------------------------------------------------------------------ 89 // For ThreadSafeValue only 90 //------------------------------------------------------------------ 91 DISALLOW_COPY_AND_ASSIGN (ThreadSafeValue); 92}; 93 94 95} // namespace lldb_private 96#endif // liblldb_ThreadSafeValue_h_ 97