[Lldb-commits] [lldb] 7c20bdf - [lldb] Fix synchronization in AlarmTest (NFC)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Tue Nov 5 18:05:49 PST 2024


Author: Jonas Devlieghere
Date: 2024-11-05T18:05:33-08:00
New Revision: 7c20bdf373d6cd7f35dee5c71cf94f0eb1be3200

URL: https://github.com/llvm/llvm-project/commit/7c20bdf373d6cd7f35dee5c71cf94f0eb1be3200
DIFF: https://github.com/llvm/llvm-project/commit/7c20bdf373d6cd7f35dee5c71cf94f0eb1be3200.diff

LOG: [lldb] Fix synchronization in AlarmTest (NFC)

ThreadSanitizer detected a data race as if synchronized via sleep.

Added: 
    

Modified: 
    lldb/unittests/Host/AlarmTest.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/unittests/Host/AlarmTest.cpp b/lldb/unittests/Host/AlarmTest.cpp
index 9f6ad189dee970..94e72af3ffe8d1 100644
--- a/lldb/unittests/Host/AlarmTest.cpp
+++ b/lldb/unittests/Host/AlarmTest.cpp
@@ -56,6 +56,9 @@ TEST(AlarmTest, Create) {
   // Leave plenty of time for all the alarms to fire.
   std::this_thread::sleep_for(TEST_TIMEOUT);
 
+  // Acquire the lock to check the callbacks.
+  std::lock_guard<std::mutex> guard(m);
+
   // Make sure all the alarms fired around the expected time.
   for (size_t i = 0; i < 5; ++i)
     EXPECT_GE(callbacks_actual[i], callbacks_expected[i]);
@@ -83,6 +86,9 @@ TEST(AlarmTest, Exit) {
     // Let the alarm go out of scope before any alarm had a chance to fire.
   }
 
+  // Acquire the lock to check the callbacks.
+  std::lock_guard<std::mutex> guard(m);
+
   // Make sure none of the alarms fired.
   for (bool callback : callbacks)
     EXPECT_TRUE(callback);
@@ -113,6 +119,9 @@ TEST(AlarmTest, Cancel) {
   // Leave plenty of time for all the alarms to fire.
   std::this_thread::sleep_for(TEST_TIMEOUT);
 
+  // Acquire the lock to check the callbacks.
+  std::lock_guard<std::mutex> guard(m);
+
   // Make sure none of the first 4 alarms fired.
   for (size_t i = 0; i < 4; ++i)
     EXPECT_FALSE(callbacks[i]);
@@ -146,6 +155,7 @@ TEST(AlarmTest, Restart) {
 
   // Update the last 2 alarms.
   for (size_t i = 3; i < 5; ++i) {
+    std::lock_guard<std::mutex> guard(m);
     callbacks_expected[i] = std::chrono::system_clock::now() + ALARM_TIMEOUT;
     EXPECT_TRUE(alarm.Restart(handles[i]));
   }
@@ -153,6 +163,9 @@ TEST(AlarmTest, Restart) {
   // Leave plenty of time for all the alarms to fire.
   std::this_thread::sleep_for(TEST_TIMEOUT);
 
+  // Acquire the lock to check the callbacks.
+  std::lock_guard<std::mutex> guard(m);
+
   // Make sure all the alarms around the expected time.
   for (size_t i = 0; i < 5; ++i)
     EXPECT_GE(callbacks_actual[i], callbacks_expected[i]);


        


More information about the lldb-commits mailing list