Refactorings and removing dead code

This commit is contained in:
Strahinja Val Markovic 2012-08-11 20:13:12 -07:00
parent 6a2f8c859c
commit 67c69197d8
4 changed files with 32 additions and 78 deletions

View File

@ -31,25 +31,28 @@
// TODO: remove all explicit uses of the boost:: prefix by adding explicit using // TODO: remove all explicit uses of the boost:: prefix by adding explicit using
// directives for the stuff we need // directives for the stuff we need
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
using boost::packaged_task;
using boost::bind; using boost::bind;
using boost::unique_future; using boost::cref;
using boost::make_shared; using boost::function;
using boost::shared_ptr;
using boost::bind;
using boost::thread;
using boost::lock_guard; using boost::lock_guard;
using boost::unique_lock; using boost::make_shared;
using boost::shared_lock;
using boost::mutex; using boost::mutex;
using boost::packaged_task;
using boost::ref;
using boost::shared_lock;
using boost::shared_mutex; using boost::shared_mutex;
using boost::unordered_map; using boost::shared_ptr;
using boost::thread;
using boost::thread_interrupted;
using boost::try_to_lock_t; using boost::try_to_lock_t;
using boost::unique_future;
using boost::unique_lock;
using boost::unordered_map;
namespace YouCompleteMe namespace YouCompleteMe
{ {
typedef boost::function< std::vector< CompletionData >() > typedef function< std::vector< CompletionData >() >
FunctionReturnsCompletionDataVector; FunctionReturnsCompletionDataVector;
extern const unsigned int MAX_ASYNC_THREADS; extern const unsigned int MAX_ASYNC_THREADS;
@ -108,7 +111,8 @@ ClangCompleter::~ClangCompleter()
// We need this mostly so that we can not use it in tests. Apparently the // We need this mostly so that we can not use it in tests. Apparently the
// GoogleTest framework goes apeshit on us if we enable threads by default. // GoogleTest framework goes apeshit on us (on some platforms, in some
// occasions) if we enable threads by default.
void ClangCompleter::EnableThreading() void ClangCompleter::EnableThreading()
{ {
threading_enabled_ = true; threading_enabled_ = true;
@ -172,26 +176,18 @@ void ClangCompleter::UpdateTranslationUnitAsync(
std::vector< UnsavedFile > unsaved_files, std::vector< UnsavedFile > unsaved_files,
std::vector< std::string > flags ) std::vector< std::string > flags )
{ {
boost::function< void() > functor = function< void() > functor =
bind( &ClangCompleter::UpdateTranslationUnit, bind( &ClangCompleter::UpdateTranslationUnit,
boost::ref( *this ), boost::ref( *this ),
boost::move( filename ), boost::move( filename ),
boost::move( unsaved_files ), boost::move( unsaved_files ),
boost::move( flags ) ); boost::move( flags ) );
// boost::lock_guard< boost::mutex > lock( file_parse_task_mutex_ );
// // Only ever set the task when it's NULL; if it's not, that means that the
// // clang thread is working on it
// if ( file_parse_task_ )
// return;
shared_ptr< ClangPackagedTask > clang_packaged_task = shared_ptr< ClangPackagedTask > clang_packaged_task =
make_shared< ClangPackagedTask >(); make_shared< ClangPackagedTask >();
clang_packaged_task->parsing_task_ = packaged_task< void >( functor ); clang_packaged_task->parsing_task_ = packaged_task< void >( functor );
clang_task_.Set( clang_packaged_task ); clang_task_.Set( clang_packaged_task );
// file_parse_task_ = make_shared< packaged_task< void > >( functor );
// file_parse_task_condition_variable_.notify_all();
} }
@ -233,7 +229,7 @@ ClangCompleter::CandidatesForQueryAndLocationInFileAsync(
return Future< AsyncCompletions >(); return Future< AsyncCompletions >();
{ {
boost::lock_guard< boost::mutex > lock( clang_data_ready_mutex_ ); lock_guard< mutex > lock( clang_data_ready_mutex_ );
clang_data_ready_ = false; clang_data_ready_ = false;
} }
@ -275,15 +271,11 @@ ClangCompleter::CandidatesForQueryAndLocationInFileAsync(
shared_ptr< ClangPackagedTask > clang_packaged_task = shared_ptr< ClangPackagedTask > clang_packaged_task =
make_shared< ClangPackagedTask >(); make_shared< ClangPackagedTask >();
clang_packaged_task->completions_task_ = packaged_task< AsyncCompletions >( clang_packaged_task->completions_task_ = packaged_task< AsyncCompletions >(
bind( ReturnValueAsShared< std::vector< CompletionData > >, bind( ReturnValueAsShared< std::vector< CompletionData > >,
candidates_for_location_functor ) ); candidates_for_location_functor ) );
// shared_ptr< packaged_task< AsyncCompletions > > task =
// make_shared< packaged_task< AsyncCompletions > >(
// bind( ReturnValueAsShared< std::vector< CompletionData > >,
// candidates_for_location_functor ) );
clang_task_.Set( clang_packaged_task ); clang_task_.Set( clang_packaged_task );
} }
@ -296,7 +288,7 @@ ClangCompleter::CandidatesForQueryAndLocationInFileAsync(
// calls this function so there is no need for a mutex, but if that changes in // calls this function so there is no need for a mutex, but if that changes in
// the future a mutex will be needed to make sure that two threads don't try to // the future a mutex will be needed to make sure that two threads don't try to
// create the same translation unit. // create the same translation unit.
boost::shared_ptr< TranslationUnit > ClangCompleter::GetTranslationUnitForFile( shared_ptr< TranslationUnit > ClangCompleter::GetTranslationUnitForFile(
const std::string &filename, const std::string &filename,
const std::vector< UnsavedFile > &unsaved_files, const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags ) const std::vector< std::string > &flags )
@ -370,58 +362,23 @@ void ClangCompleter::InitThreads()
for ( int i = 0; i < threads_to_create; ++i ) for ( int i = 0; i < threads_to_create; ++i )
{ {
sorting_threads_.create_thread( sorting_threads_.create_thread( bind( &ClangCompleter::SortingThreadMain,
bind( &ClangCompleter::SortingThreadMain,
boost::ref( *this ) ) ); boost::ref( *this ) ) );
} }
clang_thread_ = boost::thread( clang_thread_ = thread( &ClangCompleter::ClangThreadMain,
&ClangCompleter::ClangThreadMain,
boost::ref( *this ) ); boost::ref( *this ) );
} }
// void ClangCompleter::FileParseThreadMain()
// {
// while ( true )
// {
// {
// boost::unique_lock< boost::mutex > lock( file_parse_task_mutex_ );
//
// while ( !file_parse_task_ )
// {
// file_parse_task_condition_variable_.wait( lock );
// }
// }
//
// {
// unique_lock< mutex > lock( clang_access_mutex_ );
// ( *file_parse_task_ )();
// }
//
// lock_guard< mutex > lock( file_parse_task_mutex_ );
// file_parse_task_ = VoidTask();
// }
// }
void ClangCompleter::ClangThreadMain() void ClangCompleter::ClangThreadMain()
{ {
while ( true ) while ( true )
{ {
try try
{ {
// TODO: this should be a separate func, much like the file_parse_task_ part
shared_ptr< ClangPackagedTask > task = clang_task_.Get(); shared_ptr< ClangPackagedTask > task = clang_task_.Get();
// If the file parse thread is accessing clang by parsing a file, then drop
// the current completion request
// {
// lock_guard< mutex > lock( file_parse_task_mutex_ );
// if ( file_parse_task_ )
// continue;
// }
bool has_completions_task = task->completions_task_.valid(); bool has_completions_task = task->completions_task_.valid();
if ( has_completions_task ) if ( has_completions_task )
task->completions_task_(); task->completions_task_();
@ -435,20 +392,19 @@ void ClangCompleter::ClangThreadMain()
task->completions_task_.get_future(); task->completions_task_.get_future();
{ {
boost::unique_lock< boost::shared_mutex > writer_lock( unique_lock< shared_mutex > writer_lock( latest_clang_results_mutex_ );
latest_clang_results_shared_mutex_ );
latest_clang_results_ = *future.get(); latest_clang_results_ = *future.get();
} }
{ {
boost::lock_guard< boost::mutex > lock( clang_data_ready_mutex_ ); lock_guard< mutex > lock( clang_data_ready_mutex_ );
clang_data_ready_ = true; clang_data_ready_ = true;
} }
clang_data_ready_condition_variable_.notify_all(); clang_data_ready_condition_variable_.notify_all();
} }
catch ( boost::thread_interrupted& ) catch ( thread_interrupted& )
{ {
shared_lock< shared_mutex > lock( time_to_die_mutex_ ); shared_lock< shared_mutex > lock( time_to_die_mutex_ );
if ( time_to_die_ ) if ( time_to_die_ )
@ -478,14 +434,13 @@ void ClangCompleter::SortingThreadMain()
sorting_task_.Get(); sorting_task_.Get();
{ {
boost::shared_lock< boost::shared_mutex > reader_lock( shared_lock< shared_mutex > reader_lock( latest_clang_results_mutex_ );
latest_clang_results_shared_mutex_ );
( *task )(); ( *task )();
} }
} }
catch ( boost::thread_interrupted& ) catch ( thread_interrupted& )
{ {
shared_lock< shared_mutex > lock( time_to_die_mutex_ ); shared_lock< shared_mutex > lock( time_to_die_mutex_ );
if ( time_to_die_ ) if ( time_to_die_ )

View File

@ -147,11 +147,12 @@ private:
boost::condition_variable clang_data_ready_condition_variable_; boost::condition_variable clang_data_ready_condition_variable_;
std::vector< CompletionData > latest_clang_results_; std::vector< CompletionData > latest_clang_results_;
boost::shared_mutex latest_clang_results_shared_mutex_; boost::shared_mutex latest_clang_results_mutex_;
// Unfortunately clang is not thread-safe so we need to be careful when we // Unfortunately clang is not thread-safe so we need to be careful when we
// access it. Only one thread at a time is allowed to access any single // access it. Only one thread at a time is allowed to access any single
// translation unit. // translation unit. Currently we only use one thread to access clang and that
// is the thread represented by clang_thread_.
boost::thread clang_thread_; boost::thread clang_thread_;
boost::thread_group sorting_threads_; boost::thread_group sorting_threads_;

View File

@ -153,7 +153,6 @@ std::vector< CompletionData > TranslationUnit::CandidatesForLocation(
// Should only be called while holding the clang_access_mutex_ // Should only be called while holding the clang_access_mutex_
// TODO: assert that
void TranslationUnit::UpdateLatestDiagnostics() void TranslationUnit::UpdateLatestDiagnostics()
{ {
unique_lock< mutex > lock( diagnostics_mutex_ ); unique_lock< mutex > lock( diagnostics_mutex_ );

View File

@ -25,7 +25,6 @@
#include <boost/utility.hpp> #include <boost/utility.hpp>
#include <boost/thread/mutex.hpp> #include <boost/thread/mutex.hpp>
// #include <boost/unordered_map.hpp>
#include <string> #include <string>
#include <vector> #include <vector>