Some minor refactoring of TU store

This commit is contained in:
Strahinja Val Markovic 2013-08-13 09:42:55 -07:00
parent a215f933a9
commit 0f7d9ec131
2 changed files with 16 additions and 8 deletions

View File

@ -54,12 +54,10 @@ shared_ptr< TranslationUnit > TranslationUnitStore::GetOrCreate(
translation_unit_created = false; translation_unit_created = false;
{ {
lock_guard< mutex > lock( filename_to_translation_unit_mutex_ ); lock_guard< mutex > lock( filename_to_translation_unit_mutex_ );
TranslationUnitForFilename::iterator it = shared_ptr< TranslationUnit > current_unit = GetNoLock( filename );
filename_to_translation_unit_.find( filename );
if ( it != filename_to_translation_unit_.end() ) { if ( current_unit )
return it->second; return current_unit;
}
// We create and store an invalid, sentinel TU so that other threads don't // 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 // 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( shared_ptr< TranslationUnit > TranslationUnitStore::Get(
const std::string &filename ) { const std::string &filename ) {
lock_guard< mutex > lock( filename_to_translation_unit_mutex_ ); lock_guard< mutex > lock( filename_to_translation_unit_mutex_ );
return FindWithDefault( filename_to_translation_unit_, return GetNoLock( filename );
filename,
shared_ptr< TranslationUnit >() );
} }
bool TranslationUnitStore::Remove( const std::string &filename ) { bool TranslationUnitStore::Remove( const std::string &filename ) {
@ -108,4 +104,11 @@ void TranslationUnitStore::RemoveAll() {
filename_to_translation_unit_.clear(); 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 } // namespace YouCompleteMe

View File

@ -56,6 +56,11 @@ public:
void RemoveAll(); void RemoveAll();
private: 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, typedef boost::unordered_map< std::string,
boost::shared_ptr< TranslationUnit > > TranslationUnitForFilename; boost::shared_ptr< TranslationUnit > > TranslationUnitForFilename;