1 //===-- main.cpp ------------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // This test is intended to create a situation in which one thread will be
10 // created while the debugger is stepping in another thread.
11 
12 #include "pseudo_barrier.h"
13 #include <thread>
14 
15 #define do_nothing()
16 
17 pseudo_barrier_t g_barrier;
18 
19 volatile int g_thread_created = 0;
20 volatile int g_test = 0;
21 
22 void *
23 step_thread_func ()
24 {
25     g_test = 0;         // Set breakpoint here
26 
27     while (!g_thread_created)
28         g_test++;
29 
30     // One more time to provide a continue point
31     g_test++;           // Continue from here
32 
33     // Return
34     return NULL;
35 }
36 
37 void *
38 create_thread_func (void *input)
39 {
40     std::thread *step_thread = (std::thread*)input;
41 
42     // Wait until the main thread knows this thread is started.
43     pseudo_barrier_wait(g_barrier);
44 
45     // Wait until the other thread is done.
46     step_thread->join();
47 
48     // Return
49     return NULL;
50 }
51 
52 int main ()
53 {
54     // Use a simple count to simulate a barrier.
55     pseudo_barrier_init(g_barrier, 2);
56 
57     // Create a thread to hit the breakpoint.
58     std::thread thread_1(step_thread_func);
59 
60     // Wait until the step thread is stepping
61     while (g_test < 1)
62         do_nothing();
63 
64     // Create a thread to exit while we're stepping.
65     std::thread thread_2(create_thread_func, &thread_1);
66 
67     // Wait until that thread is started
68     pseudo_barrier_wait(g_barrier);
69 
70     // Let the stepping thread know the other thread is there
71     g_thread_created = 1;
72 
73     // Wait for the threads to finish.
74     thread_2.join();
75     thread_1.join();
76 
77     return 0;
78 }
79