From 618a6acd592f98e770f104021273bfa95c8e893e Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 1 Aug 2012 20:09:01 -0700 Subject: [PATCH] Protecting ALL clang access with a mutex now --- cpp/ycm/ClangCompleter.cpp | 18 +++++++++++++----- cpp/ycm/ClangCompleter.h | 5 +++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/cpp/ycm/ClangCompleter.cpp b/cpp/ycm/ClangCompleter.cpp index d483d3d6..26350d11 100644 --- a/cpp/ycm/ClangCompleter.cpp +++ b/cpp/ycm/ClangCompleter.cpp @@ -41,6 +41,7 @@ using boost::lock_guard; using boost::unique_lock; using boost::mutex; using boost::unordered_map; +using boost::try_to_lock_t; namespace YouCompleteMe { @@ -326,7 +327,7 @@ std::vector< Diagnostic > ClangCompleter::DiagnosticsForFile( const std::string &filename ) { std::vector< Diagnostic > diagnostics; - unique_lock< mutex > lock( file_parse_task_mutex_, boost::try_to_lock_t ); + unique_lock< mutex > lock( clang_access_mutex_, try_to_lock_t() ); if ( !lock.owns_lock() ) return diagnostics; @@ -354,8 +355,8 @@ std::vector< Diagnostic > ClangCompleter::DiagnosticsForFile( bool ClangCompleter::UpdatingTranslationUnit() { - lock_guard< mutex > lock( file_parse_task_mutex_ ); - return bool( file_parse_task_ ); + unique_lock< mutex > lock( clang_access_mutex_, try_to_lock_t() ); + return !lock.owns_lock(); } @@ -651,7 +652,10 @@ void ClangCompleter::FileParseThreadMain() } } - ( *file_parse_task_ )(); + { + unique_lock< mutex > lock( clang_access_mutex_ ); + ( *file_parse_task_ )(); + } lock_guard< mutex > lock( file_parse_task_mutex_ ); file_parse_task_ = VoidTask(); @@ -675,7 +679,11 @@ void ClangCompleter::ClangCompletionsThreadMain() continue; } - ( *task )(); + { + unique_lock< mutex > lock( clang_access_mutex_ ); + ( *task )(); + } + unique_future< AsyncCompletions > future = task->get_future(); { diff --git a/cpp/ycm/ClangCompleter.h b/cpp/ycm/ClangCompleter.h index 5c8f412b..5848ffed 100644 --- a/cpp/ycm/ClangCompleter.h +++ b/cpp/ycm/ClangCompleter.h @@ -132,6 +132,11 @@ private: mutable LatestTask sorting_task_; + // TODO: wrap the entire clang API with an internal class that then uses this + // mutex... actually a TU class with an internal mutex + // Only the thread that is holding this mutex can access clang functions + boost::mutex clang_access_mutex_; + // Only the clang thread can make this NULL and only the GUI thread can make // it non-NULL. The file_parse_task_mutex_ is used before checking the state // of NULL. [ NULL for a shared_ptr naturally means default-constructed