[PATCH] fixed ThreadLister test for the case of pre-existing threads and added a test for ThreadLister::Reset()

Sergey Matveev earthdok at google.com
Tue Mar 12 10:20:33 PDT 2013


Hi kcc, glider, samsonov,

Fixed incorrect handling of pre-existing threads in ThreadLister test.
Also, extended the test to check that ThreadLister::Reset() works as intended.

http://llvm-reviews.chandlerc.com/D532

Files:
  lib/sanitizer_common/tests/sanitizer_linux_test.cc

Index: lib/sanitizer_common/tests/sanitizer_linux_test.cc
===================================================================
--- lib/sanitizer_common/tests/sanitizer_linux_test.cc
+++ lib/sanitizer_common/tests/sanitizer_linux_test.cc
@@ -44,10 +44,37 @@
   return NULL;
 }
 
+std::set<pid_t> ReadTidsToSet(ThreadLister &thread_lister) {
+  std::set<pid_t> listed_tids;
+  while (true) {
+    pid_t tid = thread_lister.GetNextTID();
+    if (tid < 0)
+      break;
+    listed_tids.insert(tid);
+  }
+  EXPECT_FALSE(thread_lister.error());
+  return listed_tids;
+}
+
+void SpawnTidReporter(pthread_t *thread_id, pid_t *thread_tid) {
+  pthread_mutex_lock(&tid_reported_mutex);
+  int pthread_create_result;
+  *thread_tid = -1;
+  pthread_create_result = pthread_create(thread_id, NULL,
+                                         TIDReporterThread,
+                                         thread_tid);
+  ASSERT_EQ(pthread_create_result, 0);
+  while (*thread_tid == -1)
+    pthread_cond_wait(&tid_reported_cond, &tid_reported_mutex);
+  pthread_mutex_unlock(&tid_reported_mutex);
+}
+
 // The set of TIDs produced by ThreadLister should include the TID of every
 // thread we spawn here. The two sets may differ if there are other threads
 // running in the current process that we are not aware of.
 // Calling ThreadLister::Reset() should not change this.
+// Also, calling ThreadLister::Reset() should make recently created threads
+// visible to the ThreadLister object.
 TEST(SanitizerLinux, ThreadListerMultiThreaded) {
   pthread_mutex_init(&thread_exit_mutex, NULL);
   pthread_mutex_init(&tid_reported_mutex, NULL);
@@ -59,51 +86,45 @@
   pid_t pid = getpid();
   pid_t self_tid = GetTid();
   thread_exit = false;
-  pthread_mutex_lock(&tid_reported_mutex);
-  for (uptr i = 0; i < kThreadCount; i++) {
-    int pthread_create_result;
-    thread_tids[i] = -1;
-    pthread_create_result = pthread_create(&thread_ids[i], NULL,
-                                           TIDReporterThread,
-                                           &thread_tids[i]);
-    ASSERT_EQ(pthread_create_result, 0);
-    while (thread_tids[i] == -1)
-      pthread_cond_wait(&tid_reported_cond, &tid_reported_mutex);
-  }
-  pthread_mutex_unlock(&tid_reported_mutex);
+  for (uptr i = 0; i < kThreadCount; i++)
+    SpawnTidReporter(&thread_ids[i], &thread_tids[i]);
   std::set<pid_t> reported_tids(thread_tids, thread_tids + kThreadCount);
   reported_tids.insert(self_tid);
 
   ThreadLister thread_lister(pid);
   // There's a Reset() call between the first and second iteration.
   for (uptr i = 0; i < 2; i++) {
-    std::set<pid_t> listed_tids;
-
-    EXPECT_FALSE(thread_lister.error());
-    for (uptr i = 0; i < kThreadCount + 1; i++) {
-      pid_t tid = thread_lister.GetNextTID();
-      EXPECT_GE(tid, 0);
-      EXPECT_FALSE(thread_lister.error());
-      listed_tids.insert(tid);
-    }
-    pid_t tid = thread_lister.GetNextTID();
-    EXPECT_LT(tid, 0);
-    EXPECT_FALSE(thread_lister.error());
-
+    std::set<pid_t> listed_tids = ReadTidsToSet(thread_lister);
     std::set<pid_t> intersection;
     std::set_intersection(reported_tids.begin(), reported_tids.end(),
                           listed_tids.begin(), listed_tids.end(),
                           std::inserter(intersection, intersection.begin()));
     EXPECT_EQ(intersection, reported_tids);
     thread_lister.Reset();
   }
 
+  // Check that threads created during ThreadLister object's lifetime become
+  // visible after a Reset() call.
+  thread_lister.Reset();
+  std::set<pid_t> threads_before_extra = ReadTidsToSet(thread_lister);
+  pthread_t extra_thread_id;
+  pid_t extra_thread_tid;
+  SpawnTidReporter(&extra_thread_id, &extra_thread_tid);
+  ASSERT_EQ(threads_before_extra.find(extra_thread_tid),
+            threads_before_extra.end());
+  thread_lister.Reset();
+  std::set<pid_t> threads_after_extra = ReadTidsToSet(thread_lister);
+  ASSERT_NE(threads_after_extra.find(extra_thread_tid),
+            threads_before_extra.end());
+
+  // Cleanup.
   pthread_mutex_lock(&thread_exit_mutex);
   thread_exit = true;
   pthread_cond_broadcast(&thread_exit_cond);
   pthread_mutex_unlock(&thread_exit_mutex);
   for (uptr i = 0; i < kThreadCount; i++)
     pthread_join(thread_ids[i], NULL);
+  pthread_join(extra_thread_id, NULL);
   pthread_mutex_destroy(&thread_exit_mutex);
   pthread_mutex_destroy(&tid_reported_mutex);
   pthread_cond_destroy(&thread_exit_cond);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D532.1.patch
Type: text/x-patch
Size: 4463 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130312/8ba1598c/attachment.bin>


More information about the llvm-commits mailing list