15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
35a83710eSEric Fiselier //                     The LLVM Compiler Infrastructure
45a83710eSEric Fiselier //
55a83710eSEric Fiselier // This file is dual licensed under the MIT and the University of Illinois Open
65a83710eSEric Fiselier // Source Licenses. See LICENSE.TXT for details.
75a83710eSEric Fiselier //
85a83710eSEric Fiselier //===----------------------------------------------------------------------===//
95a83710eSEric Fiselier //
105a83710eSEric Fiselier // UNSUPPORTED: libcpp-has-no-threads
115a83710eSEric Fiselier 
125a83710eSEric Fiselier // <thread>
135a83710eSEric Fiselier 
145a83710eSEric Fiselier // class thread
155a83710eSEric Fiselier 
165a83710eSEric Fiselier // template <class F, class ...Args> thread(F&& f, Args&&... args);
175a83710eSEric Fiselier 
184b7533a1SEric Fiselier // UNSUPPORTED: sanitizer-new-delete
195a83710eSEric Fiselier 
205a83710eSEric Fiselier #include <thread>
215a83710eSEric Fiselier #include <new>
22e08afaf8SEric Fiselier #include <atomic>
235a83710eSEric Fiselier #include <cstdlib>
245a83710eSEric Fiselier #include <cassert>
255a83710eSEric Fiselier 
2610967a6eSEric Fiselier #include "test_macros.h"
2710967a6eSEric Fiselier 
28e08afaf8SEric Fiselier std::atomic<unsigned> throw_one(0xFFFF);
29e08afaf8SEric Fiselier std::atomic<unsigned> outstanding_new(0);
30e08afaf8SEric Fiselier 
315a83710eSEric Fiselier 
325a83710eSEric Fiselier void* operator new(std::size_t s) throw(std::bad_alloc)
335a83710eSEric Fiselier {
345a83710eSEric Fiselier     if (throw_one == 0)
35*08eb2148SAsiri Rathnayake         TEST_THROW(std::bad_alloc());
365a83710eSEric Fiselier     --throw_one;
37e08afaf8SEric Fiselier     ++outstanding_new;
380e9272beSEric Fiselier     void* ret = std::malloc(s);
390e9272beSEric Fiselier     if (!ret) std::abort(); // placate MSVC's unchecked malloc warning
400e9272beSEric Fiselier     return ret;
415a83710eSEric Fiselier }
425a83710eSEric Fiselier 
435a83710eSEric Fiselier void  operator delete(void* p) throw()
445a83710eSEric Fiselier {
45e08afaf8SEric Fiselier     --outstanding_new;
465a83710eSEric Fiselier     std::free(p);
475a83710eSEric Fiselier }
485a83710eSEric Fiselier 
495a83710eSEric Fiselier bool f_run = false;
505a83710eSEric Fiselier 
515a83710eSEric Fiselier void f()
525a83710eSEric Fiselier {
535a83710eSEric Fiselier     f_run = true;
545a83710eSEric Fiselier }
555a83710eSEric Fiselier 
565a83710eSEric Fiselier class G
575a83710eSEric Fiselier {
585a83710eSEric Fiselier     int alive_;
595a83710eSEric Fiselier public:
605a83710eSEric Fiselier     static int n_alive;
615a83710eSEric Fiselier     static bool op_run;
625a83710eSEric Fiselier 
635a83710eSEric Fiselier     G() : alive_(1) {++n_alive;}
645a83710eSEric Fiselier     G(const G& g) : alive_(g.alive_) {++n_alive;}
655a83710eSEric Fiselier     ~G() {alive_ = 0; --n_alive;}
665a83710eSEric Fiselier 
675a83710eSEric Fiselier     void operator()()
685a83710eSEric Fiselier     {
695a83710eSEric Fiselier         assert(alive_ == 1);
705a83710eSEric Fiselier         assert(n_alive >= 1);
715a83710eSEric Fiselier         op_run = true;
725a83710eSEric Fiselier     }
735a83710eSEric Fiselier 
745a83710eSEric Fiselier     void operator()(int i, double j)
755a83710eSEric Fiselier     {
765a83710eSEric Fiselier         assert(alive_ == 1);
775a83710eSEric Fiselier         assert(n_alive >= 1);
785a83710eSEric Fiselier         assert(i == 5);
795a83710eSEric Fiselier         assert(j == 5.5);
805a83710eSEric Fiselier         op_run = true;
815a83710eSEric Fiselier     }
825a83710eSEric Fiselier };
835a83710eSEric Fiselier 
845a83710eSEric Fiselier int G::n_alive = 0;
855a83710eSEric Fiselier bool G::op_run = false;
865a83710eSEric Fiselier 
8710967a6eSEric Fiselier #if TEST_STD_VER >= 11
885a83710eSEric Fiselier 
895a83710eSEric Fiselier class MoveOnly
905a83710eSEric Fiselier {
915a83710eSEric Fiselier     MoveOnly(const MoveOnly&);
925a83710eSEric Fiselier public:
935a83710eSEric Fiselier     MoveOnly() {}
945a83710eSEric Fiselier     MoveOnly(MoveOnly&&) {}
955a83710eSEric Fiselier 
965a83710eSEric Fiselier     void operator()(MoveOnly&&)
975a83710eSEric Fiselier     {
985a83710eSEric Fiselier     }
995a83710eSEric Fiselier };
1005a83710eSEric Fiselier 
1015a83710eSEric Fiselier #endif
1025a83710eSEric Fiselier 
103e08afaf8SEric Fiselier // Test throwing std::bad_alloc
104e08afaf8SEric Fiselier //-----------------------------
105e08afaf8SEric Fiselier // Concerns:
106e08afaf8SEric Fiselier //  A Each allocation performed during thread construction should be performed
107e08afaf8SEric Fiselier //    in the parent thread so that std::terminate is not called if
108e08afaf8SEric Fiselier //    std::bad_alloc is thrown by new.
109e08afaf8SEric Fiselier //  B std::threads constructor should properly handle exceptions and not leak
110e08afaf8SEric Fiselier //    memory.
111e08afaf8SEric Fiselier // Plan:
112e08afaf8SEric Fiselier //  1 Create a thread and count the number of allocations, 'N', it performs.
113e08afaf8SEric Fiselier //  2 For each allocation performed run a test where that allocation throws.
114e08afaf8SEric Fiselier //    2.1 check that the exception can be caught in the parent thread.
115e08afaf8SEric Fiselier //    2.2 Check that the functor has not been called.
116e08afaf8SEric Fiselier //    2.3 Check that no memory allocated by the creation of the thread is leaked.
117e08afaf8SEric Fiselier //  3 Finally check that a thread runs successfully if we throw after 'N+1'
118e08afaf8SEric Fiselier //    allocations.
119e08afaf8SEric Fiselier void test_throwing_new_during_thread_creation() {
120*08eb2148SAsiri Rathnayake #ifndef TEST_HAS_NO_EXCEPTIONS
121e08afaf8SEric Fiselier     throw_one = 0xFFF;
122e08afaf8SEric Fiselier     {
123e08afaf8SEric Fiselier         std::thread t(f);
124e08afaf8SEric Fiselier         t.join();
125e08afaf8SEric Fiselier     }
126e08afaf8SEric Fiselier     const int numAllocs = 0xFFF - throw_one;
127e08afaf8SEric Fiselier     // i <= numAllocs means the last iteration is expected not to throw.
128e08afaf8SEric Fiselier     for (int i=0; i <= numAllocs; ++i) {
129e08afaf8SEric Fiselier         throw_one = i;
130e08afaf8SEric Fiselier         f_run = false;
131e08afaf8SEric Fiselier         unsigned old_outstanding = outstanding_new;
132e08afaf8SEric Fiselier         try {
133e08afaf8SEric Fiselier             std::thread t(f);
134e08afaf8SEric Fiselier             assert(i == numAllocs); // Only final iteration will not throw.
135e08afaf8SEric Fiselier             t.join();
136e08afaf8SEric Fiselier             assert(f_run);
137e08afaf8SEric Fiselier         } catch (std::bad_alloc const&) {
138e08afaf8SEric Fiselier             assert(i < numAllocs);
139e08afaf8SEric Fiselier             assert(!f_run); // (2.2)
140e08afaf8SEric Fiselier         }
141e08afaf8SEric Fiselier         assert(old_outstanding == outstanding_new); // (2.3)
142e08afaf8SEric Fiselier     }
143e08afaf8SEric Fiselier     f_run = false;
144e08afaf8SEric Fiselier     throw_one = 0xFFF;
145*08eb2148SAsiri Rathnayake #endif
146e08afaf8SEric Fiselier }
147e08afaf8SEric Fiselier 
1485a83710eSEric Fiselier int main()
1495a83710eSEric Fiselier {
150e08afaf8SEric Fiselier     test_throwing_new_during_thread_creation();
1515a83710eSEric Fiselier     {
1525a83710eSEric Fiselier         std::thread t(f);
1535a83710eSEric Fiselier         t.join();
1545a83710eSEric Fiselier         assert(f_run == true);
1555a83710eSEric Fiselier     }
156e08afaf8SEric Fiselier 
1575a83710eSEric Fiselier     {
1585a83710eSEric Fiselier         assert(G::n_alive == 0);
1595a83710eSEric Fiselier         assert(!G::op_run);
1605a83710eSEric Fiselier         std::thread t((G()));
1615a83710eSEric Fiselier         t.join();
1625a83710eSEric Fiselier         assert(G::n_alive == 0);
1635a83710eSEric Fiselier         assert(G::op_run);
1645a83710eSEric Fiselier     }
1655a83710eSEric Fiselier     G::op_run = false;
166*08eb2148SAsiri Rathnayake #ifndef TEST_HAS_NO_EXCEPTIONS
1675a83710eSEric Fiselier     {
1685a83710eSEric Fiselier         try
1695a83710eSEric Fiselier         {
1705a83710eSEric Fiselier             throw_one = 0;
1715a83710eSEric Fiselier             assert(G::n_alive == 0);
1725a83710eSEric Fiselier             assert(!G::op_run);
1735a83710eSEric Fiselier             std::thread t((G()));
1745a83710eSEric Fiselier             assert(false);
1755a83710eSEric Fiselier         }
1765a83710eSEric Fiselier         catch (...)
1775a83710eSEric Fiselier         {
1785a83710eSEric Fiselier             throw_one = 0xFFFF;
1795a83710eSEric Fiselier             assert(G::n_alive == 0);
1805a83710eSEric Fiselier             assert(!G::op_run);
1815a83710eSEric Fiselier         }
1825a83710eSEric Fiselier     }
183*08eb2148SAsiri Rathnayake #endif
18410967a6eSEric Fiselier #if TEST_STD_VER >= 11
1855a83710eSEric Fiselier     {
1865a83710eSEric Fiselier         assert(G::n_alive == 0);
1875a83710eSEric Fiselier         assert(!G::op_run);
1885a83710eSEric Fiselier         std::thread t(G(), 5, 5.5);
1895a83710eSEric Fiselier         t.join();
1905a83710eSEric Fiselier         assert(G::n_alive == 0);
1915a83710eSEric Fiselier         assert(G::op_run);
1925a83710eSEric Fiselier     }
1935a83710eSEric Fiselier     {
1945a83710eSEric Fiselier         std::thread t = std::thread(MoveOnly(), MoveOnly());
1955a83710eSEric Fiselier         t.join();
1965a83710eSEric Fiselier     }
19710967a6eSEric Fiselier #endif
1985a83710eSEric Fiselier }
199