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