15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier //
95a83710eSEric Fiselier // UNSUPPORTED: libcpp-has-no-threads
105a83710eSEric Fiselier 
114f7fa06aSMartin Storsjö // XFAIL: LIBCXX-WINDOWS-FIXME
124f7fa06aSMartin Storsjö 
135a83710eSEric Fiselier // <thread>
145a83710eSEric Fiselier 
155a83710eSEric Fiselier // class thread
165a83710eSEric Fiselier 
175a83710eSEric Fiselier // template <class F, class ...Args> thread(F&& f, Args&&... args);
185a83710eSEric Fiselier 
194b7533a1SEric Fiselier // UNSUPPORTED: sanitizer-new-delete
205a83710eSEric Fiselier 
215a83710eSEric Fiselier #include <thread>
225a83710eSEric Fiselier #include <new>
23e08afaf8SEric Fiselier #include <atomic>
245a83710eSEric Fiselier #include <cstdlib>
255a83710eSEric Fiselier #include <cassert>
26*d7eb797eSArthur O'Dwyer #include <vector>
275a83710eSEric Fiselier 
2810967a6eSEric Fiselier #include "test_macros.h"
2910967a6eSEric Fiselier 
30e08afaf8SEric Fiselier std::atomic<unsigned> throw_one(0xFFFF);
31e08afaf8SEric Fiselier std::atomic<unsigned> outstanding_new(0);
32e08afaf8SEric Fiselier 
335a83710eSEric Fiselier 
34b6398818SEric Fiselier void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc)
355a83710eSEric Fiselier {
36b574e112SCasey Carter     unsigned expected = throw_one;
37b574e112SCasey Carter     do {
38b574e112SCasey Carter         if (expected == 0) TEST_THROW(std::bad_alloc());
39b574e112SCasey Carter     } while (!throw_one.compare_exchange_weak(expected, expected - 1));
40e08afaf8SEric Fiselier     ++outstanding_new;
410e9272beSEric Fiselier     void* ret = std::malloc(s);
420e9272beSEric Fiselier     if (!ret) std::abort(); // placate MSVC's unchecked malloc warning
430e9272beSEric Fiselier     return ret;
445a83710eSEric Fiselier }
455a83710eSEric Fiselier 
46b6398818SEric Fiselier void  operator delete(void* p) TEST_NOEXCEPT
475a83710eSEric Fiselier {
48b574e112SCasey Carter     if (!p) return;
49e08afaf8SEric Fiselier     --outstanding_new;
505a83710eSEric Fiselier     std::free(p);
515a83710eSEric Fiselier }
525a83710eSEric Fiselier 
535a83710eSEric Fiselier bool f_run = false;
545a83710eSEric Fiselier 
55*d7eb797eSArthur O'Dwyer struct F {
56*d7eb797eSArthur O'Dwyer     std::vector<int> v_;  // so f's copy-ctor calls operator new
57*d7eb797eSArthur O'Dwyer     explicit F() : v_(10) {}
58*d7eb797eSArthur O'Dwyer     void operator()() const { f_run = true; }
59*d7eb797eSArthur O'Dwyer };
60*d7eb797eSArthur O'Dwyer F f;
615a83710eSEric Fiselier 
625a83710eSEric Fiselier class G
635a83710eSEric Fiselier {
645a83710eSEric Fiselier     int alive_;
655a83710eSEric Fiselier public:
665a83710eSEric Fiselier     static int n_alive;
675a83710eSEric Fiselier     static bool op_run;
685a83710eSEric Fiselier 
695a83710eSEric Fiselier     G() : alive_(1) {++n_alive;}
705a83710eSEric Fiselier     G(const G& g) : alive_(g.alive_) {++n_alive;}
715a83710eSEric Fiselier     ~G() {alive_ = 0; --n_alive;}
725a83710eSEric Fiselier 
735a83710eSEric Fiselier     void operator()()
745a83710eSEric Fiselier     {
755a83710eSEric Fiselier         assert(alive_ == 1);
765a83710eSEric Fiselier         assert(n_alive >= 1);
775a83710eSEric Fiselier         op_run = true;
785a83710eSEric Fiselier     }
795a83710eSEric Fiselier 
805a83710eSEric Fiselier     void operator()(int i, double j)
815a83710eSEric Fiselier     {
825a83710eSEric Fiselier         assert(alive_ == 1);
835a83710eSEric Fiselier         assert(n_alive >= 1);
845a83710eSEric Fiselier         assert(i == 5);
855a83710eSEric Fiselier         assert(j == 5.5);
865a83710eSEric Fiselier         op_run = true;
875a83710eSEric Fiselier     }
885a83710eSEric Fiselier };
895a83710eSEric Fiselier 
905a83710eSEric Fiselier int G::n_alive = 0;
915a83710eSEric Fiselier bool G::op_run = false;
925a83710eSEric Fiselier 
9310967a6eSEric Fiselier #if TEST_STD_VER >= 11
945a83710eSEric Fiselier 
955a83710eSEric Fiselier class MoveOnly
965a83710eSEric Fiselier {
975a83710eSEric Fiselier     MoveOnly(const MoveOnly&);
985a83710eSEric Fiselier public:
995a83710eSEric Fiselier     MoveOnly() {}
1005a83710eSEric Fiselier     MoveOnly(MoveOnly&&) {}
1015a83710eSEric Fiselier 
1025a83710eSEric Fiselier     void operator()(MoveOnly&&)
1035a83710eSEric Fiselier     {
1045a83710eSEric Fiselier     }
1055a83710eSEric Fiselier };
1065a83710eSEric Fiselier 
1075a83710eSEric Fiselier #endif
1085a83710eSEric Fiselier 
109e08afaf8SEric Fiselier // Test throwing std::bad_alloc
110e08afaf8SEric Fiselier //-----------------------------
111e08afaf8SEric Fiselier // Concerns:
112e08afaf8SEric Fiselier //  A Each allocation performed during thread construction should be performed
113e08afaf8SEric Fiselier //    in the parent thread so that std::terminate is not called if
114e08afaf8SEric Fiselier //    std::bad_alloc is thrown by new.
115a730ed31SStephan T. Lavavej //  B std::thread's constructor should properly handle exceptions and not leak
116e08afaf8SEric Fiselier //    memory.
117e08afaf8SEric Fiselier // Plan:
118b574e112SCasey Carter //  1 Create a thread and count the number of allocations, 'numAllocs', it
119b574e112SCasey Carter //    performs.
120e08afaf8SEric Fiselier //  2 For each allocation performed run a test where that allocation throws.
121e08afaf8SEric Fiselier //    2.1 check that the exception can be caught in the parent thread.
122e08afaf8SEric Fiselier //    2.2 Check that the functor has not been called.
123e08afaf8SEric Fiselier //    2.3 Check that no memory allocated by the creation of the thread is leaked.
124b574e112SCasey Carter //  3 Finally check that a thread runs successfully if we throw after
125b574e112SCasey Carter //    'numAllocs + 1' allocations.
126b574e112SCasey Carter 
127b574e112SCasey Carter int numAllocs;
128b574e112SCasey Carter 
129e08afaf8SEric Fiselier void test_throwing_new_during_thread_creation() {
13008eb2148SAsiri Rathnayake #ifndef TEST_HAS_NO_EXCEPTIONS
131e08afaf8SEric Fiselier     throw_one = 0xFFF;
132e08afaf8SEric Fiselier     {
133e08afaf8SEric Fiselier         std::thread t(f);
134e08afaf8SEric Fiselier         t.join();
135e08afaf8SEric Fiselier     }
136b574e112SCasey Carter     numAllocs = 0xFFF - throw_one;
137e08afaf8SEric Fiselier     // i <= numAllocs means the last iteration is expected not to throw.
138e08afaf8SEric Fiselier     for (int i=0; i <= numAllocs; ++i) {
139e08afaf8SEric Fiselier         throw_one = i;
140e08afaf8SEric Fiselier         f_run = false;
141e08afaf8SEric Fiselier         unsigned old_outstanding = outstanding_new;
142e08afaf8SEric Fiselier         try {
143e08afaf8SEric Fiselier             std::thread t(f);
144e08afaf8SEric Fiselier             assert(i == numAllocs); // Only final iteration will not throw.
145e08afaf8SEric Fiselier             t.join();
146e08afaf8SEric Fiselier             assert(f_run);
147e08afaf8SEric Fiselier         } catch (std::bad_alloc const&) {
148e08afaf8SEric Fiselier             assert(i < numAllocs);
149e08afaf8SEric Fiselier             assert(!f_run); // (2.2)
150e08afaf8SEric Fiselier         }
151e08afaf8SEric Fiselier         assert(old_outstanding == outstanding_new); // (2.3)
152e08afaf8SEric Fiselier     }
153e08afaf8SEric Fiselier     f_run = false;
154e08afaf8SEric Fiselier     throw_one = 0xFFF;
15508eb2148SAsiri Rathnayake #endif
156e08afaf8SEric Fiselier }
157e08afaf8SEric Fiselier 
1582df59c50SJF Bastien int main(int, char**)
1595a83710eSEric Fiselier {
160e08afaf8SEric Fiselier     test_throwing_new_during_thread_creation();
1615a83710eSEric Fiselier     {
1625a83710eSEric Fiselier         std::thread t(f);
1635a83710eSEric Fiselier         t.join();
1645a83710eSEric Fiselier         assert(f_run == true);
1655a83710eSEric Fiselier     }
166e08afaf8SEric Fiselier 
1675a83710eSEric Fiselier     {
1685a83710eSEric Fiselier         assert(G::n_alive == 0);
1695a83710eSEric Fiselier         assert(!G::op_run);
170e04704b9SBilly Robert O'Neal III         {
171e04704b9SBilly Robert O'Neal III             G g;
172e04704b9SBilly Robert O'Neal III             std::thread t(g);
1735a83710eSEric Fiselier             t.join();
174e04704b9SBilly Robert O'Neal III         }
1755a83710eSEric Fiselier         assert(G::n_alive == 0);
1765a83710eSEric Fiselier         assert(G::op_run);
1775a83710eSEric Fiselier     }
1785a83710eSEric Fiselier     G::op_run = false;
17908eb2148SAsiri Rathnayake #ifndef TEST_HAS_NO_EXCEPTIONS
180b574e112SCasey Carter     // The test below expects `std::thread` to call `new`, which may not be the
181b574e112SCasey Carter     // case for all implementations.
182b574e112SCasey Carter     LIBCPP_ASSERT(numAllocs > 0); // libc++ should call new.
183b574e112SCasey Carter     if (numAllocs > 0) {
1845a83710eSEric Fiselier         try
1855a83710eSEric Fiselier         {
1865a83710eSEric Fiselier             throw_one = 0;
1875a83710eSEric Fiselier             assert(G::n_alive == 0);
1885a83710eSEric Fiselier             assert(!G::op_run);
1895a83710eSEric Fiselier             std::thread t((G()));
1905a83710eSEric Fiselier             assert(false);
1915a83710eSEric Fiselier         }
192b574e112SCasey Carter         catch (std::bad_alloc const&)
1935a83710eSEric Fiselier         {
1945a83710eSEric Fiselier             throw_one = 0xFFFF;
1955a83710eSEric Fiselier             assert(G::n_alive == 0);
1965a83710eSEric Fiselier             assert(!G::op_run);
1975a83710eSEric Fiselier         }
1985a83710eSEric Fiselier     }
19908eb2148SAsiri Rathnayake #endif
20010967a6eSEric Fiselier #if TEST_STD_VER >= 11
2015a83710eSEric Fiselier     {
2025a83710eSEric Fiselier         assert(G::n_alive == 0);
2035a83710eSEric Fiselier         assert(!G::op_run);
204e04704b9SBilly Robert O'Neal III         {
205e04704b9SBilly Robert O'Neal III             G g;
206e04704b9SBilly Robert O'Neal III             std::thread t(g, 5, 5.5);
2075a83710eSEric Fiselier             t.join();
208e04704b9SBilly Robert O'Neal III         }
2095a83710eSEric Fiselier         assert(G::n_alive == 0);
2105a83710eSEric Fiselier         assert(G::op_run);
2115a83710eSEric Fiselier     }
2125a83710eSEric Fiselier     {
2135a83710eSEric Fiselier         std::thread t = std::thread(MoveOnly(), MoveOnly());
2145a83710eSEric Fiselier         t.join();
2155a83710eSEric Fiselier     }
21610967a6eSEric Fiselier #endif
2172df59c50SJF Bastien 
2182df59c50SJF Bastien     return 0;
2195a83710eSEric Fiselier }
220