]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - packages/Python/lldbsuite/test/functionalities/thread/create_during_step/main.cpp
Vendor import of lldb release_39 branch r276489:
[FreeBSD/FreeBSD.git] / packages / Python / lldbsuite / test / functionalities / thread / create_during_step / main.cpp
1 //===-- main.cpp ------------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // This test is intended to create a situation in which one thread will be
11 // created while the debugger is stepping in another thread.
12
13 #include <atomic>
14 #include <thread>
15
16 #define do_nothing()
17
18 pseudo_barrier_t g_barrier;
19
20 volatile int g_thread_created = 0;
21 volatile int g_test = 0;
22
23 void *
24 step_thread_func ()
25 {
26     g_test = 0;         // Set breakpoint here
27
28     while (!g_thread_created)
29         g_test++;
30
31     // One more time to provide a continue point
32     g_test++;           // Continue from here
33
34     // Return
35     return NULL;
36 }
37
38 void *
39 create_thread_func (void *input)
40 {
41     std::thread *step_thread = (std::thread*)input;
42
43     // Wait until the main thread knows this thread is started.
44     pseudo_barrier_wait(g_barrier);
45
46     // Wait until the other thread is done.
47     step_thread->join();
48
49     // Return
50     return NULL;
51 }
52
53 int main ()
54 {
55     // Use a simple count to simulate a barrier.
56     pseudo_barrier_init(g_barrier, 2);
57
58     // Create a thread to hit the breakpoint.
59     std::thread thread_1(step_thread_func);
60
61     // Wait until the step thread is stepping
62     while (g_test < 1)
63         do_nothing();
64
65     // Create a thread to exit while we're stepping.
66     std::thread thread_2(create_thread_func, &thread_1);
67
68     // Wait until that thread is started
69     pseudo_barrier_wait(g_barrier);
70
71     // Let the stepping thread know the other thread is there
72     g_thread_created = 1;
73
74     // Wait for the threads to finish.
75     thread_2.join();
76     thread_1.join();
77
78     return 0;
79 }