1254721Semaste//===-- ThreadSafeValue.h ---------------------------------------*- C++ -*-===// 2254721Semaste// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6254721Semaste// 7254721Semaste//===----------------------------------------------------------------------===// 8254721Semaste 9254721Semaste#ifndef liblldb_ThreadSafeValue_h_ 10254721Semaste#define liblldb_ThreadSafeValue_h_ 11254721Semaste 12309124Sdim 13309124Sdim#include <mutex> 14309124Sdim 15341825Sdim#include "lldb/lldb-defines.h" 16254721Semaste 17254721Semastenamespace lldb_private { 18254721Semaste 19314564Sdimtemplate <class T> class ThreadSafeValue { 20254721Semastepublic: 21314564Sdim // Constructors and Destructors 22314564Sdim ThreadSafeValue() : m_value(), m_mutex() {} 23254721Semaste 24314564Sdim ThreadSafeValue(const T &value) : m_value(value), m_mutex() {} 25254721Semaste 26314564Sdim ~ThreadSafeValue() {} 27254721Semaste 28314564Sdim T GetValue() const { 29314564Sdim T value; 30254721Semaste { 31314564Sdim std::lock_guard<std::recursive_mutex> guard(m_mutex); 32314564Sdim value = m_value; 33254721Semaste } 34314564Sdim return value; 35314564Sdim } 36254721Semaste 37314564Sdim // Call this if you have already manually locked the mutex using the 38314564Sdim // GetMutex() accessor 39314564Sdim const T &GetValueNoLock() const { return m_value; } 40254721Semaste 41314564Sdim void SetValue(const T &value) { 42314564Sdim std::lock_guard<std::recursive_mutex> guard(m_mutex); 43314564Sdim m_value = value; 44314564Sdim } 45254721Semaste 46314564Sdim // Call this if you have already manually locked the mutex using the 47314564Sdim // GetMutex() accessor 48314564Sdim void SetValueNoLock(const T &value) { m_value = value; } 49254721Semaste 50314564Sdim std::recursive_mutex &GetMutex() { return m_mutex; } 51254721Semaste 52254721Semasteprivate: 53314564Sdim T m_value; 54314564Sdim mutable std::recursive_mutex m_mutex; 55254721Semaste 56314564Sdim // For ThreadSafeValue only 57314564Sdim DISALLOW_COPY_AND_ASSIGN(ThreadSafeValue); 58254721Semaste}; 59254721Semaste 60254721Semaste} // namespace lldb_private 61314564Sdim#endif // liblldb_ThreadSafeValue_h_ 62