xref: /llvm-project/lldb/test/API/linux/thread/create_during_instruction_step/main.cpp (revision fdea9a4ec9b0d9585b8fe8a612686d9f44f40ddc)
1 // This file deliberately uses low level linux-specific API for thread creation because:
2 // - instruction-stepping over thread creation using higher-level functions was very slow
3 // - it was also unreliable due to single-stepping bugs unrelated to this test
4 // - some threading libraries do not create or destroy threads when we would expect them to
5 
6 #include <sched.h>
7 
8 #include <atomic>
9 #include <cstdio>
10 
11 enum { STACK_SIZE = 0x2000 };
12 
13 static uint8_t child_stack[STACK_SIZE];
14 
15 pid_t child_tid;
16 
17 std::atomic<bool> flag(false);
18 
thread_main(void *)19 int thread_main(void *)
20 {
21     while (! flag) // Make sure the thread does not exit prematurely
22         ;
23 
24     return 0;
25 }
26 
main()27 int main ()
28 {
29     int ret = clone(thread_main,
30             child_stack + STACK_SIZE/2, // Don't care whether the stack grows up or down,
31                                         // just point to the middle
32             CLONE_CHILD_CLEARTID | CLONE_FILES | CLONE_FS | CLONE_PARENT_SETTID |
33             CLONE_SIGHAND | CLONE_SYSVSEM | CLONE_THREAD | CLONE_VM,
34             nullptr, // thread_main argument
35             &child_tid);
36 
37     if (ret == -1)
38     {
39         perror("clone");
40         return 1;
41     }
42 
43     flag = true;
44 
45     return 0;
46 }
47