Skip to content

[SR-1639] Move sourcekitdInProc to std::mutex #2793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions tools/SourceKit/tools/sourcekitd/bin/InProc/sourcekitdInProc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#include "llvm/Support/Mutex.h"
#include "llvm/Support/Path.h"

// FIXME: Portability ?
#include <Block.h>
#include <dispatch/dispatch.h>
#include <condition_variable>
#include <mutex>

#ifdef LLVM_ON_WIN32
#include <windows.h>
Expand Down Expand Up @@ -102,16 +102,17 @@ void sourcekitd::set_interrupted_connection_handler(
//===----------------------------------------------------------------------===//

sourcekitd_response_t sourcekitd_send_request_sync(sourcekitd_object_t req) {
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
sourcekitd_response_t ReturnedResp = nullptr;
std::condition_variable handleRequestCondition;

sourcekitd_response_t ReturnedResp;
sourcekitd::handleRequest(req, [&](sourcekitd_response_t resp) {
ReturnedResp = resp;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly because of spurious wakeup, this assignment now needs to be atomic or it can race with the read below.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The typical pattern is to use the same mutex to protect writing to the condition being waited on.

dispatch_semaphore_signal(sema);
handleRequestCondition.notify_one();
});

dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
dispatch_release(sema);
std::mutex m;
std::unique_lock<std::mutex> lk(m);
handleRequestCondition.wait(lk, [&]{ return ReturnedResp != nullptr; });
return ReturnedResp;
}

Expand Down