Renaming ConcurrentStack to ConcurrentLatestValue

This commit is contained in:
Strahinja Val Markovic 2012-07-06 12:14:25 -07:00
parent 8da18e0319
commit 174687c4bc
4 changed files with 29 additions and 16 deletions

View File

@ -42,11 +42,11 @@ namespace
const unsigned int MAX_ASYNC_THREADS = 4; const unsigned int MAX_ASYNC_THREADS = 4;
const unsigned int MIN_ASYNC_THREADS = 2; const unsigned int MIN_ASYNC_THREADS = 2;
void ThreadMain( TaskStack &task_stack ) void ThreadMain( LatestTask &latest_task )
{ {
while ( true ) while ( true )
{ {
( *task_stack.Get() )(); ( *latest_task.Get() )();
} }
} }
@ -171,7 +171,7 @@ Future Completer::CandidatesForQueryAndTypeAsync(
unique_future< AsyncResults > future = task->get_future(); unique_future< AsyncResults > future = task->get_future();
task_stack_.Set( task ); latest_task_.Set( task );
return Future( move( future ) ); return Future( move( future ) );
} }
@ -242,7 +242,7 @@ void Completer::InitThreads()
for ( int i = 0; i < threads_to_create; ++i ) for ( int i = 0; i < threads_to_create; ++i )
{ {
threads_.create_thread( bind( ThreadMain, boost::ref( task_stack_ ) ) ); threads_.create_thread( bind( ThreadMain, boost::ref( latest_task_ ) ) );
} }
} }

View File

@ -19,7 +19,7 @@
#define COMPLETER_H_7AR4UGXE #define COMPLETER_H_7AR4UGXE
#include "Candidate.h" #include "Candidate.h"
#include "ConcurrentStack.h" #include "ConcurrentLatestValue.h"
#include "Future.h" #include "Future.h"
#include <boost/utility.hpp> #include <boost/utility.hpp>
@ -49,9 +49,9 @@ typedef boost::unordered_map< std::string,
typedef boost::unordered_map< std::string, typedef boost::unordered_map< std::string,
boost::shared_ptr< FilepathToCandidates > > FiletypeMap; boost::shared_ptr< FilepathToCandidates > > FiletypeMap;
typedef ConcurrentStack< typedef ConcurrentLatestValue<
boost::shared_ptr< boost::shared_ptr<
boost::packaged_task< AsyncResults > > > TaskStack; boost::packaged_task< AsyncResults > > > LatestTask;
class Completer : boost::noncopyable class Completer : boost::noncopyable
@ -113,7 +113,7 @@ private:
FiletypeMap filetype_map_; FiletypeMap filetype_map_;
mutable TaskStack task_stack_; mutable LatestTask latest_task_;
bool threading_enabled_; bool threading_enabled_;

View File

@ -15,23 +15,36 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>. // along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
#ifndef CONCURRENTSTACK_H_SYF1JPPG #ifndef CONCURRENTLATESTVALUE_H_SYF1JPPG
#define CONCURRENTSTACK_H_SYF1JPPG #define CONCURRENTLATESTVALUE_H_SYF1JPPG
#include <stack>
#include <boost/thread.hpp> #include <boost/thread.hpp>
#include <boost/utility.hpp> #include <boost/utility.hpp>
namespace YouCompleteMe namespace YouCompleteMe
{ {
// This is is basically a multi-consumer single-producer queue, only with the
// twist that we only care about the latest value set. So the GUI thread is the
// setter, and the worker threads are the workers. The workers wait in line on
// the condition variable and when the setter sets a value, a worker is chosen
// to consume it.
//
// The point is that we always want to have one "fresh" worker thread ready to
// work on our latest value. If a newer value is set, then we don't care what
// happens to the old values.
//
// This implementation is mutex-based and is not lock-free. Normally using a
// lock-free data structure makes more sense, but since the GUI thread goes
// through VimL and Python on every keystroke, there's really no point. Those 5
// nanoseconds it takes to lock a mutex are laughably negligible compared to the
// VimL/Python overhead.
template <typename T> template <typename T>
class ConcurrentStack : boost::noncopyable class ConcurrentLatestValue : boost::noncopyable
{ {
public: public:
// TODO: rename this class, it's not a stack anymore ConcurrentLatestValue() : empty_( true ) {}
ConcurrentStack() : empty_( true ) {}
void Set( const T& data ) void Set( const T& data )
{ {
@ -59,7 +72,6 @@ public:
private: private:
// std::stack< T > stack_;
T latest_; T latest_;
bool empty_; bool empty_;
boost::mutex mutex_; boost::mutex mutex_;
@ -69,4 +81,4 @@ private:
} // namespace YouCompleteMe } // namespace YouCompleteMe
#endif /* end of include guard: CONCURRENTSTACK_H_SYF1JPPG */ #endif /* end of include guard: CONCURRENTLATESTVALUE_H_SYF1JPPG */

View File

@ -116,6 +116,7 @@ def CompletionStartColumn():
if current_column - start_column < min_num_chars: if current_column - start_column < min_num_chars:
# for vim, -2 means not found but don't trigger an error message # for vim, -2 means not found but don't trigger an error message
# see :h complete-functions
return -2 return -2
return start_column return start_column