[Lldb-commits] [lldb] r237844 - Have TestNumThreads use std::thread instead of pthreads so that it can work cross-platform.

Adrian McCarthy amccarth at google.com
Wed May 20 15:32:45 PDT 2015


Author: amccarth
Date: Wed May 20 17:32:44 2015
New Revision: 237844

URL: http://llvm.org/viewvc/llvm-project?rev=237844&view=rev
Log:
Have TestNumThreads use std::thread instead of pthreads so that it can work cross-platform.

Added:
    lldb/trunk/test/functionalities/thread/main.cpp
Removed:
    lldb/trunk/test/functionalities/thread/main.c
Modified:
    lldb/trunk/test/functionalities/thread/Makefile
    lldb/trunk/test/functionalities/thread/TestNumThreads.py

Modified: lldb/trunk/test/functionalities/thread/Makefile
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/thread/Makefile?rev=237844&r1=237843&r2=237844&view=diff
==============================================================================
--- lldb/trunk/test/functionalities/thread/Makefile (original)
+++ lldb/trunk/test/functionalities/thread/Makefile Wed May 20 17:32:44 2015
@@ -1,5 +1,5 @@
 LEVEL = ../../make
 
-C_SOURCES := main.c
+CXX_SOURCES := main.cpp
 ENABLE_THREADS := YES
 include $(LEVEL)/Makefile.rules

Modified: lldb/trunk/test/functionalities/thread/TestNumThreads.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/thread/TestNumThreads.py?rev=237844&r1=237843&r2=237844&view=diff
==============================================================================
--- lldb/trunk/test/functionalities/thread/TestNumThreads.py (original)
+++ lldb/trunk/test/functionalities/thread/TestNumThreads.py Wed May 20 17:32:44 2015
@@ -29,7 +29,7 @@ class NumberOfThreadsTestCase(TestBase):
         # Call super's setUp().
         TestBase.setUp(self)
         # Find the line number to break inside main().
-        self.line = line_number('main.c', '// Set break point at this line.')
+        self.line = line_number('main.cpp', '// Set break point at this line.')
 
     def number_of_threads_test(self):
         """Test number of threads."""
@@ -37,11 +37,11 @@ class NumberOfThreadsTestCase(TestBase):
         self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
 
         # This should create a breakpoint with 1 location.
-        lldbutil.run_break_set_by_file_and_line (self, "main.c", self.line, num_expected_locations=1)
+        lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.line, num_expected_locations=1)
 
         # The breakpoint list should show 3 locations.
         self.expect("breakpoint list -f", "Breakpoint location shown correctly",
-            substrs = ["1: file = 'main.c', line = %d, exact_match = 0, locations = 1" % self.line])
+            substrs = ["1: file = 'main.cpp', line = %d, exact_match = 0, locations = 1" % self.line])
 
         # Run the program.
         self.runCmd("run", RUN_SUCCEEDED)
@@ -57,7 +57,9 @@ class NumberOfThreadsTestCase(TestBase):
         # Get the number of threads
         num_threads = process.GetNumThreads()
 
-        self.assertTrue(num_threads == 4, 'Number of expected threads and actual threads do not match.')
+        # Using std::thread may involve extra threads, so we assert that there are
+        # at least 4 rather than exactly 4.
+        self.assertTrue(num_threads >= 4, 'Number of expected threads and actual threads do not match.')
 
 if __name__ == '__main__':
     import atexit

Removed: lldb/trunk/test/functionalities/thread/main.c
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/thread/main.c?rev=237843&view=auto
==============================================================================
--- lldb/trunk/test/functionalities/thread/main.c (original)
+++ lldb/trunk/test/functionalities/thread/main.c (removed)
@@ -1,57 +0,0 @@
-#include <pthread.h>
-
-pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
-pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
-
-void *
-thread3 (void *input)
-{
-    pthread_mutex_lock(&mutex);
-    pthread_cond_signal(&cond); // Set break point at this line. 
-    pthread_mutex_unlock(&mutex);
-    return NULL;
-}
-
-void *
-thread2 (void *input)
-{
-    pthread_mutex_lock(&mutex);
-    pthread_cond_signal(&cond);
-    pthread_cond_wait(&cond, &mutex);
-    pthread_mutex_unlock(&mutex);
-    return NULL;
-}
-
-void *
-thread1 (void *input)
-{
-    pthread_t thread_2;
-    pthread_create (&thread_2, NULL, thread2, NULL);
-
-    pthread_join(thread_2, NULL);
-
-    return NULL;
-}
-
-int main ()
-{
-    pthread_t thread_1;
-    pthread_t thread_3;
-
-    pthread_mutex_lock (&mutex);
-
-    pthread_create (&thread_1, NULL, thread1, NULL);
-
-    pthread_cond_wait (&cond, &mutex);
-
-    pthread_create (&thread_3, NULL, thread3, NULL);
-
-    pthread_cond_wait (&cond, &mutex);
-
-    pthread_mutex_unlock (&mutex);
-
-    pthread_join (thread_1, NULL);
-    pthread_join (thread_3, NULL);
-
-    return 0;
-}

Added: lldb/trunk/test/functionalities/thread/main.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/thread/main.cpp?rev=237844&view=auto
==============================================================================
--- lldb/trunk/test/functionalities/thread/main.cpp (added)
+++ lldb/trunk/test/functionalities/thread/main.cpp Wed May 20 17:32:44 2015
@@ -0,0 +1,50 @@
+#include <condition_variable>
+#include <mutex>
+#include <thread>
+
+std::mutex mutex;
+std::condition_variable cond;
+
+void *
+thread3(void *input)
+{
+    std::unique_lock<std::mutex> lock(mutex);
+    cond.notify_all(); // Set break point at this line.
+    return NULL;
+}
+
+void *
+thread2(void *input)
+{
+    std::unique_lock<std::mutex> lock(mutex);
+    cond.notify_all();
+    cond.wait(lock);
+    return NULL;
+}
+
+void *
+thread1(void *input)
+{
+    std::thread thread_2(thread2, nullptr);
+    thread_2.join();
+
+    return NULL;
+}
+
+int main()
+{
+    std::unique_lock<std::mutex> lock(mutex);
+
+    std::thread thread_1(thread1, nullptr);
+    cond.wait(lock);
+
+    std::thread thread_3(thread3, nullptr);
+    cond.wait(lock);
+
+    lock.unlock();
+
+    thread_1.join();
+    thread_3.join();
+
+    return 0;
+}





More information about the lldb-commits mailing list