[PATCH] D50860: [libc++][test] Remove non-portable assumption that thread's constructor allocates with ::new

Casey Carter via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 26 13:55:47 PDT 2018


CaseyCarter updated this revision to Diff 171344.
CaseyCarter added a comment.

Clarify use of `numAllocs`.


https://reviews.llvm.org/D50860

Files:
  test/std/thread/thread.threads/thread.thread.class/thread.thread.constr/F.pass.cpp


Index: test/std/thread/thread.threads/thread.thread.class/thread.thread.constr/F.pass.cpp
===================================================================
--- test/std/thread/thread.threads/thread.thread.class/thread.thread.constr/F.pass.cpp
+++ test/std/thread/thread.threads/thread.thread.class/thread.thread.constr/F.pass.cpp
@@ -31,17 +31,19 @@
 
 void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc)
 {
-    if (throw_one == 0)
-        TEST_THROW(std::bad_alloc());
-    --throw_one;
+    unsigned expected = throw_one;
+    do {
+        if (expected == 0) TEST_THROW(std::bad_alloc());
+    } while (!throw_one.compare_exchange_weak(expected, expected - 1));
     ++outstanding_new;
     void* ret = std::malloc(s);
     if (!ret) std::abort(); // placate MSVC's unchecked malloc warning
     return ret;
 }
 
 void  operator delete(void* p) TEST_NOEXCEPT
 {
+    if (!p) return;
     --outstanding_new;
     std::free(p);
 }
@@ -109,21 +111,23 @@
 //  B std::thread's constructor should properly handle exceptions and not leak
 //    memory.
 // Plan:
-//  1 Create a thread and count the number of allocations, 'N', it performs.
+//  1 Create a thread and count the number of allocations, 'numAllocs', it performs.
 //  2 For each allocation performed run a test where that allocation throws.
 //    2.1 check that the exception can be caught in the parent thread.
 //    2.2 Check that the functor has not been called.
 //    2.3 Check that no memory allocated by the creation of the thread is leaked.
-//  3 Finally check that a thread runs successfully if we throw after 'N+1'
+//  3 Finally check that a thread runs successfully if we throw after 'numAllocs+1'
 //    allocations.
+int numAllocs;
+
 void test_throwing_new_during_thread_creation() {
 #ifndef TEST_HAS_NO_EXCEPTIONS
     throw_one = 0xFFF;
     {
         std::thread t(f);
         t.join();
     }
-    const int numAllocs = 0xFFF - throw_one;
+    numAllocs = 0xFFF - throw_one;
     // i <= numAllocs means the last iteration is expected not to throw.
     for (int i=0; i <= numAllocs; ++i) {
         throw_one = i;
@@ -167,16 +171,16 @@
     }
     G::op_run = false;
 #ifndef TEST_HAS_NO_EXCEPTIONS
-    {
+    if (numAllocs > 0) { // Creating a thread might not call new at all.
         try
         {
             throw_one = 0;
             assert(G::n_alive == 0);
             assert(!G::op_run);
             std::thread t((G()));
             assert(false);
         }
-        catch (...)
+        catch (std::bad_alloc const&)
         {
             throw_one = 0xFFFF;
             assert(G::n_alive == 0);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50860.171344.patch
Type: text/x-patch
Size: 2623 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181026/0fbb39ee/attachment.bin>


More information about the cfe-commits mailing list