Protecting ALL clang access with a mutex now

This commit is contained in:
Strahinja Val Markovic 2012-08-01 20:09:01 -07:00
parent eab70838f0
commit 618a6acd59
2 changed files with 18 additions and 5 deletions

View File

@ -41,6 +41,7 @@ using boost::lock_guard;
using boost::unique_lock; using boost::unique_lock;
using boost::mutex; using boost::mutex;
using boost::unordered_map; using boost::unordered_map;
using boost::try_to_lock_t;
namespace YouCompleteMe namespace YouCompleteMe
{ {
@ -326,7 +327,7 @@ std::vector< Diagnostic > ClangCompleter::DiagnosticsForFile(
const std::string &filename ) const std::string &filename )
{ {
std::vector< Diagnostic > diagnostics; 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() ) if ( !lock.owns_lock() )
return diagnostics; return diagnostics;
@ -354,8 +355,8 @@ std::vector< Diagnostic > ClangCompleter::DiagnosticsForFile(
bool ClangCompleter::UpdatingTranslationUnit() bool ClangCompleter::UpdatingTranslationUnit()
{ {
lock_guard< mutex > lock( file_parse_task_mutex_ ); unique_lock< mutex > lock( clang_access_mutex_, try_to_lock_t() );
return bool( file_parse_task_ ); return !lock.owns_lock();
} }
@ -651,7 +652,10 @@ void ClangCompleter::FileParseThreadMain()
} }
} }
{
unique_lock< mutex > lock( clang_access_mutex_ );
( *file_parse_task_ )(); ( *file_parse_task_ )();
}
lock_guard< mutex > lock( file_parse_task_mutex_ ); lock_guard< mutex > lock( file_parse_task_mutex_ );
file_parse_task_ = VoidTask(); file_parse_task_ = VoidTask();
@ -675,7 +679,11 @@ void ClangCompleter::ClangCompletionsThreadMain()
continue; continue;
} }
{
unique_lock< mutex > lock( clang_access_mutex_ );
( *task )(); ( *task )();
}
unique_future< AsyncCompletions > future = task->get_future(); unique_future< AsyncCompletions > future = task->get_future();
{ {

View File

@ -132,6 +132,11 @@ private:
mutable LatestTask sorting_task_; 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 // 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 // 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 // of NULL. [ NULL for a shared_ptr naturally means default-constructed