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 //
9*a7f9895cSLouis Dionne // UNSUPPORTED: no-threads
105a83710eSEric Fiselier 
115a83710eSEric Fiselier // <thread>
125a83710eSEric Fiselier 
135a83710eSEric Fiselier // class thread
145a83710eSEric Fiselier 
155a83710eSEric Fiselier // void join();
165a83710eSEric Fiselier 
175a83710eSEric Fiselier #include <thread>
185a83710eSEric Fiselier #include <new>
195a83710eSEric Fiselier #include <cstdlib>
205a83710eSEric Fiselier #include <cassert>
214efa1ad5SEric Fiselier #include <system_error>
22d5d47778SMartin Storsjö #include <atomic>
234efa1ad5SEric Fiselier 
2456462801SLouis Dionne #include "make_test_thread.h"
254efa1ad5SEric Fiselier #include "test_macros.h"
265a83710eSEric Fiselier 
27d5d47778SMartin Storsjö std::atomic_bool done(false);
28d5d47778SMartin Storsjö 
295a83710eSEric Fiselier class G
305a83710eSEric Fiselier {
315a83710eSEric Fiselier     int alive_;
325a83710eSEric Fiselier public:
335a83710eSEric Fiselier     static int n_alive;
345a83710eSEric Fiselier     static bool op_run;
355a83710eSEric Fiselier 
G()365a83710eSEric Fiselier     G() : alive_(1) {++n_alive;}
G(const G & g)375a83710eSEric Fiselier     G(const G& g) : alive_(g.alive_) {++n_alive;}
~G()385a83710eSEric Fiselier     ~G() {alive_ = 0; --n_alive;}
395a83710eSEric Fiselier 
operator ()()405a83710eSEric Fiselier     void operator()()
415a83710eSEric Fiselier     {
425a83710eSEric Fiselier         assert(alive_ == 1);
435a83710eSEric Fiselier         assert(n_alive >= 1);
445a83710eSEric Fiselier         op_run = true;
455a83710eSEric Fiselier     }
465a83710eSEric Fiselier };
475a83710eSEric Fiselier 
485a83710eSEric Fiselier int G::n_alive = 0;
495a83710eSEric Fiselier bool G::op_run = false;
505a83710eSEric Fiselier 
foo()51d5d47778SMartin Storsjö void foo() { done = true; }
524efa1ad5SEric Fiselier 
main(int,char **)532df59c50SJF Bastien int main(int, char**)
545a83710eSEric Fiselier {
555a83710eSEric Fiselier     {
569a37bc91SEric Fiselier         G g;
5756462801SLouis Dionne         std::thread t0 = support::make_test_thread(g);
585a83710eSEric Fiselier         assert(t0.joinable());
595a83710eSEric Fiselier         t0.join();
605a83710eSEric Fiselier         assert(!t0.joinable());
614efa1ad5SEric Fiselier #ifndef TEST_HAS_NO_EXCEPTIONS
624efa1ad5SEric Fiselier         try {
634efa1ad5SEric Fiselier             t0.join();
644efa1ad5SEric Fiselier             assert(false);
654efa1ad5SEric Fiselier         } catch (std::system_error const&) {
665a83710eSEric Fiselier         }
674efa1ad5SEric Fiselier #endif
684efa1ad5SEric Fiselier     }
694efa1ad5SEric Fiselier #ifndef TEST_HAS_NO_EXCEPTIONS
704efa1ad5SEric Fiselier     {
7156462801SLouis Dionne         std::thread t0 = support::make_test_thread(foo);
724efa1ad5SEric Fiselier         t0.detach();
734efa1ad5SEric Fiselier         try {
744efa1ad5SEric Fiselier             t0.join();
754efa1ad5SEric Fiselier             assert(false);
764efa1ad5SEric Fiselier         } catch (std::system_error const&) {
774efa1ad5SEric Fiselier         }
78d5d47778SMartin Storsjö         // Wait to make sure that the detached thread has started up.
79d5d47778SMartin Storsjö         // Without this, we could exit main and start destructing global
80d5d47778SMartin Storsjö         // resources that are needed when the thread starts up, while the
81d5d47778SMartin Storsjö         // detached thread would start up only later.
82d5d47778SMartin Storsjö         while (!done) {}
834efa1ad5SEric Fiselier     }
844efa1ad5SEric Fiselier #endif
852df59c50SJF Bastien 
862df59c50SJF Bastien   return 0;
875a83710eSEric Fiselier }
88