public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] libstdc++: testsuite: Address random failure in pthread_create() [PR54185]
@ 2020-08-13 22:15 Lewis Hyatt
  2020-08-18  8:43 ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Lewis Hyatt @ 2020-08-13 22:15 UTC (permalink / raw)
  To: gcc-patches; +Cc: libstdc++

[-- Attachment #1: Type: text/plain, Size: 544 bytes --]

Hello-

The attached patch was discussed briefly on PR 54185 here:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54185#c14
The test case for this PR sometimes fails due to random failures in
pthread_create() that are not related to the original PR. This patch fixes
it up by ignoring those failures. The test case was designed to repeat the
same test 1000 times to attempt to reproduce a race condition, so I think is
OK if some of those iterations are simply skipped.

Thanks for taking a look at it; I can commit it if it makes sense.

-Lewis

[-- Attachment #2: pr54185_test.txt --]
[-- Type: text/plain, Size: 1954 bytes --]

libstdc++: testsuite: Address random failure in pthread_create() [PR54185]

The test for this PR calls pthread_create() many times in a row, which may fail
with EAGAIN sometimes. Avoid generating a test failure in this case.

libstdc++-v3/ChangeLog:

	PR libstdc++/54185
	* testsuite/30_threads/condition_variable/54185.cc: Make test robust
	to random pthread_create() failures.

diff --git a/libstdc++-v3/testsuite/30_threads/condition_variable/54185.cc b/libstdc++-v3/testsuite/30_threads/condition_variable/54185.cc
index ea0d5bb8740..cbd21e11e57 100644
--- a/libstdc++-v3/testsuite/30_threads/condition_variable/54185.cc
+++ b/libstdc++-v3/testsuite/30_threads/condition_variable/54185.cc
@@ -31,19 +31,25 @@
 std::condition_variable* cond = nullptr;
 std::mutex mx;
 int started = 0;
+bool notified = false;
 int constexpr NUM_THREADS = 10;
 
+static void finalize_cond()
+{
+  /* Lock should be held when calling this.  */
+  notified = true;
+  cond->notify_all();
+  delete cond;
+  cond = nullptr;
+}
+
 void do_thread_a()
 {
   std::unique_lock<std::mutex> lock(mx);
   if(++started >= NUM_THREADS)
-  {
-    cond->notify_all();
-    delete cond;
-    cond = nullptr;
-  }
+    finalize_cond();
   else
-    cond->wait(lock);
+    cond->wait(lock, [] { return notified; });
 }
 
 int main(){
@@ -51,11 +57,24 @@ int main(){
   for(int j = 0; j < 1000; ++j)
   {
     started = 0;
+    notified = false;
     cond = new std::condition_variable;
     for (int i = 0; i < NUM_THREADS; ++i)
-      vec.emplace_back(&do_thread_a);
-    for (int i = 0; i < NUM_THREADS; ++i)
-      vec[i].join();
+      {
+	try
+	  {
+	    vec.emplace_back(&do_thread_a);
+	  }
+	catch(const std::system_error&)
+	  {
+	    /* Thread creation may fail due to resource limits; just move on.  */
+	    std::unique_lock<std::mutex> lock(mx);
+	    finalize_cond();
+	    break;
+	  }
+      }
+    for(auto& thread: vec)
+      thread.join();
     vec.clear();
   }
 }

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-08-18 18:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-13 22:15 [PATCH] libstdc++: testsuite: Address random failure in pthread_create() [PR54185] Lewis Hyatt
2020-08-18  8:43 ` Jonathan Wakely
2020-08-18 15:20   ` Lewis Hyatt
2020-08-18 18:15     ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).