Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===-- ThreadSafeDenseMap.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_UTILITY_THREADSAFEDENSEMAP_H
0010 #define LLDB_UTILITY_THREADSAFEDENSEMAP_H
0011 
0012 #include <mutex>
0013 
0014 #include "llvm/ADT/DenseMap.h"
0015 
0016 namespace lldb_private {
0017 
0018 template <typename _KeyType, typename _ValueType> class ThreadSafeDenseMap {
0019 public:
0020   typedef llvm::DenseMap<_KeyType, _ValueType> LLVMMapType;
0021 
0022   ThreadSafeDenseMap(unsigned map_initial_capacity = 0)
0023       : m_map(map_initial_capacity), m_mutex() {}
0024 
0025   void Insert(_KeyType k, _ValueType v) {
0026     std::lock_guard<std::mutex> guard(m_mutex);
0027     m_map.insert(std::make_pair(k, v));
0028   }
0029 
0030   void Erase(_KeyType k) {
0031     std::lock_guard<std::mutex> guard(m_mutex);
0032     m_map.erase(k);
0033   }
0034 
0035   _ValueType Lookup(_KeyType k) {
0036     std::lock_guard<std::mutex> guard(m_mutex);
0037     return m_map.lookup(k);
0038   }
0039 
0040   bool Lookup(_KeyType k, _ValueType &v) {
0041     std::lock_guard<std::mutex> guard(m_mutex);
0042     auto iter = m_map.find(k), end = m_map.end();
0043     if (iter == end)
0044       return false;
0045     v = iter->second;
0046     return true;
0047   }
0048 
0049   void Clear() {
0050     std::lock_guard<std::mutex> guard(m_mutex);
0051     m_map.clear();
0052   }
0053 
0054 protected:
0055   LLVMMapType m_map;
0056   std::mutex m_mutex;
0057 };
0058 
0059 } // namespace lldb_private
0060 
0061 #endif // LLDB_UTILITY_THREADSAFEDENSEMAP_H