[clang] 527a182 - DirectoryWatcher: also wait for the notifier thread

Saleem Abdulrasool via cfe-commits cfe-commits at lists.llvm.org
Sun Jun 13 10:59:06 PDT 2021


Author: Saleem Abdulrasool
Date: 2021-06-13T10:58:55-07:00
New Revision: 527a1821e6f8e115db3335a3341c7ac491725a0d

URL: https://github.com/llvm/llvm-project/commit/527a1821e6f8e115db3335a3341c7ac491725a0d
DIFF: https://github.com/llvm/llvm-project/commit/527a1821e6f8e115db3335a3341c7ac491725a0d.diff

LOG: DirectoryWatcher: also wait for the notifier thread

Ultimately the DirectoryWatcher is not ready until the notifier thread
is also active.  Failure to wait for the notifier thread may result in
loss of events.  While this is not catastrophic in practice, the tests
are sensitive to this as depending on the thread scheduler, the thread
may fail to being execution before the operations are completed by the
fixture.  Running this in a tight loop shows no regressions locally as
previously, but this failure mode was been sighted once on a builder.

Added: 
    

Modified: 
    clang/lib/DirectoryWatcher/windows/DirectoryWatcher-windows.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/DirectoryWatcher/windows/DirectoryWatcher-windows.cpp b/clang/lib/DirectoryWatcher/windows/DirectoryWatcher-windows.cpp
index 6bcfb86e7f99..847d20bfbb6f 100644
--- a/clang/lib/DirectoryWatcher/windows/DirectoryWatcher-windows.cpp
+++ b/clang/lib/DirectoryWatcher/windows/DirectoryWatcher-windows.cpp
@@ -40,6 +40,7 @@ class DirectoryWatcherWindows : public clang::DirectoryWatcher {
 
   std::mutex Mutex;
   bool WatcherActive = false;
+  bool NotifierActive = false;
   std::condition_variable Ready;
 
   class EventQueue {
@@ -117,7 +118,9 @@ DirectoryWatcherWindows::DirectoryWatcherWindows(
   });
 
   std::unique_lock<std::mutex> lock(Mutex);
-  Ready.wait(lock, [this] { return this->WatcherActive; });
+  Ready.wait(lock, [this] {
+    return this->WatcherActive && this->NotifierActive;
+  });
 }
 
 DirectoryWatcherWindows::~DirectoryWatcherWindows() {
@@ -227,6 +230,12 @@ void DirectoryWatcherWindows::NotifierThreadProc(bool WaitForInitialSync) {
   if (!WaitForInitialSync)
     this->InitialScan();
 
+  {
+    std::unique_lock<std::mutex> lock(Mutex);
+    NotifierActive = true;
+  }
+  Ready.notify_one();
+
   while (true) {
     DirectoryWatcher::Event E = Q.pop_front();
     Callback(E, /*IsInitial=*/false);


        


More information about the cfe-commits mailing list