Skip to content

Commit

Permalink
Add a timeout to getting the translation unit
Browse files Browse the repository at this point in the history
  • Loading branch information
pfultz2 committed Oct 8, 2013
1 parent 8606a74 commit 40e4bbb
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions complete/complete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,12 @@ class async_translation_unit : public translation_unit, public std::enable_share
std::timed_mutex tus_mutex;
std::unordered_map<std::string, std::shared_ptr<async_translation_unit>> tus;

std::shared_ptr<async_translation_unit> get_tu(const char * filename, const char ** args, int argv)
std::shared_ptr<async_translation_unit> get_tu(const char * filename, const char ** args, int argv, int timeout=-1)
{
std::lock_guard<std::timed_mutex> lock(tus_mutex);
std::unique_lock<std::timed_mutex> lock(tus_mutex, std::defer_lock);
if (timeout < 0) return lock.lock();
else if (!lock.try_lock_for(std::chrono::milliseconds(timeout))) return {};

if (tus.find(filename) == tus.end())
{
tus[filename] = std::make_shared<async_translation_unit>(filename, args, argv);
Expand Down Expand Up @@ -579,9 +582,9 @@ PyObject* clang_complete_get_completions(
const char * buffer,
unsigned len)
{
auto tu = get_tu(filename, args, argv);

return export_pylist(tu->async_complete_at(line, col, prefix, timeout, buffer, len));
auto tu = get_tu(filename, args, argv, 200);
if (tu == nullptr) return export_pylist(std::vector<std::string>());
else return export_pylist(tu->async_complete_at(line, col, prefix, timeout, buffer, len));
}

PyObject* clang_complete_get_diagnostics(const char * filename, const char ** args, int argv)
Expand Down

0 comments on commit 40e4bbb

Please sign in to comment.