Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:46

0001 //===-- ThreadSafeValue.h ---------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_CORE_THREADSAFEVALUE_H
0010 #define LLDB_CORE_THREADSAFEVALUE_H
0011 
0012 #include <mutex>
0013 
0014 #include "lldb/lldb-defines.h"
0015 
0016 namespace lldb_private {
0017 
0018 template <class T> class ThreadSafeValue {
0019 public:
0020   ThreadSafeValue() = default;
0021   ThreadSafeValue(const T &value) : m_value(value) {}
0022 
0023   ~ThreadSafeValue() = default;
0024 
0025   T GetValue() const {
0026     T value;
0027     {
0028       std::lock_guard<std::recursive_mutex> guard(m_mutex);
0029       value = m_value;
0030     }
0031     return value;
0032   }
0033 
0034   // Call this if you have already manually locked the mutex using the
0035   // GetMutex() accessor
0036   const T &GetValueNoLock() const { return m_value; }
0037 
0038   void SetValue(const T &value) {
0039     std::lock_guard<std::recursive_mutex> guard(m_mutex);
0040     m_value = value;
0041   }
0042 
0043   // Call this if you have already manually locked the mutex using the
0044   // GetMutex() accessor
0045   // coverity[missing_lock]
0046   void SetValueNoLock(const T &value) { m_value = value; }
0047 
0048   std::recursive_mutex &GetMutex() { return m_mutex; }
0049 
0050 private:
0051   T m_value;
0052   mutable std::recursive_mutex m_mutex;
0053 
0054   // For ThreadSafeValue only
0055   ThreadSafeValue(const ThreadSafeValue &) = delete;
0056   const ThreadSafeValue &operator=(const ThreadSafeValue &) = delete;
0057 };
0058 
0059 } // namespace lldb_private
0060 #endif // LLDB_CORE_THREADSAFEVALUE_H