From 0f7d9ec131e7c01ee54da47dca8d6d23aa752f34 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 13 Aug 2013 09:42:55 -0700 Subject: [PATCH] Some minor refactoring of TU store --- .../ClangCompleter/TranslationUnitStore.cpp | 19 +++++++++++-------- cpp/ycm/ClangCompleter/TranslationUnitStore.h | 5 +++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/cpp/ycm/ClangCompleter/TranslationUnitStore.cpp b/cpp/ycm/ClangCompleter/TranslationUnitStore.cpp index ec62a492..600fc229 100644 --- a/cpp/ycm/ClangCompleter/TranslationUnitStore.cpp +++ b/cpp/ycm/ClangCompleter/TranslationUnitStore.cpp @@ -54,12 +54,10 @@ shared_ptr< TranslationUnit > TranslationUnitStore::GetOrCreate( translation_unit_created = false; { lock_guard< mutex > lock( filename_to_translation_unit_mutex_ ); - TranslationUnitForFilename::iterator it = - filename_to_translation_unit_.find( filename ); + shared_ptr< TranslationUnit > current_unit = GetNoLock( filename ); - if ( it != filename_to_translation_unit_.end() ) { - return it->second; - } + if ( current_unit ) + return current_unit; // We create and store an invalid, sentinel TU so that other threads don't // try to create a TU for the same file while we are trying to create this @@ -93,9 +91,7 @@ shared_ptr< TranslationUnit > TranslationUnitStore::GetOrCreate( shared_ptr< TranslationUnit > TranslationUnitStore::Get( const std::string &filename ) { lock_guard< mutex > lock( filename_to_translation_unit_mutex_ ); - return FindWithDefault( filename_to_translation_unit_, - filename, - shared_ptr< TranslationUnit >() ); + return GetNoLock( filename ); } bool TranslationUnitStore::Remove( const std::string &filename ) { @@ -108,4 +104,11 @@ void TranslationUnitStore::RemoveAll() { filename_to_translation_unit_.clear(); } +shared_ptr< TranslationUnit > TranslationUnitStore::GetNoLock( + const std::string &filename ) { + return FindWithDefault( filename_to_translation_unit_, + filename, + shared_ptr< TranslationUnit >() ); +} + } // namespace YouCompleteMe diff --git a/cpp/ycm/ClangCompleter/TranslationUnitStore.h b/cpp/ycm/ClangCompleter/TranslationUnitStore.h index b42a94f7..9c0d3000 100644 --- a/cpp/ycm/ClangCompleter/TranslationUnitStore.h +++ b/cpp/ycm/ClangCompleter/TranslationUnitStore.h @@ -56,6 +56,11 @@ public: void RemoveAll(); private: + + // WARNING: This does accesses filename_to_translation_unit_ without a lock! + boost::shared_ptr< TranslationUnit > GetNoLock( const std::string &filename ); + + typedef boost::unordered_map< std::string, boost::shared_ptr< TranslationUnit > > TranslationUnitForFilename;