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
11enum { STACK_SIZE = 0x2000 };
12
13static uint8_t child_stack[STACK_SIZE];
14
15pid_t child_tid;
16
17std::atomic<bool> flag(false);
18
19int thread_main(void *)
20{
21 while (! flag) // Make sure the thread does not exit prematurely
22 ;
23
24 return 0;
25}
26
27int main ()
28{
29 int ret = clone(fn: thread_main,
30 child_stack: 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 arg: nullptr, // thread_main argument
35 &child_tid);
36
37 if (ret == -1)
38 {
39 perror(s: "clone");
40 return 1;
41 }
42
43 flag = true;
44
45 return 0;
46}
47

source code of lldb/test/API/linux/thread/create_during_instruction_step/main.cpp