Skip to content

[IndexDatastore] Ensure IndexDatastore::waitUntilDoneInitializing() works properly #20

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

Merged
merged 1 commit into from
Apr 28, 2019
Merged
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
46 changes: 39 additions & 7 deletions lib/Index/IndexDatastore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ struct UnitEventInfo {

struct DoneInitState {
std::atomic<bool> DoneInit{false};
unsigned RemainingInitUnits = 0; // this is not accessed concurrently.
};

class StoreUnitRepo : public std::enable_shared_from_this<StoreUnitRepo> {
Expand All @@ -94,8 +95,7 @@ class StoreUnitRepo : public std::enable_shared_from_this<StoreUnitRepo> {

std::shared_ptr<FilePathWatcher> PathWatcher;

// This is shared so that it can be safely passed to an asynchronous block.
std::shared_ptr<DoneInitState> DoneInitializingPtr;
DoneInitState InitializingState;
dispatch_semaphore_t InitSemaphore;

mutable llvm::sys::Mutex StateMtx;
Expand All @@ -110,7 +110,6 @@ class StoreUnitRepo : public std::enable_shared_from_this<StoreUnitRepo> {
Delegate(std::move(Delegate)),
CanonPathCache(std::move(canonPathCache)) {

DoneInitializingPtr = std::make_shared<DoneInitState>();
InitSemaphore = dispatch_semaphore_create(0);
}
~StoreUnitRepo() {
Expand All @@ -121,7 +120,11 @@ class StoreUnitRepo : public std::enable_shared_from_this<StoreUnitRepo> {
function_ref<void(unsigned)> ReportCompleted,
function_ref<void()> DirectoryDeleted);

void setInitialUnitCount(unsigned count);
void processedInitialUnitCount(unsigned count);
void finishedUnitInitialization();
void waitUntilDoneInitializing();

void purgeStaleData();

std::shared_ptr<UnitMonitor> getUnitMonitor(IDCode unitCode) const;
Expand Down Expand Up @@ -265,9 +268,8 @@ void StoreUnitRepo::onFilesChange(std::vector<UnitEventInfo> evts,
PathWatcher = std::make_shared<FilePathWatcher>(std::move(pathEventsReceiver));
}

if (!DoneInitializingPtr->DoneInit) {
dispatch_semaphore_signal(InitSemaphore);
DoneInitializingPtr->DoneInit = true;
if (!InitializingState.DoneInit) {
processedInitialUnitCount(evts.size());
}
}

Expand Down Expand Up @@ -451,8 +453,26 @@ void StoreUnitRepo::purgeStaleData() {
// IdxStore->purgeStaleRecords(ActiveRecNames);
}

void StoreUnitRepo::setInitialUnitCount(unsigned count) {
InitializingState.RemainingInitUnits = count;
}

void StoreUnitRepo::processedInitialUnitCount(unsigned count) {
assert(!InitializingState.DoneInit);
InitializingState.RemainingInitUnits -= std::min(count, InitializingState.RemainingInitUnits);
if (InitializingState.RemainingInitUnits == 0) {
finishedUnitInitialization();
}
}

void StoreUnitRepo::finishedUnitInitialization() {
assert(!InitializingState.DoneInit);
dispatch_semaphore_signal(InitSemaphore);
InitializingState.DoneInit = true;
}

void StoreUnitRepo::waitUntilDoneInitializing() {
if (DoneInitializingPtr->DoneInit)
if (InitializingState.DoneInit)
return;
dispatch_semaphore_wait(InitSemaphore, DISPATCH_TIME_FOREVER);
}
Expand Down Expand Up @@ -765,6 +785,18 @@ bool IndexDatastoreImpl::init(IndexStoreRef idxStore,
std::weak_ptr<StoreUnitRepo> WeakUnitRepo = UnitRepo;
auto eventsDeque = std::make_shared<UnitEventInfoDeque>();
auto OnUnitsChange = [WeakUnitRepo, Delegate, eventsDeque](IndexStore::UnitEventNotification EventNote) {
if (EventNote.isInitial()) {
auto UnitRepo = WeakUnitRepo.lock();
if (!UnitRepo)
return;
size_t evtCount = EventNote.getEventsCount();
if (evtCount == 0) {
UnitRepo->finishedUnitInitialization();
} else {
UnitRepo->setInitialUnitCount(evtCount);
}
}

std::vector<UnitEventInfo> evts;
for (size_t i = 0, e = EventNote.getEventsCount(); i != e; ++i) {
auto evt = EventNote.getEvent(i);
Expand Down